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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
#[cfg(not(any(target_pointer_width = "64")))]
compile_error!("this crate requires 64-bit limbs");
use crate::bnfp::BNFp;
use crate::bnfp2::BNFp2;
use crate::bnparam::BNParam;
use crate::bnzn::BNZn;
use crate::traits::{BNField, One};
use crypto_bigint::{Choice, CtAssign, CtEq, CtSelect, Random, Uint, Zero};
use crypto_bigint::rand_core::{Rng, TryRng};
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
/// The group <b>G₂</b> ≔ <i>E'</i>[<i>n</i>](<b>F</b><sub><i>p²</i></sub>)
/// of <b>F</b><sub><i>p²</i></sub> -rational <i>n</i>-torsion points on the
/// BN curve twist <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>.
pub struct BNPoint2<BN: BNParam, const LIMBS: usize> {
pub(crate) x: BNFp2<BN, LIMBS>,
pub(crate) y: BNFp2<BN, LIMBS>,
pub(crate) z: BNFp2<BN, LIMBS>,
}
/*
// the Litany of All Saints:
pub type BN062Point2 = BNPoint2<BN062Param, 1>;
pub type BN126Point2 = BNPoint2<BN126Param, 2>;
pub type BN190Point2 = BNPoint2<BN190Param, 3>;
pub type BN254Point2 = BNPoint2<BN254Param, 4>;
pub type BN318Point2 = BNPoint2<BN318Param, 5>;
pub type BN382Point2 = BNPoint2<BN382Param, 6>;
pub type BN446Point2 = BNPoint2<BN446Param, 7>;
pub type BN510Point2 = BNPoint2<BN510Param, 8>;
pub type BN574Point2 = BNPoint2<BN574Param, 9>;
pub type BN638Point2 = BNPoint2<BN638Param, 10>;
pub type BN702Point2 = BNPoint2<BN702Param, 11>;
pub type BN766Point2 = BNPoint2<BN766Param, 12>;
// */
impl<BN: BNParam, const LIMBS: usize> BNPoint2<BN, LIMBS> {
/// Create a normalized point on a BN curve twist
/// <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>
/// from a given affine <i>X'</i>-coordinate and the least significant bit (LSB) of the <i>Y'</i>-coordinate.
///
/// NB: specify y_lsb as Choice::FALSE if LSB==0 and as Choice::TRUE if LSB==1.
#[inline]
pub(crate) fn new(x: BNFp2<BN, LIMBS>, y_lsb: Choice) -> Self {
let bt = BNFp2::from_base(BNFp::from_word(BN::CURVE_B)).div_xi();
let y2 = x.cb() + bt;
let mut y = y2.sqrt(y_lsb);
assert_eq!(y.sq(), y2);
y.ct_assign(&(-y), y.is_odd() ^ y_lsb);
Self { x, y, z: BNFp2::one() }
}
/// Determine if given projective coordinates <i>X'</i>, <i>Y'</i>, and <i>Z'</i>
/// specify a point on a BN curve twist <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>.
#[inline]
pub fn is_point(x: BNFp2<BN, LIMBS>, y: BNFp2<BN, LIMBS>, z: BNFp2<BN, LIMBS>) -> Choice {
// projective curve equation: Y'^2*Z' = X'^3 + b'*Z'^3 where b' = b/xi
(y.sq()*z).ct_eq(&(x.cb() + BNFp2::from_word(BN::CURVE_B).div_xi()*z.cb()))
}
/// Create a normalized point on a BN curve twist
/// <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>
/// from given affine coordinates <i>X'</i> and <i>Y'</i>.
#[inline]
fn from_affine(x: BNFp2<BN, LIMBS>, y: BNFp2<BN, LIMBS>) -> Self {
assert!(bool::from(Self::is_point(x, y, BNFp2::one())));
Self { x, y, z: BNFp2::one() }
}
/// Create a point on a BN curve twist <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>
/// from given projective coordinates <i>X'</i>, <i>Y'</i>, and <i>Z'</i>.
#[inline]
pub(crate) fn from_proj(x: BNFp2<BN, LIMBS>, y: BNFp2<BN, LIMBS>, z: BNFp2<BN, LIMBS>) -> Self {
assert!(bool::from(Self::is_point(x, y, z)));
Self { x: x.clone(), y: y.clone(), z: z.clone() }
}
/// Create an instance of the default generator of <i>n</i>-torsion <i>G₂ ≔ (-i, 1)</i>
/// on a BN curve twist <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>.
#[inline]
pub fn default_generator() -> Self {
Self::new(-BNFp2::i(), Choice::from(1)).elim_cof()
}
/// Hash input data into a point on the (quadratic extension field) <i>n</i>-torsion group
/// <i><b>G</b>₂</i> ≔ <i>E'</i>[<i>n</i>](<b>F</b><sub><i>p²</i></sub>)
/// of a BN curve twist <i>E'</i>/<b>F</b><sub><i>p²</i></sub> :
/// <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i> with SHAKE-128.
#[inline]
pub fn shake128(data: &[u8]) -> Self {
Self::point_factory(BNFp2::shake128(data)).elim_cof()
}
/// Hash input data into a point on the (quadratic extension field) <i>n</i>-torsion group
/// <i><b>G</b>₂</i> ≔ <i>E'</i>[<i>n</i>](<b>F</b><sub><i>p²</i></sub>)
/// of a BN curve twist <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i> with SHAKE-256.
#[inline]
pub fn shake256(data: &[u8]) -> Self {
Self::point_factory(BNFp2::shake256(data)).elim_cof()
}
/// Compute a normalized (i.e. affine) point equivalent to this
/// on a BN curve twist <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>.
#[inline]
pub(crate) fn normalize(&self) -> Self {
let ch = self.z.is_zero();
let inv = self.z.ct_select(&self.y, ch).inv();
Self {
x: self.x*inv,
y: self.y*inv,
z: BNFp2::one().ct_select(&BNFp2::zero(), ch),
}
}
/// Compute [<i>2ᵗ</i>]<i>Q'</i> for a BN curve twist point
/// <i>Q'</i> ∈ <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>
/// (i.e. double <i>t</i> times) via complete elliptic point doubling.
#[inline]
pub fn double(&self, t: usize) -> Self {
let mut d = self.clone();
d.double_self(t);
d
}
/// Compute [<i>2ᵗ</i>]<i>Q'</i> for a BN curve twist point
/// <i>Q'</i> ∈ <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>
/// (i.e. double <i>t</i> times) via complete elliptic point doubling.
///
/// Reference:
///
/// * Joost Renes, Craig Costello, Lejla Batina:
/// <a href="https://link.springer.com/content/pdf/10.1007/978-3-662-49890-3_16">
/// "Complete addition formulas for prime order elliptic curves"</a>
/// (Algorithm 9), Eurocrypt 2016, LNCS 9665 (part I), pp. 403--428, Springer, 2016.
#[inline]
pub(crate) fn double_self(&mut self, t: usize) {
let mut x = self.x;
let mut y = self.y;
let mut z = self.z;
let mut t0: BNFp2<BN, LIMBS>;
let mut t1: BNFp2<BN, LIMBS>;
let mut t2: BNFp2<BN, LIMBS>;
let mut x3: BNFp2<BN, LIMBS>;
let mut y3: BNFp2<BN, LIMBS>;
let mut z3: BNFp2<BN, LIMBS>;
for _ in 0..t {
t0 = y.sq();
z3 = t0+t0;
z3 = z3+z3;
z3 = z3+z3;
t1 = y*z;
t2 = z*z;
t2 = (3*BN::CURVE_B)*t2.div_xi();
x3 = t2*z3;
y3 = t0+t2;
z3 = t1*z3;
t1 = t2+t2;
t2 = t1+t2;
t0 = t0-t2;
y3 = t0*y3;
y3 = x3+y3;
t1 = x*y;
x3 = t0*t1;
x3 = x3+x3;
x = x3;
y = y3;
z = z3;
}
self.x = x;
self.y = y;
self.z = z;
}
/// Map a field element <i>t</i> ∈ <b>F</b><sub><i>p²</i></sub> to a point on this BN curve twist
/// using the isochronous Shallue-van de Woestijne method.
///
/// NB: the output point is only guaranteed to be on the curve,
/// <i>not</i> in the (quadratic extension field) <i>n</i>-torsion group
/// <b>G</b><i>₂</i> ≔ <i>E'</i>[<i>n</i>](<b>F</b><sub><i>p²</i></sub>),
/// that is, cofactor multiplication is not implicitly applied here.
///
/// If a point in <b>G</b><i>₂</i> is required, either resort to explicit
/// cofactor multiplication or use method BNPoint2::shake256(.) instead.
///
/// Reference:
///
/// * Andrew Shallue, Christiaan E. van de Woestijne:
/// "Construction of rational points on elliptic curves over finite fields."
/// In: Hess, F., Pauli, S., Pohst, M. E. (eds.), <i>Algorithmic Number Theory -- ANTS-VII</i>,
/// Lecture Notes in Computer Science, vol. 4076, pp. 510--524, 2006.
/// Springer, Berlin Heidelberg, 2006.
/// https://doi.org/10.1007/11792086_36
#[inline]
pub fn point_factory(t: BNFp2<BN, LIMBS>) -> BNPoint2<BN, LIMBS> {
let one = BNFp2::one();
let bt = BNFp2::from(BNFp::from_word(BN::FIELD_XI_RE), -BNFp::from_word(BN::FIELD_XI_IM));
let sqrt_m3 = BNFp::from_words(BN::SQRT_NEG_3.try_into().unwrap());
let num = sqrt_m3*t; // sqrt(-3)*t
let den = one + bt + t.sq(); // 1 + b + t^2
// Montgomery's trick to use a single inversion, (num*den)^-1, to compute
// the inverse of num = den*(num*den)^-1 and the inverse of den = num*(num*den)^-1:
let monty = (num*den).inv();
let w = num.sq()*monty; // sqrt(-3)*t/(1 + b + t^2)
let inv_w = den.sq()*monty;
let svdw = BNFp2::from_base(BNFp::from_words(BN::SVDW.try_into().unwrap())); // (-1 + sqrt(-3))/2
// candidate x-coordinates:
let x0 = svdw - t*w; // (-1 + sqrt(-3))/2 - t*w
let x1 = -(one + x0); // -1 - x_0
let x2 = one + inv_w.sq(); // 1 + 1/w^2
// quadratic characters of the corresponding curve equation RHS:
let q0 = (x0.cb() + bt).legendre(); // legendre((x0^3 + b), p)
assert_ne!(q0, 0); // no point of order 2 exists on a curve of (large) prime order
let q1 = (x1.cb() + bt).legendre(); // legendre((x1^3 + b), p)
assert_ne!(q1, 0); // no point of order 2 exists on a curve of (large) prime order
// constant-time sequential search for the proper choice of x:
let mut xc = x2;
xc.ct_assign(&x1, q1.ct_eq(&1));
xc.ct_assign(&x0, q0.ct_eq(&1));
let leg = t.legendre();
// point construction:
BNPoint2::new(xc, leg.ct_ne(&0) & leg.ct_ne(&1))
}
/// Compute the <i>k</i>-th Frobenius endomorphism on the BN curve twist
/// <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>,
/// namely the map <i>ψᵏ</i> : <i>E'</i> → <i>E'</i> defined as
/// the composition <i>ψᵏ</i> ≔ <i>φ⁻¹</i> o <i>πᵏ</i> o <i>φ</i>,
/// where <i>φ</i> : <i>E'</i> → <i>E</i> is the embedding
/// <i>φ</i>(<i>x'</i>, <i>y'</i>) = (<i>x'ξ<sup>⅓</sup></i>, <i>y'ξ<sup>½</sup></i>) and
/// <i>π</i> : <i>E</i> → <i>E</i> is the Frobenius endomorphism on <i>E</i>,
/// <i>π</i>(<i>x</i>, <i>y</i>) ≔ (<i>xᵖ</i>, <i>yᵖ</i>), with <i>0≤k<12</i>.
#[inline]
pub(crate) fn psi(&self, k: usize) -> Self {
let zeta = BNFp::from_words(BN::ZETA.try_into().unwrap());
let sigma = BNFp::from_words(BN::THETA.try_into().unwrap());
let one = BNFp::one();
assert!(k < 12);
match k {
0 => self.clone(),
1 => Self {
x: -(zeta + one)*self.x.conj().mul_i(),
y: -(zeta*sigma)*self.y.conj().mul_xi(),
z: self.z.conj(),
},
2 => Self {
x: zeta*self.x,
y: -self.y,
z: self.z,
},
3 => Self {
x: self.x.conj().mul_i(),
y: (zeta*sigma)*self.y.conj().mul_xi(),
z: self.z.conj(),
},
4 => Self {
x: -(zeta + one)*self.x,
y: self.y,
z: self.z,
},
5 => Self {
x: zeta*self.x.conj().mul_i(),
y: -(zeta*sigma)*self.y.conj().mul_xi(),
z: self.z.conj(),
},
6 => Self {
x: self.x,
y: -self.y,
z: self.z,
},
7 => Self {
x: -(zeta + one)*self.x.conj().mul_i(),
y: (zeta*sigma)*self.y.conj().mul_xi(),
z: self.z.conj(),
},
8 => Self {
x: zeta*self.x,
y: self.y,
z: self.z,
},
9 => Self {
x: self.x.conj().mul_i(),
y: -(zeta*sigma)*self.y.conj().mul_xi(),
z: self.z.conj(),
},
10 => Self {
x: -(zeta + one)*self.x,
y: -self.y,
z: self.z,
},
11 => Self {
x: zeta*self.x.conj().mul_i(),
y: (zeta*sigma)*self.y.conj().mul_xi(),
z: self.z.conj(),
},
_ => self.clone(), // just to make the compiler happy
}
}
/// Compute [<i>u</i>]<i>`self`</i>.
#[inline]
fn mul_u(&self) -> Self {
// since the coefficient u is public and fixed, the simple double-and-add method suffices:
let u: Uint<LIMBS> = Uint::from_words(BN::U.try_into().unwrap());
let mut v = self.clone();
let ubits = u.bits();
for k in (0..ubits-1).rev() {
v.double_self(1);
if bool::from(u.bit(k)) {
v += *self;
}
}
-v // NB: constant U is actually |u| = -u.
}
/// Eliminate the cofactor from this point <i>Q</i> ∈ <i>E'</i>/<b>F</b><sub><i>p²</i></sub>,
/// yielding a point of <i>n</i>-torsion <i>Q'</i> ∈ <b>G</b><i>₂</i> ≔
/// <i>E'</i>[<i>n</i>](<b>F</b><sub><i>p²</i></sub>).
///
/// NB: This operation is carried out through the efficient Frobenius endomorphism,
/// <i>not</i> by cofactor multiplication, which would be more computationally expensive.
///
/// References:
///
/// * Mike Scott, Naomi Benger, Manuel Charlemagne, LuÃs J. DomÃnguez-Pérez, Ezekiel J. Kachisa:
/// "Fast Hashing to <b>G</b><i>₂</i> on Pairing-Friendly Curves."
/// In: Shacham, H., Waters, B. (eds.),
/// <i>Pairing-Based Cryptography -- Pairing 2009</i>.
/// Lecture Notes in Computer Science, vol. 5671, pp. 102–113.
/// Springer, Berlin Heidelberg (2009).
/// https://doi.org/10.1007/978-3-642-03298-1_8
///
/// * Laura Fuentes-Castañeda, Edward Knapp, Francisco RodrÃguez-HenrÃquez:
/// "Faster Hashing to <b>G</b><i>₂</i>."
/// In: <i>Selected Areas in Cryptography</i>. SAC 2011.
/// Lecture Notes in Computer Science, vol. 7118, pp. 412--430, 2012.
/// Springer, Berlin Heidelberg (2012).
/// https://doi.org/10.1007/978-3-642-28496-0_25
#[allow(non_snake_case)]
#[inline]
pub fn elim_cof(&self) -> Self {
let Q = *self;
let uQ = Q.mul_u(); // [u]Q
let u3Q = uQ.double(1) + uQ; // [3u]Q
uQ + u3Q.psi(1) + uQ.psi(2) + Q.psi(3)
}
/// Convert `self` to byte array representation.
/// This is the ANSI X9.62 Point-to-Octet-String Conversion primitive, compressed form.
#[allow(non_snake_case)]
#[inline]
pub fn to_bytes(&self) -> Vec<u8> {
let N = self.normalize();
// ANSI X9.62 'compressed' prefix: 0x02 | lsb(N.y)
let mut cp = 0x2u8; // lsb(N.y) == 0
cp.ct_assign(&0x3u8, N.y.is_odd()); // lsb(N.y) == 1
let mut bytes = Vec::new();
bytes.push(cp);
let mut next = N.x.to_bytes(); bytes.append(&mut next);
bytes
}
}
impl<BN: BNParam, const LIMBS: usize> Add for BNPoint2<BN, LIMBS> {
type Output = Self;
/// Complete elliptic point addition
/// for a BN curve <i>E</i>/<b>F</b><sub><i>p</i></sub> : <i>Y²Z</i> = <i>X³ + bZ³</i>.
///
/// Reference:
///
/// * Joost Renes, Craig Costello, Lejla Batina:
/// <a href="https://link.springer.com/content/pdf/10.1007/978-3-662-49890-3_16">
/// "Complete addition formulas for prime order elliptic curves"</a>
/// (Algorithm 7), Eurocrypt 2016, LNCS 9665 (part I), pp. 403--428, Springer, 2016.
#[inline]
fn add(self, other: Self) -> Self::Output {
let mut point = self;
point += other;
point
}
}
impl<BN: BNParam, const LIMBS: usize> AddAssign for BNPoint2<BN, LIMBS> {
/// Complete elliptic point addition
/// for a BN curve twist <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>bZ'³</i>.
///
/// Reference:
///
/// * Joost Renes, Craig Costello, Lejla Batina:
/// <a href="https://link.springer.com/content/pdf/10.1007/978-3-662-49890-3_16">
/// "Complete addition formulas for prime order elliptic curves"</a>
/// (Algorithm 7), Eurocrypt 2016, LNCS 9665 (part I), pp. 403--428, Springer, 2016.
#[inline]
fn add_assign(&mut self, pair: Self) {
let x1 = self.x;
let y1 = self.y;
let z1 = self.z;
let x2 = pair.x;
let y2 = pair.y;
let z2 = pair.z;
let mut t0: BNFp2<BN, LIMBS>;
let mut t1: BNFp2<BN, LIMBS>;
let mut t2: BNFp2<BN, LIMBS>;
let mut t3: BNFp2<BN, LIMBS>;
let mut t4: BNFp2<BN, LIMBS>;
let mut x3: BNFp2<BN, LIMBS>;
let mut y3: BNFp2<BN, LIMBS>;
let mut z3: BNFp2<BN, LIMBS>;
t0 = x1*x2;
t1 = y1*y2;
t2 = z1*z2;
t3 = x1 + y1;
t4 = x2 + y2;
t3 = t3*t4;
t4 = t0 + t1;
t3 = t3 - t4;
t4 = y1 + z1;
x3 = y2 + z2;
t4 = t4*x3;
x3 = t1 + t2;
t4 = t4 - x3;
x3 = x1 + z1;
y3 = x2 + z2;
x3 = x3*y3;
y3 = t0 + t2;
y3 = x3 - y3;
x3 = t0 + t0;
t0 = x3 + t0;
t2 = (3*BN::CURVE_B)*t2.div_xi();
z3 = t1 + t2;
t1 = t1 - t2;
y3 = (3*BN::CURVE_B)*y3.div_xi();
x3 = t4*y3;
t2 = t3*t1;
x3 = t2 - x3;
y3 = y3*t0;
t1 = t1*z3;
y3 = t1 + y3;
t0 = t0*t3;
z3 = z3*t4;
z3 = z3 + t0;
self.x = x3;
self.y = y3;
self.z = z3;
}
}
impl<BN: BNParam, const LIMBS: usize> Clone for BNPoint2<BN, LIMBS> {
#[inline]
fn clone(&self) -> Self {
Self {
x: self.x.clone(),
y: self.y.clone(),
z: self.z.clone(),
}
}
}
impl<BN: BNParam, const LIMBS: usize> Copy for BNPoint2<BN, LIMBS> {}
impl<BN: BNParam, const LIMBS: usize> CtAssign for BNPoint2<BN, LIMBS> {
#[inline]
fn ct_assign(&mut self, src: &Self, choice: Choice) {
self.x.ct_assign(&src.x, choice);
self.y.ct_assign(&src.y, choice);
self.z.ct_assign(&src.z, choice);
}
}
impl<BN: BNParam, const LIMBS: usize> CtEq for BNPoint2<BN, LIMBS> {
#[inline]
fn ct_eq(&self, pair: &Self) -> Choice {
// x/z = pair.x/pair.z <=> x*pair.z = pair.x*z
// y/z = pair.y/pair.z <=> y*pair.z = pair.y*z
(self.x*pair.z).ct_eq(&(pair.x*self.z)) &
(self.y*pair.z).ct_eq(&(pair.y*self.z))
}
#[inline]
fn ct_ne(&self, pair: &Self) -> Choice {
// x/z != pair.x/pair.z <=> x*pair.z != pair.x*z
// y/z != pair.y/pair.z <=> y*pair.z != pair.y*z
(self.x*pair.z).ct_ne(&(pair.x*self.z)) |
(self.y*pair.z).ct_ne(&(pair.y*self.z))
}
}
impl<BN: BNParam, const LIMBS: usize> CtSelect for BNPoint2<BN, LIMBS> {
#[inline]
fn ct_select(&self, other: &Self, choice: Choice) -> Self {
Self {
x: self.x.ct_select(&other.x, choice),
y: self.y.ct_select(&other.y, choice),
z: self.z.ct_select(&other.z, choice),
}
}
}
impl<BN: BNParam, const LIMBS: usize> Debug for BNPoint2<BN, LIMBS> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self, f)
}
}
impl<BN: BNParam, const LIMBS: usize> Display for BNPoint2<BN, LIMBS> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let normal = self.normalize();
write!(f, "[{} : {} : {}]", normal.x, normal.y, normal.z)
}
}
impl<BN: BNParam, const LIMBS: usize> Mul<BNPoint2<BN, LIMBS>> for Uint<LIMBS> {
type Output = BNPoint2<BN, LIMBS>;
#[inline]
fn mul(self, point: BNPoint2<BN, LIMBS>) -> Self::Output {
let mut v = point;
v *= self;
v
}
}
impl<BN: BNParam, const LIMBS: usize> Mul<BNPoint2<BN, LIMBS>> for BNZn<BN, LIMBS> {
type Output = BNPoint2<BN, LIMBS>;
#[inline]
fn mul(self, point: BNPoint2<BN, LIMBS>) -> Self::Output {
let mut v = point;
v *= self.to_uint();
v
}
}
impl<BN: BNParam, const LIMBS: usize> MulAssign<Uint<LIMBS>> for BNPoint2<BN, LIMBS> {
/// Multiply a scalar (mod <i>n</i>) and a point via fixed-window multiplication.
///
/// Reference:
///
/// * Alfred J. Menezes, Paul C. van Oorschot, Scott A. Vanstone,
/// <a href="https://cacr.uwaterloo.ca/hac/">"Handbook of Applied Cryptography"</a>,
/// CRC Press (1997), section 14.6 (Exponentiation), algorithm 14.82.
#[inline]
fn mul_assign(&mut self, scalar: Uint<LIMBS>) {
// prepare a table such that t[d] = d*P, where 0 <= d < 16:
let mut t = [Self::zero(); 16];
t[1] = self.clone();
for d in 1..8 {
t[2*d] = t[d].double(1); // (2*d)*P = 2*(d*P)
t[2*d + 1] = t[2*d].clone() + *self; // (2*d + 1)*P = 2*(d*P) + P
}
// perform fixed-window multiplication by scalar, one hex digit at a time:
let mut v = Self::zero(); // accumulator
let s = scalar.as_words(); // scalar
for j in (0..s.len() << 4).rev() { // scan the scalar from most to least significant nybble
v.double_self(4); // multiply the accumulator by 16
let d = ((s[j >> 4] >> ((j & 0xF) << 2)) & 0xF) as usize; // hex digit at index k
// perform constant-time sequential search on t to extract t[d]:
let mut w = Self::zero();
for e in 0..16 { // t[] contains 16 points...
w.ct_assign(&t[e], e.ct_eq(&d)); // ... (of which only the d-th is to be kept)
}
v += w; // accumulate pt[d] into v
}
*self = v
}
}
impl<BN: BNParam, const LIMBS: usize> Neg for BNPoint2<BN, LIMBS> {
type Output = Self;
/// Compute the opposite of a point on a BN curve.
#[inline]
fn neg(self) -> Self::Output {
Self::Output {
x: self.x,
y: self.y.neg(),
z: self.z,
}
}
}
impl<BN: BNParam, const LIMBS: usize> PartialEq<Self> for BNPoint2<BN, LIMBS> {
#[inline]
fn eq(&self, pair: &Self) -> bool {
self.ct_eq(&pair).into()
}
#[inline]
fn ne(&self, pair: &Self) -> bool {
self.ct_ne(&pair).into()
}
}
impl<BN: BNParam, const LIMBS: usize> Random for BNPoint2<BN, LIMBS> {
/// Try to pick a uniform point from the <i>n</i>-torsion of the BN curve twist
/// <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>.
#[inline]
fn try_random_from_rng<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
match BNFp2::try_random_from_rng(rng) {
Ok(val) => Ok(Self::point_factory(val).elim_cof()),
Err(e) => Err(e),
}
}
/// Pick a uniform point from the <i>n</i>-torsion of the BN curve twist
/// <i>E'</i>/<b>F</b><sub><i>p²</i></sub> : <i>Y'²Z'</i> = <i>X'³</i> + <i>b'Z'³</i>.
#[inline]
fn random_from_rng<R: Rng + ?Sized>(rng: &mut R) -> Self {
Self::point_factory(BNFp2::random_from_rng(rng)).elim_cof()
}
}
impl<BN: BNParam, const LIMBS: usize> Sub for BNPoint2<BN, LIMBS> {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self::Output {
let mut point = self;
point -= other;
point
}
}
impl<BN: BNParam, const LIMBS: usize> SubAssign for BNPoint2<BN, LIMBS> {
#[inline]
fn sub_assign(&mut self, pair: Self) {
self.add_assign(pair.neg())
}
}
impl<BN: BNParam, const LIMBS: usize> Zero for BNPoint2<BN, LIMBS> {
/// Create an instance of the neutral element ("point at infinity") on a BN curve
/// <i>E</i>/<b>F</b><sub><i>p</i></sub> : <i>Y²Z</i> = <i>X³ + bZ³</i>
/// in projective coordinates, hence [<i>0</i> : <i>1</i> : <i>0</i>].
#[inline]
fn zero() -> Self {
Self { x: BNFp2::zero(), y: BNFp2::one(), z: BNFp2::zero() }
}
/// Determine if this projective point is the neutral element
/// on a BN curve <i>E/<b>F</b><sub>p</sub>: Y²Z = X³ + bZ³</i>.
#[inline]
fn is_zero(&self) -> Choice {
self.z.is_zero()
}
#[inline]
fn set_zero(&mut self) {
self.x.set_zero(); // otherwise the curve equation Y^2*Z = X^3 + b*Z^3 is not satisfied
self.z.set_zero()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bnparam::{BN062Param, BN126Param, BN190Param, BN254Param, BN318Param, BN382Param, BN446Param, BN510Param, BN574Param, BN638Param, BN702Param, BN766Param};
use std::time::SystemTime;
const TESTS: usize = 100;
/// General BNPoint test template.
#[allow(non_snake_case)]
fn BNPoint2_test<BN: BNParam, const LIMBS: usize>() {
//let mut rng: SmallRng = SmallRng::from_seed([0; 32]);
let mut rng = rand::rng();
let p: Uint<LIMBS> = Uint::from_words(BN::MODULUS.try_into().unwrap());
//println!("p = {}", p.to_string_radix_vartime(10));
let n: Uint<LIMBS> = Uint::from_words(BN::ORDER.try_into().unwrap());
//println!("n = {}", n.to_string_radix_vartime(10));
let t: Uint<LIMBS> = p + Uint::ONE - n;
//println!("t = {}", t.to_string_radix_vartime(10));
println!();
println!("Performing {} BN{:03}Point2 test(s)...", TESTS, 64*LIMBS - 2);
let now = SystemTime::now();
// neutral element:
let O2: BNPoint2<BN, LIMBS> = BNPoint2::zero();
//println!("O2 = {} is zero ? {}", O2, bool::from(O2.is_zero()));
assert!(bool::from(O2.is_zero()));
// default generator (-i, 1):
let Gt: BNPoint2<BN, LIMBS> = BNPoint2::new(-BNFp2::i(), Choice::from(1));
//println!("Gt = {}", Gt);
let G2: BNPoint2<BN, LIMBS> = Gt.elim_cof();
//println!("G2 = {}", G2);
//println!("[n]G2 = {}", n*G2);
assert!(bool::from((n*G2).is_zero()));
for _t in 0..TESTS {
//println!("======== {}", _t);
// hashing to G_2:
let Q1: BNPoint2<BN, LIMBS> = BNPoint2::point_factory(BNFp2::random_from_rng(&mut rng)).elim_cof();
//println!("Q1 = {}", Q1);
let Q2: BNPoint2<BN, LIMBS> = BNPoint2::point_factory(BNFp2::random_from_rng(&mut rng)).elim_cof();
//println!("Q2 = {}", Q2);
let Q3: BNPoint2<BN, LIMBS> = BNPoint2::point_factory(BNFp2::random_from_rng(&mut rng)).elim_cof();
//println!("Q3 = {}", Q3);
// point construction:
assert_eq!(Q1, BNPoint2::from_proj(Q1.x, Q1.y, Q1.z));
let P1N = Q1.normalize();
assert_eq!(Q1, BNPoint2::from_affine(P1N.x, P1N.y));
assert_eq!(Q2, BNPoint2::from_proj(Q2.x, Q2.y, Q2.z));
let P2N = Q2.normalize();
assert_eq!(Q2, BNPoint2::from_affine(P2N.x, P2N.y));
assert_eq!(Q3, BNPoint2::from_proj(Q3.x, Q3.y, Q3.z));
let P3N = Q3.normalize();
assert_eq!(Q3, BNPoint2::from_affine(P3N.x, P3N.y));
// point order:
//println!("[n]Q1 = O1 ? {}", bool::from((n*Q1).is_zero()));
assert!(bool::from((n*Q1).is_zero()));
//println!("[n]Q2 = O1 ? {}", bool::from((n*Q2).is_zero()));
assert!(bool::from((n*Q2).is_zero()));
//println!("[n]Q3 = O1 ? {}", bool::from((n*Q3).is_zero()));
assert!(bool::from((n*Q3).is_zero()));
// opposite point:
//println!("Q1 + (-Q1) = O1 ? {}", bool::from((Q1 + (-Q1)).is_zero()));
assert!(bool::from((Q1 + (-Q1)).is_zero()));
//println!("Q2 + (-Q2) = O1 ? {}", bool::from((Q2 + (-Q2)).is_zero()));
assert!(bool::from((Q2 + (-Q2)).is_zero()));
//println!("Q3 + (-Q3) = O1 ? {}", bool::from((Q3 + (-Q3)).is_zero()));
assert!(bool::from((Q3 + (-Q3)).is_zero()));
// point doubling:
//println!("[2]Q1 = Q1 + Q1 ? {}", Q1.double(1) == Q1 + Q1);
assert_eq!(Q1.double(1), Q1 + Q1);
//println!("[2]Q2 = Q2 + Q2 ? {}", Q2.double(1) == Q2 + Q2);
assert_eq!(Q2.double(1), Q2 + Q2);
//println!("[2]Q3 = Q3 + Q3 ? {}", Q3.double(1) == Q3 + Q3);
assert_eq!(Q3.double(1), Q3 + Q3);
// commutativity:
//println!("Q1 + Q2 = Q2 + Q1 ? {}", Q1 + Q2 == Q2 + Q1);
assert_eq!(Q1 + Q2, Q2 + Q1);
//println!("Q1 + Q3 = Q3 + Q1 ? {}", Q1 + Q3 == Q3 + Q1);
assert_eq!(Q1 + Q3, Q3 + Q1);
//println!("Q2 + Q3 = Q3 + Q2 ? {}", Q2 + Q3 == Q3 + Q2);
assert_eq!(Q2 + Q3, Q3 + Q2);
// associativity:
//println!("(Q1 + Q2) + Q3 = Q1 + (Q2 + Q3) ? {}", (Q1 + Q2) + Q3 == Q1 + (Q2 + Q3));
assert_eq!((Q1 + Q2) + Q3, Q1 + (Q2 + Q3));
// Frobenius endomorphism:
//println!("psi^2(Q1) - [t]psi(Q1) + [p]Q1 = {}", Q1.psi(2) - t*Q1.psi(1) + p*Q1);
assert!(bool::from((Q1.psi(2) - t*Q1.psi(1) + p*Q1).is_zero()));
for k in 0..12usize {
let mut Qpk = Q1;
for _ in 0..k {
Qpk = Qpk.psi(1);
}
assert_eq!(Qpk, Q1.psi(k));
}
}
match now.elapsed() {
Ok(elapsed) => {
println!("Elapsed time: {} ms.", (elapsed.as_micros() as f64)/1000.0);
}
Err(e) => {
println!("Error: {e:?}");
}
}
}
#[test]
#[allow(non_snake_case)]
fn BN062Point2_test() {
const LIMBS: usize = BN062Param::LIMBS;
BNPoint2_test::<BN062Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN126Point2_test() {
const LIMBS: usize = BN126Param::LIMBS;
BNPoint2_test::<BN126Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN190Point2_test() {
const LIMBS: usize = BN190Param::LIMBS;
BNPoint2_test::<BN190Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN254Point2_test() {
const LIMBS: usize = BN254Param::LIMBS;
BNPoint2_test::<BN254Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN318Point2_test() {
const LIMBS: usize = BN318Param::LIMBS;
BNPoint2_test::<BN318Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN382Point2_test() {
const LIMBS: usize = BN382Param::LIMBS;
BNPoint2_test::<BN382Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN446Point2_test() {
const LIMBS: usize = BN446Param::LIMBS;
BNPoint2_test::<BN446Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN510Point2_test() {
const LIMBS: usize = BN510Param::LIMBS;
BNPoint2_test::<BN510Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN574Point2_test() {
const LIMBS: usize = BN574Param::LIMBS;
BNPoint2_test::<BN574Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN638Point2_test() {
const LIMBS: usize = BN638Param::LIMBS;
BNPoint2_test::<BN638Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN702Point2_test() {
const LIMBS: usize = BN702Param::LIMBS;
BNPoint2_test::<BN702Param, LIMBS>();
}
#[test]
#[allow(non_snake_case)]
fn BN766Point2_test() {
const LIMBS: usize = BN766Param::LIMBS;
BNPoint2_test::<BN766Param, LIMBS>();
}
}