Skip to content

Commit

Permalink
Merge branch 'main' into stone-compatible-proof-of-work
Browse files Browse the repository at this point in the history
  • Loading branch information
schouhy authored Oct 24, 2023
2 parents 7d4f44e + f940e14 commit db0616d
Show file tree
Hide file tree
Showing 21 changed files with 559 additions and 284 deletions.
6 changes: 6 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ark-test-curves = { git = "https://github.com/arkworks-rs/algebra", rev = "ef8f7
ark-std = "0.4.0"
rand = "0.8.5"
rand_chacha = "0.3.1"
starknet-curve = { git = "https://github.com/xJonathanLEI/starknet-rs" }
starknet-ff = { git = "https://github.com/xJonathanLEI/starknet-rs" }

[dependencies.lambdaworks-math]
path = "../math"
Expand Down Expand Up @@ -39,3 +41,7 @@ harness = false
[[bench]]
name = "pow"
harness = false

[[bench]]
name = "point"
harness = false
77 changes: 77 additions & 0 deletions benches/benches/point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lambdaworks_math::{
cyclic_group::IsGroup,
elliptic_curve::{short_weierstrass::curves::stark_curve::StarkCurve, traits::IsEllipticCurve},
};
use starknet_curve::{curve_params::GENERATOR, AffinePoint, ProjectivePoint};
use std::ops::AddAssign;

const BENCHMARK_NAME: &str = "point";

pub fn criterion_benchmark(c: &mut Criterion) {
let initial_projective_point = ProjectivePoint::from(&GENERATOR);
let second_project_point = initial_projective_point;

// This is the code we are going to bench
// We test it once outside the bench to check the result matches with Lambdaworks
let mut projective_point = initial_projective_point;
for _i in 0..10000 {
projective_point.add_assign(&second_project_point);
}

let starknet_rs_x = AffinePoint::from(&projective_point).x.to_string();
println!("Starknet RS result X: {} ", starknet_rs_x);
let starknet_rs_y = AffinePoint::from(&projective_point).y.to_string();
print!("Starknet RS result Y: {} ", starknet_rs_y);

{
c.bench_function(
&format!("{} 10k Operations | Starknet RS ", BENCHMARK_NAME),
|b| {
b.iter(|| {
let mut projective_point = initial_projective_point;
// We loop to have a higher variance of numbers, and make the time of the clones not relevant
for _i in 0..10000 {
projective_point.add_assign(&second_project_point);
}
projective_point
});
},
);
}

let initial_projective_point = StarkCurve::generator();
let second_projective_point = initial_projective_point.clone();

// This is the code we are going to bench
// We test it once outside the bench to check the result matches with Starknet RS
let mut projective_point = initial_projective_point.clone();
for _i in 0..10000 {
projective_point =
black_box(projective_point.operate_with(black_box(&second_projective_point)));
}
let lambdaworks_x = projective_point.to_affine().x().to_string();
let lambdaworks_y = projective_point.to_affine().y().to_string();
println!("Lambdaworks result, X: {}", lambdaworks_x);
println!("Lambdaworks result, Y: {}", lambdaworks_y);

{
c.bench_function(
&format!("{} 10k Operations | Lambdaworks", BENCHMARK_NAME),
|b| {
b.iter(|| {
let mut projective_point = initial_projective_point.clone();
for _i in 0..10000 {
projective_point = black_box(
projective_point.operate_with(black_box(&second_projective_point)),
);
}
projective_point
});
},
);
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
135 changes: 82 additions & 53 deletions docs/src/starks/protocol.md

Large diffs are not rendered by default.

127 changes: 71 additions & 56 deletions docs/src/starks/protocol_overview.md

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions math/src/elliptic_curve/edwards/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::elliptic_curve::traits::IsEllipticCurve;
use crate::field::element::FieldElement;
use std::fmt::Debug;

use core::fmt::Debug;
/// Trait to add elliptic curves behaviour to a struct.
pub trait IsEdwards: IsEllipticCurve + Clone + Debug {
fn a() -> FieldElement<Self::BaseField>;
Expand Down
2 changes: 1 addition & 1 deletion math/src/elliptic_curve/montgomery/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::elliptic_curve::traits::IsEllipticCurve;
use crate::field::element::FieldElement;
use std::fmt::Debug;
use core::fmt::Debug;

/// Trait to add elliptic curves behaviour to a struct.
pub trait IsMontgomery: IsEllipticCurve + Clone + Debug {
Expand Down
3 changes: 1 addition & 2 deletions math/src/elliptic_curve/point.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::elliptic_curve::traits::IsEllipticCurve;
use crate::field::element::FieldElement;
use std::fmt::Debug;

use core::fmt::Debug;
/// Represents an elliptic curve point using the projective short Weierstrass form:
/// y^2 * z = x^3 + a * x * z^2 + b * z^3,
/// where `x`, `y` and `z` variables are field elements.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use super::field_extension::BLS12381PrimeField;
use crate::cyclic_group::IsGroup;
use crate::elliptic_curve::short_weierstrass::curves::bls12_381::curve::BLS12381Curve;
use crate::elliptic_curve::short_weierstrass::point::ShortWeierstrassProjectivePoint;
use crate::elliptic_curve::traits::FromAffine;
use crate::field::element::FieldElement;
use crate::unsigned_integer::element::U256;

#[cfg(feature = "std")]
use crate::{
elliptic_curve::short_weierstrass::curves::bls12_381::curve::BLS12381Curve,
errors::ByteConversionError, traits::ByteConversion,
elliptic_curve::traits::FromAffine, errors::ByteConversionError, traits::ByteConversion,
};
use std::cmp::Ordering;
use std::ops::Neg;
#[cfg(feature = "std")]
use std::{cmp::Ordering, ops::Neg};

pub type G1Point = ShortWeierstrassProjectivePoint<BLS12381Curve>;
pub type BLS12381FieldElement = FieldElement<BLS12381PrimeField>;
Expand All @@ -22,6 +23,7 @@ pub fn check_point_is_in_subgroup(point: &G1Point) -> bool {
inf == aux_point
}

#[cfg(feature = "std")]
pub fn decompress_g1_point(input_bytes: &mut [u8; 48]) -> Result<G1Point, ByteConversionError> {
let first_byte = input_bytes.first().unwrap();
// We get the 3 most significant bits
Expand Down Expand Up @@ -71,6 +73,7 @@ pub fn decompress_g1_point(input_bytes: &mut [u8; 48]) -> Result<G1Point, ByteCo
.ok_or(ByteConversionError::PointNotInSubgroup)
}

#[cfg(feature = "std")]
pub fn compress_g1_point(point: &G1Point) -> Vec<u8> {
if *point == G1Point::neutral_element() {
// point is at infinity
Expand Down Expand Up @@ -100,13 +103,15 @@ pub fn compress_g1_point(point: &G1Point) -> Vec<u8> {
#[cfg(test)]
mod tests {
use super::{BLS12381FieldElement, G1Point};
use crate::cyclic_group::IsGroup;
use crate::elliptic_curve::short_weierstrass::curves::bls12_381::curve::BLS12381Curve;
use crate::elliptic_curve::traits::{FromAffine, IsEllipticCurve};
use crate::traits::ByteConversion;
use crate::unsigned_integer::element::UnsignedInteger;

#[cfg(feature = "std")]
use super::{compress_g1_point, decompress_g1_point};
#[cfg(feature = "std")]
use crate::{
cyclic_group::IsGroup, traits::ByteConversion, unsigned_integer::element::UnsignedInteger,
};

#[test]
fn test_zero_point() {
Expand All @@ -121,6 +126,7 @@ mod tests {
assert!(!super::check_point_is_in_subgroup(&false_point2));
}

#[cfg(feature = "std")]
#[test]
fn test_g1_compress_generator() {
let g = BLS12381Curve::generator();
Expand All @@ -136,6 +142,7 @@ mod tests {
assert_eq!(*g_x, compressed_g_x);
}

#[cfg(feature = "std")]
#[test]
fn test_g1_compress_point_at_inf() {
let inf = G1Point::neutral_element();
Expand All @@ -145,6 +152,7 @@ mod tests {
assert_eq!(*first_byte >> 6, 3_u8);
}

#[cfg(feature = "std")]
#[test]
fn test_compress_decompress_generator() {
let g = BLS12381Curve::generator();
Expand All @@ -156,6 +164,7 @@ mod tests {
assert_eq!(g, decompressed_g);
}

#[cfg(feature = "std")]
#[test]
fn test_compress_decompress_2g() {
let g = BLS12381Curve::generator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::unsigned_integer::element::U384;
use crate::{
field::{
element::FieldElement,
errors::FieldError,
extensions::{
cubic::{CubicExtensionField, HasCubicNonResidue},
quadratic::{HasQuadraticNonResidue, QuadraticExtensionField},
},
fields::montgomery_backed_prime_fields::{IsModulus, MontgomeryBackendPrimeField},
traits::IsField,
use crate::field::{
element::FieldElement,
errors::FieldError,
extensions::{
cubic::{CubicExtensionField, HasCubicNonResidue},
quadratic::{HasQuadraticNonResidue, QuadraticExtensionField},
},
traits::ByteConversion,
fields::montgomery_backed_prime_fields::{IsModulus, MontgomeryBackendPrimeField},
traits::IsField,
};
use crate::unsigned_integer::element::U384;

#[cfg(feature = "std")]
use crate::traits::ByteConversion;

pub const BLS12381_PRIME_FIELD_ORDER: U384 = U384::from_hex_unchecked("1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab");

Expand Down Expand Up @@ -105,6 +105,7 @@ impl IsField for Degree2ExtensionField {
}
}

#[cfg(feature = "std")]
impl ByteConversion for FieldElement<Degree2ExtensionField> {
fn to_bytes_be(&self) -> Vec<u8> {
let mut byte_slice = ByteConversion::to_bytes_be(&self.value()[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod compression;
pub mod curve;
pub mod default_types;
pub mod field_extension;
pub mod pairing;
pub mod sqrt;
pub mod twist;

#[cfg(feature = "std")]
pub mod pairing;
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use super::{
curve::BLS12381Curve,
field_extension::{Degree12ExtensionField, Degree2ExtensionField},
twist::BLS12381TwistCurve,
use super::field_extension::{Degree12ExtensionField, Degree2ExtensionField};
use crate::{
elliptic_curve::short_weierstrass::curves::bls12_381::field_extension::Degree6ExtensionField,
field::element::FieldElement, unsigned_integer::element::UnsignedInteger,
};

use super::{curve::BLS12381Curve, twist::BLS12381TwistCurve};
use crate::{
cyclic_group::IsGroup,
elliptic_curve::{
short_weierstrass::{
curves::bls12_381::field_extension::{Degree6ExtensionField, LevelTwoResidue},
point::ShortWeierstrassProjectivePoint,
traits::IsShortWeierstrass,
},
traits::IsPairing,
},
field::{element::FieldElement, extensions::cubic::HasCubicNonResidue},
unsigned_integer::element::UnsignedInteger,
elliptic_curve::short_weierstrass::curves::bls12_381::field_extension::LevelTwoResidue,
elliptic_curve::short_weierstrass::point::ShortWeierstrassProjectivePoint,
elliptic_curve::short_weierstrass::traits::IsShortWeierstrass,
elliptic_curve::traits::IsPairing, field::extensions::cubic::HasCubicNonResidue,
};

#[derive(Clone)]
Expand Down Expand Up @@ -42,7 +38,6 @@ impl IsPairing for BLS12381AtePairing {

/// This is equal to the frobenius trace of the BLS12 381 curve minus one.
const MILLER_LOOP_CONSTANT: u64 = 0xd201000000010000;

fn double_accumulate_line(
t: &mut ShortWeierstrassProjectivePoint<BLS12381TwistCurve>,
p: &ShortWeierstrassProjectivePoint<BLS12381Curve>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::field::traits::LegendreSymbol;

use super::{curve::BLS12381FieldElement, curve::BLS12381TwistCurveFieldElement};
use std::cmp::Ordering;
use core::cmp::Ordering;

#[must_use]
pub fn select_sqrt_value_from_third_bit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ mod tests {
elliptic_curve::{
short_weierstrass::{
curves::bls12_381::field_extension::{BLS12381PrimeField, Degree2ExtensionField},
point::{Endianness, PointFormat, ShortWeierstrassProjectivePoint},
traits::IsShortWeierstrass,
},
traits::IsEllipticCurve,
Expand All @@ -98,6 +97,11 @@ mod tests {
type Level0FE = FieldElement<BLS12381PrimeField>;
type Level1FE = FieldElement<Degree2ExtensionField>;

#[cfg(feature = "std")]
use crate::elliptic_curve::short_weierstrass::point::{
Endianness, PointFormat, ShortWeierstrassProjectivePoint,
};

#[test]
fn create_generator() {
let g = BLS12381TwistCurve::generator();
Expand All @@ -108,6 +112,7 @@ mod tests {
);
}

#[cfg(feature = "std")]
#[test]
fn serialize_deserialize_generator() {
let g = BLS12381TwistCurve::generator();
Expand Down
Loading

0 comments on commit db0616d

Please sign in to comment.