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

fix: fields and curves fuzzers fix #861

Open
wants to merge 4 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
3 changes: 2 additions & 1 deletion fuzz/no_gpu_fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ num-traits = "0.2"
ibig = "0.3.6"
p3-goldilocks = { git = "https://github.com/Plonky3/Plonky3", rev = "41cd843" }
p3-mersenne-31 = { git = "https://github.com/Plonky3/Plonky3", rev = "41cd843" }
p3-field = { git = "https://github.com/Plonky3/Plonky3", rev = "41cd843" }

[[bin]]
name = "curve_bls12_381"
Expand All @@ -42,7 +43,7 @@ test = false
doc = false

[[bin]]
name = "field_fuzz_mersenne31"
name = "field_mersenne31"
path = "fuzz_targets/field/mersenne31.rs"
test = false
doc = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use lambdaworks_math::{
field::element::FieldElement,
cyclic_group::IsGroup,
elliptic_curve::{
traits::{IsEllipticCurve, IsPairing},
short_weierstrass::{
curves::bls12_381::{
curve::BLS12381Curve,
twist::BLS12381TwistCurve,
pairing::BLS12381AtePairing,
field_extension::Degree12ExtensionField,
curve::BLS12381Curve, field_extension::Degree12ExtensionField,
pairing::BLS12381AtePairing, twist::BLS12381TwistCurve,
},
point::ShortWeierstrassProjectivePoint,
}
},
traits::{IsEllipticCurve, IsPairing},
},
field::element::FieldElement,
unsigned_integer::element::U384,
};
use libfuzzer_sys::fuzz_target;

type LambdaG1 = ShortWeierstrassProjectivePoint<BLS12381Curve>;
type LambdaG2 = ShortWeierstrassProjectivePoint<BLS12381TwistCurve>;
Expand All @@ -44,38 +42,86 @@ fuzz_target!(|values: (u64, u64)| {
assert_eq!(g1_zero.neg(), g1_zero, "Neutral mul element a failed");

// P + O = O
assert_eq!(a_g1.operate_with(&g1_zero), a_g1, "Neutral operate_with element a failed");
assert_eq!(b_g1.operate_with(&g1_zero), b_g1, "Neutral operate_with element b failed");
assert_eq!(
a_g1.operate_with(&g1_zero),
a_g1,
"Neutral operate_with element a failed"
);
assert_eq!(
b_g1.operate_with(&g1_zero),
b_g1,
"Neutral operate_with element b failed"
);

// P + Q = Q + P
assert_eq!(a_g1.operate_with(&b_g1), b_g1.operate_with(&a_g1), "Commutative add property failed");
assert_eq!(
a_g1.operate_with(&b_g1),
b_g1.operate_with(&a_g1),
"Commutative add property failed"
);

// (P + Q) + R = Q + (P + R)
let c_g1 = a_g1.operate_with(&b_g1);
assert_eq!((a_g1.operate_with(&b_g1)).operate_with(&c_g1), a_g1.operate_with(&b_g1.operate_with(&c_g1)), "Associative operate_with property failed");
assert_eq!(
(a_g1.operate_with(&b_g1)).operate_with(&c_g1),
a_g1.operate_with(&b_g1.operate_with(&c_g1)),
"Associative operate_with property failed"
);

// P + -P = O
assert_eq!(a_g1.operate_with(&a_g1.neg()), g1_zero, "Inverse add a failed");
assert_eq!(b_g1.operate_with(&b_g1.neg()), g1_zero, "Inverse add b failed");
assert_eq!(
a_g1.operate_with(&a_g1.neg()),
g1_zero,
"Inverse add a failed"
);
assert_eq!(
b_g1.operate_with(&b_g1.neg()),
g1_zero,
"Inverse add b failed"
);

// G2
// -O = O
assert_eq!(g2_zero.neg(), g2_zero, "Neutral mul element a failed");

// P + O = O
assert_eq!(a_g2.operate_with(&g2_zero), a_g2, "Neutral operate_with element a failed");
assert_eq!(b_g2.operate_with(&g2_zero), b_g2, "Neutral operate_with element b failed");
assert_eq!(
a_g2.operate_with(&g2_zero),
a_g2,
"Neutral operate_with element a failed"
);
assert_eq!(
b_g2.operate_with(&g2_zero),
b_g2,
"Neutral operate_with element b failed"
);

// P + Q = Q + P
assert_eq!(a_g2.operate_with(&b_g2), b_g2.operate_with(&a_g2), "Commutative add property failed");
assert_eq!(
a_g2.operate_with(&b_g2),
b_g2.operate_with(&a_g2),
"Commutative add property failed"
);

// (P + Q) + R = Q + (P + R)
let c_g2 = a_g2.operate_with(&b_g2);
assert_eq!((a_g2.operate_with(&b_g2)).operate_with(&c_g2), a_g2.operate_with(&b_g2.operate_with(&c_g2)), "Associative operate_with property failed");
assert_eq!(
(a_g2.operate_with(&b_g2)).operate_with(&c_g2),
a_g2.operate_with(&b_g2.operate_with(&c_g2)),
"Associative operate_with property failed"
);

// P + -P = O
assert_eq!(a_g2.operate_with(&a_g2.neg()), g2_zero, "Inverse add a failed");
assert_eq!(b_g2.operate_with(&b_g2.neg()), g2_zero, "Inverse add b failed");
assert_eq!(
a_g2.operate_with(&a_g2.neg()),
g2_zero,
"Inverse add a failed"
);
assert_eq!(
b_g2.operate_with(&b_g2.neg()),
g2_zero,
"Inverse add b failed"
);

// Pairing Bilinearity
let a = U384::from_u64(a_val);
Expand All @@ -90,12 +136,14 @@ fuzz_target!(|values: (u64, u64)| {
&a_g2.neg().to_affine(),
),
]);
assert_eq!(result, FieldElement::<Degree12ExtensionField>::one());
assert_eq!(result, Ok(FieldElement::<Degree12ExtensionField>::one()));

// Ate Pairing returns one with one element is neutral element
let result = BLS12381AtePairing::compute_batch(&[(&a_g1.to_affine(), &LambdaG2::neutral_element())]);
assert_eq!(result, FieldElement::<Degree12ExtensionField>::one());
let result =
BLS12381AtePairing::compute_batch(&[(&a_g1.to_affine(), &LambdaG2::neutral_element())]);
assert_eq!(result, Ok(FieldElement::<Degree12ExtensionField>::one()));

let result = BLS12381AtePairing::compute_batch(&[(&LambdaG1::neutral_element(), &a_g2.to_affine())]);
assert_eq!(result, FieldElement::<Degree12ExtensionField>::one());
let result =
BLS12381AtePairing::compute_batch(&[(&LambdaG1::neutral_element(), &a_g2.to_affine())]);
assert_eq!(result, Ok(FieldElement::<Degree12ExtensionField>::one()));
});
127 changes: 127 additions & 0 deletions fuzz/no_gpu_fuzz/fuzz_targets/curve/bn254.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#![no_main]

use lambdaworks_math::{
cyclic_group::IsGroup,
elliptic_curve::{
short_weierstrass::{
curves::bn_254::{
curve::BN254Curve, field_extension::Degree12ExtensionField, twist::BN254TwistCurve,
},
point::ShortWeierstrassProjectivePoint,
},
traits::{IsEllipticCurve, IsPairing},
},
field::element::FieldElement,
unsigned_integer::element::U256,
};
use libfuzzer_sys::fuzz_target;

type LambdaG1 = ShortWeierstrassProjectivePoint<BN254Curve>;
type LambdaG2 = ShortWeierstrassProjectivePoint<BN254TwistCurve>;

//TODO: derive arbitrary for Affine and Projective or change this to use &[u8] as input to cover more cases
fuzz_target!(|values: (u64, u64)| {
let (a_val, b_val) = values;

let a_g1 = BN254Curve::generator().operate_with_self(a_val);
let b_g1 = BN254Curve::generator().operate_with_self(b_val);

let a_g2 = BN254TwistCurve::generator().operate_with_self(a_val);
let b_g2 = BN254TwistCurve::generator().operate_with_self(b_val);

// ***AXIOM SOUNDNESS***

let g1_zero = LambdaG1::neutral_element();

let g2_zero = LambdaG2::neutral_element();

// G1
// -O = O
assert_eq!(g1_zero.neg(), g1_zero, "Neutral mul element a failed");

// P * O = O
assert_eq!(
a_g1.operate_with(&g1_zero),
a_g1,
"Neutral operate_with element a failed"
);
assert_eq!(
b_g1.operate_with(&g1_zero),
b_g1,
"Neutral operate_with element b failed"
);

// P * Q = Q * P
assert_eq!(
a_g1.operate_with(&b_g1),
b_g1.operate_with(&a_g1),
"Commutative add property failed"
);

// (P * Q) * R = Q * (P * R)
let c_g1 = a_g1.operate_with(&b_g1);
assert_eq!(
(a_g1.operate_with(&b_g1)).operate_with(&c_g1),
a_g1.operate_with(&b_g1.operate_with(&c_g1)),
"Associative operate_with property failed"
);

// P * -P = O
assert_eq!(
a_g1.operate_with(&a_g1.neg()),
g1_zero,
"Inverse add a failed"
);
assert_eq!(
b_g1.operate_with(&b_g1.neg()),
g1_zero,
"Inverse add b failed"
);

// G2
// -O = O
assert_eq!(g2_zero.neg(), g2_zero, "Neutral mul element a failed");

// P * O = O
assert_eq!(
a_g2.operate_with(&g2_zero),
a_g2,
"Neutral operate_with element a failed"
);
assert_eq!(
b_g2.operate_with(&g2_zero),
b_g2,
"Neutral operate_with element b failed"
);

// P * Q = Q * P
assert_eq!(
a_g2.operate_with(&b_g2),
b_g2.operate_with(&a_g2),
"Commutative add property failed"
);

// (P * Q) * R = Q * (P * R)
let c_g2 = a_g2.operate_with(&b_g2);
assert_eq!(
(a_g2.operate_with(&b_g2)).operate_with(&c_g2),
a_g2.operate_with(&b_g2.operate_with(&c_g2)),
"Associative operate_with property failed"
);

// P * -P = O
assert_eq!(
a_g2.operate_with(&a_g2.neg()),
g2_zero,
"Inverse add a failed"
);
assert_eq!(
b_g2.operate_with(&b_g2.neg()),
g2_zero,
"Inverse add b failed"
);

/*
NOTE(marian): pairing fuzzer must be added here once it is implemented for the curve
*/
});
100 changes: 0 additions & 100 deletions fuzz/no_gpu_fuzz/fuzz_targets/curve/curve_bn254.rs

This file was deleted.

Loading
Loading