Skip to content

Commit

Permalink
Merge branch 'main' into affine-serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mb-dci authored Nov 27, 2023
2 parents 31208f9 + 5233228 commit bb7928c
Show file tree
Hide file tree
Showing 47 changed files with 3,190 additions and 1,409 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["math", "crypto", "gpu", "benches", "provers/plonk", "provers/stark", "provers/cairo", "provers/groth16", "examples/merkle-tree-cli"]
members = ["math", "crypto", "gpu", "benches", "provers/plonk", "provers/stark", "provers/cairo", "provers/groth16", "examples/merkle-tree-cli", "winterfell_adapter"]
exclude = ["ensure-no_std"]
resolver = "2"

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ List of symbols:
| BLS12-381 | :heavy_check_mark: | :heavy_check_mark: | | | |
| BLS12-377 | 🏗️ | :heavy_check_mark: | | :heavy_check_mark: | |
| BN-254 | :x: | :heavy_check_mark: | | | |
| Pallas | :x: | :heavy_check_mark: | | | |
| Vesta | :x: | :heavy_check_mark: | | | |
| Pallas | 🏗️ | :heavy_check_mark: | | | |
| Vesta | 🏗️ | :heavy_check_mark: | | | |
| Bandersnatch | 🏗️ | :heavy_check_mark: | | | |
| **STARKs** | **Lambdaworks** | **Arkworks** | **Halo2** | **gnark** | **Constantine** |
| STARK Prover | :heavy_check_mark: | :x: | | :x: | |
| CAIRO Prover | 🏗️ | :x: | | :x: | |
| **SNARKs** |**Lambdaworks** | **Arkworks** | **Halo2** | **gnark** | **Constantine** |
| Groth16 | :x: | :heavy_check_mark: | | :heavy_check_mark: | |
| Groth16 | 🏗️ | :heavy_check_mark: | | :heavy_check_mark: | |
| Plonk | 🏗️ | :x: | ✔️ | :heavy_check_mark: | |
| Spartan | :x: | :heavy_check_mark: | | | |
| Marlin | :x: | :heavy_check_mark: | | | |
Expand Down Expand Up @@ -70,6 +70,7 @@ So, we decided to build our library, focusing on performance, with clear documen
- [Crypto primitives](https://github.com/lambdaclass/lambdaworks/tree/main/crypto)
- [Plonk Prover](https://github.com/lambdaclass/lambdaworks/tree/main/provers/plonk)
- [Cairo Prover](https://github.com/lambdaclass/lambdaworks/tree/main/provers/cairo)
- [Groth 16](https://github.com/lambdaclass/lambdaworks/tree/main/provers/groth16)

If you are interested in proving Cairo programs, use the Cairo Prover CLI.

Expand Down
1 change: 1 addition & 0 deletions examples/merkle-tree-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
7 changes: 6 additions & 1 deletion fuzz/no_gpu_fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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" }

Expand All @@ -26,6 +25,12 @@ path = "fuzz_targets/field_fuzzer.rs"
test = false
doc = false

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

[[bin]]
name = "field_fuzz_mersenne31"
path = "fuzz_targets/field_mersenne31.rs"
Expand Down
101 changes: 101 additions & 0 deletions fuzz/no_gpu_fuzz/fuzz_targets/curve_bls12_381.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#![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,
},
point::ShortWeierstrassProjectivePoint,
}
},
unsigned_integer::element::U384,
};

type LambdaG1 = ShortWeierstrassProjectivePoint<BLS12381Curve>;
type LambdaG2 = ShortWeierstrassProjectivePoint<BLS12381TwistCurve>;

//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.
//TODO: Investigate normalization of projective coordinates in arkworks to allow for differential fuzzing.
fuzz_target!(|values: (u64, u64)| {
let (a_val, b_val) = values;

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

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();

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");

// Pairing Bilinearity
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(),
&a_g2.operate_with_self(b).to_affine(),
),
(
&a_g1.operate_with_self(a * b).to_affine(),
&a_g2.neg().to_affine(),
),
]);
assert_eq!(result, 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(&[(&LambdaG1::neutral_element(), &a_g2.to_affine())]);
assert_eq!(result, FieldElement::<Degree12ExtensionField>::one());
});
2 changes: 2 additions & 0 deletions math/src/field/extensions/cubic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::field::element::FieldElement;
use crate::field::errors::FieldError;
use crate::field::traits::IsField;
#[cfg(feature = "lambdaworks-serde-binary")]
use crate::traits::ByteConversion;
use core::fmt::Debug;
use core::marker::PhantomData;
Expand All @@ -25,6 +26,7 @@ pub trait HasCubicNonResidue {
fn residue() -> FieldElement<Self::BaseField>;
}

#[cfg(feature = "lambdaworks-serde-binary")]
impl<F> ByteConversion for [FieldElement<F>; 3]
where
F: IsField,
Expand Down
2 changes: 2 additions & 0 deletions math/src/field/extensions/quadratic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::field::element::FieldElement;
use crate::field::errors::FieldError;
use crate::field::traits::IsField;
#[cfg(feature = "lambdaworks-serde-binary")]
use crate::traits::ByteConversion;
use core::fmt::Debug;
use core::marker::PhantomData;
Expand Down Expand Up @@ -33,6 +34,7 @@ where
}
}

#[cfg(feature = "lambdaworks-serde-binary")]
impl<F> ByteConversion for [FieldElement<F>; 2]
where
F: IsField,
Expand Down
2 changes: 2 additions & 0 deletions math/src/field/fields/p448_goldilocks_prime_field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::errors::CreationError;
use crate::field::errors::FieldError;
use crate::field::traits::{IsField, IsPrimeField};
#[cfg(feature = "lambdaworks-serde-binary")]
use crate::traits::ByteConversion;
use crate::unsigned_integer::element::UnsignedInteger;

Expand All @@ -20,6 +21,7 @@ pub struct U56x8 {
limbs: [u64; 8],
}

#[cfg(feature = "lambdaworks-serde-binary")]
impl ByteConversion for U56x8 {
#[cfg(feature = "std")]
fn to_bytes_be(&self) -> Vec<u8> {
Expand Down
4 changes: 3 additions & 1 deletion math/src/field/fields/u64_goldilocks_field.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::fmt::{self, Display};

#[cfg(feature = "lambdaworks-serde-binary")]
use crate::traits::ByteConversion;
use crate::{
errors::CreationError,
field::{
Expand All @@ -8,7 +10,6 @@ use crate::{
extensions::quadratic::{HasQuadraticNonResidue, QuadraticExtensionField},
traits::{IsField, IsPrimeField},
},
traits::ByteConversion,
};

/// Goldilocks Prime Field F_p where p = 2^64 - 2^32 + 1;
Expand All @@ -21,6 +22,7 @@ impl Goldilocks64Field {
pub const NEG_ORDER: u64 = Self::ORDER.wrapping_neg();
}

#[cfg(feature = "lambdaworks-serde-binary")]
impl ByteConversion for u64 {
#[cfg(feature = "std")]
fn to_bytes_be(&self) -> Vec<u8> {
Expand Down
5 changes: 4 additions & 1 deletion math/src/field/test_fields/u32_test_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use crate::{
errors::CreationError,
field::errors::FieldError,
field::traits::{IsFFTField, IsField, IsPrimeField},
traits::ByteConversion,
};

#[cfg(feature = "lambdaworks-serde-binary")]
use crate::traits::ByteConversion;

#[derive(Debug, Clone, PartialEq, Eq)]

pub struct U32Field<const MODULUS: u32>;

#[cfg(feature = "lambdaworks-serde-binary")]
impl ByteConversion for u32 {
#[cfg(feature = "std")]
fn to_bytes_be(&self) -> Vec<u8> {
Expand Down
9 changes: 6 additions & 3 deletions math/src/field/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{element::FieldElement, errors::FieldError};
use crate::{
errors::CreationError, traits::ByteConversion, unsigned_integer::traits::IsUnsignedInteger,
};
#[cfg(feature = "lambdaworks-serde-binary")]
use crate::traits::ByteConversion;
use crate::{errors::CreationError, unsigned_integer::traits::IsUnsignedInteger};
use core::fmt::Debug;

/// Represents different configurations that powers of roots of unity can be in. Some of these may
Expand Down Expand Up @@ -54,7 +54,10 @@ pub trait IsFFTField: IsPrimeField {
pub trait IsField: Debug + Clone {
/// The underlying base type for representing elements from the field.
// TODO: Relax Unpin for non cuda usage
#[cfg(feature = "lambdaworks-serde-binary")]
type BaseType: Clone + Debug + Unpin + ByteConversion;
#[cfg(not(feature = "lambdaworks-serde-binary"))]
type BaseType: Clone + Debug + Unpin;

/// Returns the sum of `a` and `b`.
fn add(a: &Self::BaseType, b: &Self::BaseType) -> Self::BaseType;
Expand Down
1 change: 1 addition & 0 deletions provers/cairo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ build_metal:

clean:
rm -f $(CAIRO0_PROGRAMS_DIR)/*.json
rm -f $(CAIRO0_PROGRAMS_DIR)/*.proof
rm -f $(CAIRO0_PROGRAMS_DIR)/*.trace
rm -f $(CAIRO0_PROGRAMS_DIR)/*.memory

Expand Down
1 change: 1 addition & 0 deletions provers/cairo/cairo_programs/cairo0/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.json
*.proof
Loading

0 comments on commit bb7928c

Please sign in to comment.