forked from newton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathed25519_crypto.cpp
2053 lines (1806 loc) · 51.3 KB
/
ed25519_crypto.cpp
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
/*
This file is part of TON Blockchain source code.
TON Blockchain is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
TON Blockchain is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TON Blockchain. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
You must obey the GNU General Public License in all respects for all
of the code used other than OpenSSL. If you modify file(s) with this
exception, you may extend this exception to your version of the file(s),
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version. If you delete this exception statement
from all source files in the program, then also delete it here.
Copyright 2017-2020 Telegram Systems LLP
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cassert>
// ******************************************************
namespace openssl {
#include <openssl/bn.h>
}
namespace arith {
struct dec_string {
std::string str;
explicit dec_string(const std::string& s) : str(s) {
}
};
struct hex_string {
std::string str;
explicit hex_string(const std::string& s) : str(s) {
}
};
} // namespace arith
namespace arith {
using namespace openssl;
inline void bn_assert(int cond);
BN_CTX* get_ctx();
class BignumBitref {
BIGNUM* ptr;
int n;
public:
BignumBitref(BIGNUM& x, int _n) : ptr(&x), n(_n){};
operator bool() const {
return BN_is_bit_set(ptr, n);
}
BignumBitref& operator=(bool val);
};
class Bignum {
BIGNUM val;
public:
class bignum_error {};
Bignum() {
BN_init(&val);
}
Bignum(long x) {
BN_init(&val);
set_long(x);
}
~Bignum() {
BN_free(&val);
}
Bignum(const dec_string& ds) {
BN_init(&val);
set_dec_str(ds.str);
}
Bignum(const hex_string& hs) {
BN_init(&val);
set_hex_str(hs.str);
}
Bignum(const Bignum& x) {
BN_init(&val);
BN_copy(&val, &x.val);
}
//Bignum (Bignum&& x) { val = x.val; }
void clear() {
BN_clear(&val);
} // use this for sensitive data
Bignum& operator=(const Bignum& x) {
BN_copy(&val, &x.val);
return *this;
}
Bignum& operator=(Bignum&& x) {
swap(x);
return *this;
}
Bignum& operator=(long x) {
return set_long(x);
}
Bignum& operator=(const dec_string& ds) {
return set_dec_str(ds.str);
}
Bignum& operator=(const hex_string& hs) {
return set_hex_str(hs.str);
}
Bignum& swap(Bignum& x) {
BN_swap(&val, &x.val);
return *this;
}
BIGNUM* bn_ptr() {
return &val;
}
const BIGNUM* bn_ptr() const {
return &val;
}
bool is_zero() const {
return BN_is_zero(&val);
}
int sign() const {
return BN_is_zero(&val) ? 0 : (BN_is_negative(&val) ? -1 : 1);
}
bool odd() const {
return BN_is_odd(&val);
}
int num_bits() const {
return BN_num_bits(&val);
}
int num_bytes() const {
return BN_num_bytes(&val);
}
bool operator[](int n) const {
return BN_is_bit_set(&val, n);
}
BignumBitref operator[](int n) {
return BignumBitref(val, n);
}
void export_msb(unsigned char* buffer, std::size_t size) const;
Bignum& import_msb(const unsigned char* buffer, std::size_t size);
Bignum& import_msb(const std::string& s) {
return import_msb((const unsigned char*)s.c_str(), s.size());
}
void export_lsb(unsigned char* buffer, std::size_t size) const;
Bignum& import_lsb(const unsigned char* buffer, std::size_t size);
Bignum& import_lsb(const std::string& s) {
return import_lsb((const unsigned char*)s.c_str(), s.size());
}
Bignum& set_dec_str(std::string s) {
BIGNUM* tmp = &val;
bn_assert(BN_dec2bn(&tmp, s.c_str()));
return *this;
}
Bignum& set_hex_str(std::string s) {
BIGNUM* tmp = &val;
bn_assert(BN_hex2bn(&tmp, s.c_str()));
return *this;
}
Bignum& set_ulong(unsigned long x) {
bn_assert(BN_set_word(&val, x));
return *this;
}
Bignum& set_long(long x) {
set_ulong(std::abs(x));
return x < 0 ? negate() : *this;
}
Bignum& negate() {
BN_set_negative(&val, !BN_is_negative(&val));
return *this;
}
Bignum& operator+=(const Bignum& y) {
bn_assert(BN_add(&val, &val, &y.val));
return *this;
}
Bignum& operator+=(long y) {
bn_assert((y >= 0 ? BN_add_word : BN_sub_word)(&val, std::abs(y)));
return *this;
}
Bignum& operator-=(long y) {
bn_assert((y >= 0 ? BN_sub_word : BN_add_word)(&val, std::abs(y)));
return *this;
}
Bignum& operator*=(const Bignum& y) {
bn_assert(BN_mul(&val, &val, &y.val, get_ctx()));
return *this;
}
Bignum& operator*=(long y) {
if (y < 0) {
negate();
}
bn_assert(BN_mul_word(&val, std::abs(y)));
return *this;
}
Bignum& operator<<=(int r) {
bn_assert(BN_lshift(&val, &val, r));
return *this;
}
Bignum& operator>>=(int r) {
bn_assert(BN_rshift(&val, &val, r));
return *this;
}
Bignum& operator/=(const Bignum& y) {
Bignum w;
bn_assert(BN_div(&val, &w.val, &val, &y.val, get_ctx()));
return *this;
}
Bignum& operator/=(long y) {
bn_assert(BN_div_word(&val, std::abs(y)) != (BN_ULONG)(-1));
return y < 0 ? negate() : *this;
}
Bignum& operator%=(const Bignum& y) {
bn_assert(BN_mod(&val, &val, &y.val, get_ctx()));
return *this;
}
Bignum& operator%=(long y) {
BN_ULONG rem = BN_mod_word(&val, std::abs(y));
bn_assert(rem != (BN_ULONG)(-1));
return set_long(y < 0 ? -rem : rem);
}
unsigned long divmod(unsigned long y) {
BN_ULONG rem = BN_div_word(&val, y);
bn_assert(rem != (BN_ULONG)(-1));
return rem;
}
const Bignum divmod(const Bignum& y);
std::string to_str() const;
std::string to_hex() const;
};
inline void bn_assert(int cond) {
if (!cond) {
throw Bignum::bignum_error();
}
}
BN_CTX* get_ctx(void) {
static BN_CTX* ctx = BN_CTX_new();
return ctx;
}
BignumBitref& BignumBitref::operator=(bool val) {
if (val) {
BN_set_bit(ptr, n);
} else {
BN_clear_bit(ptr, n);
}
return *this;
}
const Bignum operator+(const Bignum& x, const Bignum& y) {
Bignum z;
bn_assert(BN_add(z.bn_ptr(), x.bn_ptr(), y.bn_ptr()));
return z;
}
const Bignum operator+(const Bignum& x, long y) {
if (y > 0) {
Bignum z(x);
bn_assert(BN_add_word(z.bn_ptr(), y));
return z;
} else if (y < 0) {
Bignum z(x);
bn_assert(BN_sub_word(z.bn_ptr(), -y));
return z;
} else {
return x;
}
}
/*
const Bignum operator+ (Bignum&& x, long y) {
if (y > 0) {
bn_assert (BN_add_word (x.bn_ptr(), y));
} else if (y < 0) {
bn_assert (BN_sub_word (x.bn_ptr(), -y));
}
return std::move (x);
}
*/
const Bignum operator+(long y, const Bignum& x) {
return x + y;
}
/*
const Bignum operator+ (long y, Bignum&& x) {
return x + y;
}
*/
const Bignum operator-(const Bignum& x, const Bignum& y) {
Bignum z;
bn_assert(BN_sub(z.bn_ptr(), x.bn_ptr(), y.bn_ptr()));
return z;
}
const Bignum operator-(const Bignum& x, long y) {
return x + (-y);
}
/*
const Bignum operator- (Bignum&& x, long y) {
return x + (-y);
}
*/
const Bignum operator*(const Bignum& x, const Bignum& y) {
Bignum z;
bn_assert(BN_mul(z.bn_ptr(), x.bn_ptr(), y.bn_ptr(), get_ctx()));
return z;
}
const Bignum operator*(const Bignum& x, long y) {
if (y > 0) {
Bignum z(x);
bn_assert(BN_mul_word(z.bn_ptr(), y));
return z;
} else if (y < 0) {
Bignum z(x);
z.negate();
bn_assert(BN_mul_word(z.bn_ptr(), -y));
return z;
} else {
Bignum z(0);
return z;
}
}
/*
const Bignum operator* (Bignum&& x, long y) {
if (y > 0) {
bn_assert (BN_mul_word (x.bn_ptr(), y));
} else if (y < 0) {
x.negate();
bn_assert (BN_mul_word (x.bn_ptr(), -y));
} else {
x = 0;
}
return std::move (x);
}
*/
const Bignum operator*(long y, const Bignum& x) {
return x * y;
}
const Bignum operator/(const Bignum& x, const Bignum& y) {
Bignum z, w;
bn_assert(BN_div(z.bn_ptr(), w.bn_ptr(), x.bn_ptr(), y.bn_ptr(), get_ctx()));
return z;
}
const Bignum Bignum::divmod(const Bignum& y) {
Bignum w;
bn_assert(BN_div(&val, w.bn_ptr(), &val, y.bn_ptr(), get_ctx()));
return w;
}
const Bignum operator%(const Bignum& x, const Bignum& y) {
Bignum z;
bn_assert(BN_mod(z.bn_ptr(), x.bn_ptr(), y.bn_ptr(), get_ctx()));
return z;
}
unsigned long operator%(const Bignum& x, unsigned long y) {
BN_ULONG rem = BN_mod_word(x.bn_ptr(), y);
bn_assert(rem != (BN_ULONG)(-1));
return rem;
}
const Bignum operator<<(const Bignum& x, int r) {
Bignum z;
bn_assert(BN_lshift(z.bn_ptr(), x.bn_ptr(), r));
return z;
}
const Bignum operator>>(const Bignum& x, int r) {
Bignum z;
bn_assert(BN_rshift(z.bn_ptr(), x.bn_ptr(), r));
return z;
}
const Bignum abs(const Bignum& x) {
Bignum T(x);
if (T.sign() < 0) {
T.negate();
}
return T;
}
const Bignum sqr(const Bignum& x) {
Bignum z;
bn_assert(BN_sqr(z.bn_ptr(), x.bn_ptr(), get_ctx()));
return z;
}
void Bignum::export_msb(unsigned char* buffer, std::size_t size) const {
bn_assert(size >= 0 && size <= (1 << 20));
bn_assert(sign() >= 0);
int n = BN_num_bytes(&val);
bn_assert(n >= 0 && (unsigned)n <= size);
bn_assert(BN_bn2bin(&val, buffer + size - n) == n);
std::memset(buffer, 0, size - n);
}
Bignum& Bignum::import_msb(const unsigned char* buffer, std::size_t size) {
bn_assert(size >= 0 && size <= (1 << 20));
std::size_t i = 0;
while (i < size && !buffer[i]) {
i++;
}
bn_assert(BN_bin2bn(buffer + i, size - i, &val) == &val);
return *this;
}
void Bignum::export_lsb(unsigned char* buffer, std::size_t size) const {
bn_assert(size >= 0 && size <= (1 << 20));
bn_assert(sign() >= 0);
std::size_t n = BN_num_bytes(&val);
bn_assert(n >= 0 && (unsigned)n <= size);
bn_assert(BN_bn2bin(&val, buffer) == (int)n);
std::memset(buffer + n, 0, size - n);
for (std::size_t i = 0; 2 * i + 1 < n; i++) {
std::swap(buffer[i], buffer[n - 1 - i]);
}
}
Bignum& Bignum::import_lsb(const unsigned char* buffer, std::size_t size) {
bn_assert(size >= 0 && size <= (1 << 20));
while (size > 0 && !buffer[size - 1]) {
size--;
}
if (!size) {
bn_assert(BN_zero(&val));
return *this;
}
unsigned char tmp[size], *ptr = tmp + size;
for (std::size_t i = 0; i < size; i++) {
*--ptr = buffer[i];
}
bn_assert(BN_bin2bn(tmp, size, &val) == &val);
return *this;
}
int cmp(const Bignum& x, const Bignum& y) {
return BN_cmp(x.bn_ptr(), y.bn_ptr());
}
bool operator==(const Bignum& x, const Bignum& y) {
return cmp(x, y) == 0;
}
bool operator!=(const Bignum& x, const Bignum& y) {
return cmp(x, y) != 0;
}
bool operator<(const Bignum& x, const Bignum& y) {
return cmp(x, y) < 0;
}
bool operator<=(const Bignum& x, const Bignum& y) {
return cmp(x, y) <= 0;
}
bool operator>(const Bignum& x, const Bignum& y) {
return cmp(x, y) > 0;
}
bool operator>=(const Bignum& x, const Bignum& y) {
return cmp(x, y) >= 0;
}
bool operator==(const Bignum& x, long y) {
if (y >= 0) {
return BN_is_word(x.bn_ptr(), y);
} else {
return x == Bignum(y);
}
}
bool operator!=(const Bignum& x, long y) {
if (y >= 0) {
return !BN_is_word(x.bn_ptr(), y);
} else {
return x != Bignum(y);
}
}
std::string Bignum::to_str() const {
char* ptr = BN_bn2dec(&val);
std::string z(ptr);
OPENSSL_free(ptr);
return z;
}
std::string Bignum::to_hex() const {
char* ptr = BN_bn2hex(&val);
std::string z(ptr);
OPENSSL_free(ptr);
return z;
}
std::ostream& operator<<(std::ostream& os, const Bignum& x) {
return os << x.to_str();
}
std::istream& operator>>(std::istream& is, Bignum& x) {
std::string word;
is >> word;
x = dec_string(word);
return is;
}
bool is_prime(const Bignum& p, int nchecks = 64, bool trial_div = true) {
return BN_is_prime_fasttest_ex(p.bn_ptr(), BN_prime_checks, get_ctx(), trial_div, 0);
}
} // namespace arith
namespace arith {
using namespace openssl;
class Residue;
class ResidueRing;
class ResidueRing {
public:
struct bad_modulus {};
struct elem_cnt_mismatch {
int cnt;
elem_cnt_mismatch(int x) : cnt(x) {
}
};
private:
const Bignum modulus;
mutable int cnt;
bool prime;
void cnt_assert(bool b) {
if (!b) {
throw elem_cnt_mismatch(cnt);
}
}
Residue* Zero;
Residue* One;
Residue* Img_i;
void init();
public:
typedef Residue element;
explicit ResidueRing(Bignum mod) : modulus(mod), cnt(0), prime(arith::is_prime(mod)), Zero(0), One(0) {
init();
}
~ResidueRing();
int incr_count() {
return ++cnt;
}
int decr_count() {
--cnt;
cnt_assert(cnt >= 0);
return cnt;
}
const Bignum& get_modulus() const {
return modulus;
}
bool is_prime() const {
return prime;
}
const Residue& zero() const {
return *Zero;
}
const Residue& one() const {
return *One;
}
const Residue& img_i();
Residue frac(long num, long denom = 1);
Residue convert(long num);
Residue convert(const Bignum& x);
Bignum reduce(const Bignum& x) {
Bignum r = x % modulus;
if (r.sign() < 0) {
r += modulus;
}
return r;
}
Bignum& do_reduce(Bignum& x) {
x %= modulus;
if (x.sign() < 0) {
x += modulus;
}
return x;
}
};
class Residue {
public:
struct not_same_ring {};
private:
ResidueRing* ring;
mutable Bignum val;
Residue& reduce() {
ring->do_reduce(val);
return *this;
}
public:
explicit Residue(ResidueRing& R) : ring(&R) {
R.incr_count();
}
Residue(const Bignum& x, ResidueRing& R) : ring(&R), val(R.reduce(x)) {
R.incr_count();
}
~Residue() {
ring->decr_count();
ring = 0;
}
Residue(const Residue& x) : ring(x.ring), val(x.val) {
ring->incr_count();
}
Bignum extract() const {
return val;
}
const Bignum& extract_raw() const {
return val;
}
const Bignum& modulus() const {
return ring->get_modulus();
}
void same_ring(const Residue& y) const {
if (ring != y.ring) {
throw not_same_ring();
}
}
ResidueRing& ring_of() const {
return *ring;
}
bool is_zero() const {
return (val == 0);
}
Residue& operator=(const Residue& x) {
same_ring(x);
val = x.val;
return *this;
}
Residue& operator=(const Bignum& x) {
val = ring->reduce(x);
return *this;
}
Residue& operator+=(const Residue& y);
Residue& operator-=(const Residue& y);
Residue& operator*=(const Residue& y);
Residue& operator+=(long y) {
val += y;
return reduce();
}
Residue& operator-=(long y) {
val -= y;
return reduce();
}
Residue& operator*=(long y) {
val *= y;
return reduce();
}
Residue& negate() {
val.negate();
return reduce();
}
friend const Residue operator+(const Residue& x, const Residue& y);
friend const Residue operator-(const Residue& x, const Residue& y);
friend const Residue operator*(const Residue& x, const Residue& y);
friend const Residue operator-(const Residue& x);
friend Residue sqr(const Residue& x);
friend Residue power(const Residue& x, const Bignum& y);
friend Residue inverse(const Residue& x);
std::string to_str() const;
};
void ResidueRing::init() {
Zero = new Residue(0, *this);
One = new Residue(1, *this);
}
ResidueRing::~ResidueRing() {
delete Zero;
delete One;
Zero = One = 0;
cnt_assert(!cnt);
}
const Residue operator+(const Residue& x, const Residue& y) {
x.same_ring(y);
Residue z(x.ring_of());
bn_assert(BN_mod_add(z.val.bn_ptr(), x.val.bn_ptr(), y.val.bn_ptr(), x.modulus().bn_ptr(), get_ctx()));
return z;
}
const Residue operator-(const Residue& x, const Residue& y) {
x.same_ring(y);
Residue z(x.ring_of());
bn_assert(BN_mod_sub(z.val.bn_ptr(), x.val.bn_ptr(), y.val.bn_ptr(), x.modulus().bn_ptr(), get_ctx()));
return z;
}
const Residue operator*(const Residue& x, const Residue& y) {
x.same_ring(y);
Residue z(x.ring_of());
bn_assert(BN_mod_mul(z.val.bn_ptr(), x.val.bn_ptr(), y.val.bn_ptr(), x.modulus().bn_ptr(), get_ctx()));
return z;
}
const Residue operator-(const Residue& x) {
Residue z(x);
z.val.negate();
return z.reduce();
}
Residue& Residue::operator+=(const Residue& y) {
same_ring(y);
bn_assert(BN_mod_add(val.bn_ptr(), val.bn_ptr(), y.val.bn_ptr(), modulus().bn_ptr(), get_ctx()));
return *this;
}
Residue& Residue::operator-=(const Residue& y) {
same_ring(y);
bn_assert(BN_mod_sub(val.bn_ptr(), val.bn_ptr(), y.val.bn_ptr(), modulus().bn_ptr(), get_ctx()));
return *this;
}
Residue& Residue::operator*=(const Residue& y) {
same_ring(y);
bn_assert(BN_mod_mul(val.bn_ptr(), val.bn_ptr(), y.val.bn_ptr(), modulus().bn_ptr(), get_ctx()));
return *this;
}
bool operator==(const Residue& x, const Residue& y) {
x.same_ring(y);
return x.extract() == y.extract();
}
bool operator!=(const Residue& x, const Residue& y) {
x.same_ring(y);
return x.extract() != y.extract();
}
Residue sqr(const Residue& x) {
Residue z(x.ring_of());
bn_assert(BN_mod_sqr(z.val.bn_ptr(), x.val.bn_ptr(), x.modulus().bn_ptr(), get_ctx()));
return z;
}
Residue power(const Residue& x, const Bignum& y) {
Residue z(x.ring_of());
bn_assert(BN_mod_exp(z.val.bn_ptr(), x.val.bn_ptr(), y.bn_ptr(), x.modulus().bn_ptr(), get_ctx()));
return z;
}
Residue inverse(const Residue& x) {
assert(x.ring_of().is_prime());
return power(x, x.ring_of().get_modulus() - 2);
}
const Residue& ResidueRing::img_i() {
if (!Img_i) {
assert(is_prime());
assert(modulus % 4 == 1);
int g = 2;
Bignum n = (modulus - 1) / 4;
while (true) {
Residue t = power(frac(g), n);
if (t != one() && t != frac(-1)) {
Img_i = new Residue(t);
break;
}
}
}
return *Img_i;
}
Residue sqrt(const Residue& x) {
assert(x.ring_of().is_prime());
ResidueRing& R = x.ring_of();
const Bignum& p = R.get_modulus();
if (x.is_zero() || !p.odd()) {
return x;
}
if (p[1]) { // p=3 (mod 4)
return power(x, (p + 1) >> 2);
} else if (p[2]) {
// p=5 (mod 8)
Residue t = power(x, (p + 3) >> 3);
return (sqr(t) == x) ? t : R.img_i() * t;
} else {
assert(p[2]);
return R.zero();
}
}
Residue ResidueRing::frac(long num, long denom) {
assert(denom);
if (denom < 0) {
num = -num;
denom = -denom;
}
if (!(num % denom)) {
return Residue(num / denom, *this);
} else {
return Residue(num, *this) * inverse(Residue(denom, *this));
}
}
inline Residue ResidueRing::convert(long x) {
return Residue(x, *this);
}
inline Residue ResidueRing::convert(const Bignum& x) {
return Residue(x, *this);
}
std::string Residue::to_str() const {
return "Mod(" + val.to_str() + "," + modulus().to_str() + ")";
}
std::ostream& operator<<(std::ostream& os, const Residue& x) {
return os << x.to_str();
}
std::istream& operator>>(std::istream& is, Residue& x) {
std::string word;
is >> word;
x = dec_string(word);
return is;
}
} // namespace arith
// ******************************************************
namespace ellcurve {
using namespace arith;
const Bignum& P25519() {
static Bignum P25519 = (Bignum(1) << 255) - 19;
return P25519;
}
ResidueRing& Fp25519() {
static ResidueRing Fp25519(P25519());
return Fp25519;
}
} // namespace ellcurve
// ******************************************************
namespace ellcurve {
using namespace arith;
class MontgomeryCurve {
ResidueRing& ring;
int A_short; // v^2 = u^2 + Au + 1
int Gu_short; // u(G)
int a_short; // (A+2)/4
Residue A;
Residue Gu;
Bignum P;
Bignum L;
Bignum Order;
Bignum cofactor;
int cofactor_short;
void init();
public:
MontgomeryCurve(int _A, int _Gu, ResidueRing& _R)
: ring(_R)
, A_short(_A)
, Gu_short(_Gu)
, a_short((_A + 2) / 4)
, A(_A, _R)
, Gu(_Gu, _R)
, P(_R.get_modulus())
, cofactor_short(0) {
init();
}
const Residue& get_gen_u() const {
return Gu;
}
const Bignum& get_ell() const {
return L;
}
const Bignum& get_order() const {
return Order;
}
ResidueRing& get_base_ring() const {
return ring;
}
const Bignum& get_p() const {
return P;
}
void set_order_cofactor(const Bignum& order, int cof);
struct PointXZ {
Residue X, Z;
PointXZ(Residue x, Residue z) : X(x), Z(z) {
x.same_ring(z);
}
PointXZ(ResidueRing& r) : X(r.one()), Z(r.zero()) {
}
explicit PointXZ(Residue u) : X(u), Z(u.ring_of().one()) {
}
explicit PointXZ(Residue y, bool) : X(y.ring_of().one() - y), Z(y + y.ring_of().one()) {
}
PointXZ(const PointXZ& P) : X(P.X), Z(P.Z) {
}
PointXZ& operator=(const PointXZ& P) {
X = P.X;
Z = P.Z;
return *this;
}
Residue get_u() const {
return X * inverse(Z);
}
Residue get_v(bool sign_v = false) const;
bool is_infty() const {
return Z.is_zero();
}
Residue get_y() const {
return (X - Z) * inverse(X + Z);
}
bool export_point_y(unsigned char buffer[32]) const;
bool export_point_u(unsigned char buffer[32]) const;
void zeroize() {
X = Z = Z.ring_of().zero();
}
};
PointXZ power_gen_xz(const Bignum& n) const;
PointXZ power_xz(const Residue& u, const Bignum& n) const;
PointXZ power_xz(const PointXZ& P, const Bignum& n) const;
PointXZ add_xz(const PointXZ& P, const PointXZ& Q) const;
PointXZ double_xz(const PointXZ& P) const;
PointXZ import_point_u(const unsigned char point[32]) const;
PointXZ import_point_y(const unsigned char point[32]) const;
};
void MontgomeryCurve::init() {
assert(!((a_short + 2) & 3) && a_short >= 0);
}
void MontgomeryCurve::set_order_cofactor(const Bignum& order, int cof) {
assert(order > 0);
assert(cof >= 0);
assert(cof == 0 || (order % cof) == 0);
Order = order;
cofactor = cofactor_short = cof;
if (cof > 0) {
L = order / cof;
assert(is_prime(L));
}
assert(!power_gen_xz(1).is_infty());
assert(power_gen_xz(Order).is_infty());
}
// computes u(P+Q)*u(P-Q) as X/Z
MontgomeryCurve::PointXZ MontgomeryCurve::add_xz(const MontgomeryCurve::PointXZ& P,