/
ec.hpp
2021 lines (1830 loc) · 69 KB
/
ec.hpp
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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2023 ZeroPass <zeropass@pm.me>
// Author: Crt Vavros
#pragma once
#include <cstdint>
#include <span>
#include <type_traits>
#include <ack/bigint.hpp>
#include <ack/fp.hpp>
#include <ack/types.hpp>
#include <ack/utils.hpp>
namespace ack {
template<std::size_t N>
using ec_fixed_bigint = fixed_bigint<bitsize_to_wordsize(N) * word_bit_size * 2>; // 2x size required for multiplication
// Affine coordinates representation of an elliptic curve point
template<typename PointT, typename CurveT>
struct ec_point_base
{
using int_type = typename CurveT::int_type;
constexpr ec_point_base() : // represents point at infinity
curve_( nullptr )
{}
constexpr ec_point_base(const PointT& p) = delete;
constexpr ec_point_base(PointT&& p) noexcept = delete;
constexpr PointT& operator=(const PointT& p) = delete;
constexpr PointT& operator=(PointT&& p) noexcept = delete;
/**
* Returns the curve this point belongs to.
* @warning If this point is the identity element of the curve, the curve can be nullptr.
* Make sure to check if this point is not identity before calling this method.
* @return the curve this point belongs to.
*/
const CurveT& curve() const
{
check( curve_ != nullptr, "curve is null" );
return *curve_;
}
/**
* Checks if this point is the identity element of the curve, i.e. point at infinity.
* @return true if this point is the identity element of the curve, false otherwise
*/
inline constexpr bool is_identity() const
{
return underlying().is_identity();
}
/**
* Checks if this point is on the curve.
* @return true if this point is on the curve, false otherwise
*/
[[nodiscard]] inline bool is_on_curve() const
{
return underlying().is_on_curve();
}
/**
* Checks if this point is valid point generated by the generator point.
* @note Should do SEC 1 section 3.2.2.1 like verification.
* @return true if this point is valid, false otherwise
*/
[[nodiscard]] inline bool is_valid() const
{
return underlying().is_valid();
}
/**
* Returns the inverse of this point.
* R = -this
*
* @return the inverse of this point
*/
[[nodiscard]] inline constexpr PointT inverted() const
{
return underlying().inverted();
}
/**
* Adds the given point to this point.
* R = this + a
*
* @param a - the point to add to this point
* @return reference to this point
*/
[[nodiscard]] inline PointT add(const PointT& a) const
{
return underlying().add( a );
}
/**
* Adds the given point to this point.
* R = this + a
*
* @param a - the point to add to this point
* @return reference to this point
*/
[[nodiscard]] inline PointT add(const ec_point_base& a) const
{
return add( a.underlying() );
}
/**
* Returns the double of this point.
* R = 2 * this
*
* @return the double of this point
*/
[[nodiscard]] inline PointT doubled() const
{
return underlying().doubled();
}
/**
* Subtracts the given point from this point.
* R = this - a
*
* @param a - the point to subtract from this point
* @return reference to this point
*/
[[nodiscard]] inline PointT sub(const PointT& a) const
{
return underlying().sub( a );
}
/**
* Subtracts the given point from this point.
* R = this - a
*
* @param a - the point to subtract from this point
* @return reference to this point
*/
[[nodiscard]] inline PointT sub(const ec_point_base& a) const
{
return sub( a.underlying() );
}
/**
* Multiplies this point by the given scalar.
* R = this * scalar
*
* @param scalar - the scalar to multiply this point by
* @return the resulting point
*/
[[nodiscard]] inline PointT mul(const int_type& scalar) const
{
return underlying().mul( scalar );
}
/**
* Adds the given point to this point.
* this = this + a
*
* @param a - the point to add other point
* @param b - the point to add to the other point
* @return reference to this point
*/
[[nodiscard]] friend inline PointT operator + (const PointT& a, const PointT& b)
{
return a.add( b );
}
[[nodiscard]] friend inline PointT operator + (const ec_point_base& a, const ec_point_base& b)
{
return a.add( b );
}
/**
* Adds the given point to this point.
* this = this + a
*
* @param a - the point to add to this point
* @return reference to this point
*/
inline PointT& operator += (const PointT& a)
{
return underlying() = add( a );
}
/**
* Adds the given point to this point.
* this = this + a
*
* @param a - the point to add to this point
* @return reference to this point
*/
inline PointT& operator += (const ec_point_base& a)
{
return this-> operator += ( a.underlying() );
}
/**
* Subtracts the given point from this point.
* R = P - Q
*
* @param a - the point to subtract other point from
* @param b - the point to subtract from the other point
* @return the result point of the subtraction
*/
[[nodiscard]] friend inline PointT operator - (const PointT& a, const PointT& b)
{
return a.sub( b );
}
/**
* Subtracts the given point from this point.
* R = P - Q
*
* @param a - the point to subtract other point from
* @param b - the point to subtract from the other point
* @return the result point of the subtraction
*/
[[nodiscard]] friend inline PointT operator - (const ec_point_base& a, const ec_point_base& b)
{
return a.sub( b );
}
/**
* Subtracts the given point from this point.
* this = this - a
*
* @param a - the point to subtract from this point
* @return reference to this point
*/
inline PointT& operator -= (const PointT& a)
{
return underlying() = sub( a );
}
/**
* Subtracts the given point from this point.
* this = this - a
*
* @param a - the point to subtract from this point
* @return reference to this point
*/
inline PointT& operator -= (const ec_point_base& a)
{
return this->operator -= ( a.underlying() );
}
/**
* Multiplies given point by the given scalar.
* R = P * s
*
* @param p - the point to multiply
* @param s - the scalar to multiply the point by
* @return the result point of the multiplication
*/
[[nodiscard]] friend inline PointT operator * (const PointT& p, const int_type& s)
{
return p.mul( s );
}
/**
* Multiplies given point by the given scalar.
* R = P * s
*
* @param p - the point to multiply
* @param s - the scalar to multiply the point by
* @return the result point of the multiplication
*/
[[nodiscard]] friend inline PointT operator * (const ec_point_base& p, const int_type& s)
{
return p.mul( s );
}
/**
* Multiplies the given scalar by this point.
* R = s * P
*
* @param s - the scalar to multiply this point by
* @param p - the point to multiply
* @return the result point of the multiplication
*/
[[nodiscard]] friend inline PointT operator * (const int_type& s, const PointT& p)
{
return p.mul( s );
}
/**
* Multiplies the given scalar by this point.
* R = s * P
*
* @param s - the scalar to multiply this point by
* @param p - the point to multiply
* @return the result point of the multiplication
*/
[[nodiscard]] friend inline PointT operator * (const int_type& s, const ec_point_base& p)
{
return p.mul( s );
}
/**
* Multiplies this point by the given scalar.
* this = this * s
*
* @param s - the scalar to multiply this point by.
* @return reference to this point
*/
inline PointT& operator *= (const int_type& s)
{
return underlying() = mul( s );
}
/**
* Returns the inverse of this point.
* R = -this
*
* @return the inverse of this point
*/
[[nodiscard]] inline constexpr PointT operator - () const
{
return inverted();
}
private:
constexpr ec_point_base( const CurveT& curve ) :
curve_( &curve )
{}
inline constexpr PointT& underlying()
{
return static_cast<PointT&>( *this );
}
inline constexpr const PointT& underlying() const
{
return static_cast<const PointT&>( *this );
}
private:
friend PointT;
friend CurveT;
const CurveT* curve_;
};
// Forward declaration of ec_curve_fp
template<typename IntT, typename CurveTag>
struct ec_curve_fp;
/**
* Type trait to check if the given curve type is derived from ec_curve_fp
* @tparam CurveT - the curve type to check.
*/
template<typename CurveT>
constexpr bool is_ec_curve_fp = std::is_same_v<CurveT, ec_curve_fp<typename CurveT::int_type, typename CurveT::curve_tag>>;
/**
* Struct represents a affine point on an elliptic curve
* over a prime finite field GF(p) with short Weierstrass equation:
* y^2 = x^3 + ax + b
*
* The implementation follows the algorithms described in:
* - SECG standards SEC 1: Elliptic Curve Cryptography, Version 2.0
* https://www.secg.org/sec1-v2.pdf
* - RFC-6090: https://www.rfc-editor.org/rfc/rfc6090
*
* @warning The point's curve is stored as a pointer to the curve object.
* The curve object must outlive the point object.
*
* @tparam CurveT - the curve type. Required to be an instance of ec_curve_fp.
*/
template<typename CurveT>
struct ec_point_fp : ec_point_base<ec_point_fp<CurveT>, CurveT>
{
static_assert( is_ec_curve_fp<std::remove_cv_t<CurveT>> );
using base_type = ec_point_base<ec_point_fp<CurveT>, CurveT>;
using int_type = typename CurveT::int_type;
using field_element_type = typename CurveT::field_element_type;
using base_type::base_type;
field_element_type x;
field_element_type y;
/**
* Constructs a point at infinity
*/
constexpr ec_point_fp() :
base_type(),
x( field_element_type::zero() ),
y( field_element_type::zero() )
{}
/**
* Checks if this point is the identity element of the curve, i.e. point at infinity.
* @return true if this point is the identity element of the curve, false otherwise
*/
inline constexpr bool is_identity() const
{
return this->curve_ == nullptr || ( x.is_zero() && y.is_zero() );
}
/**
* Checks if this point is on the curve by calculating
* the left and right hand side of the equation: y^2 = x^3 + ax + b
*
* @return true if this point is on the curve, false otherwise
*/
[[nodiscard]] bool is_on_curve() const
{
if ( is_identity() ) {
return true;
}
return ( y.sqr() - ( ( x.sqr() + this->curve().a ) * x + this->curve().b )) == 0;
}
/**
* Performs SEC 1 section 3.2.2.1 point check,
* i.e. if point was generated using curve generator g and is not identity element.
* Checks:
* - point is not the identity element
* - point is on the curve
* - point has order n
*
* @note Slow operation.
*
* @return true if this point is valid, false otherwise
*/
[[nodiscard]] bool is_valid() const
{
if ( is_identity() ) {
return false;
}
if ( !is_on_curve() ) {
return false;
}
if ( !(this->curve().n * *this).is_identity() ) {
return false;
}
return true;
}
/**
* Returns the inverse of this point.
* R = -this
*
* @return the inverse of this point
*/
[[nodiscard]] constexpr ec_point_fp inverted() const
{
if ( is_identity() ) {
return *this;
}
return ec_point_fp( this->curve(), x, -y );
}
/**
* Adds the given point to this point.
* R = this + a
*
* @param a - the point to add to this point
* @return reference to this point
*/
[[nodiscard]] ec_point_fp add(const ec_point_fp& a) const
{
if ( is_identity() ) {
return a;
}
if ( a.is_identity() ) {
return *this;
}
// TODO: before this sub operation caused error in wasm: memcpy with overlapping memory memcpy can only accept non-aliasing pointers
auto s = a.x - x;
if ( s.is_zero() ) {
if ( y == a.y ) { // double point
return this->doubled();
}
return ec_point_fp(); // point at infinity
}
// Calculate tangent slope
s = ( a.y - y ) / s;
// Calculate new x and y
auto x3 = s.sqr() - x - a.x;
auto y3 = s * ( x - x3 ) - y;
return ec_point_fp( this->curve(), std::move( x3 ), std::move( y3 ) );
}
/**
* Returns the double of this point.
* R = 2 * this
*
* @return the double of this point
*/
[[nodiscard]] ec_point_fp doubled() const
{
if ( is_identity() || y.is_zero() ) { // check for y == 0 handles division by zero issue
return ec_point_fp();
}
// Calculate tangent slope
const auto s = ( 3 * x.sqr() + this->curve().a ) / ( 2 * y ) ;
// Calculate new x and y
auto x2 = s.sqr() - 2 * x;
auto y2 = s * ( x - x2 ) - y;
return ec_point_fp( this->curve(), std::move( x2 ), std::move( y2 ) );
}
/**
* Subtracts the given point from this point.
* R = this - a
*
* @param a - the point to subtract from this point
* @return reference to this point
*/
[[nodiscard]] inline ec_point_fp sub(const ec_point_fp& a) const
{
return *this + (-a);
}
/**
* Multiplies this point by the given scalar using double and add algorithm.
* R = this * scalar
*
* @param scalar - the scalar to multiply this point by
* @return the resulting point
*/
[[nodiscard]] ec_point_fp mul(const int_type& scalar) const
{
if ( scalar.is_one() || is_identity() ) {
return *this;
}
if ( scalar.is_zero() ) {
return ec_point_fp();
}
if ( scalar < 0 ) {
return inverted() * -scalar;
}
auto r = ec_point_fp();
auto tmp = *this;
auto s = scalar;
while ( s != 0 ) {
if ( s.is_odd() ) {
r += tmp;
}
tmp = tmp.doubled();
s >>= 1;
}
return r;
}
/**
* Compares 2 points for equality.
* Points are equal if they have the same x and y coordinates.
*
* @param a - the first point to compare
* @param b - the second point to compare
* @return true if the points are equal, false otherwise
*/
constexpr friend bool operator == (const ec_point_fp& a, const ec_point_fp& b)
{
if ( a.x == b.x && a.y == b.y ) {
return ( a.curve_ == b.curve_ ) || a.x == 0; // a.x == 0 means point at infinity
}
return false;
}
/**
* Compares 2 points for inequality.
* @note See operator == for details.
*
* @param a - the first point to compare
* @param b - the second point to compare
* @return true if the points are not equal, false otherwise
*/
constexpr friend inline bool operator != (const ec_point_fp& a, const ec_point_fp& b)
{
return !(a == b);
}
private:
friend CurveT;
template<typename>
friend struct ec_point_fp_proj;
template<typename>
friend struct ec_point_fp_jacobi;
constexpr ec_point_fp( const CurveT& curve, field_element_type x, field_element_type y ) :
base_type( curve ),
x( std::move(x) ),
y( std::move(y) )
{}
};
/**
* Struct represents a point on an elliptic curve in standard projective coordinates (homogeneous coordinates)
* over a prime finite field GF(p) with short Weierstrass equation: y^2 * z = x^3 + ax * z^2 + b * z^3
* Due to the nature of projective coordinates the arithmetic operations are more efficient than in affine coordinates.
* i.e. addition and doubling of points are faster due to the lack of division, which is deferred to conversion to affine coordinates.
*
* Implementation follows the RFC 6090: https://www.rfc-editor.org/rfc/rfc6090
*
* @warning The point's curve is stored as a pointer to the curve object.
* The curve object must outlive the point object.
*
* @tparam CurveT - the curve type. Required to be an instance of ec_curve_fp.
*/
template<typename CurveT>
struct ec_point_fp_proj : ec_point_base<ec_point_fp_proj<CurveT>, CurveT>
{
static_assert( is_ec_curve_fp<std::remove_cv_t<CurveT>> );
using base_type = ec_point_base<ec_point_fp_proj<CurveT>, CurveT>;
using int_type = typename CurveT::int_type;
using field_element_type = typename CurveT::field_element_type;
using affine_point_type = ec_point_fp<CurveT>;
using base_type::base_type;
field_element_type x;
field_element_type y;
field_element_type z;
/**
* Constructs a point at infinity
*/
constexpr ec_point_fp_proj() :
base_type(),
x( field_element_type::zero() ),
y( field_element_type::one() ),
z( field_element_type::zero() )
{}
/**
* Constructs this point from the given affine point.
* @warning The point's curve is stored as a pointer to the curve object.
* The curve object must outlive the point object.
*
* @param p - the affine point to construct this point from.
*/
explicit constexpr ec_point_fp_proj(affine_point_type p) :
ec_point_fp_proj()
{
if ( !p.is_identity() ) {
this->curve_ = &p.curve();
x = std::move(p.x);
y = std::move(p.y);
z = field_element_type( 1, p.curve().p );
}
}
/**
* Normalizes this point.
* This ensures Z coordinate is 1 therefore the x, y coordinates reflect
* those of the equivalent to point in an affine coordinate system.
*
* @return this point
*/
ec_point_fp_proj& normalize()
{
if ( !is_identity() && !z.is_one() ) {
const auto z_inv = z.inv();
x *= z_inv;
y *= z_inv;
z = 1;
}
return *this;
}
/**
* Returns the normalized representation of this point.
* This ensures Z coordinate is 1 therefore the x, y coordinates reflect
* those of the equivalent to point in an affine coordinate system.
*/
[[nodiscard]] ec_point_fp_proj normalized() const
{
auto r = *this;
r.normalize();
return r;
}
/**
* Returns the affine representation of this point.
* @note No point verification is performed.
* @note Slow operation due to division.
*
* @return this point in affine form.
*/
[[nodiscard]] const affine_point_type to_affine() const
{
if ( is_identity() ) {
return affine_point_type();
}
if ( z.is_one() ) {
return affine_point_type( this->curve(), x, y );
}
// It is assumed that the point is on the curve and therefore calculated x and y are valid.
// Calling affine_point_type() constructor will skip verification check.
const auto z_inv = z.inv();
return affine_point_type( this->curve(), x * z_inv, y * z_inv );
}
/**
* Checks if this point is the identity element of the curve, i.e. point at infinity.
* @return true if this point is the identity element of the curve, false otherwise
*/
[[nodiscard]] constexpr bool is_identity() const
{
return this->curve_ == nullptr || ( z.is_zero() );
}
/**
* Checks if this point is on the curve by calculating
* the left and right hand side of the equation: y^2 * z = x^3 + ax * z^2 + b * z^3
*
* @return true if this point is on the curve, false otherwise
*/
[[nodiscard]] bool is_on_curve() const
{
if ( is_identity() ) {
return true;
}
const auto z2 = z.sqr();
return ( y.sqr() * z - ( ( ( x.sqr() + this->curve().a * z2 ) * x + this->curve().b * z * z2 ) ) ) == 0;
}
/**
* Performs SEC 1 section 3.2.2.1 point check,
* i.e. if point was generated using curve generator g and is not identity element.
* Checks:
* - point is not the identity element
* - point is on the curve
* - point has order n
*
* @note Slow operation.
*
* @return true if this point is valid, false otherwise
*/
[[nodiscard]] bool is_valid() const
{
if ( is_identity() ) {
return false;
}
if ( !is_on_curve() ) {
return false;
}
if ( !(this->curve().n * *this).is_identity() ) {
return false;
}
return true;
}
/**
* Returns the inverse of this point.
* R = -this
*
* @return the inverse of this point
*/
[[nodiscard]] constexpr ec_point_fp_proj inverted() const
{
if ( is_identity() ) {
return *this;
}
return ec_point_fp_proj( this->curve(), x, -y, z );
}
/**
* Adds the given point to this point.
* R = this + a
*
* @param a - the point to add to this point
* @return reference to this point
*/
[[nodiscard]] ec_point_fp_proj add(const ec_point_fp_proj& q) const
{
const auto& p = *this;
if ( p.is_identity() ) {
return q;
}
if ( q.is_identity() ) {
return p;
}
const auto t0 = p.y * q.z;
const auto t1 = q.y * p.z;
const auto u0 = p.x * q.z;
const auto u1 = q.x * p.z;
if ( u0 == u1 ) {
if ( t0 == t1 ) {
return doubled();
} else {
return ec_point_fp_proj();
}
}
// Note: Wrapping the following code in 3 lambdas
// can make slightly faster execution time (few 10s of us)
const auto t = t0 - t1;
const auto u = u0 - u1;
const auto u2 = u.sqr();
const auto u3 = u * u2;
const auto v = p.z * q.z;
const auto w = t.sqr() * v - u2 * ( u0 + u1 );
auto rx = u * w;
auto ry = t * ( u0 * u2 - w ) - t0 * u3;
auto rz = u3 * v;
return make_point( std::move(rx), std::move(ry), std::move(rz) );
}
/**
* Returns the double of this point.
* R = 2 * this
*
* @return the double of this point
*/
[[nodiscard]] ec_point_fp_proj doubled() const
{
const auto& p = *this;
if ( p.is_identity() || p.y == 0 ) {
return ec_point_fp_proj(); // identity
}
const auto t = []( const ec_point_fp_proj& p) {
const auto x2 = p.x.sqr();
if ( p.curve().a_is_zero ) {
return 3 * x2;
}
if ( p.curve().a_is_minus_3 ) {
return 3 * ( x2 - p.z.sqr() );
}
return 3 * x2 + p.curve().a * p.z.sqr();
}( p );
const auto dy = 2 * p.y;
const auto u = dy * p.z;
const auto v = u * p.x * dy;
const auto w = t.sqr() - v * 2;
auto rx = u * w;
const auto u2 = u.sqr();
auto ry = t * ( v - w ) - u2 * p.y.sqr() * 2;
auto rz = u2 * u;
return make_point( std::move(rx), std::move(ry), std::move(rz) );
}
/**
* Subtracts the given point from this point.
* R = this - a
*
* @param a - the point to subtract from this point
* @return reference to this point
*/
[[nodiscard]] inline ec_point_fp_proj sub(const ec_point_fp_proj& a) const
{
return *this + (-a);
}
/**
* Multiplies this point by the given scalar using the double and add algorithm.
* R = this * scalar
*
* @param scalar - the scalar to multiply this point by
* @return the resulting point
*/
[[nodiscard]] ec_point_fp_proj mul(const int_type& scalar) const
{
if ( scalar.is_one() || is_identity() ) {
return *this;
}
if ( scalar.is_zero() ) {
return ec_point_fp_proj();
}
if ( scalar < 0 ) {
return inverted() * -scalar;
}
auto r = ec_point_fp_proj();
auto tmp = *this;
auto s = scalar;
while ( s != 0 ) {
if ( s.is_odd() ) {
r += tmp;
}
tmp = tmp.doubled();
s >>= 1;
}
return r;
}
/**
* Compares 2 points for equality.
* Points are equal if they are both identity or if
* a.x * b.z == b.x * a.z and a.y * b.z == b.y * a.z
*
* @note Calling this function can be slow.
*
* @param a - the first point to compare
* @param b - the second point to compare
* @return true if the points are equal, false otherwise
*/
constexpr friend bool operator == (const ec_point_fp_proj& a, const ec_point_fp_proj& b)
{
if ( a.is_identity() || b.is_identity() ) {
return a.is_identity() && b.is_identity();
}
return ( a.curve_ == b.curve_ ) &&
( a.x * b.z == b.x * a.z ) &&
( a.y * b.z == b.y * a.z );
}
/**
* Compares 2 points for inequality.
* @note See operator == for details.
*
* @param a - the first point to compare
* @param b - the second point to compare
* @return true if the points are not equal, false otherwise
*/
constexpr friend inline bool operator != (const ec_point_fp_proj& a, const ec_point_fp_proj& b)
{
return !( a == b );
}
private:
friend CurveT;
constexpr ec_point_fp_proj(const CurveT& curve, field_element_type x, field_element_type y, field_element_type z) :
base_type( curve ),
x( std::move(x) ),
y( std::move(y) ),
z( std::move(z) )
{}
ec_point_fp_proj make_point(field_element_type x, field_element_type y, field_element_type z) const
{
return ec_point_fp_proj( this->curve(), std::move(x), std::move(y), std::move(z) );
}
};
/**
* Struct representing a point on an elliptic curve in Jacobian coordinates
* over a prime finite field GF(p) for a short Weierstrass-form elliptic curve satisfying the equation:
* y^2 = x^3 + ax * z^4 + b * z^6
*
* The use of Jacobian coordinates results in more efficient arithmetic operations
* compared to projective or affine coordinates.
*
*
* @warning The point's curve is stored as a pointer to the curve object.
* The curve object must outlive the point object.
*
* @tparam CurveT - the curve type. Required to be an instance of ec_curve_fp.
*/
template<typename CurveT>
struct ec_point_fp_jacobi : ec_point_base<ec_point_fp_jacobi<CurveT>, CurveT>
{
static_assert( is_ec_curve_fp<std::remove_cv_t<CurveT>> );
using base_type = ec_point_base<ec_point_fp_jacobi<CurveT>, CurveT>;
using int_type = typename CurveT::int_type;
using field_element_type = typename CurveT::field_element_type;
using affine_point_type = ec_point_fp<CurveT>;
using base_type::base_type;
field_element_type x;
field_element_type y;
field_element_type z;
/**
* Constructs a point at infinity
*/
constexpr ec_point_fp_jacobi() :
base_type(),
x( field_element_type::zero() ),
y( field_element_type::one() ),
z( field_element_type::zero() )
{}
/**
* Constructs this point from the given affine point.
* @warning The point's curve is stored as a pointer to the curve object.
* The curve object must outlive the point object.
*
* @param p - the affine point to construct this point from.
*/