Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pseduo-remove MapToCurve::new, renamed to check_parameters #627

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub fn parity<F: Field>(element: &F) -> bool {
}

impl<P: SWUConfig> MapToCurve<Projective<P>> for SWUMap<P> {
/// Constructs a new map if `P` represents a valid map.
fn new() -> Result<Self, HashToCurveError> {
/// 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(
Expand All @@ -49,13 +49,13 @@ impl<P: SWUConfig> MapToCurve<Projective<P>> for SWUMap<P> {
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
/// <https://github.com/zcash/pasta_curves/blob/main/src/hashtocurve.rs>.
fn map_to_curve(&self, point: P::BaseField) -> Result<Affine<P>, HashToCurveError> {
fn map_to_curve(point: P::BaseField) -> Result<Affine<P>, 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)
Expand Down Expand Up @@ -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::<TestSWUMapToCurveConfig>::new().unwrap();
SWUMap::<TestSWUMapToCurveConfig>::check_parameters().unwrap();

let mut map_range: Vec<Affine<TestSWUMapToCurveConfig>> = 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::<TestSWUMapToCurveConfig>::map_to_curve(point).unwrap());
}

let mut counts = HashMap::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ pub trait WBConfig: SWCurveConfig + Sized {
}

pub struct WBMap<P: WBConfig> {
swu_field_curve_hasher: SWUMap<P::IsogenousCurve>,
swu_field_curve_hasher: PhantomData<SWUMap<P::IsogenousCurve>>,
curve_params: PhantomData<fn() -> P>,
}

impl<P: WBConfig> MapToCurve<Projective<P>> for WBMap<P> {
/// Constructs a new map if `P` represents a valid map.
fn new() -> Result<Self, HashToCurveError> {
/// 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() {
Expand All @@ -95,21 +95,18 @@ impl<P: WBConfig> MapToCurve<Projective<P>> for WBMap<P> {
Err(e) => return Err(e),
}

Ok(WBMap {
swu_field_curve_hasher: SWUMap::<P::IsogenousCurve>::new().unwrap(),
curve_params: PhantomData,
})
SWUMap::<P::IsogenousCurve>::check_parameters().unwrap(); // Or ?
Ok(())
}

/// Map random field point to a random curve point
/// inspired from
/// <https://github.com/zcash/pasta_curves/blob/main/src/hashtocurve.rs>
fn map_to_curve(
&self,
element: <Affine<P> as AffineRepr>::BaseField,
) -> Result<Affine<P>, 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::<P::IsogenousCurve>::map_to_curve(element).unwrap();
P::ISOGENY_MAP.apply(point_on_isogenious_curve)
}
}
Expand Down
23 changes: 11 additions & 12 deletions ec/src/hashing/map_to_curve_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use ark_std::marker::PhantomData;

/// Trait for mapping a random field element to a random curve point.
pub trait MapToCurve<T: CurveGroup>: Sized {
/// Constructs a new mapping.
fn new() -> Result<Self, HashToCurveError>;
/// 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<T::Affine, HashToCurveError>;
fn map_to_curve(point: T::BaseField) -> Result<T::Affine, HashToCurveError>;
}

/// Helper struct that can be used to construct elements on the elliptic curve
Expand All @@ -21,7 +21,7 @@ where
M2C: MapToCurve<T>,
{
field_hasher: H2F,
curve_mapper: M2C,
_curve_mapper: PhantomData<M2C>,
_params_t: PhantomData<T>,
}

Expand All @@ -32,13 +32,12 @@ where
M2C: MapToCurve<T>,
{
fn new(domain: &[u8]) -> Result<Self, HashToCurveError> {
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),
_curve_mapper: PhantomData,
_params_t: PhantomData,
})
}

Expand All @@ -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();
Expand Down