diff --git a/ec/src/hashing/curve_maps/swu/mod.rs b/ec/src/hashing/curve_maps/swu.rs similarity index 95% rename from ec/src/hashing/curve_maps/swu/mod.rs rename to ec/src/hashing/curve_maps/swu.rs index 47330c52c..c989ece48 100644 --- a/ec/src/hashing/curve_maps/swu/mod.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 check_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,15 +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::::check_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(), - ); + let point = F127::from(current_field_element as u64); + map_range.push(SWUMap::::map_to_curve(point).unwrap()); } let mut counts = HashMap::new(); diff --git a/ec/src/hashing/curve_maps/wb/mod.rs b/ec/src/hashing/curve_maps/wb.rs similarity index 96% rename from ec/src/hashing/curve_maps/wb/mod.rs rename to ec/src/hashing/curve_maps/wb.rs index 10ddbb0f7..46b635b1c 100644 --- a/ec/src/hashing/curve_maps/wb/mod.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 check_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::::check_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 bbb005767..fa50a5e57 100644 --- a/ec/src/hashing/map_to_curve_hasher.rs +++ b/ec/src/hashing/map_to_curve_hasher.rs @@ -7,11 +7,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 check_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 @@ -24,8 +24,7 @@ where M2C: MapToCurve, { field_hasher: H2F, - curve_mapper: M2C, - _params_t: PhantomData, + _phantom: PhantomData<(T, M2C)>, } impl HashToCurve for MapToCurveBasedHasher @@ -35,13 +34,11 @@ 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::check_parameters()?; Ok(MapToCurveBasedHasher { - field_hasher, - curve_mapper, - _params_t, + field_hasher: H2F::new(domain), + _phantom: PhantomData, }) } @@ -61,8 +58,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();