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

Basic fuzzing #33

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
24 changes: 24 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "sylow-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
sha3 = "0.11.0-pre.4"
crypto-bigint = "0.6.0-rc.3"
num-traits = "0.2.19"

[dependencies.sylow]
path = ".."

[[bin]]
name = "fuzz_sylow_api"
path = "fuzz_targets/fuzz_sylow_api.rs"
test = false
doc = false
bench = false
74 changes: 74 additions & 0 deletions fuzz/fuzz_targets/fuzz_sylow_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use sylow::{
KeyPair, sign, verify, G1Projective, G2Projective, pairing, Fp, Fr,
GroupTrait, FieldExtensionTrait, Gt, XMDExpander
};
use sha3::Keccak256;
use crypto_bigint::rand_core::OsRng;

fuzz_target!(|data: &[u8]| {
if data.len() < 64 {
return;
}

// Test case from g1::tests::test_addition_commutativity
let a = G1Projective::rand(&mut OsRng);
let b = G1Projective::rand(&mut OsRng);
assert_eq!(a + b, b + a, "G1 addition is not commutative");

// Test case from g2::tests::test_addition_commutativity
let c = G2Projective::rand(&mut OsRng);
let d = G2Projective::rand(&mut OsRng);
assert_eq!(c + d, d + c, "G2 addition is not commutative");

// Test case from g1::tests::test_doubling
assert_eq!(a.double(), a + a, "G1 doubling failed");

// Test case from g2::tests::test_doubling
assert_eq!(c.double(), c + c, "G2 doubling failed");

// Test case from g1::tests::test_scalar_mul
let three = Fp::from(3u64);
assert_eq!(a + (a + a), a * three, "G1 scalar multiplication failed");

// Test case from g2::tests::test_scalar_mul
assert_eq!(c + (c + c), c * three, "G2 scalar multiplication failed");

// Test case from gt::tests::test_bilinearity
let p = G1Projective::rand(&mut OsRng);
let q = G2Projective::rand(&mut OsRng);
let s = Fr::rand(&mut OsRng);
let sp = G1Projective::from(p) * s.into();
let sq = G2Projective::from(q) * s.into();

let a = pairing(&p, &q) * s;
let b = pairing(&sp, &q);
let c = pairing(&p, &sq);

assert_eq!(a, b, "Pairing bilinearity property failed");
assert_eq!(a, c, "Pairing bilinearity property failed");

let t = -Fr::ONE;
assert_ne!(a, Gt::identity(), "Pairing result should not be identity");
assert_eq!(&(a * t) + &a, Gt::identity(), "Pairing inverse property failed");

// Test case from g1::tests::test_hash_to_curve
let dst = b"QUUX-V01-CS02-with-expander-SHA256-128";
let expander = XMDExpander::<Keccak256>::new(dst, 128);
if let Ok(hash_point) = G1Projective::hash_to_curve(&expander, &data[..32]) {
assert!(!hash_point.is_zero(), "Hash to curve resulted in zero point");
}

// Test BLS signature scheme
let key_pair = KeyPair::generate();
match sign(&key_pair.secret_key, &data[32..64]) {
Ok(signature) => {
match verify(&key_pair.public_key, &data[32..64], &signature) {
Ok(is_valid) => assert!(is_valid, "Signature verification failed"),
Err(_) => panic!("Verification error"),
}
}
Err(_) => println!("Signing error"),
}
});
4 changes: 2 additions & 2 deletions src/groups/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ impl<const D: usize, const N: usize, F: FieldExtensionTrait<D, N>> GroupProjecti

/// Checks if this point is the point at infinity.
#[inline(always)]
pub(crate) fn is_zero(&self) -> bool {
pub fn is_zero(&self) -> bool {
self.z.is_zero()
}

/// Doubles this point using an optimized algorithm for curves with j-invariant 0.
pub(crate) fn double(&self) -> Self {
pub fn double(&self) -> Self {
// This implementation is based on Algorithm 9 from a Ref (1) above - since BN254 has
// j-invariant 0, we can use some nice simplifications to the arithmetic.
//
Expand Down
Loading