Skip to content

Commit

Permalink
remove diff fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
PatStiles committed Nov 22, 2023
1 parent eef6b8d commit 2f237b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 182 deletions.
7 changes: 0 additions & 7 deletions fuzz/no_gpu_fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,9 @@ num-traits = "0.2"
ibig = "0.3.6"
p3-goldilocks = { git = "https://github.com/Plonky3/Plonky3", rev = "41cd843" }
p3-field = { 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" }

ark-bls12-381 = "0.4.0"
ark-ec = "0.4.0"
ark-ff = "0.4.0"
ark-poly = "0.4.0"
ark-std = "0.4.0"

[[bin]]
name = "field_fuzzer"
path = "fuzz_targets/field_fuzzer.rs"
Expand Down
186 changes: 11 additions & 175 deletions fuzz/no_gpu_fuzz/fuzz_targets/curve_bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use lambdaworks_math::{
field::element::FieldElement,
cyclic_group::IsGroup,
elliptic_curve::{
traits::{IsEllipticCurve, EllipticCurveError, IsPairing},
traits::{IsEllipticCurve, IsPairing},
short_weierstrass::{
curves::bls12_381::{
curve::{BLS12381FieldElement, BLS12381TwistCurveFieldElement, BLS12381Curve},
curve::BLS12381Curve,
twist::BLS12381TwistCurve,
pairing::BLS12381AtePairing,
field_extension::Degree12ExtensionField,
Expand All @@ -17,186 +17,22 @@ use lambdaworks_math::{
}
},
unsigned_integer::element::U384,
traits::ByteConversion,
};

use ark_bls12_381::{Fr, Fq, G1Projective as G1, G2Projective as G2};
use ark_ec::{CurveGroup, short_weierstrass::Affine};
use ark_ff::{PrimeField, QuadExtField, BigInteger};


type FeG1 = BLS12381FieldElement;
type FeG2 = BLS12381TwistCurveFieldElement;
type LambdaG1 = ShortWeierstrassProjectivePoint<BLS12381Curve>;
type LambdaG2 = ShortWeierstrassProjectivePoint<BLS12381TwistCurve>;

fn create_g1_points(points: (u64, u64, u64, u64)) -> Result<(LambdaG1, LambdaG1), EllipticCurveError> {
let (val_a_x, val_a_y, val_b_x, val_b_y) = points;

let a_x = FeG1::from(val_a_x);
let a_y = FeG1::from(val_a_y);

let b_x = FeG1::from(val_b_x);
let b_y = FeG1::from(val_b_y);

// Create G1 points
let a = BLS12381Curve::create_point_from_affine(a_x, a_y)?;
let b = BLS12381Curve::create_point_from_affine(b_x, b_y)?;
Ok((a, b))
}

fn create_g2_points(points: (u64, u64, u64, u64)) -> Result<(LambdaG2, LambdaG2), EllipticCurveError> {
let (val_a_x, val_a_y, val_b_x, val_b_y) = points;

let a_x = FeG2::from(val_a_x);
let a_y = FeG2::from(val_a_y);

let b_x = FeG2::from(val_b_x);
let b_y = FeG2::from(val_b_y);

// Create G2 points
let a = BLS12381TwistCurve::create_point_from_affine(a_x, a_y)?;
let b = BLS12381TwistCurve::create_point_from_affine(b_x, b_y)?;
Ok((a, b))
}

fn create_ark_g1_points(points: (u64, u64, u64, u64)) -> (G1, G1) {
let (val_a_x, val_a_y, val_b_x, val_b_y) = points;

let a = G1::from(
Affine::new(
Fq::from(val_a_x),
Fq::from(val_a_y)
)
);
let b = G1::from(
Affine::new(
Fq::from(val_b_x),
Fq::from(val_b_y)
)
);

(a, b)
}

fn create_ark_g2_points(points: (u64, u64, u64, u64)) -> (G2, G2) {
let (val_a_x, val_a_y, val_b_x, val_b_y) = points;

let a = G2::from(
Affine::new(
QuadExtField::from(val_a_x),
QuadExtField::from(val_a_y)
)
);
let b = G2::from(
Affine::new(
QuadExtField::from(val_b_x),
QuadExtField::from(val_b_y)
)
);

(a, b)
}

fn g1_equal(lambda_g1: &LambdaG1, ark_g1: &G1) {
assert_eq!(lambda_g1.x().to_bytes_be(), ark_g1.x.into_bigint().to_bytes_be());
assert_eq!(lambda_g1.y().to_bytes_be(), ark_g1.y.into_bigint().to_bytes_be());
assert_eq!(lambda_g1.z().to_bytes_be(), ark_g1.z.into_bigint().to_bytes_be());
}

fn g2_equal(lambda_g2: &LambdaG2, ark_g2: &G2) {
// https://github.com/arkworks-rs/algebra/blob/master/ff/src/fields/models/quadratic_extension.rs#L106
assert_eq!(lambda_g2.x().value()[0].to_bytes_be(), ark_g2.x.c0.into_bigint().to_bytes_be());
assert_eq!(lambda_g2.x().value()[1].to_bytes_be(), ark_g2.x.c1.into_bigint().to_bytes_be());
assert_eq!(lambda_g2.y().value()[0].to_bytes_be(), ark_g2.y.c0.into_bigint().to_bytes_be());
assert_eq!(lambda_g2.y().value()[1].to_bytes_be(), ark_g2.y.c1.into_bigint().to_bytes_be());
assert_eq!(lambda_g2.z().value()[0].to_bytes_be(), ark_g2.z.c0.into_bigint().to_bytes_be());
assert_eq!(lambda_g2.z().value()[1].to_bytes_be(), ark_g2.z.c1.into_bigint().to_bytes_be());
}

//TODO: derive arbitrary for Affine and Projective or change this to use &[u8] as input to cover more cases.
//TODO: use more advanced options to generate values over curve specifically given most inputs will fail curve check.
//NOTE: Ark serialize for QuadExtField compresses the first coeff.
fuzz_target!(|values: ((u64, u64, u64, u64), (u64, u64, u64, u64))| {

let (g1_points, g2_points) = values;

// Create G1 Lambdaworks points
let (a_g1, b_g1) = match create_g1_points(g1_points) {
Ok(v) => v,
Err(_) => return
};

// Create G2 Lambdaworks points
let (a_g2, b_g2) = match create_g2_points(g2_points) {
Ok(v) => v,
Err(_) => return
};

// Create G1 Arkworks points
let (a_expected_g1, b_expected_g1) = create_ark_g1_points(g1_points);

// Create G2 Arkworks points
let (a_expected_g2, b_expected_g2) = create_ark_g2_points(g2_points);

// ***OPERATION SOUNDNESS***
// op_with
let op_with_g1 = a_g1.operate_with(&a_g1);
let op_with_g2 = a_g2.operate_with(&a_g2);
let operate_with_g1 = a_expected_g1 + b_expected_g1;
let operate_with_g2 = a_expected_g2 + b_expected_g2;
// op_with G1
g1_equal(&op_with_g1, &operate_with_g1);
// op_with() G2
g2_equal(&op_with_g2, &operate_with_g2);


// Neg()
let neg_g1 = a_g1.neg();
let neg_g2 = a_g2.neg();
let negative_g1 = -a_expected_g1;
let negative_g2 = -a_expected_g2;
// Neg() G1
g1_equal(&neg_g1, &negative_g1);
// Neg() G2
g2_equal(&neg_g2, &negative_g2);

// -
let sub_g1 = a_g1.operate_with(&b_g1.neg());
let sub_g2 = a_g2.operate_with(&b_g2.neg());
let subtraction_g1 = &a_expected_g1 - &b_expected_g1;
let subtraction_g2 = &a_expected_g2 - &b_expected_g2;
// - G1
g1_equal(&sub_g1, &subtraction_g1);
// - G2
g2_equal(&sub_g2, &subtraction_g2);


// operate_with_self
let op_with_self_g1 = a_g1.operate_with_self(g1_points.0);
let op_with_self_g2 = a_g2.operate_with_self(g2_points.0);
let operate_with_self_g1 = a_expected_g1 * Fr::from(g1_points.0);
let operate_with_self_g2 = a_expected_g2 * Fr::from(g2_points.0);
// operate_with_self G1
g1_equal(&op_with_self_g1, &operate_with_self_g1);
// operate_with_self G2
g2_equal(&op_with_self_g2, &operate_with_self_g2);

//to_affine()
let to_aff_g1 = a_g1.to_affine();
let to_aff_g2 = a_g2.to_affine();
let to_affine_g1 = a_expected_g1.into_affine();
let to_affine_g2 = a_expected_g2.into_affine();
//TODO: Investigate normalization of projective coordinates in arkworks to allow for differential fuzzing.
fuzz_target!(|values: (u64, u64)| {
let (a_val, b_val) = values;

// to_affine() G1
assert_eq!(to_aff_g1.x().to_bytes_be(), to_affine_g1.x.into_bigint().to_bytes_be());
assert_eq!(to_aff_g1.y().to_bytes_be(), to_affine_g1.y.into_bigint().to_bytes_be());
let a_g1 = BLS12381Curve::generator().operate_with_self(a_val);
let b_g1 = BLS12381Curve::generator().operate_with_self(b_val);

// to_affine() G2
assert_eq!(to_aff_g2.x().value()[0].to_bytes_be(), to_affine_g2.x.c0.into_bigint().to_bytes_be());
assert_eq!(to_aff_g2.x().value()[1].to_bytes_be(), to_affine_g2.x.c1.into_bigint().to_bytes_be());
assert_eq!(to_aff_g2.y().value()[0].to_bytes_be(), to_affine_g2.y.c0.into_bigint().to_bytes_be());
assert_eq!(to_aff_g2.y().value()[1].to_bytes_be(), to_affine_g2.y.c1.into_bigint().to_bytes_be());
let a_g2 = BLS12381TwistCurve::generator().operate_with_self(a_val);
let b_g2 = BLS12381TwistCurve::generator().operate_with_self(b_val);

// ***AXIOM SOUNDNESS***
let g1_zero = LambdaG1::neutral_element();
Expand Down Expand Up @@ -242,8 +78,8 @@ fuzz_target!(|values: ((u64, u64, u64, u64), (u64, u64, u64, u64))| {
assert_eq!(b_g2.operate_with(&b_g2.neg()), g2_zero, "Inverse add b failed");

// Pairing Bilinearity
let a = U384::from_u64(11);
let b = U384::from_u64(93);
let a = U384::from_u64(a_val);
let b = U384::from_u64(b_val);
let result = BLS12381AtePairing::compute_batch(&[
(
&a_g1.operate_with_self(a).to_affine(),
Expand Down

0 comments on commit 2f237b4

Please sign in to comment.