From 4bdeb6c407cbc7a3ac05430c15196fbddf5c9e27 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 25 Jan 2025 12:55:34 +0700 Subject: [PATCH 1/4] refactor(zkvm): refactor and move limbs_mut to utils to avoid duplicate --- crates/zkvm/lib/src/bls12381.rs | 15 ++++----------- crates/zkvm/lib/src/bn254.rs | 15 ++++----------- crates/zkvm/lib/src/secp256k1.rs | 15 ++++----------- crates/zkvm/lib/src/secp256r1.rs | 15 ++++----------- crates/zkvm/lib/src/utils.rs | 19 +++++++++++++------ 5 files changed, 29 insertions(+), 50 deletions(-) diff --git a/crates/zkvm/lib/src/bls12381.rs b/crates/zkvm/lib/src/bls12381.rs index 0839e4050..878d5700c 100644 --- a/crates/zkvm/lib/src/bls12381.rs +++ b/crates/zkvm/lib/src/bls12381.rs @@ -49,17 +49,6 @@ impl AffinePoint for Bls12381Point { } } - fn limbs_mut(&mut self) -> &mut [u32; N] { - match &mut self.0 { - WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), - WeierstrassPoint::Affine(limbs) => limbs, - } - } - - fn complete_add_assign(&mut self, other: &Self) { - self.weierstrass_add_assign(other); - } - fn add_assign(&mut self, other: &Self) { let a = self.limbs_mut(); let b = other.limbs_ref(); @@ -68,6 +57,10 @@ impl AffinePoint for Bls12381Point { } } + fn complete_add_assign(&mut self, other: &Self) { + self.weierstrass_add_assign(other); + } + fn double(&mut self) { let a = self.limbs_mut(); unsafe { diff --git a/crates/zkvm/lib/src/bn254.rs b/crates/zkvm/lib/src/bn254.rs index 2d9456102..b2e7a5485 100644 --- a/crates/zkvm/lib/src/bn254.rs +++ b/crates/zkvm/lib/src/bn254.rs @@ -42,17 +42,6 @@ impl AffinePoint for Bn254Point { } } - fn limbs_mut(&mut self) -> &mut [u32; N] { - match &mut self.0 { - WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), - WeierstrassPoint::Affine(limbs) => limbs, - } - } - - fn complete_add_assign(&mut self, other: &Self) { - self.weierstrass_add_assign(other); - } - fn add_assign(&mut self, other: &Self) { let a = self.limbs_mut(); let b = other.limbs_ref(); @@ -61,6 +50,10 @@ impl AffinePoint for Bn254Point { } } + fn complete_add_assign(&mut self, other: &Self) { + self.weierstrass_add_assign(other); + } + fn double(&mut self) { let a = self.limbs_mut(); unsafe { diff --git a/crates/zkvm/lib/src/secp256k1.rs b/crates/zkvm/lib/src/secp256k1.rs index 24ba13651..43f91fb71 100644 --- a/crates/zkvm/lib/src/secp256k1.rs +++ b/crates/zkvm/lib/src/secp256k1.rs @@ -44,17 +44,6 @@ impl AffinePoint for Secp256k1Point { } } - fn limbs_mut(&mut self) -> &mut [u32; N] { - match &mut self.0 { - WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), - WeierstrassPoint::Affine(limbs) => limbs, - } - } - - fn complete_add_assign(&mut self, other: &Self) { - self.weierstrass_add_assign(other); - } - fn add_assign(&mut self, other: &Self) { let a = self.limbs_mut(); let b = other.limbs_ref(); @@ -63,6 +52,10 @@ impl AffinePoint for Secp256k1Point { } } + fn complete_add_assign(&mut self, other: &Self) { + self.weierstrass_add_assign(other); + } + fn double(&mut self) { match &mut self.0 { WeierstrassPoint::Infinity => (), diff --git a/crates/zkvm/lib/src/secp256r1.rs b/crates/zkvm/lib/src/secp256r1.rs index cb2a1f882..d8054c229 100644 --- a/crates/zkvm/lib/src/secp256r1.rs +++ b/crates/zkvm/lib/src/secp256r1.rs @@ -44,17 +44,6 @@ impl AffinePoint for Secp256r1Point { } } - fn limbs_mut(&mut self) -> &mut [u32; N] { - match &mut self.0 { - WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), - WeierstrassPoint::Affine(limbs) => limbs, - } - } - - fn complete_add_assign(&mut self, other: &Self) { - self.weierstrass_add_assign(other); - } - fn add_assign(&mut self, other: &Self) { let a = self.limbs_mut(); let b = other.limbs_ref(); @@ -63,6 +52,10 @@ impl AffinePoint for Secp256r1Point { } } + fn complete_add_assign(&mut self, other: &Self) { + self.weierstrass_add_assign(other); + } + fn double(&mut self) { match &mut self.0 { WeierstrassPoint::Infinity => (), diff --git a/crates/zkvm/lib/src/utils.rs b/crates/zkvm/lib/src/utils.rs index 4ec6280cd..eb40398cc 100644 --- a/crates/zkvm/lib/src/utils.rs +++ b/crates/zkvm/lib/src/utils.rs @@ -7,13 +7,20 @@ pub trait AffinePoint: Clone + Sized { /// Creates a new [`AffinePoint`] that corresponds to the identity point. fn identity() -> Self; + // Add default implementations for limbs_ref and limbs_mut + fn limbs_ref(&self) -> &[u32; N] { + match self.point() { + WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), + WeierstrassPoint::Affine(limbs) => limbs, + } + } - /// Returns a reference to the limbs. - fn limbs_ref(&self) -> &[u32; N]; - - /// Returns a mutable reference to the limbs. If the point is the infinity point, this will - /// panic. - fn limbs_mut(&mut self) -> &mut [u32; N]; + fn limbs_mut(&mut self) -> &mut [u32; N] { + match self.point_mut() { + WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), + WeierstrassPoint::Affine(limbs) => limbs, + } + } /// Creates a new [`AffinePoint`] from the given x and y coordinates. /// From a21425f6c11c239015e0da30ba711e0467ab354d Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 25 Jan 2025 13:23:28 +0700 Subject: [PATCH 2/4] rollback --- crates/zkvm/lib/src/bls12381.rs | 7 +++++++ crates/zkvm/lib/src/bn254.rs | 7 +++++++ crates/zkvm/lib/src/secp256k1.rs | 7 +++++++ crates/zkvm/lib/src/secp256r1.rs | 7 +++++++ crates/zkvm/lib/src/utils.rs | 19 ++++++------------- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/crates/zkvm/lib/src/bls12381.rs b/crates/zkvm/lib/src/bls12381.rs index 878d5700c..b1999c9a1 100644 --- a/crates/zkvm/lib/src/bls12381.rs +++ b/crates/zkvm/lib/src/bls12381.rs @@ -49,6 +49,13 @@ impl AffinePoint for Bls12381Point { } } + fn limbs_mut(&mut self) -> &mut [u32; crate::bn254::N] { + match &mut self.0 { + WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), + WeierstrassPoint::Affine(limbs) => limbs, + } + } + fn add_assign(&mut self, other: &Self) { let a = self.limbs_mut(); let b = other.limbs_ref(); diff --git a/crates/zkvm/lib/src/bn254.rs b/crates/zkvm/lib/src/bn254.rs index b2e7a5485..b1bc88d0d 100644 --- a/crates/zkvm/lib/src/bn254.rs +++ b/crates/zkvm/lib/src/bn254.rs @@ -42,6 +42,13 @@ impl AffinePoint for Bn254Point { } } + fn limbs_mut(&mut self) -> &mut [u32; N] { + match &mut self.0 { + WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), + WeierstrassPoint::Affine(limbs) => limbs, + } + } + fn add_assign(&mut self, other: &Self) { let a = self.limbs_mut(); let b = other.limbs_ref(); diff --git a/crates/zkvm/lib/src/secp256k1.rs b/crates/zkvm/lib/src/secp256k1.rs index 43f91fb71..3fc6fe603 100644 --- a/crates/zkvm/lib/src/secp256k1.rs +++ b/crates/zkvm/lib/src/secp256k1.rs @@ -44,6 +44,13 @@ impl AffinePoint for Secp256k1Point { } } + fn limbs_mut(&mut self) -> &mut [u32; crate::bn254::N] { + match &mut self.0 { + WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), + WeierstrassPoint::Affine(limbs) => limbs, + } + } + fn add_assign(&mut self, other: &Self) { let a = self.limbs_mut(); let b = other.limbs_ref(); diff --git a/crates/zkvm/lib/src/secp256r1.rs b/crates/zkvm/lib/src/secp256r1.rs index d8054c229..7eed0ac4b 100644 --- a/crates/zkvm/lib/src/secp256r1.rs +++ b/crates/zkvm/lib/src/secp256r1.rs @@ -44,6 +44,13 @@ impl AffinePoint for Secp256r1Point { } } + fn limbs_mut(&mut self) -> &mut [u32; crate::bn254::N] { + match &mut self.0 { + WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), + WeierstrassPoint::Affine(limbs) => limbs, + } + } + fn add_assign(&mut self, other: &Self) { let a = self.limbs_mut(); let b = other.limbs_ref(); diff --git a/crates/zkvm/lib/src/utils.rs b/crates/zkvm/lib/src/utils.rs index eb40398cc..4ec6280cd 100644 --- a/crates/zkvm/lib/src/utils.rs +++ b/crates/zkvm/lib/src/utils.rs @@ -7,20 +7,13 @@ pub trait AffinePoint: Clone + Sized { /// Creates a new [`AffinePoint`] that corresponds to the identity point. fn identity() -> Self; - // Add default implementations for limbs_ref and limbs_mut - fn limbs_ref(&self) -> &[u32; N] { - match self.point() { - WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), - WeierstrassPoint::Affine(limbs) => limbs, - } - } - fn limbs_mut(&mut self) -> &mut [u32; N] { - match self.point_mut() { - WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), - WeierstrassPoint::Affine(limbs) => limbs, - } - } + /// Returns a reference to the limbs. + fn limbs_ref(&self) -> &[u32; N]; + + /// Returns a mutable reference to the limbs. If the point is the infinity point, this will + /// panic. + fn limbs_mut(&mut self) -> &mut [u32; N]; /// Creates a new [`AffinePoint`] from the given x and y coordinates. /// From 472192efe00e4c9ca21d504fda648466b48f880d Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 25 Jan 2025 13:25:13 +0700 Subject: [PATCH 3/4] rollback --- crates/zkvm/lib/src/bls12381.rs | 2 +- crates/zkvm/lib/src/secp256k1.rs | 2 +- crates/zkvm/lib/src/secp256r1.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/zkvm/lib/src/bls12381.rs b/crates/zkvm/lib/src/bls12381.rs index b1999c9a1..004052755 100644 --- a/crates/zkvm/lib/src/bls12381.rs +++ b/crates/zkvm/lib/src/bls12381.rs @@ -49,7 +49,7 @@ impl AffinePoint for Bls12381Point { } } - fn limbs_mut(&mut self) -> &mut [u32; crate::bn254::N] { + fn limbs_mut(&mut self) -> &mut [u32; N] { match &mut self.0 { WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), WeierstrassPoint::Affine(limbs) => limbs, diff --git a/crates/zkvm/lib/src/secp256k1.rs b/crates/zkvm/lib/src/secp256k1.rs index 3fc6fe603..3926b4550 100644 --- a/crates/zkvm/lib/src/secp256k1.rs +++ b/crates/zkvm/lib/src/secp256k1.rs @@ -44,7 +44,7 @@ impl AffinePoint for Secp256k1Point { } } - fn limbs_mut(&mut self) -> &mut [u32; crate::bn254::N] { + fn limbs_mut(&mut self) -> &mut [u32; N] { { match &mut self.0 { WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), WeierstrassPoint::Affine(limbs) => limbs, diff --git a/crates/zkvm/lib/src/secp256r1.rs b/crates/zkvm/lib/src/secp256r1.rs index 7eed0ac4b..147b1c968 100644 --- a/crates/zkvm/lib/src/secp256r1.rs +++ b/crates/zkvm/lib/src/secp256r1.rs @@ -44,7 +44,7 @@ impl AffinePoint for Secp256r1Point { } } - fn limbs_mut(&mut self) -> &mut [u32; crate::bn254::N] { + fn limbs_mut(&mut self) -> &mut [u32; N] { match &mut self.0 { WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), WeierstrassPoint::Affine(limbs) => limbs, From b38d25be6fbf7782562114799fecaab559bd885c Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 25 Jan 2025 13:26:08 +0700 Subject: [PATCH 4/4] rollback --- crates/zkvm/lib/src/secp256k1.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/zkvm/lib/src/secp256k1.rs b/crates/zkvm/lib/src/secp256k1.rs index 3926b4550..0aa0b094d 100644 --- a/crates/zkvm/lib/src/secp256k1.rs +++ b/crates/zkvm/lib/src/secp256k1.rs @@ -44,7 +44,7 @@ impl AffinePoint for Secp256k1Point { } } - fn limbs_mut(&mut self) -> &mut [u32; N] { { + fn limbs_mut(&mut self) -> &mut [u32; N] { match &mut self.0 { WeierstrassPoint::Infinity => panic!("Infinity point has no limbs"), WeierstrassPoint::Affine(limbs) => limbs, @@ -71,4 +71,4 @@ impl AffinePoint for Secp256k1Point { }, } } -} +} \ No newline at end of file