From 5c710c5cf284b70c1e234aeb50863bbccea679e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Jeff?= <¨burdges@gnunet.org¨> Date: Mon, 20 Mar 2023 23:21:13 +0100 Subject: [PATCH 01/10] Avoid unused subdirectories --- ec/src/hashing/curve_maps/{swu/mod.rs => swu.rs} | 0 ec/src/hashing/curve_maps/{wb/mod.rs => wb.rs} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename ec/src/hashing/curve_maps/{swu/mod.rs => swu.rs} (100%) rename ec/src/hashing/curve_maps/{wb/mod.rs => wb.rs} (100%) diff --git a/ec/src/hashing/curve_maps/swu/mod.rs b/ec/src/hashing/curve_maps/swu.rs similarity index 100% rename from ec/src/hashing/curve_maps/swu/mod.rs rename to ec/src/hashing/curve_maps/swu.rs diff --git a/ec/src/hashing/curve_maps/wb/mod.rs b/ec/src/hashing/curve_maps/wb.rs similarity index 100% rename from ec/src/hashing/curve_maps/wb/mod.rs rename to ec/src/hashing/curve_maps/wb.rs From 91891aa0ccc4e2671c366896a3c28b5b85f1ea6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Jeff?= <¨burdges@gnunet.org¨> Date: Mon, 20 Mar 2023 23:55:12 +0100 Subject: [PATCH 02/10] Pseduo-remove MapToCurve::new, renamed to test_parameters MapToCurve::new seemingly originates from a more runtime oriented elliptic curve crate: https://github.com/armfazh/redox-ecc/blob/master/src/ellipticcurve.rs#L36 Arguably test_parameters should be inherent methods, invoked by whoever defined the parameters, but maybe not? I left the trait method for now. --- ec/src/hashing/curve_maps/swu.rs | 14 ++++++-------- ec/src/hashing/curve_maps/wb.rs | 15 ++++++--------- ec/src/hashing/map_to_curve_hasher.rs | 23 +++++++++++------------ 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/ec/src/hashing/curve_maps/swu.rs b/ec/src/hashing/curve_maps/swu.rs index 47330c52c..e1f66bef6 100644 --- a/ec/src/hashing/curve_maps/swu.rs +++ b/ec/src/hashing/curve_maps/swu.rs @@ -35,8 +35,8 @@ pub fn parity(element: &F) -> bool { } impl MapToCurve> for SWUMap

{ - /// Constructs a new map if `P` represents a valid map. - fn new() -> Result { + /// Checks if `P` represents a valid map. + fn test_parameters() -> Result<(), HashToCurveError> { // Verifying that ZETA is a non-square if P::ZETA.legendre().is_qr() { return Err(HashToCurveError::MapToCurveError( @@ -49,13 +49,13 @@ impl MapToCurve> for SWUMap

{ return Err(HashToCurveError::MapToCurveError("Simplified SWU requires a * b != 0 in the short Weierstrass form of y^2 = x^3 + a*x + b ".to_string())); } - Ok(SWUMap(PhantomData)) + Ok(()) } /// Map an arbitrary base field element to a curve point. /// Based on /// . - fn map_to_curve(&self, point: P::BaseField) -> Result, HashToCurveError> { + fn map_to_curve(point: P::BaseField) -> Result, HashToCurveError> { // 1. tv1 = inv0(Z^2 * u^4 + Z * u^2) // 2. x1 = (-B / A) * (1 + tv1) // 3. If tv1 == 0, set x1 = B / (Z * A) @@ -256,14 +256,12 @@ mod test { /// elements should be mapped to curve successfully. everything can be mapped #[test] fn map_field_to_curve_swu() { - let test_map_to_curve = SWUMap::::new().unwrap(); + SWUMap::::test_parameters().unwrap(); let mut map_range: Vec> = vec![]; for current_field_element in 0..127 { map_range.push( - test_map_to_curve - .map_to_curve(F127::from(current_field_element as u64)) - .unwrap(), + SWUMap::::map_to_curve(F127::from(current_field_element as u64)).unwrap(), ); } diff --git a/ec/src/hashing/curve_maps/wb.rs b/ec/src/hashing/curve_maps/wb.rs index 4e2644009..04e97d36d 100644 --- a/ec/src/hashing/curve_maps/wb.rs +++ b/ec/src/hashing/curve_maps/wb.rs @@ -79,13 +79,13 @@ pub trait WBConfig: SWCurveConfig + Sized { } pub struct WBMap { - swu_field_curve_hasher: SWUMap, + swu_field_curve_hasher: PhantomData>, curve_params: PhantomData P>, } impl MapToCurve> for WBMap

{ - /// Constructs a new map if `P` represents a valid map. - fn new() -> Result { + /// Checks if `P` represents a valid map. + fn test_parameters() -> Result<(), HashToCurveError> { match P::ISOGENY_MAP.apply(P::IsogenousCurve::GENERATOR) { Ok(point_on_curve) => { if !point_on_curve.is_on_curve() { @@ -95,21 +95,18 @@ impl MapToCurve> for WBMap

{ Err(e) => return Err(e), } - Ok(WBMap { - swu_field_curve_hasher: SWUMap::::new().unwrap(), - curve_params: PhantomData, - }) + SWUMap::::test_parameters().unwrap(); // Or ? + Ok(()) } /// Map random field point to a random curve point /// inspired from /// fn map_to_curve( - &self, element: as AffineRepr>::BaseField, ) -> Result, HashToCurveError> { // first we need to map the field point to the isogenous curve - let point_on_isogenious_curve = self.swu_field_curve_hasher.map_to_curve(element).unwrap(); + let point_on_isogenious_curve = SWUMap::::map_to_curve(element).unwrap(); P::ISOGENY_MAP.apply(point_on_isogenious_curve) } } diff --git a/ec/src/hashing/map_to_curve_hasher.rs b/ec/src/hashing/map_to_curve_hasher.rs index 59fe4bbf5..da21400ef 100644 --- a/ec/src/hashing/map_to_curve_hasher.rs +++ b/ec/src/hashing/map_to_curve_hasher.rs @@ -4,11 +4,11 @@ use ark_std::marker::PhantomData; /// Trait for mapping a random field element to a random curve point. pub trait MapToCurve: Sized { - /// Constructs a new mapping. - fn new() -> Result; + /// Checks whether supplied parameters represent a valid map. + fn test_parameters() -> Result<(), HashToCurveError>; /// Map an arbitary field element to a corresponding curve point. - fn map_to_curve(&self, point: T::BaseField) -> Result; + fn map_to_curve(point: T::BaseField) -> Result; } /// Helper struct that can be used to construct elements on the elliptic curve @@ -21,7 +21,7 @@ where M2C: MapToCurve, { field_hasher: H2F, - curve_mapper: M2C, + _curve_mapper: PhantomData, _params_t: PhantomData, } @@ -32,13 +32,12 @@ where M2C: MapToCurve, { fn new(domain: &[u8]) -> Result { - let field_hasher = H2F::new(domain); - let curve_mapper = M2C::new()?; - let _params_t = PhantomData; + #[cfg(test)] + M2C::test_parameters() ?; Ok(MapToCurveBasedHasher { - field_hasher, - curve_mapper, - _params_t, + field_hasher: H2F::new(domain), + _curve_mapper: PhantomData, + _params_t: PhantomData, }) } @@ -58,8 +57,8 @@ where let rand_field_elems = self.field_hasher.hash_to_field(msg, 2); - let rand_curve_elem_0 = self.curve_mapper.map_to_curve(rand_field_elems[0])?; - let rand_curve_elem_1 = self.curve_mapper.map_to_curve(rand_field_elems[1])?; + let rand_curve_elem_0 = M2C::map_to_curve(rand_field_elems[0])?; + let rand_curve_elem_1 = M2C::map_to_curve(rand_field_elems[1])?; let rand_curve_elem = (rand_curve_elem_0 + rand_curve_elem_1).into(); let rand_subgroup_elem = rand_curve_elem.clear_cofactor(); From 8c31c53027cf4cc9a34c7989fad4235e00ba7862 Mon Sep 17 00:00:00 2001 From: Jeff Burdges Date: Tue, 21 Mar 2023 01:16:02 +0100 Subject: [PATCH 03/10] Update ec/src/hashing/curve_maps/wb.rs Co-authored-by: Pratyush Mishra --- ec/src/hashing/curve_maps/wb.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec/src/hashing/curve_maps/wb.rs b/ec/src/hashing/curve_maps/wb.rs index 04e97d36d..4280c22e4 100644 --- a/ec/src/hashing/curve_maps/wb.rs +++ b/ec/src/hashing/curve_maps/wb.rs @@ -85,7 +85,7 @@ pub struct WBMap { impl MapToCurve> for WBMap

{ /// Checks if `P` represents a valid map. - fn test_parameters() -> Result<(), HashToCurveError> { + fn check_parameters() -> Result<(), HashToCurveError> { match P::ISOGENY_MAP.apply(P::IsogenousCurve::GENERATOR) { Ok(point_on_curve) => { if !point_on_curve.is_on_curve() { From ebe0a41dd3e3eaffd10a5c37384d70bc2af7c410 Mon Sep 17 00:00:00 2001 From: Jeff Burdges Date: Tue, 21 Mar 2023 01:16:11 +0100 Subject: [PATCH 04/10] Update ec/src/hashing/curve_maps/swu.rs Co-authored-by: Pratyush Mishra --- ec/src/hashing/curve_maps/swu.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec/src/hashing/curve_maps/swu.rs b/ec/src/hashing/curve_maps/swu.rs index e1f66bef6..28b1f15cc 100644 --- a/ec/src/hashing/curve_maps/swu.rs +++ b/ec/src/hashing/curve_maps/swu.rs @@ -36,7 +36,7 @@ pub fn parity(element: &F) -> bool { impl MapToCurve> for SWUMap

{ /// Checks if `P` represents a valid map. - fn test_parameters() -> Result<(), HashToCurveError> { + fn check_parameters() -> Result<(), HashToCurveError> { // Verifying that ZETA is a non-square if P::ZETA.legendre().is_qr() { return Err(HashToCurveError::MapToCurveError( From caf56c044f1697db3b2e0ebaa6762c381300eba0 Mon Sep 17 00:00:00 2001 From: Jeff Burdges Date: Tue, 21 Mar 2023 01:16:19 +0100 Subject: [PATCH 05/10] Update ec/src/hashing/map_to_curve_hasher.rs Co-authored-by: Pratyush Mishra --- ec/src/hashing/map_to_curve_hasher.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec/src/hashing/map_to_curve_hasher.rs b/ec/src/hashing/map_to_curve_hasher.rs index da21400ef..90edfd393 100644 --- a/ec/src/hashing/map_to_curve_hasher.rs +++ b/ec/src/hashing/map_to_curve_hasher.rs @@ -5,7 +5,7 @@ use ark_std::marker::PhantomData; /// Trait for mapping a random field element to a random curve point. pub trait MapToCurve: Sized { /// Checks whether supplied parameters represent a valid map. - fn test_parameters() -> Result<(), HashToCurveError>; + fn check_parameters() -> Result<(), HashToCurveError>; /// Map an arbitary field element to a corresponding curve point. fn map_to_curve(point: T::BaseField) -> Result; From 40cdde9e32e542e3cfc8856b6f6ccf30fa6624cc Mon Sep 17 00:00:00 2001 From: Jeff Burdges Date: Tue, 21 Mar 2023 01:16:29 +0100 Subject: [PATCH 06/10] Update ec/src/hashing/curve_maps/swu.rs Co-authored-by: Pratyush Mishra --- ec/src/hashing/curve_maps/swu.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec/src/hashing/curve_maps/swu.rs b/ec/src/hashing/curve_maps/swu.rs index 28b1f15cc..556b963bc 100644 --- a/ec/src/hashing/curve_maps/swu.rs +++ b/ec/src/hashing/curve_maps/swu.rs @@ -256,7 +256,7 @@ mod test { /// elements should be mapped to curve successfully. everything can be mapped #[test] fn map_field_to_curve_swu() { - SWUMap::::test_parameters().unwrap(); + SWUMap::::check_parameters().unwrap(); let mut map_range: Vec> = vec![]; for current_field_element in 0..127 { From a05eb81c40dc5819cfaa6cf5484284bda078cb83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Jeff?= <¨burdges@gnunet.org¨> Date: Tue, 21 Mar 2023 11:35:19 +0100 Subject: [PATCH 07/10] Two more renames --- ec/src/hashing/curve_maps/wb.rs | 2 +- ec/src/hashing/map_to_curve_hasher.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ec/src/hashing/curve_maps/wb.rs b/ec/src/hashing/curve_maps/wb.rs index 4280c22e4..32a6acf4c 100644 --- a/ec/src/hashing/curve_maps/wb.rs +++ b/ec/src/hashing/curve_maps/wb.rs @@ -95,7 +95,7 @@ impl MapToCurve> for WBMap

{ Err(e) => return Err(e), } - SWUMap::::test_parameters().unwrap(); // Or ? + SWUMap::::check_parameters().unwrap(); // Or ? Ok(()) } diff --git a/ec/src/hashing/map_to_curve_hasher.rs b/ec/src/hashing/map_to_curve_hasher.rs index 90edfd393..e3cc4f5b1 100644 --- a/ec/src/hashing/map_to_curve_hasher.rs +++ b/ec/src/hashing/map_to_curve_hasher.rs @@ -33,7 +33,7 @@ where { fn new(domain: &[u8]) -> Result { #[cfg(test)] - M2C::test_parameters() ?; + M2C::check_parameters() ?; Ok(MapToCurveBasedHasher { field_hasher: H2F::new(domain), _curve_mapper: PhantomData, From ae014ae574e481a356a093892083e33a6754394f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Jeff?= <¨burdges@gnunet.org¨> Date: Tue, 21 Mar 2023 20:31:30 +0100 Subject: [PATCH 08/10] make rustfmt happier --- ec/src/hashing/curve_maps/swu.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ec/src/hashing/curve_maps/swu.rs b/ec/src/hashing/curve_maps/swu.rs index 556b963bc..b5884bf75 100644 --- a/ec/src/hashing/curve_maps/swu.rs +++ b/ec/src/hashing/curve_maps/swu.rs @@ -260,9 +260,8 @@ mod test { let mut map_range: Vec> = vec![]; for current_field_element in 0..127 { - map_range.push( - SWUMap::::map_to_curve(F127::from(current_field_element as u64)).unwrap(), - ); + let point = F127::from(current_field_element as u64); + map_range.push( SWUMap::::map_to_curve(point).unwrap() ); } let mut counts = HashMap::new(); From f01abb9eb59e5169e085a27b50afec768bce5ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A8Jeff?= <¨burdges@gnunet.org¨> Date: Tue, 21 Mar 2023 20:35:57 +0100 Subject: [PATCH 09/10] Again rustfmt --- ec/src/hashing/curve_maps/swu.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec/src/hashing/curve_maps/swu.rs b/ec/src/hashing/curve_maps/swu.rs index b5884bf75..c989ece48 100644 --- a/ec/src/hashing/curve_maps/swu.rs +++ b/ec/src/hashing/curve_maps/swu.rs @@ -261,7 +261,7 @@ mod test { let mut map_range: Vec> = vec![]; for current_field_element in 0..127 { let point = F127::from(current_field_element as u64); - map_range.push( SWUMap::::map_to_curve(point).unwrap() ); + map_range.push(SWUMap::::map_to_curve(point).unwrap()); } let mut counts = HashMap::new(); From e828f28da703a6f6fa699625284a5015d55d86fd Mon Sep 17 00:00:00 2001 From: Jeff Burdges Date: Tue, 21 Mar 2023 21:14:11 +0100 Subject: [PATCH 10/10] rustfmt is wrong on this one but whatever rustfmt is wrong that f()? should be preferred over f() ?, as errors paths should often be highlighted, but nobody merged that fix yet, so whatever. https://github.com/rust-lang/rustfmt/issues/5595 --- ec/src/hashing/map_to_curve_hasher.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec/src/hashing/map_to_curve_hasher.rs b/ec/src/hashing/map_to_curve_hasher.rs index e3cc4f5b1..b1004b656 100644 --- a/ec/src/hashing/map_to_curve_hasher.rs +++ b/ec/src/hashing/map_to_curve_hasher.rs @@ -33,7 +33,7 @@ where { fn new(domain: &[u8]) -> Result { #[cfg(test)] - M2C::check_parameters() ?; + M2C::check_parameters()?; Ok(MapToCurveBasedHasher { field_hasher: H2F::new(domain), _curve_mapper: PhantomData,