-
Notifications
You must be signed in to change notification settings - Fork 97
/
ecverify.C
1673 lines (1413 loc) · 40.5 KB
/
ecverify.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
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/securerom/ecverify.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2016,2017 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
/** ECDSA verification on fixed curve/s (currently, on NIST P-521)
* The code below works for a compile-time constant curve, and requires
* a single bignumber pair (public key) to specify a key
*
* Knowledge of our environment allows the following simplifications:
* - modular operations are always mod P
* - there (multiple) unused bits in the most significant word of bignums
* Further assumptions:
* - bignumber indices fit 7 bits (8-bit counter sufficient for double bn's)
* Search for "P521", which flags curve dependencies.
*/
#define __STDC_FORMAT_MACROS 1 /* add 64-bit printf modifiers */
#include <stdio.h>
#include <string.h>
#include <stdint.h> /* uint_fast8_t, uintN_t */
#include "inttypes.H" /* PRIx64 used to format bn_t's */
#define NDEBUG
/**
* Define __LITTLE_ENDIAN or __BIG_ENDIAN for target.
*/
#if defined __BIG_ENDIAN__ || defined _BIG_ENDIAN
#define __BIG_ENDIAN
#undef __LITTLE_ENDIAN
#else
#undef __BIG_ENDIAN
#define __LITTLE_ENDIAN
#endif
#include <securerom/ecverify.H>
#define EC_PRIMEBITS 521 /* P521 */
#define EC_STACKTRACE 1 /* debug only; currently, glibc */
#define NO_EC_DOUBLE_XY 1 /* do not implement ec_double_xy */
typedef uint64_t bn_t;
typedef uint32_t hbn_t; /* half-bignumber */
typedef uint_fast8_t bnindex_t;
#define BN_FMT "%016" PRIx64 /* PRIx64 from inttypes.h */
#if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
#error "Please define target endianness (__LITTLE_ENDIAN or __BIG_ENDIAN)"
#endif
#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
#error "Please define one target endianness (__LITTLE_ENDIAN or __BIG_ENDIAN)"
#endif
#define BN_BITS (8*sizeof(bn_t))
#define HBN_BITS (8*sizeof(hbn_t))
#define EC_PRIMEBYTES ((EC_PRIMEBITS +7) /8)
#define BN_MAXBIT (((bn_t) 1) << (BN_BITS -1))
#define BITS2BN(bits) (((bits) +BN_BITS -1) / BN_BITS)
// we only deal with big numbers of fixed size
#define NWORDS BITS2BN( EC_PRIMEBITS )
#define BNBYTES (NWORDS*sizeof(bn_t))
#define BN_MSW(p) ((p)[0])
#define BN_LSW(p) ((p)[ NWORDS-1 ])
#define bn_is_odd(p) (1 & BN_LSW(p))
#ifndef BN_POWER64_CPY
#define BN_COPY(dst, src) memcpy((dst), (src), NWORDS*sizeof(bn_t))
#else
static void __attribute__((noinline)) BN_COPY (bn_t *dst, const bn_t *src)
{
size_t i;
for(i=0;i<NWORDS;i++)
{
*dst++ = *src++;
}
}
#endif
#ifdef BN_POWER64_DBG
static void __attribute__((noinline)) BN_DUMP (int i, bn_t *top)
{
asm volatile ("nop" : : "r" (i), "r" (top));
}
static void BN_EXIT (void)
{
asm volatile("b .Check_Stop");
}
#else
#define BN_DUMP(_i,_bn) ((void)0)
#define BN_EXIT() ((void)0)
#endif
#if defined(EC_DEBUG)
static void bn_print (const char *msg, const bn_t *m) ;
static void bn_dprint (const char *msg, const bn_t *m) ;
#else
#define bn_print(msg, m) ((void) 0)
#define bn_dprint(msg, m) ((void) 0)
#endif
#if !defined(NDEBUG)
#define EC_ASSERT(cond) assert(cond)
#define EC_DEVASSERT(cond) assert(cond)
#else
#define EC_ASSERT(cond) ((void) 0) // removed '((void) cond)' which still did the cond test
#define EC_DEVASSERT(cond) ((void) 0) // removed '((void) cond)' which still did the cond test
#endif
static bn_t bn_sub (bn_t *a, const bn_t *b) ;
static void bn_add (bn_t *a, const bn_t *b) ;
static void bn_mul (bn_t *r, const bn_t *a, const bn_t *b) ;
static void bn_modadd (bn_t *a, const bn_t *b) ;
static void bn_modsub (bn_t *a, const bn_t *b) ;
static int bn_cmp (const bn_t a[NWORDS], const bn_t b[NWORDS]) ;
// P521: a==-3, fixed curve parameter
static int ec_double (bn_t *x, bn_t *y, bn_t *z) ;
//============================================ prime-specific functions ====
// this section contains all prime/order-specific functionality
// if we ever need to support other curves, #ifdef their equivalent functions
//
// this code is limited to p = 2^521 -1 (P-521) and its order
#define BN_PRIME_MSW 0x1ff
#define BN_PRIME_MSW_MASK 0x1ff /* equal, as coincidence, for P521 */
#define BN_PRIME_MSW_BITS (EC_PRIMEBITS % BN_BITS)
typedef struct {
bn_t ec_prime[ NWORDS ];
bn_t ec_order[ NWORDS ];
bn_t prime_px[ NWORDS ];
bn_t prime_py[ NWORDS ];
bn_t ec_order_qn[ NWORDS ];
} consts_t;
extern "C"
const consts_t consts = {
//const bn_t ec_prime[ NWORDS ] =
{
BN_PRIME_MSW,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
},
//const bn_t ec_order[ NWORDS ] =
{
0x00000000000001ffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0xffffffffffffffffLL,
0xfffffffffffffffaLL,
0x51868783bf2f966bLL,
0x7fcc0148f709a5d0LL,
0x3bb5c9b8899c47aeLL,
0xbb6fb71e91386409LL,
},
//const bn_t prime_px[ NWORDS ] = {
{
0x00000000000000c6LL,
0x858e06b70404e9cdLL,
0x9e3ecb662395b442LL,
0x9c648139053fb521LL,
0xf828af606b4d3dbaLL,
0xa14b5e77efe75928LL,
0xfe1dc127a2ffa8deLL,
0x3348b3c1856a429bLL,
0xf97e7e31c2e5bd66LL,
},
//const bn_t prime_py[ NWORDS ] = {
{
0x0000000000000118LL,
0x39296a789a3bc004LL,
0x5c8a5fb42c7d1bd9LL,
0x98f54449579b4468LL,
0x17afbd17273e662cLL,
0x97ee72995ef42640LL,
0xc550b9013fad0761LL,
0x353c7086a272c240LL,
0x88be94769fd16650LL,
},
//-------------------------- mod mul by order (n) -------
// MS 521 bits of Q/N, fractional part
//
// static const bn_t ec_order_qn[ NWORDS ] =
{
0LL,
0LL,
0LL,
0LL,
0x0000000000000005LL,
0xae79787c40d06994LL,
0x8033feb708f65a2fLL,
0xc44a36477663b851LL,
0x449048e16ec79bf6LL,
}
} ;
inline const consts_t* __attribute__((pure)) consts_p()
{
#ifdef EMULATE_HW
return &consts;
#else
consts_t* result_consts_p;
asm volatile("li %0,(__toc_start)@l ### %0 := base+0x8000 \n\t" // because li does not work
"sub %0,2,%0 \n\t" // because subi does not work
"addi %0,%0,(consts-0x8000)@l" : "=r" (result_consts_p) );
return result_consts_p;
#endif
}
#define bn_ge_prime(val) (bn_cmp((val), consts_p()->ec_prime) >= 0)
#define bn_ge_order(val) (bn_cmp((val), consts_p()->ec_order) >= 0)
// P521: MSW has unused bits
#define BN_MSW_UNUSED_BITS (BN_BITS - BN_PRIME_MSW_BITS)
#define BN_MSW_UNUSED_BYTES ((BN_MSW_UNUSED_BITS +7) >>3)
#define BN_MSW_UNUSED_MASK ((((bn_t) 1) << BN_MSW_UNUSED_BITS) -1)
// not general-purpose shl: we only need to shift products (2*NWORDS)
// to two EC_PRIMEBITS, with BN_MSW_UNUSED_BITS
//
// acc contains MSW of lower half
//
static bn_t bn_shl (bn_t *a, bn_t acc)
{
bnindex_t i = NWORDS;
bn_t cf = 0;
EC_ASSERT(NULL != a);
EC_ASSERT(0 == a[0]);
a += NWORDS;
while (0<i--)
{
cf = *(--a);
*a <<= BN_MSW_UNUSED_BITS;
*a |= BN_MSW_UNUSED_MASK & (acc >> BN_PRIME_MSW_BITS);
acc = cf;
}
return cf;
}
//========================================================= diagnostics ====
#if defined(EC_DEBUG)
static void bn_printn (const char *msg, const bn_t *m, bnindex_t i)
{
EC_ASSERT(NULL != m);
if (NULL != msg)
{
printf("%s", msg);
}
while (0 < i--)
{
#if defined(EC_DEBUG_WORDS)
if (i<NWORDS-1)
{
printf(".");
}
#endif
printf(BN_FMT, *(m++));
}
printf("\n");
}
static void bn_print (const char *msg, const bn_t *m)
{
bn_printn(msg, m, NWORDS);
}
static void bn_dprint (const char *msg, const bn_t *m)
{
bn_printn(msg, m, NWORDS+NWORDS);
}
#endif /* defined(EC_DEBUG) */
//============================================== modular multiplication ====
// this section should be routed to hardware, when it becomes available
#ifndef BN_POWER64_CLR
#define bn_clear(n) memset((n), 0, BNBYTES)
#define bn_dclear(n) memset((n), 0, 2*BNBYTES)
#else
#define bn_clear(n) bn_clr((n), NWORDS)
#define bn_dclear(n) bn_clr((n), 2*NWORDS)
static void __attribute__((noinline)) bn_clr (bn_t *dst, size_t s)
{
size_t i;
dst--;
for(i=0;i<s;i++)
{
*(++dst) = 0LL;
}
}
#endif
#ifndef BN_POWER64_MUL
// high bn_t of a*b
// XXX use inline asm if possible; Intel code is enormous
// XXX alternatively, replace with hbn_t-by-hbn_t-blocked multiplication
//
static bn_t bn_dmul (bn_t a, bn_t b)
{
#ifdef EC_POWER64_ASM
bn_t t;
asm("mulhdu %0,%1,%2" : "=r" (t) : "r" (a), "r" (b) );
return t;
#else
hbn_t ah, al, bh, bl;
bn_t t;
al = a;
ah = (hbn_t) (a >> HBN_BITS);
bl = b;
bh = (hbn_t) (b >> HBN_BITS);
a = ((bn_t) ah) * bh; // collects high word
b = ((bn_t) al) * bl; // collects low word
t = ((bn_t) ah) * bl;
a += t >> HBN_BITS;
t <<= HBN_BITS;
if (b+t < t)
{
++a;
}
b += t;
t = ((bn_t) al) * bh;
a += t >> HBN_BITS;
t <<= HBN_BITS;
if (b+t < t)
{
++a;
}
return a;
#endif
}
/** multiply (a,NWORDS) by (b,NWORDS) into (r,2*NWORDS)
* we collect 2-word multiples, and carries across columns in two
* arrays:
*
* products
* a[0].b[0] a[1].b[0] a[2].b[0]
* a[0].b[1] a[1].b[1]
* a[0].b[2]
* carry in column to:
* carry[0] carry[1] carry[2]...
*
* delaying carry-collection simplifies multiply loop
*/
// XXX split to half-words' array; get rid of bn_dmul()
//
static void bn_mul (bn_t *r, const bn_t *a, const bn_t *b)
{
unsigned char cf[ NWORDS+NWORDS ]; /* carry collector */
bnindex_t i, j;
bn_t ph, pl; /* product high,low words */
EC_ASSERT(NULL != r);
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
bn_dclear(r);
memset(cf, 0, sizeof(cf));
for (j=0; j<NWORDS; ++j)
{
for (i=0; i<NWORDS; ++i)
{
ph = bn_dmul(a[i], b[j]);
pl = a[i] * b[j];
#ifdef EC_POWER64_ASM
asm("addc %0,%2,%4\n"
"addze %1,%3"
: "=r" (r[i+j]), "=r" (cf[i+j])
: "0" (r[i+j]), "1" (cf[i+j]), "r" (ph)
);
asm("addc %0,%2,%4\n"
"addze %1,%3"
: "=r" (r[i+j+1]), "=r" (cf[i+j+1])
: "0" (r[i+j+1]), "1" (cf[i+j+1]), "r" (pl)
);
#else
r[i+j] += ph;
if (r[i+j] < ph)
{
EC_ASSERT(i+j>0); // MSW can't carry to left
(cf[i+j-1])++;
}
r[i+j+1] += pl;
if (r[i+j+1] < pl)
{
(cf[i+j])++;
}
#endif
}
}
// propagate carries (LS to MS)
#ifdef EC_POWER64_ASM
i=NWORDS+NWORDS-2;
asm("addc %0,%1,%2"
: "=r" (r[i])
: "0" (r[i]), "r" (cf[i+1])
);
for ( ; 0<i; )
{
--i;
asm("adde %0,%1,%2"
: "=r" (r[i])
: "0" (r[i]), "r" (cf[i+1])
);
#else
for (i=NWORDS+NWORDS; 0<i; )
{
if (cf[--i])
{
r[i] += cf[i];
if (r[i] < cf[i])
{
EC_ASSERT(0 < i);
cf[i-1]++;
}
}
#endif
}
}
#else
static void bn_mul (bn_t *r, const bn_t *a, const bn_t *b)
{
bnindex_t i, j;
bn_t ph, pl, th, tb; /* product high,low words */
EC_ASSERT(NULL != r);
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
bn_dclear(r);
r += NWORDS;
b += NWORDS;
for (j=0; j<NWORDS; j++)
{
th = 0LL;
tb = *(--b);
r += NWORDS;
a += NWORDS;
for (i=0; i<NWORDS; i++)
{
asm("mulld %0,%1,%2" //pl = *(--a) * tb
: "=r" (pl)
: "r" (*(--a)), "r" (tb)
);
asm("mulhdu %0,%1,%2" //ph = *a * tb
: "=r" (ph)
: "r" (*a), "r" (tb)
);
asm("addc %1,%5,%4\n" //pl += *(--r)
"addze %2,%6\n" //ph += ca
"addc %0,%5,%7\n" //*r = pl + th
"addze %3,%6" //th = ph + ca
: "=r" (*r), "=r" (pl), "=r" (ph), "=r" (th)
: "0" (*(--r)), "1" (pl), "2" (ph), "3" (th)
);
}
*(--r) = th;
}
}
#endif
#ifdef EC_POWER64_ALG
#ifdef BN_POWER64_SQR
static void bn_sqr (bn_t *r, const bn_t *a)
{
bnindex_t i, j;
const bn_t *b; /* product high,low words */
bn_t *c, ph, pl, ta, t0, t1, t2; /* product high,low words */
EC_ASSERT(NULL != r);
EC_ASSERT(NULL != a);
bn_dclear(r);
r += 2*NWORDS;
a += NWORDS;
for (j=0; j<NWORDS-1; j++)
{
ta = *(--a);
c = r;
b = a;
asm("mulld %0,%2,%2\n" //pl = ta * ta
"mulhdu %1,%2,%2" //ph = ta * ta
: "=r" (pl), "=r" (ph)
: "r" (ta)
);
asm("addc %0,%2,%4\n" //*r = *(--r) + pl
"addze %1,%3" //t0 = ph + ca
: "=r" (*c), "=r" (t0)
: "0" (*(--c)), "r" (ph), "r" (pl)
);
t1 = 0L;
for (i=j+1; i<NWORDS; i++)
{
t2 = 0L;
asm("mulld %0,%1,%2" //pl = *(--b) * ta
: "=r" (pl)
: "r" (*(--b)), "r" (ta)
);
asm("mulhdu %0,%1,%2" //ph = *b * ta
: "=r" (ph)
: "r" (*b), "r" (ta)
);
asm("addc %1,%7,%7\n" //pl += pl
"adde %2,%8,%8\n" //ph += ph + ca
"addze %5,%11\n" //t2 += ca
"addc %1,%7,%9\n" //pl += t0
"adde %2,%8,%10\n" //ph += t1 + ca
"addze %5,%11\n" //t2 += ca
"addc %0,%6,%7\n" //*r = *(--r) + pl
"addze %3,%8\n" //t0 = ph + ca
"addze %4,%11" //t1 = t2 + ca
: "=r" (*c), "=r" (pl), "=r" (ph), "=r" (t0), "=r" (t1), "=r" (t2)
: "0" (*(--c)), "1" (pl), "2" (ph), "3" (t0), "4" (t1), "5" (t2)
);
}
asm("addc %0,%2,%4\n" //*r = *(--r) + t0
"addze %1,%3" //t1 += ca
: "=r" (*c), "=r" (t1)
: "0" (*(--c)), "1" (t1), "r" (t0)
);
*(--c) = t1;
r -= 2;
}
ta = *(--a);
asm("mulld %0,%2,%2\n" //pl = ta * ta
"mulhdu %1,%2,%2" //ph = ta * ta
: "=r" (pl), "=r" (ph)
: "r" (ta)
);
asm("addc %0,%2,%4\n" //*r = *(--r) + pl
"addze %1,%3" //ph += ca
: "=r" (*r), "=r" (ph)
: "0" (*(--r)), "1" (ph), "r" (pl)
);
*(--r) += ph;
}
#endif
#endif
//---------------- mod mul by generator prime (p) -------
// we only need to reduce with two moduluses, ec_prime or ec_order
// ec_prime has special form
//
// multiply to (prod,2*NWORDS), then reduce
// we use specific primes, with specific (faster) mod reductions
// a is double-length bignumber, i.e., 2*NWORDS
// always produced by a modular product, i.e., <=2*EC_PRIMEBITS total
//
// P521: specific form
// destroys LS bignumber of (a,2*NWORDS)
//
#ifndef EC_POWER64_RED
static void bn_modred_p521 (bn_t *r, bn_t *a)
{
bn_t *al;
bn_t *rc = r;
EC_ASSERT(NULL != r);
EC_ASSERT(NULL != a);
EC_ASSERT((const bn_t *) r != a);
al = a+NWORDS;
// P521: product is 1042 bits, MSW of double-width bignum always 0
//
EC_ASSERT(0 == a[0]);
BN_COPY(rc, a);
bn_shl(rc, *al);
*al &= BN_PRIME_MSW_MASK;
if (bn_cmp(rc, consts_p()->ec_prime) >= 0)
{
bn_sub(rc, consts_p()->ec_prime); // XXX can this happen? (mod-based input)
}
if (bn_cmp(al, consts_p()->ec_prime) >= 0)
bn_sub(al, consts_p()->ec_prime);
{
EC_ASSERT(!bn_ge_prime(al)); // al must have bitlen <= ec_prime
}
bn_add(rc, al);
if (bn_cmp(rc, consts_p()->ec_prime) >= 0)
{
bn_sub(rc, consts_p()->ec_prime);
}
}
#else
#ifdef BN_POWER64_SQR
static void __attribute__((noinline)) bn_modred_fast (bn_t *r, bn_t *a)
#else
static void bn_modred_fast (bn_t *r, bn_t *a)
#endif
{
bn_t *ah = a + NWORDS;
bn_t *al = a + 2*NWORDS;
bn_t t0 = (*(a+1) >> 18) + (*ah >> 9);
bn_t t1, t2, t3=0;
size_t i;
r += NWORDS;
for (i=0; i<NWORDS-2; i++) {
t1 = *(--ah) << 55;
t2 = *ah >> 9;
asm("addc %3,%7,%5\n" //t3 = *(--al) + t0;
"addze %2,%6\n" //t2 += ca;
"addc %0,%4,%8\n" //*(--r) = t3 + t1;
"addze %1,%6" //t0 = t2 + ca;
: "=r" (*(--r)), "=r" (t0), "=r" (t2), "=r" (t3)
: "3" (t3), "1" (t0), "2" (t2), "r" (*(--al)), "r" (t1)
);
}
t1 = *(--ah) << 55;
t2 = (*ah >> 9)&BN_PRIME_MSW_MASK;
asm("addc %3,%7,%5\n" //t3 = *(--al) + t0;
"addze %2,%6\n" //t2 += ca;
"addc %0,%4,%8\n" //*(--r) = t3 + t1;
"addze %1,%6" //t0 = t2 + ca;
: "=r" (*(--r)), "=r" (t0), "=r" (t2), "=r" (t3)
: "3" (t3), "1" (t0), "2" (t2), "r" (*(--al)), "r" (t1)
);
*(--r) = (*(--al)&BN_PRIME_MSW_MASK) + t0;
}
static void __attribute__((noinline)) bn_modred_slow (bn_t *r)
{
size_t i;
if (*r > BN_PRIME_MSW_MASK)
{
bn_t t0 = *r >> 9;
*r &= BN_PRIME_MSW_MASK;
r += NWORDS;
asm("addc %0,%1,%2"
: "=r" (*r)
: "0" (*(--r)), "r" (t0)
);
for (i=0; i<NWORDS-1; i++)
{
asm("addze %0,%1"
: "=r" (*r)
: "0" (*(--r))
);
}
}
if (bn_ge_prime(r))
{
bn_sub(r, consts_p()->ec_prime);
}
}
#endif
static void bn_modmul_prime (bn_t *a, const bn_t *b)
{
bn_t prod[ NWORDS+NWORDS ];
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
bn_mul(prod, a, b);
#ifdef EC_POWER64_RED
bn_modred_fast(a, prod); // accepts upto 46 extra bits => outputs at most 1 extra bit (522)
#else
bn_modred_p521(a, prod);
#endif
}
#ifdef EC_POWER64_ALG
static void bn_modsqr_prime (bn_t *a)
{
#ifdef BN_POWER64_SQR
bn_t prod[ NWORDS+NWORDS ];
EC_ASSERT(NULL != a);
bn_sqr(prod, a);
#ifdef EC_POWER64_RED
bn_modred_fast(a, prod); // accepts upto 46 extra bits => outputs at most 1 extra bit (522)
#else
bn_modred_p521(a, prod);
#endif
#else
bn_modmul_prime(a, a);
#endif
}
#endif
// mod reduce 2*NWORDS to NWORDS through approximate division
//
// input (a,2*NWORDS) <= N^2 -2*N +1
//
// N = 2^521 -Q (Q is approx 2^260)
// A = AH * 2^521 + AL (AH < 2^251)
// A/N = (AH*R + AL)/N = AH + (AH*Q + AL) /N ~ AH + (AH*Q /N)
// AH*Q /N =~ AH* floor(Q/N)
//
// dividend may be two too low:
// 1. we neglect AL/N, which may add add one (AL<N)
// 2. we truncate the multiplication, possibly ignoring one carry from below
// so, keep subtracting N until result <N; up to twice is enough
//
// r,a must not overlap
//
static void bn_modred_p521_order (bn_t *r, const bn_t *a)
{
bn_t dbl[ NWORDS+NWORDS ];
EC_ASSERT(NULL != r);
EC_ASSERT(NULL != a);
EC_ASSERT((const bn_t *) r != a);
// XXX full overlap check
// P521: product is 1042 bits, MSW of double-width bignum always 0
//
EC_ASSERT(0 == a[0]);
BN_COPY(r, a);
bn_shl(r, a[NWORDS]);
bn_mul(dbl, r, consts_p()->ec_order_qn);
bn_shl(dbl, dbl[NWORDS]); // MS 521 bits of product
bn_add(r, dbl);
bn_mul(dbl, r, consts_p()->ec_order); // N * floor(A / N)
EC_ASSERT(bn_cmp(dbl, a) <= 0);
EC_ASSERT(bn_cmp(dbl+NWORDS, a+NWORDS) <= 0);
BN_COPY(r, a+NWORDS);
bn_sub(r, dbl+NWORDS); // A - (N * floor(A/N))
if (bn_cmp(r, consts_p()->ec_order) >= 0)
{
bn_sub(r, consts_p()->ec_order);
}
if (bn_cmp(r, consts_p()->ec_order) >= 0)
{
bn_sub(r, consts_p()->ec_order); // XXX can this still be 2+ over?
}
EC_ASSERT(bn_cmp(r, consts_p()->ec_order) < 0);
}
static void bn_modmul_order (bn_t *a, const bn_t *b)
{
bn_t prod[ NWORDS+NWORDS ];
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
bn_mul(prod, a, b);
bn_modred_p521_order(a, prod);
}
// negative,0,positive for a<b, a==b, a>b
//
#if defined(__BIG_ENDIAN) && !defined(BN_POWER64_CMP)
static int bn_cmp (const bn_t *a, const bn_t *b)
{
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
return memcmp(a, b, sizeof(bn_t)*NWORDS);
}
#else /* defined(__BIG_ENDIAN) */
static int __attribute__((noinline)) bn_cmp (const bn_t *a, const bn_t *b)
{
bnindex_t i;
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
for (i=0; i<NWORDS; ++i)
{
if (a[i] != b[i])
{
return 1 - ((a[i] < b[i]) <<1);
}
}
return 0;
}
#endif /* defined(__BIG_ENDIAN) */
//removed:
//static const bn_t bn_zero[ NWORDS ];
// mn: how many words to skip (least significant ones)
//
static int bn_is_zero (const bn_t *m, unsigned int mn)
{
EC_ASSERT(NULL != m);
EC_ASSERT(mn < NWORDS);
const unsigned char *p2 = (const unsigned char *) m;
size_t n=sizeof(bn_t)*(NWORDS-mn);
while (n-- > 0)
{
if (0 != *p2)
{
return !(0 - *p2);
}
p2 += 1;
}
return !0;
}
static void __attribute__((noinline)) bn_add (bn_t *a, const bn_t *b)
{
bn_t aw, cf = 0; /* aw: copy of current word to allow a==b */
bnindex_t i = NWORDS;
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
a += NWORDS-1;
b += NWORDS-1;
while (0 < i--)
{
aw = *a;
if (cf)
{
cf = (0 == ++aw);
}
aw += *b;
cf |= (aw < *(b--));
*(a--) = aw;
}
}
// a,b < prime
// never with order as base
//
static void bn_modadd (bn_t *a, const bn_t *b)
{
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
//EC_ASSERT(!bn_ge_prime(a));
//EC_ASSERT(!bn_ge_prime(b));
bn_add(a, b); // P521: can not generate carry (unused MSW bits)
// other curves need to handle this carry
#ifndef EC_POWER64_RED
if (bn_ge_prime(a))
{
bn_sub(a, consts_p()->ec_prime);
}
#endif
}
// never with order as base
static bn_t bn_sub (bn_t *a, const bn_t *b)
{
bnindex_t i = NWORDS;
bn_t bw, cf = 0;
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
a += NWORDS-1;
b += NWORDS-1;
while (0 < i--) {
if (cf)
{
cf = (0 == (*a)--);
}
bw = *b;
cf |= (*a < *(b--));
*(a--) -= bw;
}
return cf;
}
// never modular-subtracting with ec_order[], only with ec_prime[]
// therefore, implicit modulus
//
static void bn_modsub (bn_t *a, const bn_t *b)
{
EC_ASSERT(NULL != a);
EC_ASSERT(NULL != b);
EC_ASSERT(!bn_ge_prime(b));
if (bn_sub(a, b))
{
bn_add(a, consts_p()->ec_prime);
}
}
// only rn LS words are touched
//
static void bn_shl_n (bn_t r[NWORDS], unsigned int rn, unsigned int bits)
{
bn_t cf = 0, cfin;
EC_DEVASSERT(NULL != r);
EC_ASSERT(rn <= NWORDS);
r += NWORDS-rn;
if (bits >= BN_BITS) // unlikely, most modinv shift is <5 bits
{
cfin = bits / BN_BITS; // whole words
memmove(r, r+cfin, (NWORDS-cfin)*sizeof(bn_t));
#ifndef BN_POWER64_CLR
memset(r+NWORDS-cfin, 0, cfin*sizeof(bn_t));
#else
bn_clr(r+NWORDS-cfin, cfin);
#endif
bits %= BN_BITS;
}
if (bits)