forked from trezor/trezor-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bignum.c
1909 lines (1620 loc) · 62.8 KB
/
bignum.c
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 (c) 2013-2014 Tomas Dzetkulic
* Copyright (c) 2013-2014 Pavol Rusnak
* Copyright (c) 2015 Jochen Hoenicke
* Copyright (c) 2016 Alex Beregszaszi
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "bignum.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "memzero.h"
#include "script.h"
/*
This library implements 256-bit numbers arithmetic.
An unsigned 256-bit number is represented by a bignum256 structure, that is an
array of nine 32-bit values called limbs. Limbs are digits of the number in
the base 2**29 representation in the little endian order. This means that
bignum256 x;
represents the value
sum([x[i] * 2**(29*i) for i in range(9)).
A limb of a bignum256 is *normalized* iff it's less than 2**29.
A bignum256 is *normalized* iff every its limb is normalized.
A number is *fully reduced modulo p* iff it is less than p.
A number is *partly reduced modulo p* iff is is less than 2*p.
The number p is usually a prime number such that 2^256 - 2^224 <= p <= 2^256.
All functions except bn_fast_mod expect that all their bignum256 inputs are
normalized. (The function bn_fast_mod allows the input number to have the
most significant limb unnormalized). All bignum256 outputs of all functions
are guaranteed to be normalized.
A number can be partly reduced with bn_fast_mod, a partly reduced number can
be fully reduced with bn_mod.
A function has *constant control flow with regard to its argument* iff the
order in which instructions of the function are executed doesn't depend on the
value of the argument.
A function has *constant memory access flow with regard to its argument* iff
the memory addresses that are acessed and the order in which they are accessed
don't depend on the value of the argument.
A function *has contant control (memory access) flow* iff it has constant
control (memory access) flow with regard to all its arguments.
The following function has contant control flow with regard to its arugment
n, however is doesn't have constant memory access flow with regard to it:
void (int n, int *a) }
a[0] = 0;
a[n] = 0; // memory address reveals the value of n
}
Unless stated otherwise all functions are supposed to have both constant
control flow and constant memory access flow.
*/
#define BN_MAX_DECIMAL_DIGITS \
79 // floor(log(2**(LIMBS * BITS_PER_LIMB), 10)) + 1
// y = (bignum256) x
// Assumes x is normalized and x < 2**261 == 2**(BITS_PER_LIMB * LIMBS)
// Guarantees y is normalized
void bn_copy_lower(const bignum512 *x, bignum256 *y) {
for (int i = 0; i < BN_LIMBS; i++) {
y->val[i] = x->val[i];
}
}
// out_number = (bignum256) in_number
// Assumes in_number is a raw bigendian 256-bit number
// Guarantees out_number is normalized
void bn_read_be(const uint8_t *in_number, bignum256 *out_number) {
uint32_t temp = 0;
for (int i = 0; i < BN_LIMBS - 1; i++) {
uint32_t limb = read_be(in_number + (BN_LIMBS - 2 - i) * 4);
temp |= limb << (BN_EXTRA_BITS * i);
out_number->val[i] = temp & BN_LIMB_MASK;
temp = limb >> (32 - BN_EXTRA_BITS * (i + 1));
}
out_number->val[BN_LIMBS - 1] = temp;
}
// out_number = (bignum512) in_number
// Assumes in_number is a raw bigendian 512-bit number
// Guarantees out_number is normalized
void bn_read_be_512(const uint8_t *in_number, bignum512 *out_number) {
bignum256 lower = {0}, upper = {0};
bn_read_be(in_number + 32, &lower);
bn_read_be(in_number, &upper);
const int shift_length = BN_BITS_PER_LIMB * BN_LIMBS - 256;
uint32_t shift = upper.val[0] & ((1 << shift_length) - 1);
for (int i = 0; i < shift_length; i++) {
bn_rshift(&upper);
}
lower.val[BN_LIMBS - 1] |= shift << (BN_BITS_PER_LIMB - shift_length);
for (int i = 0; i < BN_LIMBS; i++) {
out_number->val[i] = lower.val[i];
}
for (int i = 0; i < BN_LIMBS; i++) {
out_number->val[i + BN_LIMBS] = upper.val[i];
}
}
// out_number = (256BE) in_number
// Assumes in_number < 2**256
// Guarantess out_number is a raw bigendian 256-bit number
void bn_write_be(const bignum256 *in_number, uint8_t *out_number) {
uint32_t temp = in_number->val[BN_LIMBS - 1];
for (int i = BN_LIMBS - 2; i >= 0; i--) {
uint32_t limb = in_number->val[i];
temp = (temp << (BN_BITS_PER_LIMB - BN_EXTRA_BITS * i)) |
(limb >> (BN_EXTRA_BITS * i));
write_be(out_number + (BN_LIMBS - 2 - i) * 4, temp);
temp = limb;
}
}
// out_number = (bignum256) in_number
// Assumes in_number is a raw little endian 256-bit number
// Guarantees out_number is normalized
void bn_read_le(const uint8_t *in_number, bignum256 *out_number) {
uint32_t temp = 0;
for (int i = 0; i < BN_LIMBS - 1; i++) {
uint32_t limb = read_le(in_number + i * 4);
temp |= limb << (BN_EXTRA_BITS * i);
out_number->val[i] = temp & BN_LIMB_MASK;
temp = limb >> (32 - BN_EXTRA_BITS * (i + 1));
}
out_number->val[BN_LIMBS - 1] = temp;
}
// out_number = (256LE) in_number
// Assumes in_number < 2**256
// Guarantess out_number is a raw little endian 256-bit number
void bn_write_le(const bignum256 *in_number, uint8_t *out_number) {
uint32_t temp = in_number->val[BN_LIMBS - 1];
for (int i = BN_LIMBS - 2; i >= 0; i--) {
uint32_t limb = in_number->val[i];
temp = (temp << (BN_BITS_PER_LIMB - BN_EXTRA_BITS * i)) |
(limb >> (BN_EXTRA_BITS * i));
write_le(out_number + i * 4, temp);
temp = limb;
}
}
// out_number = (bignum256) in_number
// Guarantees out_number is normalized
void bn_read_uint32(uint32_t in_number, bignum256 *out_number) {
out_number->val[0] = in_number & BN_LIMB_MASK;
out_number->val[1] = in_number >> BN_BITS_PER_LIMB;
for (uint32_t i = 2; i < BN_LIMBS; i++) out_number->val[i] = 0;
}
// out_number = (bignum256) in_number
// Guarantees out_number is normalized
void bn_read_uint64(uint64_t in_number, bignum256 *out_number) {
out_number->val[0] = in_number & BN_LIMB_MASK;
out_number->val[1] = (in_number >>= BN_BITS_PER_LIMB) & BN_LIMB_MASK;
out_number->val[2] = in_number >> BN_BITS_PER_LIMB;
for (uint32_t i = 3; i < BN_LIMBS; i++) out_number->val[i] = 0;
}
// Returns the bitsize of x
// Assumes x is normalized
// The function doesn't have neither constant control flow nor constant memory
// access flow
int bn_bitcount(const bignum256 *x) {
for (int i = BN_LIMBS - 1; i >= 0; i--) {
uint32_t limb = x->val[i];
if (limb != 0) {
// __builtin_clz returns the number of leading zero bits starting at the
// most significant bit position
return i * BN_BITS_PER_LIMB + (32 - __builtin_clz(limb));
}
}
return 0;
}
// Returns the number of decimal digits of x; if x is 0, returns 1
// Assumes x is normalized
// The function doesn't have neither constant control flow nor constant memory
// access flow
unsigned int bn_digitcount(const bignum256 *x) {
bignum256 val = {0};
bn_copy(x, &val);
unsigned int digits = 1;
for (unsigned int i = 0; i < BN_MAX_DECIMAL_DIGITS; i += 3) {
uint32_t limb = 0;
bn_divmod1000(&val, &limb);
if (limb >= 100) {
digits = i + 3;
} else if (limb >= 10) {
digits = i + 2;
} else if (limb >= 1) {
digits = i + 1;
}
}
memzero(&val, sizeof(val));
return digits;
}
// x = 0
// Guarantees x is normalized
void bn_zero(bignum256 *x) {
for (int i = 0; i < BN_LIMBS; i++) {
x->val[i] = 0;
}
}
// x = 1
// Guarantees x is normalized
void bn_one(bignum256 *x) {
x->val[0] = 1;
for (int i = 1; i < BN_LIMBS; i++) {
x->val[i] = 0;
}
}
// Returns x == 0
// Assumes x is normalized
int bn_is_zero(const bignum256 *x) {
uint32_t result = 0;
for (int i = 0; i < BN_LIMBS; i++) {
result |= x->val[i];
}
return !result;
}
// Returns x == 1
// Assumes x is normalized
int bn_is_one(const bignum256 *x) {
uint32_t result = x->val[0] ^ 1;
for (int i = 1; i < BN_LIMBS; i++) {
result |= x->val[i];
}
return !result;
}
// Returns x < y
// Assumes x, y are normalized
int bn_is_less(const bignum256 *x, const bignum256 *y) {
uint32_t res1 = 0;
uint32_t res2 = 0;
for (int i = BN_LIMBS - 1; i >= 0; i--) {
res1 = (res1 << 1) | (x->val[i] < y->val[i]);
res2 = (res2 << 1) | (x->val[i] > y->val[i]);
}
return res1 > res2;
}
// Returns x == y
// Assumes x, y are normalized
int bn_is_equal(const bignum256 *x, const bignum256 *y) {
uint32_t result = 0;
for (int i = 0; i < BN_LIMBS; i++) {
result |= x->val[i] ^ y->val[i];
}
return !result;
}
// res = cond if truecase else falsecase
// Assumes cond is either 0 or 1
// Works properly even if &res == &truecase or &res == &falsecase or
// &truecase == &falsecase or &res == &truecase == &falsecase
void bn_cmov(bignum256 *res, volatile uint32_t cond, const bignum256 *truecase,
const bignum256 *falsecase) {
// Intentional use of bitwise OR operator to ensure constant-time
assert((int)(cond == 1) | (int)(cond == 0));
uint32_t tmask = -cond; // tmask = 0xFFFFFFFF if cond else 0x00000000
uint32_t fmask = ~tmask; // fmask = 0x00000000 if cond else 0xFFFFFFFF
for (int i = 0; i < BN_LIMBS; i++) {
res->val[i] = (truecase->val[i] & tmask) | (falsecase->val[i] & fmask);
}
}
// x = -x % prime if cond else x,
// Explicitly x = (3 * prime - x if x > prime else 2 * prime - x) if cond else
// else (x if x > prime else x + prime)
// Assumes x is normalized and partly reduced
// Assumes cond is either 1 or 0
// Guarantees x is normalized
// Assumes prime is normalized and
// 0 < prime < 2**260 == 2**(BITS_PER_LIMB * LIMBS - 1)
void bn_cnegate(volatile uint32_t cond, bignum256 *x, const bignum256 *prime) {
// Intentional use of bitwise OR operator to ensure constant time
assert((int)(cond == 1) | (int)(cond == 0));
uint32_t tmask = -cond; // tmask = 0xFFFFFFFF if cond else 0x00000000
uint32_t fmask = ~tmask; // fmask = 0x00000000 if cond else 0xFFFFFFFF
bn_mod(x, prime);
// x < prime
uint32_t acc1 = 1;
uint32_t acc2 = 0;
for (int i = 0; i < BN_LIMBS; i++) {
acc1 += (BN_BASE - 1) + 2 * prime->val[i] - x->val[i];
// acc1 neither overflows 32 bits nor underflows 0
// Proof:
// acc1 + (BASE - 1) + 2 * prime[i] - x[i]
// >= (BASE - 1) - x >= (2**BITS_PER_LIMB - 1) - (2**BITS_PER_LIMB - 1)
// == 0
// acc1 + (BASE - 1) + 2 * prime[i] - x[i]
// <= acc1 + (BASE - 1) + 2 * prime[i]
// <= (2**(32 - BITS_PER_LIMB) - 1) + 2 * (2**BITS_PER_LIMB - 1) +
// (2**BITS_PER_LIMB - 1)
// == 7 + 3 * 2**29 < 2**32
acc2 += prime->val[i] + x->val[i];
// acc2 doesn't overflow 32 bits
// Proof:
// acc2 + prime[i] + x[i]
// <= 2**(32 - BITS_PER_LIMB) - 1 + 2 * (2**BITS_PER_LIMB - 1)
// == 2**(32 - BITS_PER_LIMB) + 2**(BITS_PER_LIMB + 1) - 2
// == 2**30 + 5 < 2**32
// x = acc1 & LIMB_MASK if cond else acc2 & LIMB_MASK
x->val[i] = ((acc1 & tmask) | (acc2 & fmask)) & BN_LIMB_MASK;
acc1 >>= BN_BITS_PER_LIMB;
// acc1 <= 7 == 2**(32 - BITS_PER_LIMB) - 1
// acc1 == 2**(BITS_PER_LIMB * (i + 1)) + 2 * prime[:i + 1] - x[:i + 1]
// >> BITS_PER_LIMB * (i + 1)
acc2 >>= BN_BITS_PER_LIMB;
// acc2 <= 7 == 2**(32 - BITS_PER_LIMB) - 1
// acc2 == prime[:i + 1] + x[:i + 1] >> BITS_PER_LIMB * (i + 1)
}
// assert(acc1 == 1); // assert prime <= 2**260
// assert(acc2 == 0);
// clang-format off
// acc1 == 1
// Proof:
// acc1 == 2**(BITS_PER_LIMB * LIMBS) + 2 * prime[:LIMBS] - x[:LIMBS] >> BITS_PER_LIMB * LIMBS
// == 2**(BITS_PER_LIMB * LIMBS) + 2 * prime - x >> BITS_PER_LIMB * LIMBS
// <= 2**(BITS_PER_LIMB * LIMBS) + 2 * prime >> BITS_PER_LIMB * LIMBS
// <= 2**(BITS_PER_LIMB * LIMBS) + 2 * (2**(BITS_PER_LIMB * LIMBS - 1) - 1) >> BITS_PER_LIMB * LIMBS
// <= 2**(BITS_PER_LIMB * LIMBS) + 2**(BITS_PER_LIMB * LIMBS) - 2 >> BITS_PER_LIMB * LIMBS
// == 1
// acc1 == 2**(BITS_PER_LIMB * LIMBS) + 2 * prime[:LIMBS] - x[:LIMBS] >> BITS_PER_LIMB * LIMBS
// == 2**(BITS_PER_LIMB * LIMBS) + 2 * prime - x >> BITS_PER_LIMB * LIMBS
// >= 2**(BITS_PER_LIMB * LIMBS) + 0 >> BITS_PER_LIMB * LIMBS
// == 1
// acc2 == 0
// Proof:
// acc2 == prime[:LIMBS] + x[:LIMBS] >> BITS_PER_LIMB * LIMBS
// == prime + x >> BITS_PER_LIMB * LIMBS
// <= 2 * prime - 1 >> BITS_PER_LIMB * LIMBS
// <= 2 * (2**(BITS_PER_LIMB * LIMBS - 1) - 1) - 1 >> 261
// == 2**(BITS_PER_LIMB * LIMBS) - 3 >> BITS_PER_LIMB * LIMBS
// == 0
// clang-format on
}
// x <<= 1
// Assumes x is normalized, x < 2**260 == 2**(LIMBS*BITS_PER_LIMB - 1)
// Guarantees x is normalized
void bn_lshift(bignum256 *x) {
for (int i = BN_LIMBS - 1; i > 0; i--) {
x->val[i] = ((x->val[i] << 1) & BN_LIMB_MASK) |
(x->val[i - 1] >> (BN_BITS_PER_LIMB - 1));
}
x->val[0] = (x->val[0] << 1) & BN_LIMB_MASK;
}
// x >>= 1, i.e. x = floor(x/2)
// Assumes x is normalized
// Guarantees x is normalized
// If x is partly reduced (fully reduced) modulo prime,
// guarantess x will be partly reduced (fully reduced) modulo prime
void bn_rshift(bignum256 *x) {
for (int i = 0; i < BN_LIMBS - 1; i++) {
x->val[i] =
(x->val[i] >> 1) | ((x->val[i + 1] & 1) << (BN_BITS_PER_LIMB - 1));
}
x->val[BN_LIMBS - 1] >>= 1;
}
// Sets i-th least significant bit (counting from zero)
// Assumes x is normalized and 0 <= i < 261 == LIMBS*BITS_PER_LIMB
// Guarantees x is normalized
// The function has constant control flow but not constant memory access flow
// with regard to i
void bn_setbit(bignum256 *x, uint16_t i) {
assert(i < BN_LIMBS * BN_BITS_PER_LIMB);
x->val[i / BN_BITS_PER_LIMB] |= (1u << (i % BN_BITS_PER_LIMB));
}
// clears i-th least significant bit (counting from zero)
// Assumes x is normalized and 0 <= i < 261 == LIMBS*BITS_PER_LIMB
// Guarantees x is normalized
// The function has constant control flow but not constant memory access flow
// with regard to i
void bn_clearbit(bignum256 *x, uint16_t i) {
assert(i < BN_LIMBS * BN_BITS_PER_LIMB);
x->val[i / BN_BITS_PER_LIMB] &= ~(1u << (i % BN_BITS_PER_LIMB));
}
// returns i-th least significant bit (counting from zero)
// Assumes x is normalized and 0 <= i < 261 == LIMBS*BITS_PER_LIMB
// The function has constant control flow but not constant memory access flow
// with regard to i
uint32_t bn_testbit(const bignum256 *x, uint16_t i) {
assert(i < BN_LIMBS * BN_BITS_PER_LIMB);
return (x->val[i / BN_BITS_PER_LIMB] >> (i % BN_BITS_PER_LIMB)) & 1;
}
// res = x ^ y
// Assumes x, y are normalized
// Guarantees res is normalized
// Works properly even if &res == &x or &res == &y or &res == &x == &y
void bn_xor(bignum256 *res, const bignum256 *x, const bignum256 *y) {
for (int i = 0; i < BN_LIMBS; i++) {
res->val[i] = x->val[i] ^ y->val[i];
}
}
// x = x / 2 % prime
// Explicitly x = x / 2 if is_even(x) else (x + prime) / 2
// Assumes x is normalized, x + prime < 261 == LIMBS * BITS_PER_LIMB
// Guarantees x is normalized
// If x is partly reduced (fully reduced) modulo prime,
// guarantess x will be partly reduced (fully reduced) modulo prime
// Assumes prime is an odd number and normalized
void bn_mult_half(bignum256 *x, const bignum256 *prime) {
// x = x / 2 if is_even(x) else (x + prime) / 2
uint32_t x_is_odd_mask =
-(x->val[0] & 1); // x_is_odd_mask = 0xFFFFFFFF if is_odd(x) else 0
uint32_t acc = (x->val[0] + (prime->val[0] & x_is_odd_mask)) >> 1;
// acc < 2**BITS_PER_LIMB
// Proof:
// acc == x[0] + prime[0] & x_is_odd_mask >> 1
// <= (2**(BITS_PER_LIMB) - 1) + (2**(BITS_PER_LIMB) - 1) >> 1
// == 2**(BITS_PER_LIMB + 1) - 2 >> 1
// < 2**(BITS_PER_LIMB)
for (int i = 0; i < BN_LIMBS - 1; i++) {
uint32_t temp = (x->val[i + 1] + (prime->val[i + 1] & x_is_odd_mask));
// temp < 2**(BITS_PER_LIMB + 1)
// Proof:
// temp == x[i + 1] + val[i + 1] & x_is_odd_mask
// <= (2**(BITS_PER_LIMB) - 1) + (2**(BITS_PER_LIMB) - 1)
// < 2**(BITS_PER_LIMB + 1)
acc += (temp & 1) << (BN_BITS_PER_LIMB - 1);
// acc doesn't overflow 32 bits
// Proof:
// acc + (temp & 1 << BITS_PER_LIMB - 1)
// <= 2**(BITS_PER_LIMB + 1) + 2**(BITS_PER_LIMB - 1)
// <= 2**30 + 2**28 < 2**32
x->val[i] = acc & BN_LIMB_MASK;
acc >>= BN_BITS_PER_LIMB;
acc += temp >> 1;
// acc < 2**(BITS_PER_LIMB + 1)
// Proof:
// acc + (temp >> 1)
// <= (2**(32 - BITS_PER_LIMB) - 1) + (2**(BITS_PER_LIMB + 1) - 1 >> 1)
// == 7 + 2**(BITS_PER_LIMB) - 1 < 2**(BITS_PER_LIMB + 1)
// acc == x[:i+2]+(prime[:i+2] & x_is_odd_mask) >> BITS_PER_LIMB * (i+1)
}
x->val[BN_LIMBS - 1] = acc;
// assert(acc >> BITS_PER_LIMB == 0);
// acc >> BITS_PER_LIMB == 0
// Proof:
// acc
// == x[:LIMBS] + (prime[:LIMBS] & x_is_odd_mask) >> BITS_PER_LIMB*LIMBS
// == x + (prime & x_is_odd_mask) >> BITS_PER_LIMB * LIMBS
// <= x + prime >> BITS_PER_LIMB * LIMBS
// <= 2**(BITS_PER_LIMB * LIMBS) - 1 >> BITS_PER_LIMB * LIMBS
// == 0
}
// x = x * k % prime
// Assumes x is normalized, 0 <= k <= 8 = 2**(32 - BITS_PER_LIMB)
// Assumes prime is normalized and 2^256 - 2^224 <= prime <= 2^256
// Guarantees x is normalized and partly reduced modulo prime
void bn_mult_k(bignum256 *x, uint8_t k, const bignum256 *prime) {
assert(k <= 8);
for (int i = 0; i < BN_LIMBS; i++) {
x->val[i] = k * x->val[i];
// x[i] doesn't overflow 32 bits
// k * x[i] <= 2**(32 - BITS_PER_LIMB) * (2**BITS_PER_LIMB - 1)
// < 2**(32 - BITS_PER_LIMB) * 2**BITS_PER_LIMB == 2**32
}
bn_fast_mod(x, prime);
}
// Reduces partly reduced x modulo prime
// Explicitly x = x if x < prime else x - prime
// Assumes x is partly reduced modulo prime
// Guarantees x is fully reduced modulo prime
// Assumes prime is nonzero and normalized
void bn_mod(bignum256 *x, const bignum256 *prime) {
uint32_t x_less_prime = bn_is_less(x, prime);
bignum256 temp = {0};
bn_subtract(x, prime, &temp);
bn_cmov(x, x_less_prime, x, &temp);
memzero(&temp, sizeof(temp));
}
// Auxiliary function for bn_multiply
// res = k * x
// Assumes k and x are normalized
// Guarantees res is normalized 18 digit little endian number in base 2**29
void bn_multiply_long(const bignum256 *k, const bignum256 *x, bignum512 *res) {
// Uses long multiplication in base 2**29, see
// https://en.wikipedia.org/wiki/Multiplication_algorithm#Long_multiplication
uint64_t acc = 0;
// compute lower half
for (int i = 0; i < BN_LIMBS; i++) {
for (int j = 0; j <= i; j++) {
acc += k->val[j] * (uint64_t)x->val[i - j];
// acc doesn't overflow 64 bits
// Proof:
// acc <= acc + sum([k[j] * x[i-j] for j in range(i)])
// <= (2**(64 - BITS_PER_LIMB) - 1) +
// LIMBS * (2**BITS_PER_LIMB - 1) * (2**BITS_PER_LIMB - 1)
// == (2**35 - 1) + 9 * (2**29 - 1) * (2**29 - 1)
// <= 2**35 + 9 * 2**58 < 2**64
}
res->val[i] = acc & BN_LIMB_MASK;
acc >>= BN_BITS_PER_LIMB;
// acc <= 2**35 - 1 == 2**(64 - BITS_PER_LIMB) - 1
}
// compute upper half
for (int i = BN_LIMBS; i < 2 * BN_LIMBS - 1; i++) {
for (int j = i - BN_LIMBS + 1; j < BN_LIMBS; j++) {
acc += k->val[j] * (uint64_t)x->val[i - j];
// acc doesn't overflow 64 bits
// Proof:
// acc <= acc + sum([k[j] * x[i-j] for j in range(i)])
// <= (2**(64 - BITS_PER_LIMB) - 1)
// LIMBS * (2**BITS_PER_LIMB - 1) * (2**BITS_PER_LIMB - 1)
// == (2**35 - 1) + 9 * (2**29 - 1) * (2**29 - 1)
// <= 2**35 + 9 * 2**58 < 2**64
}
res->val[i] = acc & (BN_BASE - 1);
acc >>= BN_BITS_PER_LIMB;
// acc < 2**35 == 2**(64 - BITS_PER_LIMB)
}
res->val[2 * BN_LIMBS - 1] = acc;
}
// Auxiliary function for bn_multiply
// Assumes 0 <= d <= 8 == LIMBS - 1
// Assumes res is normalized and res < 2**(256 + 29*d + 31)
// Guarantess res in normalized and res < 2 * prime * 2**(29*d)
// Assumes prime is normalized, 2**256 - 2**224 <= prime <= 2**256
void bn_multiply_reduce_step(bignum512 *res, const bignum256 *prime,
uint32_t d) {
// clang-format off
// Computes res = res - (res // 2**(256 + BITS_PER_LIMB * d)) * prime * 2**(BITS_PER_LIMB * d)
// res - (res // 2**(256 + BITS_PER_LIMB * d)) * prime * 2**(BITS_PER_LIMB * d) < 2 * prime * 2**(BITS_PER_LIMB * d)
// Proof:
// res - res // (2**(256 + BITS_PER_LIMB * d)) * 2**(BITS_PER_LIMB * d) * prime
// == res - res // (2**(256 + BITS_PER_LIMB * d)) * 2**(BITS_PER_LIMB * d) * (2**256 - (2**256 - prime))
// == res - res // (2**(256 + BITS_PER_LIMB * d)) * 2**(BITS_PER_LIMB * d) * 2**256 + res // (2**(256 + BITS_PER_LIMB * d)) * 2**(BITS_PER_LIMB * d) * (2**256 - prime)
// == (res % 2**(256 + BITS_PER_LIMB * d)) + res // (2**256 + BITS_PER_LIMB * d) * 2**(BITS_PER_LIMB * d) * (2**256 - prime)
// <= (2**(256 + 29*d + 31) % 2**(256 + 29*d)) + (2**(256 + 29*d + 31) - 1) / (2**256 + 29*d) * 2**(29*d) * (2**256 - prime)
// <= 2**(256 + 29*d) + 2**(256 + 29*d + 31) / (2**256 + 29*d) * 2**(29*d) * (2**256 - prime)
// == 2**(256 + 29*d) + 2**31 * 2**(29*d) * (2**256 - prime)
// == 2**(29*d) * (2**256 + 2**31 * (2*256 - prime))
// <= 2**(29*d) * (2**256 + 2**31 * 2*224)
// <= 2**(29*d) * (2**256 + 2**255)
// <= 2**(29*d) * 2 * (2**256 - 2**224)
// <= 2 * prime * 2**(29*d)
// clang-format on
uint32_t coef =
(res->val[d + BN_LIMBS - 1] >>
(256 - (BN_LIMBS - 1) * BN_BITS_PER_LIMB)) +
(res->val[d + BN_LIMBS] << ((BN_LIMBS * BN_BITS_PER_LIMB) - 256));
// coef == res // 2**(256 + BITS_PER_LIMB * d)
// coef < 2**31
// Proof:
// coef == res // 2**(256 + BITS_PER_LIMB * d)
// < 2**(256 + 29 * d + 31) // 2**(256 + 29 * d)
// == 2**31
const int shift = 31;
uint64_t acc = 1ull << shift;
for (int i = 0; i < BN_LIMBS; i++) {
acc += (((uint64_t)(BN_BASE - 1)) << shift) + res->val[d + i] -
prime->val[i] * (uint64_t)coef;
// acc neither overflow 64 bits nor underflow zero
// Proof:
// acc + ((BASE - 1) << shift) + res[d + i] - prime[i] * coef
// >= ((BASE - 1) << shift) - prime[i] * coef
// == 2**shift * (2**BITS_PER_LIMB - 1) - (2**BITS_PER_LIMB - 1) *
// (2**31 - 1)
// == (2**shift - 2**31 + 1) * (2**BITS_PER_LIMB - 1)
// == (2**31 - 2**31 + 1) * (2**29 - 1)
// == 2**29 - 1 > 0
// acc + ((BASE - 1) << shift) + res[d + i] - prime[i] * coef
// <= acc + ((BASE - 1) << shift) + res[d+i]
// <= (2**(64 - BITS_PER_LIMB) - 1) + 2**shift * (2**BITS_PER_LIMB - 1)
// + (2*BITS_PER_LIMB - 1)
// == (2**(64 - BITS_PER_LIMB) - 1) + (2**shift + 1) *
// (2**BITS_PER_LIMB - 1)
// == (2**35 - 1) + (2**31 + 1) * (2**29 - 1)
// <= 2**35 + 2**60 + 2**29 < 2**64
res->val[d + i] = acc & BN_LIMB_MASK;
acc >>= BN_BITS_PER_LIMB;
// acc <= 2**(64 - BITS_PER_LIMB) - 1 == 2**35 - 1
// acc == (1 << BITS_PER_LIMB * (i + 1) + shift) + res[d : d + i + 1]
// - coef * prime[:i + 1] >> BITS_PER_LIMB * (i + 1)
}
// acc += (((uint64_t)(BASE - 1)) << shift) + res[d + LIMBS];
// acc >>= BITS_PER_LIMB;
// assert(acc <= 1ul << shift);
// clang-format off
// acc == 1 << shift
// Proof:
// acc
// == (1 << BITS_PER_LIMB * (LIMBS + 1) + shift) + res[d : d + LIMBS + 1] - coef * prime[:LIMBS] >> BITS_PER_LIMB * (LIMBS + 1)
// == (1 << BITS_PER_LIMB * (LIMBS + 1) + shift) + res[d : d + LIMBS + 1] - coef * prime >> BITS_PER_LIMB * (LIMBS + 1)
// == (1 << BITS_PER_LIMB * (LIMBS + 1) + shift) + (res[d : d + LIMBS + 1] - coef * prime) >> BITS_PER_LIMB * (LIMBS + 1)
// <= (1 << BITS_PER_LIMB * (LIMBS + 1) + shift) + (res[:d] + BASE**d * res[d : d + LIMBS + 1] - BASE**d * coef * prime)//BASE**d >> BITS_PER_LIMB * (LIMBS + 1)
// <= (1 << BITS_PER_LIMB * (LIMBS + 1) + shift) + (res - BASE**d * coef * prime) // BASE**d >> BITS_PER_LIMB * (LIMBS + 1)
// == (1 << BITS_PER_LIMB * (LIMBS + 1) + shift) + (2 * prime * BASE**d) // BASE**d >> BITS_PER_LIMB * (LIMBS + 1)
// <= (1 << 321) + 2 * 2**256 >> 290
// == 1 << 31 == 1 << shift
// == (1 << BITS_PER_LIMB * (LIMBS + 1) + shift) + res[d : d + LIMBS + 1] - coef * prime[:LIMBS + 1] >> BITS_PER_LIMB * (LIMBS + 1)
// >= (1 << BITS_PER_LIMB * (LIMBS + 1) + shift) + 0 >> BITS_PER_LIMB * (LIMBS + 1)
// == 1 << shift
// clang-format on
res->val[d + BN_LIMBS] = 0;
}
// Partly reduces x
// Assumes x in normalized and res < 2**519
// Guarantees x is normalized and partly reduced modulo prime
// Assumes prime is normalized, 2**256 - 2**224 <= prime <= 2**256
void bn_reduce(bignum512 *x, const bignum256 *prime) {
for (int i = BN_LIMBS - 1; i >= 0; i--) {
// res < 2**(256 + 29*i + 31)
// Proof:
// if i == LIMBS - 1:
// res < 2**519
// == 2**(256 + 29 * 8 + 31)
// == 2**(256 + 29 * (LIMBS - 1) + 31)
// else:
// res < 2 * prime * 2**(29 * (i + 1))
// <= 2**256 * 2**(29*i + 29) < 2**(256 + 29*i + 31)
bn_multiply_reduce_step(x, prime, i);
}
}
// x = k * x % prime
// Assumes k, x are normalized, k * x < 2**519
// Guarantees x is normalized and partly reduced modulo prime
// Assumes prime is normalized, 2**256 - 2**224 <= prime <= 2**256
void bn_multiply(const bignum256 *k, bignum256 *x, const bignum256 *prime) {
bignum512 res = {0};
bn_multiply_long(k, x, &res);
bn_reduce(&res, prime);
bn_copy_lower(&res, x);
memzero(&res, sizeof(res));
}
// Partly reduces x modulo prime
// Assumes limbs of x except the last (the most significant) one are normalized
// Assumes prime is normalized and 2^256 - 2^224 <= prime <= 2^256
// Guarantees x is normalized and partly reduced modulo prime
void bn_fast_mod(bignum256 *x, const bignum256 *prime) {
// Computes x = x - (x // 2**256) * prime
// x < 2**((LIMBS - 1) * BITS_PER_LIMB + 32) == 2**264
// x - (x // 2**256) * prime < 2 * prime
// Proof:
// x - (x // 2**256) * prime
// == x - (x // 2**256) * (2**256 - (2**256 - prime))
// == x - ((x // 2**256) * 2**256) + (x // 2**256) * (2**256 - prime)
// == (x % prime) + (x // 2**256) * (2**256 - prime)
// <= prime - 1 + (2**264 // 2**256) * (2**256 - prime)
// <= 2**256 + 2**8 * 2**224 == 2**256 + 2**232
// < 2 * (2**256 - 2**224)
// <= 2 * prime
// x - (x // 2**256 - 1) * prime < 2 * prime
// Proof:
// x - (x // 2**256) * prime + prime
// == x - (x // 2**256) * (2**256 - (2**256 - prime)) + prime
// == x - ((x//2**256) * 2**256) + (x//2**256) * (2**256 - prime) + prime
// == (x % prime) + (x // 2**256) * (2**256 - prime) + prime
// <= 2 * prime - 1 + (2**264 // 2**256) * (2**256 - prime)
// <= 2 * prime + 2**8 * 2**224 == 2**256 + 2**232 + 2**256 - 2**224
// < 2 * (2**256 - 2**224)
// <= 2 * prime
uint32_t coef =
x->val[BN_LIMBS - 1] >> (256 - ((BN_LIMBS - 1) * BN_BITS_PER_LIMB));
// clang-format off
// coef == x // 2**256
// 0 <= coef < 2**((LIMBS - 1) * BITS_PER_LIMB + 32 - 256) == 256
// Proof:
//* Let x[[a : b] be the number consisting of a-th to (b-1)-th bit of the number x.
// x[LIMBS - 1] >> (256 - ((LIMBS - 1) * BITS_PER_LIMB))
// == x[[(LIMBS - 1) * BITS_PER_LIMB : (LIMBS - 1) * BITS_PER_LIMB + 32]] >> (256 - ((LIMBS - 1) * BITS_PER_LIMB))
// == x[[256 - ((LIMBS - 1) * BITS_PER_LIMB) + (LIMBS - 1) * BITS_PER_LIMB : (LIMBS - 1) * BITS_PER_LIMB + 32]]
// == x[[256 : (LIMBS - 1) * BITS_PER_LIMB + 32]]
// == x[[256 : 264]] == x // 2**256
// clang-format on
const int shift = 8;
uint64_t acc = 1ull << shift;
for (int i = 0; i < BN_LIMBS; i++) {
acc += (((uint64_t)(BN_BASE - 1)) << shift) + x->val[i] -
prime->val[i] * (uint64_t)coef;
// acc neither overflows 64 bits nor underflows 0
// Proof:
// acc + (BASE - 1 << shift) + x[i] - prime[i] * coef
// >= (BASE - 1 << shift) - prime[i] * coef
// >= 2**shift * (2**BITS_PER_LIMB - 1) - (2**BITS_PER_LIMB - 1) * 255
// == (2**shift - 255) * (2**BITS_PER_LIMB - 1)
// == (2**8 - 255) * (2**29 - 1) == 2**29 - 1 >= 0
// acc + (BASE - 1 << shift) + x[i] - prime[i] * coef
// <= acc + ((BASE - 1) << shift) + x[i]
// <= (2**(64 - BITS_PER_LIMB) - 1) + 2**shift * (2**BITS_PER_LIMB - 1)
// + (2**32 - 1)
// == (2**35 - 1) + 2**8 * (2**29 - 1) + 2**32
// < 2**35 + 2**37 + 2**32 < 2**64
x->val[i] = acc & BN_LIMB_MASK;
acc >>= BN_BITS_PER_LIMB;
// acc <= 2**(64 - BITS_PER_LIMB) - 1 == 2**35 - 1
// acc == (1 << BITS_PER_LIMB * (i + 1) + shift) + x[:i + 1]
// - coef * prime[:i + 1] >> BITS_PER_LIMB * (i + 1)
}
// assert(acc == 1 << shift);
// clang-format off
// acc == 1 << shift
// Proof:
// acc
// == (1 << BITS_PER_LIMB * LIMBS + shift) + x[:LIMBS] - coef * prime[:LIMBS] >> BITS_PER_LIMB * LIMBS
// == (1 << BITS_PER_LIMB * LIMBS + shift) + (x - coef * prime) >> BITS_PER_LIMB * LIMBS
// <= (1 << BITS_PER_LIMB * LIMBS + shift) + (2 * prime) >> BITS_PER_LIMB * LIMBS
// <= (1 << BITS_PER_LIMB * LIMBS + shift) + 2 * 2**256 >> BITS_PER_LIMB * LIMBS
// <= 2**269 + 2**257 >> 2**261
// <= 1 << 8 == 1 << shift
// acc
// == (1 << BITS_PER_LIMB * LIMBS + shift) + x[:LIMBS] - coef * prime[:LIMBS] >> BITS_PER_LIMB * LIMBS
// >= (1 << BITS_PER_LIMB * LIMBS + shift) + 0 >> BITS_PER_LIMB * LIMBS
// == (1 << BITS_PER_LIMB * LIMBS + shift) + 0 >> BITS_PER_LIMB * LIMBS
// <= 1 << 8 == 1 << shift
// clang-format on
}
// res = x**e % prime
// Assumes both x and e are normalized, x < 2**259
// Guarantees res is normalized and partly reduced modulo prime
// Works properly even if &x == &res
// Assumes prime is normalized, 2**256 - 2**224 <= prime <= 2**256
// The function doesn't have neither constant control flow nor constant memory
// access flow with regard to e
void bn_power_mod(const bignum256 *x, const bignum256 *e,
const bignum256 *prime, bignum256 *res) {
// Uses iterative right-to-left exponentiation by squaring, see
// https://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method
bignum256 acc = {0};
bn_copy(x, &acc);
bn_one(res);
for (int i = 0; i < BN_LIMBS; i++) {
uint32_t limb = e->val[i];
for (int j = 0; j < BN_BITS_PER_LIMB; j++) {
// Break if the following bits of the last limb are zero
if (i == BN_LIMBS - 1 && limb == 0) break;
if (limb & 1)
// acc * res < 2**519
// Proof:
// acc * res <= max(2**259 - 1, 2 * prime) * (2 * prime)
// == max(2**259 - 1, 2**257) * 2**257 < 2**259 * 2**257
// == 2**516 < 2**519
bn_multiply(&acc, res, prime);
limb >>= 1;
// acc * acc < 2**519
// Proof:
// acc * acc <= max(2**259 - 1, 2 * prime)**2
// <= (2**259)**2 == 2**518 < 2**519
bn_multiply(&acc, &acc, prime);
}
// acc == x**(e[:i + 1]) % prime
}
memzero(&acc, sizeof(acc));
}
// x = sqrt(x) % prime
// Explicitly x = x**((prime+1)/4) % prime
// The other root is -sqrt(x)
// Assumes x is normalized, x < 2**259 and quadratic residuum mod prime
// Assumes prime is a prime number, prime % 4 == 3, it is normalized and
// 2**256 - 2**224 <= prime <= 2**256
// Guarantees x is normalized and fully reduced modulo prime
// The function doesn't have neither constant control flow nor constant memory
// access flow with regard to prime
void bn_sqrt(bignum256 *x, const bignum256 *prime) {
// Uses the Lagrange formula for the primes of the special form, see
// http://en.wikipedia.org/wiki/Quadratic_residue#Prime_or_prime_power_modulus
// If prime % 4 == 3, then sqrt(x) % prime == x**((prime+1)//4) % prime
assert(prime->val[0] % 4 == 3);
// e = (prime + 1) // 4
bignum256 e = {0};
bn_copy(prime, &e);
bn_addi(&e, 1);
bn_rshift(&e);
bn_rshift(&e);
bn_power_mod(x, &e, prime, x);
bn_mod(x, prime);
memzero(&e, sizeof(e));
}
// a = 1/a % 2**n
// Assumes a is odd, 1 <= n <= 32
// The function doesn't have neither constant control flow nor constant memory
// access flow with regard to n
uint32_t inverse_mod_power_two(uint32_t a, uint32_t n) {
// Uses "Explicit Quadratic Modular inverse modulo 2" from section 3.3 of "On
// Newton-Raphson iteration for multiplicative inverses modulo prime powers"
// by Jean-Guillaume Dumas, see
// https://arxiv.org/pdf/1209.6626.pdf
// 1/a % 2**n
// = (2-a) * product([1 + (a-1)**(2**i) for i in range(1, floor(log2(n)))])
uint32_t acc = 2 - a;
uint32_t f = a - 1;
// mask = (1 << n) - 1
uint32_t mask = n == 32 ? 0xFFFFFFFF : (1u << n) - 1;
for (uint32_t i = 1; i < n; i <<= 1) {
f = (f * f) & mask;
acc = (acc * (1 + f)) & mask;
}
return acc;
}
// x = (x / 2**BITS_PER_LIMB) % prime
// Assumes both x and prime are normalized
// Assumes prime is an odd number and normalized
// Guarantees x is normalized
// If x is partly reduced (fully reduced) modulo prime,
// guarantess x will be partly reduced (fully reduced) modulo prime
void bn_divide_base(bignum256 *x, const bignum256 *prime) {
// Uses an explicit formula for the modular inverse of power of two
// (x / 2**n) % prime == (x + ((-x / prime) % 2**n) * prime) // 2**n
// Proof:
// (x + ((-x / prime) % 2**n) * prime) % 2**n
// == (x - x / prime * prime) % 2**n
// == 0
// (x + ((-1 / prime) % 2**n) * prime) % prime
// == x
// if x < prime:
// (x + ((-x / prime) % 2**n) * prime) // 2**n
// <= ((prime - 1) + (2**n - 1) * prime) / 2**n
// == (2**n * prime - 1) / 2**n == prime - 1 / 2**n < prime
// if x < 2 * prime:
// (x + ((-x / prime) % 2**n) * prime) // 2**n
// <= ((2 * prime - 1) + (2**n - 1) * prime) / 2**n
// == (2**n * prime + prime - 1) / 2**n
// == prime + (prime - 1) / 2**n < 2 * prime
// m = (-x / prime) % 2**BITS_PER_LIMB
uint32_t m = (x->val[0] * (BN_BASE - inverse_mod_power_two(
prime->val[0], BN_BITS_PER_LIMB))) &
BN_LIMB_MASK;
// m < 2**BITS_PER_LIMB
uint64_t acc = x->val[0] + (uint64_t)m * prime->val[0];
acc >>= BN_BITS_PER_LIMB;
for (int i = 1; i < BN_LIMBS; i++) {
acc = acc + x->val[i] + (uint64_t)m * prime->val[i];
// acc does not overflow 64 bits
// acc == acc + x + m * prime
// <= 2**(64 - BITS_PER_LIMB) + 2**(BITS_PER_LIMB)
// 2**(BITS_PER_LIMB) * 2**(BITS_PER_LIMB)
// <= 2**(2 * BITS_PER_LIMB) + 2**(64 - BITS_PER_LIMB) +
// 2**(BITS_PER_LIMB)
// <= 2**58 + 2**35 + 2**29 < 2**64
x->val[i - 1] = acc & BN_LIMB_MASK;
acc >>= BN_BITS_PER_LIMB;
// acc < 2**35 == 2**(64 - BITS_PER_LIMB)
// acc == x[:i + 1] + m * prime[:i + 1] >> BITS_PER_LIMB * (i + 1)
}
x->val[BN_LIMBS - 1] = acc;
assert(acc >> BN_BITS_PER_LIMB == 0);
// clang-format off
// acc >> BITS_PER_LIMB == 0
// Proof:
// acc >> BITS_PER_LIMB
// == (x[:LIMB] + m * prime[:LIMB] >> BITS_PER_LIMB * LIMBS) >> BITS_PER_LIMB * (LIMBS + 1)
// == x + m * prime >> BITS_PER_LIMB * (LIMBS + 1)
// <= (2**(BITS_PER_LIMB * LIMBS) - 1) + (2**BITS_PER_LIMB - 1) * (2**(BITS_PER_LIMB * LIMBS) - 1) >> BITS_PER_LIMB * (LIMBS + 1)
// == 2**(BITS_PER_LIMB * LIMBS) - 1 + 2**(BITS_PER_LIMB * (LIMBS + 1)) - 2**(BITS_PER_LIMB * LIMBS) - 2**BITS_PER_LIMB + 1 >> BITS_PER_LIMB * (LIMBS + 1)
// == 2**(BITS_PER_LIMB * (LIMBS + 1)) - 2**BITS_PER_LIMB >> BITS_PER_LIMB * (LIMBS + 1)
// == 0
// clang-format on
}
#if !USE_INVERSE_FAST
// x = 1/x % prime if x != 0 else 0