From e742d2694932eb0f9083f2999541dc6d1a0caa49 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Mon, 8 Jul 2024 11:53:05 +0200 Subject: [PATCH] Finish porting code --- src/codec.rs | 8 +++---- src/ring.rs | 13 +++++------ src/suites/secp256.rs | 50 ------------------------------------------- src/testing.rs | 6 +++--- src/utils.rs | 35 ++++++++++++++++-------------- 5 files changed, 33 insertions(+), 79 deletions(-) diff --git a/src/codec.rs b/src/codec.rs index 6c432cd..3cce437 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -49,7 +49,7 @@ impl Codec for Sec1Codec where BaseField: ark_ff::PrimeField, CurveConfig: SWCurveConfig, - AffinePoint: utils::IntoSW> + utils::FromSW>, + AffinePoint: utils::SWMapping>, { const BIG_ENDIAN: bool = true; @@ -57,13 +57,13 @@ where /// (https://www.secg.org/sec1-v2.pdf) with point compression on. fn point_encode(pt: &AffinePoint, buf: &mut Vec) { use ark_ff::biginteger::BigInteger; - let mut tmp = Vec::new(); - use utils::IntoSW; + use utils::SWMapping; if pt.is_zero() { buf.push(0x00); return; } + let mut tmp = Vec::new(); let sw = pt.into_sw(); let is_odd = sw.y.into_bigint().is_odd(); @@ -78,7 +78,7 @@ where /// (https://www.secg.org/sec1-v2.pdf) with point compression on. fn point_decode(buf: &[u8]) -> AffinePoint { use ark_ff::biginteger::BigInteger; - use utils::FromSW; + use utils::SWMapping; type SWAffine = ark_ec::short_weierstrass::Affine; if buf.len() == 1 && buf[0] == 0x00 { return AffinePoint::::zero(); diff --git a/src/ring.rs b/src/ring.rs index 60341fa..4e7f7d2 100644 --- a/src/ring.rs +++ b/src/ring.rs @@ -1,3 +1,4 @@ +use crate::utils::SWMapping; use crate::*; use ark_ec::short_weierstrass::SWCurveConfig; use pedersen::{PedersenSuite, Proof as PedersenProof}; @@ -118,7 +119,7 @@ impl Verifier for Public where BaseField: ark_ff::PrimeField, CurveConfig: SWCurveConfig, - AffinePoint: IntoSW>, + AffinePoint: SWMapping>, { fn verify( input: Input, @@ -156,7 +157,7 @@ impl RingContext where BaseField: ark_ff::PrimeField, CurveConfig: SWCurveConfig + Clone, - AffinePoint: IntoSW>, + AffinePoint: SWMapping>, { /// Construct a new ring context suitable to manage the given ring size. pub fn from_seed(ring_size: usize, seed: [u8; 32]) -> Self { @@ -232,7 +233,7 @@ impl CanonicalSerialize for RingContext where BaseField: ark_ff::PrimeField, CurveConfig: SWCurveConfig + Clone, - AffinePoint: IntoSW>, + AffinePoint: SWMapping>, { fn serialize_with_mode( &self, @@ -252,7 +253,7 @@ impl CanonicalDeserialize for RingContext where BaseField: ark_ff::PrimeField, CurveConfig: SWCurveConfig + Clone, - AffinePoint: IntoSW>, + AffinePoint: SWMapping>, { fn deserialize_with_mode( mut reader: R, @@ -277,7 +278,7 @@ impl ark_serialize::Valid for RingContext where BaseField: ark_ff::PrimeField, CurveConfig: SWCurveConfig + Clone, - AffinePoint: IntoSW>, + AffinePoint: SWMapping>, { fn check(&self) -> Result<(), ark_serialize::SerializationError> { self.pcs_params.check() @@ -288,7 +289,7 @@ pub(crate) fn make_piop_params(domain_size: usize) -> PiopParams: ark_ff::PrimeField, CurveConfig: SWCurveConfig, - AffinePoint: IntoSW>, + AffinePoint: SWMapping>, { let domain = ring_proof::Domain::new(domain_size, true); PiopParams::::setup( diff --git a/src/suites/secp256.rs b/src/suites/secp256.rs index c8a6d7e..eefe321 100644 --- a/src/suites/secp256.rs +++ b/src/suites/secp256.rs @@ -74,56 +74,6 @@ impl Suite for P256Sha256Tai { fn data_to_point(data: &[u8]) -> Option { utils::hash_to_curve_tai_rfc_9381::(data, true) } - - // /// Encode point according to Section 2.3.3 "SEC 1: Elliptic Curve Cryptography", - // /// (https://www.secg.org/sec1-v2.pdf) with point compression on. - // fn point_encode(pt: &AffinePoint, buf: &mut Vec) { - // use ark_ff::biginteger::BigInteger; - // let mut tmp = Vec::new(); - - // if pt.is_zero() { - // buf.push(0x00); - // return; - // } - // let is_odd = pt.y.into_bigint().is_odd(); - // buf.push(if is_odd { 0x03 } else { 0x02 }); - - // pt.x.serialize_compressed(&mut tmp).unwrap(); - // tmp.reverse(); - // buf.extend_from_slice(&tmp[..]); - // } - - // /// Encode point according to Section 2.3.3 "SEC 1: Elliptic Curve Cryptography", - // /// (https://www.secg.org/sec1-v2.pdf) with point compression on. - // fn point_decode(buf: &[u8]) -> AffinePoint { - // use ark_ff::biginteger::BigInteger; - // if buf.len() == 1 && buf[0] == 0x00 { - // return AffinePoint::zero(); - // } - // let mut tmp = buf.to_vec(); - // tmp.reverse(); - // let y_flag = tmp.pop().unwrap(); - - // let x = BaseField::deserialize_compressed(&mut &tmp[..]).unwrap(); - // let (y1, y2) = AffinePoint::get_ys_from_x_unchecked(x).unwrap(); - // let y = if ((y_flag & 0x01) != 0) == y1.into_bigint().is_odd() { - // y1 - // } else { - // y2 - // }; - // AffinePoint::new_unchecked(x, y) - // } - - // fn scalar_encode(sc: &ScalarField, buf: &mut Vec) { - // let mut tmp = Vec::new(); - // sc.serialize_compressed(&mut tmp).unwrap(); - // tmp.reverse(); - // buf.extend_from_slice(&tmp[..]); - // } - - // fn scalar_decode(buf: &[u8]) -> ScalarField { - // ScalarField::from_be_bytes_mod_order(buf) - // } } impl PedersenSuite for P256Sha256Tai { diff --git a/src/testing.rs b/src/testing.rs index 352b995..5229907 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -72,7 +72,7 @@ pub fn ring_prove_verify() where BaseField: ark_ff::PrimeField, CurveConfig: ark_ec::short_weierstrass::SWCurveConfig + Clone, - AffinePoint: ring::IntoSW>, + AffinePoint: utils::SWMapping>, { use ring::{Prover, RingContext, Verifier}; @@ -105,9 +105,9 @@ pub fn check_complement_point() where BaseField: ark_ff::PrimeField, CurveConfig: ark_ec::short_weierstrass::SWCurveConfig + Clone, - AffinePoint: ring::IntoSW>, + AffinePoint: utils::SWMapping>, { - use ring::IntoSW; + use utils::SWMapping; let pt = S::COMPLEMENT_POINT.into_sw(); assert!(pt.is_on_curve()); assert!(!pt.is_in_correct_subgroup_assuming_on_curve()); diff --git a/src/utils.rs b/src/utils.rs index 7a39bf0..7b61feb 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -303,35 +303,38 @@ pub(crate) mod ark_next { } } -pub trait IntoSW { +pub trait SWMapping { + fn from_sw(sw: ark_ec::short_weierstrass::Affine) -> Self; fn into_sw(self) -> ark_ec::short_weierstrass::Affine; } -impl IntoSW +impl SWMapping for ark_ec::short_weierstrass::Affine { + #[inline(always)] + fn from_sw(sw: ark_ec::short_weierstrass::Affine) -> Self { + sw + } + + #[inline(always)] fn into_sw(self) -> ark_ec::short_weierstrass::Affine { self } } -impl IntoSW for ark_ec::twisted_edwards::Affine { - fn into_sw(self) -> ark_ec::short_weierstrass::Affine { +impl SWMapping for ark_ec::twisted_edwards::Affine { + #[inline(always)] + fn from_sw(sw: ark_ec::short_weierstrass::Affine) -> Self { const ERR_MSG: &str = - "'IntoSW' is expected to be implemented only for curves supporting the mapping"; - ark_next::map_te_to_sw(&self).expect(ERR_MSG) + "SW to TE is expected to be implemented only for curves supporting the mapping"; + ark_next::map_sw_to_te(&sw).expect(ERR_MSG) } -} -pub trait FromSW { - fn from_sw(sw: ark_ec::short_weierstrass::Affine) -> Self; -} - -impl FromSW - for ark_ec::short_weierstrass::Affine -{ - fn from_sw(sw: ark_ec::short_weierstrass::Affine) -> Self { - sw + #[inline(always)] + fn into_sw(self) -> ark_ec::short_weierstrass::Affine { + const ERR_MSG: &str = + "TE to SW is expected to be implemented only for curves supporting the mapping"; + ark_next::map_te_to_sw(&self).expect(ERR_MSG) } }