From 5e611116c4adc43ba4046babb932d15ef9299e83 Mon Sep 17 00:00:00 2001 From: c-node Date: Sun, 29 Dec 2024 10:34:08 -0500 Subject: [PATCH 1/4] create fr chip --- .../core/machine/src/syscall/precompiles/fptower/fr.rs | 10 ++++++++++ .../machine/src/syscall/precompiles/fptower/mod.rs | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 crates/core/machine/src/syscall/precompiles/fptower/fr.rs diff --git a/crates/core/machine/src/syscall/precompiles/fptower/fr.rs b/crates/core/machine/src/syscall/precompiles/fptower/fr.rs new file mode 100644 index 0000000000..495e5b2119 --- /dev/null +++ b/crates/core/machine/src/syscall/precompiles/fptower/fr.rs @@ -0,0 +1,10 @@ +use std::marker::PhantomData; +pub struct FrOpChip

{ + _marker: PhantomData

, +} + +impl

FrOpChip

{ + pub const fn new() -> Self { + Self { _marker: PhantomData } + } +} \ No newline at end of file diff --git a/crates/core/machine/src/syscall/precompiles/fptower/mod.rs b/crates/core/machine/src/syscall/precompiles/fptower/mod.rs index bf097adc05..123d743e89 100644 --- a/crates/core/machine/src/syscall/precompiles/fptower/mod.rs +++ b/crates/core/machine/src/syscall/precompiles/fptower/mod.rs @@ -1,10 +1,12 @@ mod fp; +mod fr; mod fp2_addsub; mod fp2_mul; pub use fp::*; pub use fp2_addsub::*; pub use fp2_mul::*; +pub use fr::*; #[cfg(test)] mod tests { From 854694f1d4201c5239035a316206085b0fc92ca3 Mon Sep 17 00:00:00 2001 From: c-node Date: Sun, 29 Dec 2024 10:58:22 -0500 Subject: [PATCH 2/4] event --- crates/core/executor/src/events/precompiles/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/core/executor/src/events/precompiles/mod.rs b/crates/core/executor/src/events/precompiles/mod.rs index 29e62fd17c..7d3340969d 100644 --- a/crates/core/executor/src/events/precompiles/mod.rs +++ b/crates/core/executor/src/events/precompiles/mod.rs @@ -59,6 +59,9 @@ pub enum PrecompileEvent { Bn254Fp2AddSub(Fp2AddSubEvent), /// Bn254 quadratic field mul precompile event. Bn254Fp2Mul(Fp2MulEvent), + /// Bn254 scalar field operation precompile event. + /// (Maybe it can use the FpOpEvent?) + Bn254Fr(FpOpEvent), /// Bls12-381 curve add precompile event. Bls12381Add(EllipticCurveAddEvent), /// Bls12-381 curve double precompile event. @@ -126,7 +129,9 @@ impl PrecompileLocalMemory for Vec<(SyscallEvent, PrecompileEvent)> { PrecompileEvent::U256xU2048Mul(e) => { iterators.push(e.local_mem_access.iter()); } - PrecompileEvent::Bls12381Fp(e) | PrecompileEvent::Bn254Fp(e) => { + PrecompileEvent::Bls12381Fp(e) + | PrecompileEvent::Bn254Fp(e) + | PrecompileEvent::Bn254Fr(e) => { iterators.push(e.local_mem_access.iter()); } PrecompileEvent::Bls12381Fp2AddSub(e) | PrecompileEvent::Bn254Fp2AddSub(e) => { From b800a06d7cdbd54c6de79bc30e2474c96a39bb7e Mon Sep 17 00:00:00 2001 From: c-node Date: Sun, 29 Dec 2024 20:34:13 -0500 Subject: [PATCH 3/4] add type to bn254 --- .../src/syscall/precompiles/fptower/fr.rs | 10 ----- .../src/syscall/precompiles/fptower/mod.rs | 2 - crates/curves/src/weierstrass/bn254.rs | 39 +++++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) delete mode 100644 crates/core/machine/src/syscall/precompiles/fptower/fr.rs diff --git a/crates/core/machine/src/syscall/precompiles/fptower/fr.rs b/crates/core/machine/src/syscall/precompiles/fptower/fr.rs deleted file mode 100644 index 495e5b2119..0000000000 --- a/crates/core/machine/src/syscall/precompiles/fptower/fr.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::marker::PhantomData; -pub struct FrOpChip

{ - _marker: PhantomData

, -} - -impl

FrOpChip

{ - pub const fn new() -> Self { - Self { _marker: PhantomData } - } -} \ No newline at end of file diff --git a/crates/core/machine/src/syscall/precompiles/fptower/mod.rs b/crates/core/machine/src/syscall/precompiles/fptower/mod.rs index 123d743e89..bf097adc05 100644 --- a/crates/core/machine/src/syscall/precompiles/fptower/mod.rs +++ b/crates/core/machine/src/syscall/precompiles/fptower/mod.rs @@ -1,12 +1,10 @@ mod fp; -mod fr; mod fp2_addsub; mod fp2_mul; pub use fp::*; pub use fp2_addsub::*; pub use fp2_mul::*; -pub use fr::*; #[cfg(test)] mod tests { diff --git a/crates/curves/src/weierstrass/bn254.rs b/crates/curves/src/weierstrass/bn254.rs index a2dd2aa32c..c95b38e288 100644 --- a/crates/curves/src/weierstrass/bn254.rs +++ b/crates/curves/src/weierstrass/bn254.rs @@ -15,6 +15,40 @@ pub struct Bn254Parameters; pub type Bn254 = SwCurve; +#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)] +/// Bn254 scalar field parameter +pub struct Bn254ScalarField; +impl FieldParameters for Bn254ScalarField { + const MODULUS: &'static [u8] = &[ + 1, 0, 0, 240, 147, 245, 225, 67, 145, 112, 185, 121, 72, 232, 51, 40, 93, 88, 129, + 129, 182, 69, 80, 184, 41, 160, 49, 225, 114, 78, 100, 48, + ]; + const WITNESS_OFFSET: usize = 1usize << 14; + fn modulus() -> BigUint { + BigUint::from_str_radix( + "21888242871839275222246405745257275088548364400416034343698204186575808495617", + 10, + ) + .unwrap() + } + +} + +impl Bn254ScalarField { + pub fn print_modulus() { + println!("{:?}", Self::modulus().to_bytes_le()); + } +} + +impl FpOpField for Bn254ScalarField { + const FIELD_TYPE: FieldType = FieldType::Bn254; +} + +impl NumLimbs for Bn254ScalarField { + type Limbs = U32; + type Witness = U62; +} + #[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)] /// Bn254 base field parameter pub struct Bn254BaseField; @@ -95,6 +129,11 @@ mod tests { use super::*; use crate::utils::biguint_from_limbs; + #[test] + fn test_print_scalar_modulus() { + Bn254ScalarField::print_modulus(); + } + #[test] fn test_weierstrass_biguint_scalar_mul() { assert_eq!(biguint_from_limbs(Bn254BaseField::MODULUS), Bn254BaseField::modulus()); From d35e338977b88842c1a7e9176b290e275265de63 Mon Sep 17 00:00:00 2001 From: c-node Date: Sun, 29 Dec 2024 20:41:59 -0500 Subject: [PATCH 4/4] rmove debug stuff --- crates/curves/src/weierstrass/bn254.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/crates/curves/src/weierstrass/bn254.rs b/crates/curves/src/weierstrass/bn254.rs index c95b38e288..f793744160 100644 --- a/crates/curves/src/weierstrass/bn254.rs +++ b/crates/curves/src/weierstrass/bn254.rs @@ -34,12 +34,6 @@ impl FieldParameters for Bn254ScalarField { } -impl Bn254ScalarField { - pub fn print_modulus() { - println!("{:?}", Self::modulus().to_bytes_le()); - } -} - impl FpOpField for Bn254ScalarField { const FIELD_TYPE: FieldType = FieldType::Bn254; } @@ -129,11 +123,6 @@ mod tests { use super::*; use crate::utils::biguint_from_limbs; - #[test] - fn test_print_scalar_modulus() { - Bn254ScalarField::print_modulus(); - } - #[test] fn test_weierstrass_biguint_scalar_mul() { assert_eq!(biguint_from_limbs(Bn254BaseField::MODULUS), Bn254BaseField::modulus());