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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use core::marker::PhantomData;
use crate::{models::short_weierstrass::SWCurveConfig, CurveConfig};
use ark_ff::batch_inversion;
use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial, Polynomial};
use crate::{
hashing::{map_to_curve_hasher::MapToCurve, HashToCurveError},
models::short_weierstrass::{Affine, Projective},
AffineRepr,
};
use super::swu::{SWUConfig, SWUMap};
type BaseField<MP> = <MP as CurveConfig>::BaseField;
pub struct IsogenyMap<
'a,
Domain: SWCurveConfig,
Codomain: SWCurveConfig<BaseField = BaseField<Domain>>,
> {
pub x_map_numerator: &'a [BaseField<Domain>],
pub x_map_denominator: &'a [BaseField<Codomain>],
pub y_map_numerator: &'a [BaseField<Domain>],
pub y_map_denominator: &'a [BaseField<Codomain>],
}
impl<'a, Domain, Codomain> IsogenyMap<'a, Domain, Codomain>
where
Domain: SWCurveConfig,
Codomain: SWCurveConfig<BaseField = BaseField<Domain>>,
{
fn apply(&self, domain_point: Affine<Domain>) -> Result<Affine<Codomain>, HashToCurveError> {
match domain_point.xy() {
Some((x, y)) => {
let x_num = DensePolynomial::from_coefficients_slice(self.x_map_numerator);
let x_den = DensePolynomial::from_coefficients_slice(self.x_map_denominator);
let y_num = DensePolynomial::from_coefficients_slice(self.y_map_numerator);
let y_den = DensePolynomial::from_coefficients_slice(self.y_map_denominator);
let mut v: [BaseField<Domain>; 2] = [x_den.evaluate(x), y_den.evaluate(x)];
batch_inversion(&mut v);
let img_x = x_num.evaluate(x) * v[0];
let img_y = (y_num.evaluate(x) * y) * v[1];
Ok(Affine::<Codomain>::new_unchecked(img_x, img_y))
},
None => Ok(Affine::identity()),
}
}
}
pub trait WBConfig: SWCurveConfig + Sized {
type IsogenousCurve: SWUConfig<BaseField = BaseField<Self>>;
const ISOGENY_MAP: IsogenyMap<'static, Self::IsogenousCurve, Self>;
}
pub struct WBMap<P: WBConfig> {
swu_field_curve_hasher: SWUMap<P::IsogenousCurve>,
curve_params: PhantomData<fn() -> P>,
}
impl<P: WBConfig> MapToCurve<Projective<P>> for WBMap<P> {
fn new() -> Result<Self, HashToCurveError> {
match P::ISOGENY_MAP.apply(P::IsogenousCurve::GENERATOR) {
Ok(point_on_curve) => {
if !point_on_curve.is_on_curve() {
return Err(HashToCurveError::MapToCurveError(format!("the isogeny maps the generator of its domain: {} into {} which does not belong to its codomain.",P::IsogenousCurve::GENERATOR, point_on_curve)));
}
},
Err(e) => return Err(e),
}
Ok(WBMap {
swu_field_curve_hasher: SWUMap::<P::IsogenousCurve>::new().unwrap(),
curve_params: PhantomData,
})
}
fn map_to_curve(
&self,
element: <Affine<P> as AffineRepr>::BaseField,
) -> Result<Affine<P>, HashToCurveError> {
let point_on_isogenious_curve = self.swu_field_curve_hasher.map_to_curve(element).unwrap();
P::ISOGENY_MAP.apply(point_on_isogenious_curve)
}
}
#[cfg(test)]
mod test {
use crate::{
hashing::{
curve_maps::{
swu::SWUConfig,
wb::{IsogenyMap, WBConfig, WBMap},
},
map_to_curve_hasher::MapToCurveBasedHasher,
HashToCurve,
},
models::short_weierstrass::SWCurveConfig,
short_weierstrass::{Affine, Projective},
CurveConfig,
};
use ark_ff::{field_hashers::DefaultFieldHasher, fields::Fp64, MontBackend, MontFp};
#[derive(ark_ff::MontConfig)]
#[modulus = "127"]
#[generator = "6"]
pub struct F127Config;
pub type F127 = Fp64<MontBackend<F127Config, 1>>;
const F127_ZERO: F127 = MontFp!("0");
const F127_ONE: F127 = MontFp!("1");
struct TestWBF127MapToCurveConfig;
impl CurveConfig for TestWBF127MapToCurveConfig {
const COFACTOR: &'static [u64] = &[1];
#[rustfmt::skip]
const COFACTOR_INV: F127 = F127_ONE;
type BaseField = F127;
type ScalarField = F127;
}
impl SWCurveConfig for TestWBF127MapToCurveConfig {
const COEFF_A: F127 = F127_ZERO;
#[rustfmt::skip]
const COEFF_B: F127 = MontFp!("3");
const GENERATOR: Affine<Self> = Affine::new_unchecked(MontFp!("62"), MontFp!("70"));
}
struct TestSWU127MapToIsogenousCurveConfig;
impl CurveConfig for TestSWU127MapToIsogenousCurveConfig {
const COFACTOR: &'static [u64] = &[1];
#[rustfmt::skip]
const COFACTOR_INV: F127 = F127_ONE;
type BaseField = F127;
type ScalarField = F127;
}
impl SWCurveConfig for TestSWU127MapToIsogenousCurveConfig {
const COEFF_A: F127 = MontFp!("109");
#[rustfmt::skip]
const COEFF_B: F127 = MontFp!("124");
const GENERATOR: Affine<Self> = Affine::new_unchecked(MontFp!("84"), MontFp!("2"));
}
impl SWUConfig for TestSWU127MapToIsogenousCurveConfig {
const ZETA: F127 = MontFp!("-1");
}
const ISOGENY_MAP_TESTWBF127: IsogenyMap<
'_,
TestSWU127MapToIsogenousCurveConfig,
TestWBF127MapToCurveConfig,
> = IsogenyMap {
x_map_numerator: &[
MontFp!("4"),
MontFp!("63"),
MontFp!("23"),
MontFp!("39"),
MontFp!("-14"),
MontFp!("23"),
MontFp!("-32"),
MontFp!("32"),
MontFp!("-13"),
MontFp!("40"),
MontFp!("34"),
MontFp!("10"),
MontFp!("-21"),
MontFp!("-57"),
],
x_map_denominator: &[
MontFp!("2"),
MontFp!("31"),
MontFp!("-10"),
MontFp!("-20"),
MontFp!("63"),
MontFp!("-44"),
MontFp!("34"),
MontFp!("30"),
MontFp!("-30"),
MontFp!("-33"),
MontFp!("11"),
MontFp!("-13"),
MontFp!("1"),
],
y_map_numerator: &[
MontFp!("-34"),
MontFp!("-57"),
MontFp!("30"),
MontFp!("-18"),
MontFp!("-60"),
MontFp!("-43"),
MontFp!("-63"),
MontFp!("-18"),
MontFp!("-49"),
MontFp!("36"),
MontFp!("12"),
MontFp!("62"),
MontFp!("5"),
MontFp!("6"),
MontFp!("-7"),
MontFp!("48"),
MontFp!("41"),
MontFp!("59"),
MontFp!("10"),
],
y_map_denominator: &[
MontFp!("32"),
MontFp!("-18"),
MontFp!("-24"),
MontFp!("23"),
MontFp!("18"),
MontFp!("-55"),
MontFp!("-16"),
MontFp!("-61"),
MontFp!("-46"),
MontFp!("-13"),
MontFp!("-42"),
MontFp!("11"),
MontFp!("-30"),
MontFp!("38"),
MontFp!("3"),
MontFp!("52"),
MontFp!("-63"),
MontFp!("44"),
MontFp!("1"),
],
};
impl WBConfig for TestWBF127MapToCurveConfig {
type IsogenousCurve = TestSWU127MapToIsogenousCurveConfig;
const ISOGENY_MAP: super::IsogenyMap<'static, Self::IsogenousCurve, Self> =
ISOGENY_MAP_TESTWBF127;
}
#[test]
fn hash_arbitrary_string_to_curve_wb() {
use sha2::Sha256;
let test_wb_to_curve_hasher = MapToCurveBasedHasher::<
Projective<TestWBF127MapToCurveConfig>,
DefaultFieldHasher<Sha256, 128>,
WBMap<TestWBF127MapToCurveConfig>,
>::new(&[1])
.unwrap();
let hash_result = test_wb_to_curve_hasher.hash(b"if you stick a Babel fish in your ear you can instantly understand anything said to you in any form of language.").expect("fail to hash the string to curve");
assert!(
hash_result.x != F127_ZERO && hash_result.y != F127_ZERO,
"we assume that not both a and b coefficienst are zero for the test curve"
);
assert!(
hash_result.is_on_curve(),
"hash results into a point off the curve"
);
}
}