-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrsa3.cpp
2651 lines (2376 loc) · 66.8 KB
/
rsa3.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
#include "D:\study\密码学\my特别快的RSA and BN\bignum3.h"
#include<Windows.h>
using namespace std;
//加法可行的,取1024位
int add_b(BN a, BN b, BN sum)
{
memset(sum, 0, BNSIZE);
uint32_t *aptr, *bptr, *sptr = LSDPTR_B(sum);
uint32_t *maptr, *mbptr;
uint64_t carry = 0U;
int flag = FLAG_OK;
if (DIGITS_B(a) < DIGITS_B(b))//让a表示长的那个
{
aptr = LSDPTR_B(b);
maptr = MSDPTR_B(b);
bptr = LSDPTR_B(a);
mbptr = MSDPTR_B(a);
SETDIGITS_B(sum, DIGITS_B(b));
}
else
{
aptr = LSDPTR_B(a);
maptr = MSDPTR_B(a);
bptr = LSDPTR_B(b);
mbptr = MSDPTR_B(b);
SETDIGITS_B(sum, DIGITS_B(a));
}
while (bptr <= mbptr)//短的还没有加完
{
carry = (uint64_t)((uint64_t)(*aptr) + (uint64_t)(*bptr) + (uint64_t)(uint32_t)(carry >> BITPERDIGIT));
*sptr = (uint32_t)carry;
aptr++; bptr++; sptr++;
}
while (aptr <= maptr)//和刚刚产生的或许有的进位相加,否则和自己加
{
carry = (uint64_t)((uint64_t)(*aptr) + (uint64_t)(uint32_t)(carry >> BITPERDIGIT));
*sptr = (uint32_t)carry;
aptr++; sptr++;
}
if (carry& BASE)//如果还有进位1
{
*sptr = 1U;
INCDIGITS_B(sum);
}
if (DIGITS_B(sum) > (uint32_t)BNMAXDGT)//超过了32“位”,上溢出了
{
SETDIGITS_B(sum, BNMAXDGT);//先设置32“位”,可能有前导0,模掉大于32“位”的
RMLDZRS_B(sum);
flag = FLAG_OF;//加法上溢出了
}
return flag;
}
//加法,可以吞下BND的加法
void add(BN a, BN b, BN sum)
{
uint32_t *aptr, *bptr, *sptr = LSDPTR_B(sum);
uint32_t *maptr, *mbptr;
uint64_t carry = 0U;
if (DIGITS_B(a) < DIGITS_B(b))//让a表示长的那个
{
aptr = LSDPTR_B(b);
maptr = MSDPTR_B(b);
bptr = LSDPTR_B(a);
mbptr = MSDPTR_B(a);
SETDIGITS_B(sum, DIGITS_B(b));
}
else
{
aptr = LSDPTR_B(a);
maptr = MSDPTR_B(a);
bptr = LSDPTR_B(b);
mbptr = MSDPTR_B(b);
SETDIGITS_B(sum, DIGITS_B(a));
}
while (bptr <= mbptr)//短的还没有加完
{
carry = (uint64_t)((uint64_t)(*aptr) + (uint64_t)(*bptr) + (uint64_t)(uint32_t)(carry >> BITPERDIGIT));
*sptr = (uint32_t)carry;
aptr++; bptr++; sptr++;
}
while (aptr <= maptr)//和刚刚产生的或许有的进位相加,否则和自己加
{
carry = (uint64_t)((uint64_t)(*aptr) + (uint64_t)(uint32_t)(carry >> BITPERDIGIT));
*sptr = (uint32_t)carry;
aptr++; sptr++;
}
if (carry& BASE)//如果还有进位1
{
*sptr = 1U;
INCDIGITS_B(sum);
}
RMLDZRS_B(sum);
}
//result=a+b(mod n)
int modadd_b(BN a, BN b, BN n, BN & result)
{
int flag = FLAG_OK;
uint32_t temp1[BNMAXDGT + 2];
memset(temp1, 0, sizeof(temp1));
BN temp2;
memset(temp1, 0, BNSIZE);
memset(temp2, 0, BNSIZE);
if (cmp_b(n, ZERO_BN) == 0)//如果模数为0
add(a, b, result);
else
{
add(a, b, temp1);
flag = modn_b(temp1, n, temp2);
cpy_b(result, temp2);
}
return flag;
}
//加一个uint32
int adduint_b(BN a, uint32_t b, BN sum)
{
BND temp = { 0 };
temp[0] = 1; temp[1] = b;//现在temp是大数
add(a, temp, sum);
return 0;
}
int adduint_b(uint32_t a, BN b, BN &sum)
{
BN temp = { 0 };
temp[0] = 1; temp[1] = a;//现在temp是大数
int flag = add_b(b, temp, sum);
return flag;
}
//模1024位的取模的
int sub_b(BN a, BN b, BN result)
{
memset(result, 0, BNSIZE);
uint64_t carry = 0ULL;
uint32_t *aptr, *bptr, *rptr, *maptr, *mbptr;
int flag = FLAG_OK;//检验是否发生意外下溢
uint32_t a_t[BITPERDIGIT + 2];//多了1位
BN b_t;
memset(b_t, 0, sizeof(b_t));
cpy_b(a_t, a);
cpy_b(b_t, b);
aptr = LSDPTR_B(a_t);
bptr = LSDPTR_B(b_t);
rptr = LSDPTR_B(result);
maptr = MSDPTR_B(a_t);
mbptr = MSDPTR_B(b_t);
if (cmp_b(a_t, b_t) == -1)//如果a<b
{
setmax_b(a_t);
maptr = a_t + BNMAXDGT;//指向[31]
SETDIGITS_B(result, BNMAXDGT);//怕是结果也有这么多位,先设这么多最后消除
flag = FLAG_UF;//下溢了
}
else//没有发生下溢
{
SETDIGITS_B(result, DIGITS_B(a_t));//位数应该和a是一样的
}
while (bptr <= mbptr)//b还有位数,类比加法
{
carry = (uint64_t)*aptr - (uint64_t)*bptr - ((carry&BASE) >> BITPERDIGIT);//a-b-ci(可能之前借位了)
*rptr = (uint32_t)carry;
aptr++; bptr++; rptr++;
}
while (aptr <= maptr)//a还没有完,类比加法
{
carry = (uint64_t)*aptr - ((carry&BASE) >> BITPERDIGIT);//a-b-ci(可能之前借位了)
*rptr = (uint32_t)carry;
aptr++; rptr++;
}
RMLDZRS_B(result);//消除前导0
if (flag == FLAG_UF)//如果下溢了,更正一下
{
add_b(result, a, result);
add_b(result, ONE_BN, result);//(Nm-b+a)+1
}
return flag;
}
//减一个uint32
int subuint_b(BN a, uint32_t b, BN &result)
{
BN temp = { 0 };
temp[0] = 1; temp[1] = b;//现在temp是大数
int flag = sub_b(a, temp, result);
return flag;
}
//减法
void sub(BN a, BN b, BN result)
{
uint32_t *aptr, *bptr, *rptr, *maptr, *mbptr;
uint64_t carry = 0U;
aptr = LSDPTR_B(a);
bptr = LSDPTR_B(b);
maptr = MSDPTR_B(a);
mbptr = MSDPTR_B(b);
rptr = LSDPTR_B(result);
SETDIGITS_B(result, DIGITS_B(a));
//如果ai<bi,carry的高位会全为1而不是0,判断第33位
//例如2位二进制,基数为4的情况,carry是4位;01-1则caryy=1110;10-11=1111。判断借位的时候,判断33位二进制位是否为0就可以
while (bptr <= mbptr)
{
carry = (uint64_t)((uint64_t)(*aptr) - (uint64_t)(*bptr) - (uint64_t)((carry&BASE) >> BITPERDIGIT));
*rptr = (uint32_t)carry;
aptr++; bptr++; rptr++;
}
while (aptr <= maptr)//可能连续借位
{
carry = (uint64_t)((uint64_t)(*aptr) - (uint64_t)((carry&BASE) >> BITPERDIGIT));
*rptr = (uint32_t)carry;
aptr++; rptr++;
}
RMLDZRS_B(result);
}
void modsub_b(BN a, BN b, BN n, BN &result)
{
BN a_t, b_t, rem, temp;
memset(rem, 0, sizeof(rem));
memset(rem, 0, sizeof(temp));
cpy_b(a_t, a);
cpy_b(b_t, b);
if (cmp_b(a_t, b_t) >= 0)//如果a>=b
{
//sub_b(a_t, b_t, temp);
sub(a_t, b_t, temp);
//cout << bn2str(a_t) << " - " << bn2str(b_t) << " = " << bn2str(temp) << endl;
modn_b(temp, n, rem);
//cout << "zhe cuo le line 424!" << endl;
//cout << bn2str(temp) << " % " << bn2str(n) << " = " << bn2str(rem) << endl;
cpy_b(result, rem);
}
else //a<b...那就b-a=t,然后用n-t 12-19 mod 5 19-12 mod 5 =2 5-2=3
{
//sub_b(b_t, a_t, temp);
sub(b_t, a_t, temp);
modn_b(temp, n, rem);
//cout << bn2str(temp) << " % " << bn2str(n) << " = " << bn2str(rem) << endl;
//sub_b(n, rem, temp);
sub(n, rem, temp);
//cout << bn2str(n) << " - " << bn2str(rem) << " = " << bn2str(temp) << endl;
cpy_b(result, temp);
}
}
//乘法取1024位
int mul_b(BN a, BN b, BN & result)
{
BN a_t = { 0 }, b_t = { 0 };
BND temp = { 0 };
uint32_t *aptr, *bptr, *maptr, *mbptr, *pptr;
uint64_t carry = 0;
if (DIGITS_B(a) == 0 || DIGITS_B(b) == 0)
{
cpy_b(result, ZERO_BN);
return FLAG_OK;
}
//if (DIGITS_B(a) < DIGITS_B(b))
//{
// cpy_b(a_t, b);
// cpy_b(b_t, a);
//}
//else
//{
// cpy_b(a_t, a);
// cpy_b(b_t, b);
//}
cpy_b(a_t, a);
cpy_b(b_t, b);
maptr = MSDPTR_B(a_t);
mbptr = MSDPTR_B(b_t);
int pos = 0;
for (bptr = LSDPTR_B(b_t), pptr = LSDPTR_B(temp); bptr <= mbptr; bptr++)
{
carry = 0;//前一次进位已经存在了最高位
for (aptr = LSDPTR_B(a_t); aptr <= maptr; aptr++, pptr++)
{
carry = (uint64_t)((uint64_t)(*aptr) * (uint64_t)(*bptr) + (uint64_t)(*pptr)
+ (uint64_t)(uint32_t)(carry >> BITPERDIGIT));
*pptr = (uint32_t)carry;//低32是本位积
}
pos++;//滑动一位
*pptr = (uint32_t)(carry >> BITPERDIGIT);//最后一位进位
pptr = LSDPTR_B(temp) + pos;//回到开头
}
SETDIGITS_B(temp, DIGITS_B(a) + DIGITS_B(b));
RMLDZRS_B(temp);
if (DIGITS_B(temp) > BNMAXDGT)//大于1024取1024位
{
SETDIGITS_B(temp, BNMAXDGT);
RMLDZRS_B(temp);
}
cpy_b(result, temp);
return FLAG_OK;
}
//乘法
int mul(BN a, BN b, BN result)
{
BN a_t = { 0 }, b_t = { 0 };
BND temp = { 0 };
uint32_t *aptr, *bptr, *maptr, *mbptr, *pptr;
uint64_t carry = 0U;
if (DIGITS_B(a) == 0 || DIGITS_B(b) == 0)
{
cpy_b(result, ZERO_BN);
return FLAG_OK;
}
//if (DIGITS_B(a) < DIGITS_B(b))
//{
// cpy_b(a_t, b);
// cpy_b(b_t, a);
//}
//else
//{
// cpy_b(a_t,a);
// cpy_b(b_t,b);
//}
cpy_b(a_t, a);
cpy_b(b_t, b);
maptr = MSDPTR_B(a_t);
mbptr = MSDPTR_B(b_t);
int pos = 0;//每次p往前滑动一位
for (bptr = LSDPTR_B(b_t), pptr = LSDPTR_B(temp); bptr <= mbptr; bptr++)
{
carry = 0;//前一次进位已经存在了最高位
for (aptr = LSDPTR_B(a_t); aptr <= maptr; aptr++, pptr++)
{
carry = (uint64_t)((uint64_t)(*aptr) * (uint64_t)(*bptr) + (uint64_t)(*pptr)
+ (uint64_t)(uint32_t)(carry >> BITPERDIGIT));
*pptr = (uint32_t)carry;//低32是本位积
}
pos++;//滑动一位
*pptr = (uint32_t)(carry >> BITPERDIGIT);//最后一位进位
pptr = LSDPTR_B(temp) + pos;//回到开头
}
/*cout << "bits of a is" << getbits_b(a_t) << endl;
cout << "pos is " << pos << endl;
cout << "bits of b is" << getbits_b(b_t) << endl;*/
SETDIGITS_B(temp, DIGITS_B(a) + DIGITS_B(b));
RMLDZRS_B(temp);
//cout << "bits of temp is" << getbits_b(temp) << endl;
cpy_b(result, temp);
return FLAG_OK;
}
//模乘
void modmul(BN a, BN b, BN n, BN & result)
{
//cout << "line 369 a位数为 " << getbits_b(a) << endl;
//cout << "line 369 b位数为 " << getbits_b(b) << endl;
//cout << "line 369 n位数为 " << getbits_b(n) << endl;
memset(result, 0, sizeof(result));
BN a_t = { 0 }, b_t = { 0 };
BND temp = { 0 };
if (cmp_b(a, ZERO_BN) == 0 || cmp_b(b, ZERO_BN) == 0)
{
cmp_b(result, ZERO_BN);
return;
}
cpy_b(a_t, a);
cpy_b(b_t, b);
mul(a_t, b_t, temp);
//cout << "line 384 乘积位数为 " << getbits_b(temp) << endl;
//cout << "lint652?问题在653!!!" << endl;
modn_b(temp, n, result);
}
//蒙哥马利约简,result=DT*R' (mod N )
void mont_redc(BN DT, BN N, BN Np, BN R,BN & result)
{
BN temp1 = { 0 };
BND temp2 = { 0 };
BN m = { 0 }, t = { 0 };
unsigned int R_bits = getbits_b(R);//模除R就是保留这么多位
int Bits = (R_bits + 31) / 32;//换算成大的位数
int remain = R_bits - 32 * (Bits-1);//最高的“位”的第[remain-1]位为1,原封不动保留[remain-2]..[0]位就可以
//if(remain>1) 保留R[0];否则R[0]-1,保留所有R[0]-1"位"
//对于R[Bits],保留R[Bits][remain-2]..R[Bits][0]
//if (Bits>1) 保留R[Bits-1]..R[1]
//进行temp=T%R
if (getbits_b(DT) >= R_bits)//只有多的时候才要取余,R是0100,T是1101也要取余,不如构造一个,100异或101就可以了,但是不能有最高位
{
for (unsigned int i = 1; i <R[0]; i++)//全保留,都是0嘛
{
temp1[i] = DT[i];
}
if (remain >1)//最高位至少是10,remain=2,可以保留1位
{
temp1[R[0]] = (DT[R[0]] & (uint32_t)(1U<<(remain-1))-1U );
temp1[0] = R[0];
}
else {
temp1[0] = R[0]-1;
}
}
else {
cpy_b(temp1, DT);
}
//(T%R)*N',进行乘N'的操作,可能会超过1024位
mul(temp1, Np, temp2);
//m=temp1%R
if (getbits_b(temp2) >= R_bits)//只有多的时候才要取余,R是0100,T是1101也要取余,不如构造一个,100异或101就可以了,但是不能有最高位
{
for (unsigned int i = 1; i < R[0]; i++)//全保留,都是0嘛
{
m[i] = temp2[i];
}
if (remain > 1)//最高位至少是10,remain=2,可以保留1位
{
m[R[0]] = (temp2[R[0]] & (uint32_t)(1U << (remain - 1)) - 1U);
m[0] = R[0];
}
else {
m[0] = R[0] - 1;
}
}
else {
cpy_b(m, temp2);
}
memset(temp2, 0, sizeof(temp2));
mul(m, N, temp2);
add(DT, temp2, temp2);
//cout << "t*R= " << bn2str(temp2) <<endl;
for (int i = getbits_b(R)-1;i>0;i--)
{
shr_b(temp2);
}
//cout << "t= " << bn2str(temp2) << endl << endl;
cpy_b(t, temp2);
if(cmp_b(t, N) >= 0)
{
sub(t, N, t);//t=t-N
}
cpy_b(result,t);
}
//蒙哥马利算法的模乘 result=x*y (mod n)
void mont_modmul(BN x, BN y, BN N ,BN & result)
{
BND temp1 = { 0 }, temp2 = { 0 }, temp3 = { 0 };//可能会超过1024位!肯定不能只有1024位
BN Xp = { 0 }, Yp = { 0 }, Zp = { 0 };
int m = getbits_b(N);//模幂里基本上不会出现要模偶数,尤其加解密不太可能出现,认为是+1,R=2^m一定大于n
BN RRN = { 0 };//R*R (mod n)
BN R = { 0 }, Rp = { 0 }, Np = { 0 };
int Bits = (m + 31) / 32;//换算成大的位数
int remain = m - 32 * (Bits - 1);//最高的“位”的第[remain-1]位为1,原封不动保留[remain-2]..[0]位就可以
R[0] = Bits;
R[Bits] = (uint32_t)(1U <<remain);
//inv_b(R, N, Rp);
sub(R, N, temp1);//temp1=-N
inv_b(temp1, R, Np);
modmul(R, R, N, RRN);
//cout << "R= " << bn2str(R) << endl;
//cout << "N= " << bn2str(N) << endl;
//cout << "temp1= " << bn2str(temp1)<< endl;
//cout << "Np= " << bn2str(Np) << endl<<endl;
mul(x, RRN, temp1);
mul(y, RRN, temp2);
mont_redc(temp1, N, Np, R, Xp);
//cout << "Xp= " << bn2str(Xp) << endl << endl;
mont_redc(temp2, N, Np, R, Yp);
//cout << "Yp= " << bn2str(Yp) << endl << endl;
mul(Xp, Yp, temp3);
mont_redc(temp3, N, Np, R, Zp);
//cout << "Zp= " << bn2str(Zp) << endl << endl;
mont_redc(Zp, N, Np, R, result);
}
void mont_modmul(BN x, BN y, BN R,BN N,BN Np,BN RRN, BN & result)//用于模幂的模乘
{
BND temp1 = { 0 }, temp2 = { 0 }, temp3 = { 0 };//可能会超过1024位!肯定不能只有1024位
BN Xp = { 0 }, Yp = { 0 }, Zp = { 0 };
mul(x, RRN, temp1);
mul(y, RRN, temp2);
mont_redc(temp1, N, Np, R, Xp);
mont_redc(temp2, N, Np, R, Yp);
mul(Xp, Yp, temp3);
mont_redc(temp3, N, Np, R, Zp);
mont_redc(Zp, N, Np, R, result);
}
void mont_modexp(BN a, BN b, BN N, BN & result)//蒙哥马利模幂 a^b mod N
{
int m = getbits_b(N);//模幂里基本上不会出现要模偶数,尤其加解密不太可能出现,认为是+1,R=2^m一定大于n
BN RRN = { 0 };//R*R (mod n)
BN R = { 0 }, Rp = { 0 }, Np = { 0 };
BN a_t = { 1,1 }, b_t;//a=1;n做了二进制展开
BN temp1 = { 0 }, temp2 = { 0 };//计算作为result有个清零操作
BN Xp = { 0 }, Yp = { 0 };
int Bits = (m + 31) / 32;//换算成大的位数
int remain = m - 32 * (Bits - 1);//最高的“位”的第[remain-1]位为1,原封不动保留[remain-2]..[0]位就可以
R[0] = Bits;
R[Bits] = (uint32_t)(1U << (remain - 1));
sub(R, N, temp1);//temp1=-N
inv_b(temp1, R, Np);
modmul(R, R, N, RRN);
//b^n (mod m) --->a^b mod N
memset(result, 0, sizeof(result));
cpy_b(b_t, a);//b_t=b,初始化
uint32_t *nptr, *mnptr;
nptr = LSDPTR_B(b);
mnptr = MSDPTR_B(b);//!!!!!!!
char binform[33];//每个32bit的uint32转化为二进制即可,一次次取出来
int i = 0;
while (nptr <= mnptr)//没越界就都来做
{
memset(binform, 0, sizeof(binform));
_ultoa(*nptr, binform, 2);
i = strlen(binform) - 1;//到达最后一位
for (int j = 31; j >= 0; j--)//开始模平方
{
if (i >= 0)//正事儿,否则只是平方b取模
{
if (binform[i] == '1')
{
mont_modmul(a_t,b_t, R, N, Np, RRN, a_t);
}
i--;
}
mont_modmul(b_t, b_t, R, N, Np, RRN, b_t);
}
nptr++;
}
cpy_b(result, a_t);
RMLDZRS_B(result);
}
string bn2str(BN bignum)//大数转为字符串
{
if (DIGITS_B(bignum) == 0)//如果是0需要显示这个0
return string("0");
//char strbignum[265] = { 0 };//(1024+32)/4
//BN temp;
char strbignum[520] = { 0 };//(1024+32)/4
BND temp;
cpy_b(temp, bignum);
for (int i = DIGITS_B(temp), j = 0; i > 0; i = i - 1, j++)//第几位索引就是几!!!
{
sprintf(&strbignum[8 * j], "%08X", temp[i]);
}
//printf("bignum[i]=%08X", temp[DIGITS_B(temp)]);
// printf("bignum[i]=%08X", temp[DIGITS_B(temp)]);
//string rmzero(strbignum, strbignum + 7);
int zeros = 0;
for (int i = 0; i < 7; i++)
{
if (strbignum[i] == '0')
zeros++;
else break;
}
//cout << "zeros =" << zeros << endl;
//int index=rmzero.rfind("0");//去掉看得见的前导0
if (zeros >= 1)
{
//string finalstr(strbignum + zeros, &strbignum[264]);
string finalstr(strbignum + zeros, &strbignum[519]);
char * cstr = new char[finalstr.length() + 1];
std::strcpy(cstr, finalstr.c_str());
finalstr = string(cstr);//去掉NULL
//printf("length of bn2str is %d\n", finalstr.length());
delete[] cstr;
return finalstr;
}
else
{
string finalstr(strbignum);
char * cstr = new char[finalstr.length() + 1];
std::strcpy(cstr, finalstr.c_str());
finalstr = string(cstr);
//printf("length of bn2str is %d\n", finalstr.length());
delete[] cstr;
return finalstr;
return string(strbignum);
}
}
int str2bn(BN & bignum, string strbn)//字符串转换为大数
{
BN t = { 0 };
char *perbit = new char[9];
memset(perbit, 0, 9);
int digits = 0;
digits = (strbn.length() + 7) / 8;//位数
//cout << "digits=" << digits << endl;
int i = 0;
int pos = strbn.length() - 8;
for (i = 0; i < digits - 1; i++)//不能是digits-1!!!!!
{
//printf("i=%d\n", i);
//temp.insert(0, strbn, pos,8);
strbn.copy(perbit, 8, pos);
//printf("perbit=%s\n", perbit);
t[i + 1] = strtoul(perbit, NULL, 16);
//temp.erase();
pos = pos - 8;
memset(perbit, 0, 9);
}
//printf("i=%d\n", i);
if (strbn.length() % 8 == 0)
strbn.copy(perbit, 8, 0);
else
strbn.copy(perbit, strbn.length() % 8, 0);
//printf("perbit=%s\n", perbit);
t[i + 1] = strtoul(perbit, NULL, 16);
SETDIGITS_B(t, 32);
cpy_b(bignum, t);
delete[]perbit;
return FLAG_OK;
}
int readbn(BN &bignum, string filename)//从文本中读取大数
{
BN temp;
ifstream fin;
fin.open(filename);
if (fin.bad())
{
cout << "open" << filename << " failed!\n";
return FLAG_FILE_ERROR;
}
string strbignum;
fin >> strbignum;
//cout << "lengh is" << strbignum.length() << endl;
SETDIGITS_B(temp, 32);
str2bn(temp, strbignum);
cpy_b(bignum, temp);
//cout << "大数是 " << bn2str(temp)<< endl;
fin.close();
return FLAG_OK;
}
int writebn(string filename, BN bignum)//写入大数到文本
{
ofstream fout;
fout.open(filename);
if (fout.bad())
{
cout << "write to " << filename << " failed!\n";
return FLAG_FILE_ERROR;
}
string strbignum = bn2str(bignum);
int index = strbignum.find(" ");
//printf("index=%d\n", index);
if (index >= 0)
{
string finalstr;
finalstr.insert(0, strbignum, 0, index);
//strbignum[index + 1], &strbignum + strbignum.length() - 1);
fout << finalstr;
}
else
{
fout << strbignum;
}
fout.close();
return FLAG_OK;
}
//rem=a mod n
int modn_b(BN a, BN n, BN & rem)
{
//cout << "in line 527 modn_b a mod n =rem " << endl;
//cout << "line 527 a位数为 " << getbits_b(a) << endl;
//cout << "line 527 n位数为 " << getbits_b(n) << endl;
//cout << "line 527 a为 " << bn2str(a) << endl;
//cout << "line 527 n为 " << bn2str(n) << endl;
//cout << "OKD" << endl;
int flag = FLAG_OK;
BND temp;
BN result;
memset(rem, 0, BNSIZE);
memset(temp, 0, sizeof(temp));
memset(result, 0, sizeof(result));
if (cmp_b(a, ZERO_BN) == 0)
cpy_b(rem, ZERO_BN);
else
{
flag = div_b(a, n, temp, result);
//flag = remdiv_b(a, n, result);
cpy_b(rem, result);
//cout << "in modn_b:\na = " << bn2str(a) << endl;
//cout << "n = " << bn2str(n) << endl<<endl;
//cout << "rem = " << bn2str(result) << endl <<"end of modn"<< endl;
}
return flag;
}
//a= bq + rem
int remdiv_b(BN a, BN b, BN & rem)
{
memset(rem, 0, BNSIZE);
int flag = FLAG_OK;
BN q, temp;
memset(q, 0, BNSIZE);
memset(temp, 0, BNSIZE);
flag = div_b(a, b, q, temp);
//flag = div_b(a, b, q, temp);
cpy_b(rem, temp);
return flag;
}
//result=(a,b)
int gcd_b(BN a, BN b, BN & result)
{
memset(result, 0, BNSIZE);
int flag = FLAG_OK;
BN a_t, b_t, rn_1, rn;//存放两个余数用
memset(a_t, 0, BNSIZE);
memset(b_t, 0, BNSIZE);
memset(rn, 0, BNSIZE);
cpy_b(a_t, a);
cpy_b(b_t, b);
cpy_b(rn_1, b);//万一rn=0
if (DIGITS_B(a_t) == 0)
{
cpy_b(result, b_t);
return flag;
}
if (DIGITS_B(b_t) == 0)
{
cpy_b(result, a_t);
return flag;
}
remdiv_b(a_t, b_t, rn);
//cout << "rem= " << bn2str(rn) << endl;
while (DIGITS_B(rn) != 0)
{
cpy_b(a_t, b_t);
cpy_b(b_t, rn);
//memset(rn_1, 0, BNSIZE);
cpy_b(rn_1, rn);
remdiv_b(a_t, b_t, rn);
RMLDZRS_B(rn);
//cout<< endl;
/*cout << "a= " << bn2str(a_t) << endl;
cout << "b= " << bn2str(b_t)<<endl;
cout << "rem= " << bn2str(rn) << endl;*/
}
cpy_b(result, rn_1);
return flag;
}
//如果(a,n)=1,则有ax = 1 (mod n);否则异常没有逆元,返回0,显然逆元不可能为0
int inv_b(BN a, BN n, BN & x)
{
memset(x, 0, BNSIZE);
BN u = { 0 }, g = { 0 }, v1 = { 0 }, v3 = { 0 }, q = { 0 }, t3 = { 0 }, t1 = { 0 };
BN temp;//保存中间结果
gcd_b(a, n, g);
if (cmp_b(g, ONE_BN) != 0)//如果不互素,没有逆元
{
SETZERO_B(x);
return FLAG_NOINV;
}
memset(g, 0, sizeof(g));
//初始化
cpy_b(u, ONE_BN);//u=1
cpy_b(g, a);//g=a
SETZERO_B(v1);//v1=0
cpy_b(v3, n);
do
{
//div_b(g, v3, q, t3);
div_b(g, v3, q, t3);
modmul(q, v1, n, temp);
modsub_b(u, temp, n, t1);
cpy_b(u, v1);
cpy_b(g, v3);
cpy_b(v1, t1);
cpy_b(v3, t3);
/*cout << "g= " << bn2str(g) << endl;
cout << "v3= " << bn2str(v3) << endl;
cout << "q= " << bn2str(q) << endl;
cout << "t3= " << bn2str(t3) << endl;
cout << "u= " << bn2str(u) << endl;
cout << endl;*/
} while (cmp_b(v3, ZERO_BN) != 0);
cpy_b(x, u);
return FLAG_OK;
}
//另一个尽量用移位和加减法实现的求逆方法,理论上应该更快
//如果(a,n)=1,则有ax = 1 (mod n);否则异常没有逆元,返回0,显然逆元不可能为0
int new_inv(BN a, BN n, BN & x)
{
memset(x, 0, BNSIZE);
BN x1 = { 0 }, y1 = { 0 }, x2 = { 0 }, y2 = { 0 };
BN temp1 = { 0 }, temp2 = { 0 };//保存中间结果
if (cmp_b(a, ONE_BN) == 0)//如果是1,逆元就是1
{
SETONEBIT_B(x, 1U);
return FLAG_OK;
}
gcd_b(a, n, temp1);//求公因子,判断有没有逆元
if (cmp_b(temp1, ONE_BN) != 0)//如果不互素,没有逆元
{
SETZERO_B(x);
return FLAG_NOINV;
}
//有逆元,就开始求。方程x=y*a (mod n)有平凡解(x1,y1)=(a,1) (x2,y2)=(n,0)
//初始化x1=a,y1=1 x2=n,y2=0
cpy_b(x1, a);
cpy_b(x2, n);
cpy_b(y1, ONE_BN);
cpy_b(y2, ZERO_BN);
int i = 0;
do
{
while (uint32_t(x1[1] & 1U) == 0U)//C中需要注明强制类型,否则x1[1] & 1U居然不等于0U,也不等于0,在本系统默认是0UL
{
shr_b(x1);
if(uint32_t(y1[1] & 1U) == 0U)
shr_b(y1);
else {
add(y1, n, y1);
shr_b(y1);
}
}
while (uint32_t(x2[1] & 1U) == 0U)
{
shr_b(x2);
if (uint32_t(y2[1] & 1U) == 0U)
shr_b(y2);
else {
add(y2, n, y2);
shr_b(y2);
}
}
if ((x1[0] == 1 && x1[1] == 1) || (x2[0] == 1 && x2[1] == 1))
break;
if (x1[0]>=x2[0] && cmp_b(x1, x2) > 0)//x1>x2时,x1=x1-x2,y1=y1-y2
{
sub(x1, x2, x1);
if (y1[0] <= y2[0] && cmp_b(y1, y2) < 0)//如果y1<y2,y1=n-(y2-y1)
{
sub(y2, y1, temp1);
sub(n, temp1, y1);
}
else {
sub(y1, y2, y1);
}
}
else//x1<x2,x2=x2-x1,y2=y2-y1
{
sub(x2, x1, x2);
if (y2[0] <= y1[0] && cmp_b(y2, y1) < 0)//如果y2<y1,y1=n-(y1-y2)
{
sub(y1, y2, temp1);
sub(n, temp1, y2);
}
else {
sub(y2, y1, y2);
}
}
if ((x1[0] == 1 && x1[1] == 1) || (x2[0] == 1 && x2[1] == 1))
break;
} while (1);
//初始化
if (x1[0] == 1 && x1[1] == 1)
{
cpy_b(x, y1);
}
else {
cpy_b(x, y2);
}
return FLAG_OK;
}
//result= b^n (mod m)
int modexp_b(BN b, BN n, BN m, BN & result)
{
//cout << "a = " << bn2str(b) << endl;
//cout << "b = " << bn2str(n) << endl;
//cout << "m = " << bn2str(m) << endl;
memset(result, 0, sizeof(result));
BN a_t = { 1,1 }, b_t;//a=1;n做了二进制展开
BN temp1 = { 0 }, temp2 = { 0 };//计算作为result有个清零操作
cpy_b(b_t, b);//b_t=b,初始化
uint32_t *nptr, *mnptr;
nptr = LSDPTR_B(n);
mnptr = MSDPTR_B(n);//!!!!!!!
char binform[33];//每个32bit的uint32转化为二进制即可,一次次取出来
int i = 0;
//cout << "\na= " << bn2str(a_t) << " b= " << bn2str(b_t) << endl << endl;
while (nptr <= mnptr)//没越界就都来做
{
memset(binform, 0, sizeof(binform));
_ultoa(*nptr, binform, 2);
//printf("binform=%s\n", binform);
i = strlen(binform) - 1;//到达最后一位
for (int j = 31; j >= 0; j--)//开始模平方
{
//cout << "\nai-1= " << bn2str(a_t) << " bi-1= " << bn2str(b_t) << endl;
if (i >= 0)//正事儿,否则只是平方b取模
{
/*cout <<endl<<31-j<< ": ai-1= " << bn2str(a_t) << " bi-1= " << bn2str(b_t) << endl;*/
//printf("i=%d : %c\n", i, binform[i]);
if (binform[i] == '1')
{
modmul(a_t, b_t, m, temp1);//a=a*b mod m
//modmul(b_t, b_t, m, temp2);//b=b*b mod m
cpy_b(a_t, temp1);
//cpy_b(b_t, temp2);
//cout << "i=" << i << endl;
}
//printf("i=%d : %c\n", i, binform[i]);
//其它情况下不用动a_t
i--;
}
//cout << "j=" << j << endl;
/*cout << "b_t位数为 " << getbits_b(b_t) << endl;*/
modmul(b_t, b_t, m, temp2);//b=b*b mod m
/*cout << "问题出在modmul!!!line 1776" << endl;*/
cpy_b(b_t, temp2);
/*cout <<endl<<31-j<< ": ai= " << bn2str(a_t) << " bi= " << bn2str(b_t) << endl << endl;*/
}
nptr++;
}
cpy_b(result, a_t);
RMLDZRS_B(result);
/*cout << "int function result = " << bn2str(result) << endl;*/
return FLAG_OK;
}
//费马检测
int fermat_b(BN a)