Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuzz tests for prove crypto #27

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions near/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[workspace]
members = [
"block_finality",
"crypto/plonky2_ed25519",
"crypto/plonky2_sha512",
"block_finality",
"crypto/plonky2_sha256_u32",
"crypto/plonky2_bn128"
, "xtask"]
"crypto/plonky2_bn128",
"xtask"]

[workspace.package]
authors = [ "Zpoken" ]
Expand Down
4 changes: 4 additions & 0 deletions near/block_finality/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
53 changes: 53 additions & 0 deletions near/block_finality/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[package]
name = "block_finality-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
hex = "0.4.3"
# mb change to workspace dependencies
plonky2 = { git = "https://github.com/wormhole-foundation/plonky2-near", rev = "2244a9d802aa74f15c32ca7f4139959c61126819", features = ["parallel"] }
plonky2_ed25519 = { path = "../../crypto/plonky2_ed25519" }
ed25519-compact = "2.0.4"
near-primitives = "0.19.0"

[dependencies.block_finality]
path = ".."

[workspace]
members = ["."]

[[bin]]
name = "ed25519_proof"
path = "fuzz_targets/prove_crypto/ed25519_proof.rs"
test = false
doc = false

[[bin]]
name = "sha256_proof"
path = "fuzz_targets/prove_crypto/sha256_proof.rs"
test = false
doc = false

[[bin]]
name = "prove_sub_hashes"
path = "fuzz_targets/prove_crypto/prove_sub_hashes.rs"
test = false
doc = false

[[bin]]
name = "ed25519_circuit_targets_caching"
path = "fuzz_targets/prove_crypto/ed25519_circuit_targets_caching.rs"
test = false
doc = false

[[bin]]
name = "ed25519_proof_reuse_circuit"
path = "fuzz_targets/prove_crypto/ed25519_proof_reuse_circuit.rs"
test = false
doc = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#![no_main]

use ed25519_compact::*;
use plonky2::plonk::{
circuit_data::CircuitData,
config::{GenericConfig, PoseidonGoldilocksConfig},
};
use plonky2_ed25519::gadgets::eddsa::EDDSATargets;
use std::collections::HashMap;

use libfuzzer_sys::fuzz_target;

use block_finality::prove_crypto::get_ed25519_circuit_targets;

fuzz_target!(|data: &[u8]| {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

let msg1 = data.to_vec();
let msg2 = if data.len() > 0 {
data[0..(data.len() - 1)].to_vec()
} else {
data.to_vec()
};

let mut circuit_data_targets: HashMap<usize, (CircuitData<F, C, D>, EDDSATargets)> =
HashMap::new();

let (_data, _targets) =
get_ed25519_circuit_targets::<F, C, D>(msg1.len(), &mut circuit_data_targets);
assert!(circuit_data_targets.len() == 1);
let (_data, _targets) =
get_ed25519_circuit_targets::<F, C, D>(msg2.len(), &mut circuit_data_targets);
if data.len() > 0 {
assert!(circuit_data_targets.len() == 2);
} else {
assert!(circuit_data_targets.len() == 1);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![no_main]

use ed25519_compact::*;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

use libfuzzer_sys::fuzz_target;

use block_finality::prove_crypto::{ed25519_proof, get_ed25519_targets};

fuzz_target!(|data: &[u8]| {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

let data_vec = data.to_vec();

//let keys = KeyPair::generate();
//let pk = keys.pk.to_vec();

let pk_bytes = [
248, 137, 130, 32, 199, 137, 101, 139, 162, 79, 53, 29, 125, 5, 62, 32, 88, 106, 168, 15,
155, 158, 173, 39, 231, 105, 142, 127, 253, 203, 13, 63,
]
.to_vec();
let sk_bytes = [
149, 252, 231, 211, 124, 212, 39, 115, 225, 16, 86, 79, 63, 48, 179, 141, 203, 95, 7, 144,
198, 235, 65, 177, 228, 77, 143, 238, 212, 110, 208, 152, 248, 137, 130, 32, 199, 137, 101,
139, 162, 79, 53, 29, 125, 5, 62, 32, 88, 106, 168, 15, 155, 158, 173, 39, 231, 105, 142,
127, 253, 203, 13, 63,
]
.to_vec();
let sk = SecretKey::from_slice(&sk_bytes).expect("Error getting secret key.");

let sig_bytes = sk.sign(data_vec.clone(), None).to_vec();

let (data, targets) =
get_ed25519_targets::<F, C, D>(data_vec.len() * 8).expect("Error getting targets.");
let proof = ed25519_proof::<F, C, D>(&data_vec, &sig_bytes, &pk_bytes, (data.clone(), targets))
.expect("Error generating proof.");

assert!(data.verify(proof).is_ok());
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![no_main]

use ed25519_compact::*;
use plonky2::plonk::{
circuit_data::CircuitData,
config::{GenericConfig, PoseidonGoldilocksConfig},
};
use plonky2_ed25519::gadgets::eddsa::EDDSATargets;
use std::collections::HashMap;

use libfuzzer_sys::fuzz_target;

use block_finality::prove_crypto::ed25519_proof_reuse_circuit;

fuzz_target!(|data: &[u8]| {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

let data_vec = data.to_vec();

let keys = KeyPair::generate();
let pk1 = keys.pk.to_vec();
let sig1 = keys.sk.sign(data_vec.clone(), None).to_vec();

let mut circuit_data_targets: HashMap<usize, (CircuitData<F, C, D>, EDDSATargets)> =
HashMap::new();

let (d1, p1) =
ed25519_proof_reuse_circuit::<F, C, D>(&data_vec, &sig1, &pk1, &mut circuit_data_targets)
.expect("Error generating proof.");
d1.verify(p1).expect("Proof verification failed.");
assert!(circuit_data_targets.len() == 1);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![no_main]

use near_primitives::hash::hash;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

use libfuzzer_sys::fuzz_target;

use block_finality::prove_crypto::{prove_sub_hashes_u32, sha256_proof_u32};

fuzz_target!(|data: &[u8]| {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

let lim = data.len() / 2;
let msg1 = data[0..lim].to_vec();
let msg2 = data[lim..].to_vec();
let hash1 = hash(&msg1);
let hash2 = hash(&msg2);
let msg3 = [hash1.0, hash2.0].concat();
let hash3 = hash(&msg3);

let (d1, p1) = sha256_proof_u32::<F, C, D>(&msg1, &hash1.0).expect("Error proving first hash.");
d1.verify(p1.clone())
.expect("First proof verification failed.");
let (d2, p2) =
sha256_proof_u32::<F, C, D>(&msg2, &hash2.0).expect("Error proving second hash.");
d2.verify(p2.clone())
.expect("Second proof verification failed.");
let (d3, p3) =
sha256_proof_u32::<F, C, D>(&msg3, &hash3.0).expect("Error proving concatenated hashes.");
d3.verify(p3.clone())
.expect("Third proof verification failed.");

let (data, proof) = prove_sub_hashes_u32(
true,
true,
&p1.public_inputs,
&p2.public_inputs,
Some(&hash3.0.to_vec()),
(&d1.common, &d1.verifier_only, &p1),
Some((&d2.common, &d2.verifier_only, &p2)),
)
.expect("Error proving subhashes.");

assert!(data.verify(proof).is_ok(), "Proof verification failed.");
});
23 changes: 23 additions & 0 deletions near/block_finality/fuzz/fuzz_targets/prove_crypto/sha256_proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_main]

use near_primitives::hash::hash;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};

use libfuzzer_sys::fuzz_target;

use block_finality::prove_crypto::sha256_proof_u32;

fuzz_target!(|data: &[u8]| {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

let data_vec = data.to_vec();

let hash = hash(&data_vec);

let (data, proof) =
sha256_proof_u32::<F, C, D>(&data_vec, &hash.0).expect("Error sha256 proof.");

assert!(data.verify(proof).is_ok(), "Proof verification failed.");
});
Loading