1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::{hashing::*, AffineRepr, CurveGroup};
use ark_ff::field_hashers::HashToField;
use ark_std::marker::PhantomData;
pub trait MapToCurve<T: CurveGroup>: Sized {
fn new() -> Result<Self, HashToCurveError>;
fn map_to_curve(&self, point: T::BaseField) -> Result<T::Affine, HashToCurveError>;
}
pub struct MapToCurveBasedHasher<T, H2F, M2C>
where
T: CurveGroup,
H2F: HashToField<T::BaseField>,
M2C: MapToCurve<T>,
{
field_hasher: H2F,
curve_mapper: M2C,
_params_t: PhantomData<T>,
}
impl<T, H2F, M2C> HashToCurve<T> for MapToCurveBasedHasher<T, H2F, M2C>
where
T: CurveGroup,
H2F: HashToField<T::BaseField>,
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;
Ok(MapToCurveBasedHasher {
field_hasher,
curve_mapper,
_params_t,
})
}
fn hash(&self, msg: &[u8]) -> Result<T::Affine, HashToCurveError> {
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 = (rand_curve_elem_0 + rand_curve_elem_1).into();
let rand_subgroup_elem = rand_curve_elem.clear_cofactor();
Ok(rand_subgroup_elem)
}
}