-
Notifications
You must be signed in to change notification settings - Fork 1
/
affine.rs
316 lines (260 loc) · 9.2 KB
/
affine.rs
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
use algebra::{
bls12_381::{Fq, Fq2, G1Affine, G2Affine},
bytes::FromBytes,
curves::models::short_weierstrass_jacobian::GroupAffine,
fields::{Field, PrimeField, SquareRootField},
io::Cursor,
prelude::Zero,
BigInteger384, SWModelParameters,
};
use core::ops::{AddAssign, MulAssign, Neg};
pub fn read_g1affine(data: [u8; 96]) -> Result<G1Affine, ()> {
let uncompressed = G1Uncompressed::new(data);
uncompressed.into_affine()
}
pub fn read_g2affine(data: [u8; 192]) -> Result<G2Affine, ()> {
let uncompressed = G2Uncompressed::new(data);
uncompressed.into_affine()
}
pub fn read_compressed_g1affine(data: [u8; 48]) -> Result<G1Affine, ()> {
let uncompressed = G1Compressed::new(data);
uncompressed.into_affine()
}
pub fn read_compressed_g2affine(data: [u8; 96]) -> Result<G2Affine, ()> {
let uncompressed = G2Compressed::new(data);
uncompressed.into_affine()
}
fn read_fq(cursor: &mut Cursor<&[u8]>) -> Result<Fq, ()> {
let mut bi = BigInteger384::read(cursor).map_err(|_e| ())?;
let mut res: BigInteger384 = 0.into();
for (i, res) in bi.as_mut().iter_mut().zip(res.as_mut().iter_mut().rev()) {
*res = i.to_be();
}
Ok(Fq::from_repr(res))
}
struct G1Uncompressed {
data: [u8; 96],
}
impl G1Uncompressed {
fn new(data: [u8; 96]) -> Self {
G1Uncompressed { data }
}
fn into_affine(&self) -> Result<G1Affine, ()> {
let affine = self.into_affine_unchecked()?;
if !affine.is_on_curve() {
return Err(());
} else if !affine.is_in_correct_subgroup_assuming_on_curve() {
return Err(());
} else {
Ok(affine)
}
}
fn into_affine_unchecked(&self) -> Result<G1Affine, ()> {
// Create a copy of this representation.
let mut copy = self.data;
if copy[0] & (1 << 7) != 0 {
// Distinguisher bit is set, but this should be uncompressed!
return Err(());
}
if copy[0] & (1 << 6) != 0 {
// This is the point at infinity, which means that if we mask away
// the first two bits, the entire representation should consist
// of zeroes.
copy[0] &= 0x3f;
if copy.iter().all(|b| *b == 0) {
Ok(G1Affine::zero())
} else {
return Err(());
}
} else {
if copy[0] & (1 << 5) != 0 {
// The bit indicating the y-coordinate should be lexicographically
// largest is set, but this is an uncompressed element.
return Err(());
}
// Unset the three most significant bits.
copy[0] &= 0x1f;
let mut cursor = Cursor::new(© as &[u8]);
let x = read_fq(&mut cursor)?;
let y = read_fq(&mut cursor)?;
Ok(G1Affine::new(x, y, false))
}
}
}
struct G2Uncompressed {
data: [u8; 192],
}
impl G2Uncompressed {
fn new(data: [u8; 192]) -> Self {
G2Uncompressed { data }
}
fn into_affine(&self) -> Result<G2Affine, ()> {
let affine = self.into_affine_unchecked()?;
if !affine.is_on_curve() {
return Err(());
} else if !affine.is_in_correct_subgroup_assuming_on_curve() {
return Err(());
} else {
Ok(affine)
}
}
fn into_affine_unchecked(&self) -> Result<G2Affine, ()> {
// Create a copy of this representation.
let mut copy = self.data;
if copy[0] & (1 << 7) != 0 {
// Distinguisher bit is set, but this should be uncompressed!
return Err(());
}
if copy[0] & (1 << 6) != 0 {
// This is the point at infinity, which means that if we mask away
// the first two bits, the entire representation should consist
// of zeroes.
copy[0] &= 0x3f;
if copy.iter().all(|b| *b == 0) {
Ok(G2Affine::zero())
} else {
Err(())
}
} else {
if copy[0] & (1 << 5) != 0 {
// The bit indicating the y-coordinate should be lexicographically
// largest is set, but this is an uncompressed element.
return Err(());
}
// Unset the three most significant bits.
copy[0] &= 0x1f;
let mut cursor = Cursor::new(© as &[u8]);
let x_c1 = read_fq(&mut cursor)?;
let x_c0 = read_fq(&mut cursor)?;
let y_c1 = read_fq(&mut cursor)?;
let y_c0 = read_fq(&mut cursor)?;
Ok(G2Affine::new(
Fq2::new(x_c0, x_c1),
Fq2::new(y_c0, y_c1),
false,
))
}
}
}
fn get_point_from_x<P: SWModelParameters>(
x: P::BaseField,
greatest: bool,
) -> Result<GroupAffine<P>, ()> {
// Compute x^3 + b
let mut x3b = x;
x3b.square_in_place();
x3b.mul_assign(&x);
x3b.add_assign(&P::COEFF_B);
x3b.sqrt()
.map(|y| {
let negy = y.neg();
GroupAffine::new(x, if (y < negy) ^ greatest { y } else { negy }, false)
})
.ok_or_else(|| ())
}
struct G1Compressed {
data: [u8; 48],
}
impl G1Compressed {
fn new(data: [u8; 48]) -> Self {
G1Compressed { data }
}
fn into_affine(&self) -> Result<G1Affine, ()> {
let affine = self.into_affine_unchecked()?;
// decompression guarantees that this is on the curve
if !affine.is_in_correct_subgroup_assuming_on_curve() {
return Err(());
} else {
Ok(affine)
}
}
fn into_affine_unchecked(&self) -> Result<G1Affine, ()> {
// Create a copy of this representation.
let mut copy = self.data;
if copy[0] & (1 << 7) == 0 {
// Distinguisher bit is set, but this should be uncompressed!
return Err(());
}
if copy[0] & (1 << 6) != 0 {
// This is the point at infinity, which means that if we mask away
// the first two bits, the entire representation should consist
// of zeroes.
copy[0] &= 0x3f;
if copy.iter().all(|b| *b == 0) {
Ok(G1Affine::zero())
} else {
return Err(());
}
} else {
// Determine if the intended y coordinate must be greater
// lexicographically.
let greatest = copy[0] & (1 << 5) != 0;
// Unset the three most significant bits.
copy[0] &= 0x1f;
let mut cursor = Cursor::new(© as &[u8]);
let x = read_fq(&mut cursor)?;
get_point_from_x(x, greatest)
}
}
}
struct G2Compressed {
data: [u8; 96],
}
impl G2Compressed {
fn new(data: [u8; 96]) -> Self {
G2Compressed { data }
}
fn into_affine(&self) -> Result<G2Affine, ()> {
let affine = self.into_affine_unchecked()?;
// decompression guarantees that this is on the curve
if !affine.is_in_correct_subgroup_assuming_on_curve() {
return Err(());
} else {
Ok(affine)
}
}
fn into_affine_unchecked(&self) -> Result<G2Affine, ()> {
// Create a copy of this representation.
let mut copy = self.data;
if copy[0] & (1 << 7) == 0 {
// Distinguisher bit is set, but this should be uncompressed!
return Err(());
}
if copy[0] & (1 << 6) != 0 {
// This is the point at infinity, which means that if we mask away
// the first two bits, the entire representation should consist
// of zeroes.
copy[0] &= 0x3f;
if copy.iter().all(|b| *b == 0) {
Ok(G2Affine::zero())
} else {
return Err(());
}
} else {
// Determine if the intended y coordinate must be greater
// lexicographically.
let greatest = copy[0] & (1 << 5) != 0;
// Unset the three most significant bits.
copy[0] &= 0x1f;
let mut cursor = Cursor::new(© as &[u8]);
let x_c1 = read_fq(&mut cursor)?;
let x_c0 = read_fq(&mut cursor)?;
get_point_from_x(Fq2::new(x_c0, x_c1), greatest)
}
}
}
#[cfg(test)]
mod tests {
use super::{read_g1affine, read_g2affine};
use hex_literal::hex;
#[test]
fn test_readg1affine() {
let t1 = hex!("0db882cf5db3e8567f16b4db1772d4d1f5a3fe8d62f0df2eb8a5cfa50806702afde8fc25335eb5ec859c2818b2610b2e19ab445dac720bb1f2b0cd3336f7a1acc62bf1b3a321826264dc7e469281e23b218394d598689da04e136878ff9a7897");
let _value = read_g1affine(t1).unwrap();
}
#[test]
fn test_readg2affine() {
let t2 = hex!("0a416b8187450b28f025c421e3ff14d38f9abd9af2f1046b914b53ab37e9aebba683cb25284e5c22fa341129985250a103547de5d005df48265f7cb258162253d56fbc682d106a1ecb07666ebf7524a364e512c37aa62f82d6e7dd4ed8838478104376a98072766c29959358e9cde6a4985618f65ea257e8f288974f4aedde52e5dac2fb7ae5d30eab7cd828a2c8b15f15b16f139f2c33ef33d63befe404e696c97077d17ea42f4ff9d82ec456aaf43914a3d07968111a3a348f157e64c0278a");
let _value = read_g2affine(t2).unwrap();
}
}