-
Notifications
You must be signed in to change notification settings - Fork 3
/
bn.c
1063 lines (804 loc) · 18.1 KB
/
bn.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 2016 Luka Malisa <luka.malisha@gmail.com>
* Licensed under the terms of the GNU GPL, version 2
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
#if !defined(NAKED)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#if defined(_WIN32) || defined(_MSC_VER)
#include <windows.h>
#endif
#endif
#include "bn.h"
// Fast Montgomery initialization (taken from PolarSSL)
static void _bn_mon_init(bn_t *n)
{
ul_t x, m0 = n->l[0];
x = m0;
x += ((m0 + 2) & 4) << 1;
for(int i = BN_LIMB_BITS; i >= 8; i /= 2)
x *= (2 - (m0 * x));
n->mp = ~x + 1;
}
static u8 _bn_str_to_u8(const s8 *str)
{
u32 i = 2;
u8 t, res = 0;
u8 c;
while(i--)
{
c = *str++;
if(c >= '0' && c <= '9')
t = c - '0';
else if(c >= 'a' && c <= 'f')
t = c - 'a' + 10;
else if(c >= 'A' && c <= 'F')
t = c - 'A' + 10;
else
t = 0;
res |= t << (i * 4);
}
return res;
}
static int _bn_add(bn_t *d, bn_t *a, bn_t *b)
{
ull_t C = 0;
// assert(a->n == b->n);
for(int i = 0; i < MIN(a->n_limbs, b->n_limbs); i++)
{
C += (ull_t)a->l[i] + b->l[i];
d->l[i] = C;
C >>= BN_LIMB_BITS;
}
return C;
}
static int _bn_add_ui(bn_t *d, bn_t *a, ul_t b)
{
ull_t C = b;
for(int i = 0; i < a->n_limbs; i++)
{
C += (ull_t)a->l[i];
d->l[i] = C;
C >>= BN_LIMB_BITS;
}
return C;
}
static int _bn_sub(bn_t *d, bn_t *a, bn_t *b)
{
ull_t C = 1;
// assert(a->n == b->n);
for(int i = 0; i < MIN(a->n_limbs, b->n_limbs); i++)
{
C += (ull_t)a->l[i] + BN_MAX_DIGIT - b->l[i];
d->l[i] = C;
C >>= BN_LIMB_BITS;
}
return 1 - C;
}
static int _bn_sub_ui(bn_t *d, bn_t *a, ul_t b)
{
ull_t C = 1 + BN_MAX_DIGIT - b;
for(int i = 0; i < a->n_limbs; i++)
{
C += (ull_t)a->l[i];
d->l[i] = C;
C >>= BN_LIMB_BITS;
}
return 1 - C;
}
// Runtime endianess detection. A bit slower, but no macro magic needed
static int _bn_big_endian()
{
u32 t = 0x11223344;
u8 *p = (u8 *)&t;
if(p[0] == 0x11)
return 1;
else
return 0;
}
static bn_t *_bn_lshift_limbs(bn_t *a, int n)
{
int i;
for(i = a->n_limbs; i >= n; i--)
a->l[i] = a->l[i-n];
while(i >= 0)
a->l[i--] = 0;
return a;
}
static bn_t *_bn_lshift(bn_t *a, int b)
{
ul_t mask = -1;
ul_t prev_c = 0;
ul_t c = 0;
if(b != BN_LIMB_BITS)
mask = ~(mask >> b);
for(int x = 0; x < a->n_limbs; x++)
{
c = a->l[x] & mask;
// Shift left by amount
a->l[x] <<= b;
// Add the carry part from the previous limb
a->l[x] |= prev_c >> (BN_LIMB_BITS - b);
prev_c = c;
}
return a;
}
static bn_t *_bn_rshift_limbs(bn_t *a, int n)
{
// Makes the code run faster
memmove(a->l, &a->l[n], (a->n_limbs - n) * BN_LIMB_BYTES);
/*
int x;
for(x = 0; x < a->n_limbs - n; x++)
a->l[x] = a->l[x+n];
while(x <= n)
a->l[x++] = 0;
*/
return a;
}
static bn_t *_bn_rshift(bn_t *a, int b)
{
ull_t mask;
ul_t prev_c = 0;
ul_t c = 0;
// Create the mask
mask = ((ull_t)1 << b) - 1;
for(int x = a->n_limbs - 1; x >= 0; x--)
{
c = a->l[x] & mask;
// Shift right by amount
a->l[x] >>= b;
// Add the carry part from the previous limb
a->l[x] |= prev_c << (BN_LIMB_BITS - b);
prev_c = c;
}
return a;
}
// Helper function which multiplies and adds in a single run.
static bn_t *_bn_mad_ui(bn_t *d, bn_t *a, ul_t b)
{
// D = D + A * b
int x;
ull_t S = 0;
// Can a hold the result?
assert(d->n >= (a->n + 2));
for(x = 0; x < a->n_limbs; x++)
{
S += (ull_t)a->l[x] * (ull_t)b + (ull_t)d->l[x];
d->l[x] = S;
S >>= BN_LIMB_BITS;
}
// Add in the remaining carry
while(S)
{
S += d->l[x];
d->l[x] = S;
S >>= BN_LIMB_BITS;
x++;
}
return d;
}
int bn_maxbit(bn_t *a)
{
for(int x = a->n_limbs * BN_LIMB_BITS; x >= 0; x--)
if(bn_getbit(a, x) != 0)
return x;
return 0;
}
int bn_getbit(bn_t *a, int x)
{
return (a->l[x / BN_LIMB_BITS] >> (x % BN_LIMB_BITS)) & 1;
}
void bn_setbit(bn_t *a, int x)
{
a->l[x / BN_LIMB_BITS] |= 1 << (x % BN_LIMB_BITS);
}
bn_t *bn_from_bin(bn_t *a, s8 *s, int len)
{
int x, y, z, w;
ul_t limb;
u8 *p_limb = (u8 *)&limb;
for(x = len - 1, y = 0; x >= 0; y++)
{
limb = 0;
for(z = 0, w = 0; z < BN_LIMB_BYTES; z += 1, w += 1)
{
if(x < 0)
break;
p_limb[w] = s[x];
x -= 1;
}
if(_bn_big_endian())
limb = SWAP(limb);
a->l[y] = limb;
}
return a;
}
u8 *bn_to_bin(u8 *s, bn_t *a)
{
ul_t *p = (ul_t *)s;
int be = _bn_big_endian();
for(int x = a->n_limbs - 1, y = 0; x >= 0; x--, y++)
{
if(be)
p[y] = a->l[x];
else
p[y] = SWAP(a->l[x]);
}
return s;
}
bn_t *bn_from_str(bn_t *a, const s8 *s)
{
int len = strlen(s);
int x, y, z, w;
ul_t limb;
u8 *p_limb = (u8 *)&limb;
// The x2 is for two ascii chars representing a single byte
int step = (BN_LIMB_BYTES * 2);
if(len % 2)
return NULL;
for(x = len - 2, y = 0; x >= 0; y++)
{
limb = 0;
for(z = 0, w = 0; z < step && x >= 0; z += 2, w += 1)
{
p_limb[w] = _bn_str_to_u8(&s[x]);
x -= 2;
}
if(_bn_big_endian())
limb = SWAP(limb);
a->l[y] = limb;
}
return a;
}
bn_t *bn_zero(bn_t *a)
{
memset((char *)a->l, 0, a->n_limbs * BN_LIMB_BYTES);
return a;
}
bn_t *bn_alloc(int size)
{
int s;
bn_t *ret = (bn_t *)mem_alloc(sizeof(bn_t));
memset((char *)ret, 0x00, sizeof(bn_t));
ret->n = size;
ret->n_limbs = BYTES_TO_LIMBS(size);
// Always allocate 4 limbs more than we need, so that potential bn_mon_mul is faster
s = sizeof(ul_t) * (ret->n_limbs + 4);
ret->l = (ul_t *)mem_alloc(s);
memset((char *)ret->l, 0x00, s);
return ret;
}
bn_t *bn_alloc_limbs(int limbs)
{
return bn_alloc(LIMBS_TO_BYTES(limbs));
}
bn_t *bn_copy(bn_t *a, bn_t *b)
{
int s = MIN(a->n_limbs, b->n_limbs);
bn_zero(a);
memcpy((s8 *)a->l, (s8 *)b->l, sizeof(ul_t) * s);
return a;
}
void bn_free(bn_t *a)
{
// Zero it out, just for good measure
bn_zero(a);
mem_free(a->l);
mem_free(a);
}
inline bn_t *bn_set_ui(bn_t *a, u64 val)
{
for(int x = 0; x < sizeof(val) / sizeof(ul_t); x++)
{
a->l[x] = val & (ul_t)-1;
val >>= BN_LIMB_BITS;
}
return a;
}
bn_t *bn_add(bn_t *d, bn_t *a, bn_t *b, bn_t *n)
{
// D = A + B % N
// Prevent overflow
if(_bn_add(d, a, b))
_bn_sub(d, d, n);
bn_reduce(d, n);
return d;
}
bn_t *bn_add_ui(bn_t *d, bn_t *a, unsigned int b, bn_t *n)
{
// D = A + B % N
// Prevent overflow
if(_bn_add_ui(d, a, b))
_bn_sub(d, d, n);
bn_reduce(d, n);
return d;
}
bn_t *bn_sub(bn_t *d, bn_t *a, bn_t *b, bn_t *n)
{
// D = A - B % N
// Prevent underflow
if(_bn_sub(d, a, b))
_bn_add(d, d, n);
bn_reduce(d, n);
return d;
}
bn_t *bn_sub_ui(bn_t *d, bn_t *a, unsigned int b, bn_t *n)
{
// D = A - B % N
// Prevent underflow
if(_bn_sub_ui(d, a, b))
_bn_add(d, d, n);
bn_reduce(d, n);
return d;
}
int bn_cmp(bn_t *a, bn_t *b)
{
// assert(a->n_limbs == b->n_limbs);
// First check the high limbs, if any
if(a->n_limbs > b->n_limbs)
{
for(int i = b->n_limbs; i < a->n_limbs; i++)
if(a->l[i])
return BN_CMP_G;
}
else if(b->n_limbs > a->n_limbs)
{
for(int i = a->n_limbs; i < b->n_limbs; i++)
if(b->l[i])
return BN_CMP_G;
}
// ...then check the main limbs
for(int x = MIN(a->n_limbs, b->n_limbs); x >= 0; x--)
{
if(a->l[x] < b->l[x])
return BN_CMP_L;
else if(a->l[x] > b->l[x])
return BN_CMP_G;
}
return BN_CMP_E;
}
int bn_cmp_ui(bn_t *a, ul_t b)
{
int ret = 0;
if(a->l[0] < b)
ret = BN_CMP_L;
else if(a->l[0] > b)
ret = BN_CMP_G;
else if(a->l[0] == b)
ret = BN_CMP_E;
// Let's walk over all other digits of a (if any)
for(int x = 1; x < a->n_limbs; x++)
{
if(a->l[x] != 0)
{
// So a has some non-zero digits. It's clearly bigger than b
ret = BN_CMP_G;
break;
}
}
return ret;
}
int bn_is_zero(bn_t *a)
{
for(int x = 0; x < a->n_limbs; x++)
if(a->l[x] != 0)
return 0;
return 1;
}
bn_t *bn_reduce(bn_t *a, bn_t *n)
{
while(bn_cmp(a, n) >= 0)
_bn_sub(a, a, n);
return a;
}
bn_t *bn_reduce_slow(bn_t *a, bn_t *n)
{
bn_t *q = bn_alloc_limbs(n->n_limbs);
bn_t *r = bn_alloc_limbs(n->n_limbs);
bn_divrem(q, r, a, n);
bn_copy(a, r);
bn_free(q);
bn_free(r);
return a;
}
bn_t *bn_lshift(bn_t *a, int b)
{
// Single largest shift we can do is one limb
while(b > BN_LIMB_BITS)
{
_bn_lshift_limbs(a, 1);
b -= BN_LIMB_BITS;
}
return _bn_lshift(a, b);
}
bn_t *bn_rshift(bn_t *a, int b)
{
// Single largest shift we can do is one limb
while(b > BN_LIMB_BITS)
{
_bn_rshift_limbs(a, 1);
b -= BN_LIMB_BITS;
}
return _bn_rshift(a, b);
}
int bn_lsb(bn_t *a)
{
return a->l[0] & 1;
}
int bn_msb(bn_t *a)
{
return a->l[a->n - 1] >> (BN_LIMB_BITS - 1);
}
#if defined(BN_PRINT_FUNCS)
void bn_print(FILE *fp, const s8 *pre, bn_t *a, const s8 *post)
{
int i, init = 1;
fputs((char *)pre, fp);
//Skip zero limbs.
for(i = a->n_limbs - 1; i >= 0; i--)
if(a->l[i] != 0)
break;
for(; i >= 0; i--)
{
if(init)
{
init = 0;
fprintf(fp, BN_PRINT_FORMAT_I, a->l[i]);
}
else
fprintf(fp, BN_PRINT_FORMAT, a->l[i]);
}
fputs((char *)post, fp);
}
bn_t *bn_read(FILE *fp, bn_t *dst)
{
s8 *data = mem_alloc(dst->n);
fread(data, sizeof(u8), dst->n, fp);
bn_from_bin(dst, data, dst->n);
mem_free(data);
return dst;
}
bn_t *bn_write(FILE *fp, bn_t *num)
{
u8 *data = mem_alloc(num->n);
bn_to_bin(data, num);
fwrite(data, 1, num->n, fp);
mem_free(data);
return num;
}
#endif
bn_t *bn_mul(bn_t *d, bn_t *a, bn_t *b)
{
// D = A * b
// Can a hold the result?
assert(d->n >= (a->n * 2 + 1));
bn_zero(d);
for(int i = 0; i < a->n_limbs; i++)
{
ull_t S = 0;
for(int j = 0; j < a->n_limbs; j++)
{
S += (ull_t)a->l[i] * b->l[j];
d->l[i+j] += S;
S >>= BN_LIMB_BITS;
}
d->l[i + a->n_limbs] = S;
}
return d;
}
bn_t *bn_mul_ui(bn_t *d, bn_t *a, ul_t b)
{
// D = A * b
ull_t S = 0;
// Can a hold the result?
assert(d->n >= (a->n + 1));
bn_zero(d);
for(int x = 0; x < a->n_limbs; x++)
{
S += (ull_t)a->l[x] * b;
d->l[x] = S;
S >>= BN_LIMB_BITS;
}
d->l[a->n_limbs] = S;
return d;
}
bn_t *bn_divrem(bn_t *q, bn_t *r, bn_t *a, bn_t *b)
{
bn_zero(q);
bn_zero(r);
for(int i = bn_maxbit(a); i >= 0 ;i--)
{
bn_lshift(r, 1);
bn_lshift(q, 1);
if(bn_getbit(a, i))
bn_setbit(r, 0);
if(bn_cmp(r, b) >= 0)
{
_bn_sub(r, r, b);
bn_setbit(q, 0);
}
}
return q;
}
bn_t *bn_rand(bn_t *a)
{
int size = a->n;
u8 *tmp = (u8 *)mem_alloc(size);
#if defined(_WIN32) || defined(_MSC_VER)
HCRYPTPROV hProvider;
CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
CryptGenRandom(hProvider, size, tmp);
#else
int fd = open("/dev/urandom", O_RDONLY, 0);
read(fd, tmp, size);
close(fd);
#endif
bn_zero(a);
bn_from_bin(a, (s8 *)tmp, size);
mem_free(tmp);
return a;
}
// Generate random a \in [x, b - y].
bn_t *bn_rand_range(bn_t *a, int x, bn_t *b, int y)
{
bn_t *t = bn_alloc(b->n);
_bn_sub_ui(t, b, y);
while(1)
{
bn_rand(a);
if(bn_cmp_ui(a, x) <= 0) // Check a < x
continue;
if(bn_cmp(a, t) > 0) // Check a > b - y
continue;
break;
}
bn_free(t);
return a;
}
bn_t *bn_to_mon(bn_t *a, bn_t *n)
{
bn_t *at = bn_copy(bn_alloc_limbs(a->n_limbs + 1), a);
bn_t *nt = bn_copy(bn_alloc_limbs(n->n_limbs + 1), n);
// We can't loop bn_add here since bn_add calls bn_reduce which in turn calls bn_to_mon.
// POOF, infinite recursion.
for(int x = 0; x < BN_LIMB_BITS * a->n_limbs; x++)
{
_bn_add(at, at, at);
bn_reduce(at, nt);
}
bn_copy(a, at);
bn_free(at);
bn_free(nt);
return a;
}
bn_t *bn_from_mon(bn_t *a, bn_t *n)
{
bn_t *t = bn_alloc(a->n);
bn_set_ui(t, 1);
bn_mon_mul(a, a, t, n);
bn_free(t);
return a;
}
bn_t *bn_mon_mul(bn_t *d, bn_t *a, bn_t *b, bn_t *n)
{
ul_t q;
ull_t r = (ull_t)1 << BN_LIMB_BITS;
// The calculation of the mp value needs to be done only once.
if(n->mp == 0)
_bn_mon_init(n);
// This num needs to be 4 digits bigger so we prevent overflows.
// The mul_ui increases digit count by 1, and add's possibly increase
// the count by one (each).
bn_t *t = bn_alloc_limbs(n->n_limbs + 4);
for(int x = 0; x < a->n_limbs; x++)
{
q = (t->l[0] + a->l[x] * b->l[0]) * n->mp;
// q % r == q & (r-1), for r power of two
q = q & (r - 1);
_bn_mad_ui(t, n, q);
_bn_mad_ui(t, b, a->l[x]);
// Shift right by one limb
_bn_rshift_limbs(t, 1);
}
if(bn_cmp(t, n) >= 0)
_bn_sub(t, t, n);
bn_copy(d, t);
bn_free(t);
return d;
}
// Montgomery reduction
bn_t *bn_mon_reduce(bn_t *a, bn_t *n)
{
ull_t r = (ull_t)1 << BN_LIMB_BITS;
ul_t mu;
// The calculation of the mp value needs to be done only once.
if(n->mp == 0)
_bn_mon_init(n);
bn_t *at = bn_copy(bn_alloc_limbs(a->n_limbs * 2 + 1), a);
bn_t *tmp = bn_alloc_limbs(a->n_limbs * 2 + 1);
for(int x = 0; x < a->n_limbs; x++)
{
mu = at->l[x] * n->mp % r;
bn_mul_ui(tmp, n, mu);
_bn_lshift_limbs(tmp, x);
_bn_add(at, at, tmp);
}
_bn_rshift_limbs(at, a->n_limbs);
bn_copy(a, at);
bn_free(at);
bn_free(tmp);
return a;
}
// Square and multiply exponentiation
bn_t *bn_mon_pow_sm(bn_t *d, bn_t *a, bn_t *e, bn_t *n)
{
// D = A**E % N
bn_t *s = bn_copy(bn_alloc(a->n), a);
bn_t *t = bn_copy(bn_alloc(d->n), d);
bn_set_ui(t, 1);
bn_to_mon(t, n);
for(int i = 0; i <= bn_maxbit(e); i++)
{
if(bn_getbit(e, i))
bn_mon_mul(t, t, s, n);
bn_mon_mul(s, s, s, n);
}
bn_copy(d, t);
bn_free(s);
bn_free(t);
return d;
}
// Square and multiply montgomery power ladder
bn_t *bn_mon_pow_ml(bn_t *d, bn_t *a, bn_t *e, bn_t *n)
{
// D = A**E % N
bn_t *r0 = bn_set_ui(bn_alloc(a->n), 1);
bn_t *r1 = bn_copy(bn_alloc(a->n), a);
bn_to_mon(r0, n);
for(int i = e->n * 8; i >= 0; i--)
{
if(bn_getbit(e, i))
{
bn_mon_mul(r0, r0, r1, n);
bn_mon_mul(r1, r1, r1, n);
}
else
{
bn_mon_mul(r1, r0, r1, n);
bn_mon_mul(r0, r0, r0, n);
}
}
bn_copy(d, r0);
bn_free(r0);
bn_free(r1);
return d;
}
// Sliding-window exponentiation (HAC 14.83)
bn_t *bn_mon_pow_sw(bn_t *d, bn_t *a, bn_t *e, bn_t *n)
{
// D = A**E % N
bn_t *s = bn_copy(bn_alloc(a->n), a);
bn_t *t = bn_copy(bn_alloc(d->n), d);
// Select which window size to use
int blen = BN_LIMB_BITS * n->n_limbs;
int wsize = (blen > 671) ? 6 : (blen > 239) ? 5 : (blen > 79) ? 4 : (blen > 23) ? 3 : 1;
//
// Initialize the cache
//
bn_t *cache[1 << wsize];
bn_set_ui(t, 1);
bn_to_mon(t, n);
// Initialize the cache first
for(int i = 0; i < (1 << wsize); i++)
cache[i] = bn_zero(bn_alloc(a->n));
// 1st and 2nd elements are always the same
bn_copy(cache[1], a);
bn_mon_mul(cache[2], a, a, n);
for(int i = 1; i < 1 << (wsize - 1); i++)
bn_mon_mul(cache[2*i+1], cache[2*i-1], cache[2], n);
// And iterate...
for(int i = bn_maxbit(e); i >= 0;)
{
// In the 0-bit case, just square
if(!bn_getbit(e, i))
{
bn_mon_mul(t, t, t, n);
i--;
}
else
{
int num = 0;
int sub = 0;
int idx = 0;
for(int j = 0; j < wsize; j++)
{
if(i - j < 0)
break;
int bit = bn_getbit(e, i - j);
idx = (idx << 1) | bit;
if(bit)
{
num = idx;
sub = j + 1;
}
}
// Square first
for(int j = 0; j < sub; j++)
bn_mon_mul(t, t, t, n);
// ...then multiply with cache
bn_mon_mul(t, t, cache[num], n);
i -= sub;
}
}
bn_copy(d, t);
bn_free(s);
bn_free(t);
for(int i = 0; i < (1 << wsize); i++)
bn_free(cache[i]);
return d;
}
bn_t *bn_inv(bn_t *d, bn_t *a, bn_t *n)
{
// D = A**-1 % N
bn_t *q = bn_alloc_limbs(n->n_limbs);
bn_t *r = bn_alloc_limbs(n->n_limbs);
bn_t *m = bn_alloc_limbs(n->n_limbs);
bn_t *t = bn_alloc_limbs(2*n->n_limbs);
bn_t *b = bn_copy(bn_alloc_limbs(n->n_limbs), n);
bn_t *nn = bn_copy(bn_alloc_limbs(2*n->n_limbs), n);
bn_t *x = bn_alloc_limbs(n->n_limbs);
bn_t *u = bn_alloc_limbs(n->n_limbs);