-
Notifications
You must be signed in to change notification settings - Fork 2
/
sb_sw_lib.c
1971 lines (1617 loc) · 72.4 KB
/
sb_sw_lib.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
/*
* sb_sw_lib.c: operations on short Weierstrass elliptic curves
*
* This file is part of Sweet B, a safe, compact, embeddable elliptic curve
* cryptography library.
*
* Sweet B is provided under the terms of the included LICENSE file. All
* other rights are reserved.
*
* Copyright 2017 Wearable Inc.
*
*/
#include "sb_test.h"
#include "sb_fe.h"
#include "sb_sw_lib.h"
#include "sb_sw_curves.h"
#include "sb_hmac_drbg.h"
#include <stddef.h>
#include <string.h>
// Used for point addition and conjugate addition
#define C_X1(ct) (&(ct)->c[0])
#define C_Y1(ct) (&(ct)->c[1])
#define C_X2(ct) (&(ct)->c[2])
#define C_Y2(ct) (&(ct)->c[3])
#define C_T5(ct) (&(ct)->c[4])
#define C_T6(ct) (&(ct)->c[5])
#define C_T7(ct) (&(ct)->c[6])
#define C_T8(ct) (&(ct)->c[7])
// The scalar used for point multiplication
#define MULT_K(ct) (&(ct)->h[0])
// The initial Z value, and the current Z coordinate in multiplication-addition
#define MULT_Z(ct) (&(ct)->h[1])
// The point to be multiplied, for shared secret generation and signature
// verification
#define MULT_POINT(ct) (&(ct)->h[2]) // 2 and 3
// The message to be signed as a scalar
#define SIGN_MESSAGE(ct) (&(ct)->h[2])
// The private key used in signing as a scalar (K is the signature k)
#define SIGN_PRIVATE(ct) (&(ct)->h[3])
// The scalar to multiply the base point by in signature verification
#define MULT_ADD_KG(ct) (&(ct)->c[8])
// Stores P + G in signature verification
#define MULT_ADD_PG(ct) (&(ct)->c[9]) // 9 and 10
// The message to be verified as a scalar
#define VERIFY_MESSAGE(ct) (&(ct)->c[9])
// The two working components of the signature, R and S
#define VERIFY_QS(ct) (&(ct)->c[10])
#define VERIFY_QR(ct) (&(ct)->c[11])
// All multiplication in Sweet B takes place using Montgomery multiplication
// MM(x, y) = x * y * R^-1 mod M where R = 2^SB_FE_BITS
// This has the nice property that MM(x * R, y * R) = x * y * R
// which means that sequences of operations can be chained together
// The inner loop of the Montgomery ladder takes place with coordinates that have been
// pre-multiplied by R. Point addition involves no constants, only additions, subtractions,
// and multiplications (and squarings). As such, the factor of R in coordinates is maintained
// throughout: mont_mult(a * R, b * R) = (a * b) * R, a * R + b * R = (a + b) * R, etc.
// For simplicity, the factor R will be ignored in the following comments.
// Initial point doubling: compute 2P in Jacobian coordinates from P in
// affine coordinates.
// Algorithm 23 from Rivain 2011, modified slightly
// Input: P = (x2, y2) in affine coordinates
// Output: (x1, y1) = P', (x2, y2) = 2P in co-Z with t5 = Z = 2 * y2
// Cost: 6MM + 11A
static void sb_sw_point_initial_double(sb_sw_context_t c[static const 1],
const sb_sw_curve_t s[static const 1])
{
sb_fe_mod_double(C_T5(c), C_Y2(c), s->p); // t5 = Z
sb_fe_mont_square(C_Y1(c), C_X2(c), s->p); // t2 = x^2
sb_fe_mod_sub(C_Y1(c), C_Y1(c), s->minus_a_r_over_three,
s->p); // t2 = x^2 + a / 3
sb_fe_mod_double(C_X1(c), C_Y1(c), s->p); // t1 = 2 * (x^2 + a / 3)
sb_fe_mod_add(C_Y1(c), C_Y1(c), C_X1(c),
s->p); // t2 = (3 * x^2 + a) = B
sb_fe_mont_square(C_T6(c), C_Y2(c), s->p); // t6 = y^2
sb_fe_mod_double(C_Y2(c), C_T6(c), s->p); // t4 = 2 * y^2
sb_fe_mod_double(C_T6(c), C_Y2(c), s->p); // t6 = 4 * y^2
sb_fe_mont_mult(C_X1(c), C_X2(c), C_T6(c),
s->p); // t1 = 4 * x * y^2 = A
sb_fe_mont_square(C_X2(c), C_Y1(c),
s->p); // t3 = B^2
sb_fe_mod_sub(C_X2(c), C_X2(c), C_X1(c), s->p); // t2 = B^2 - A
sb_fe_mod_sub(C_X2(c), C_X2(c), C_X1(c),
s->p); // x2 = B^2 - 2 * A = X2
sb_fe_mod_sub(C_T6(c), C_X1(c), C_X2(c), s->p); // t6 = A - X2
sb_fe_mont_mult(C_T7(c), C_Y1(c), C_T6(c),
s->p); // t7 = B * (A - X2)
sb_fe_mont_square(C_Y1(c), C_Y2(c),
s->p); // t2 = (2 * y^2)^2 = 4 * y^4
sb_fe_mod_double(C_Y1(c), C_Y1(c), s->p); // Y1 = 8 * y^4 = Z^3 * y
sb_fe_mod_sub(C_Y2(c), C_T7(c), C_Y1(c),
s->p); // Y2 = B * (A - X2) - Y1
}
// Co-Z point addition with update:
// Input: P = (x1, y1), Q = (x2, y2) in co-Z, with x2 - x1 in t6
// Output: P + Q = (x3, y3) in (x1, y1), P = (x1', y1') in (x2, y2)
// B + C = t5 with Z' = Z * (x2 - x1)
// or: P = P + Q, Q = P'
// Uses: t5, t6, t7; leaves t8 unmodified (used by conjugate addition and Z recovery)
// Cost: 6MM + 6A
static void sb_sw_point_co_z_add_update_zup(sb_sw_context_t c[static const 1],
const sb_sw_curve_t s[static const 1])
{
sb_fe_mont_square(C_T5(c), C_T6(c),
s->p); // t5 = (x2 - x1)^2 = (Z' / Z)^2 = A
sb_fe_mont_mult(C_T6(c), C_X2(c), C_T5(c), s->p); // t6 = x2 * A = C
sb_fe_mont_mult(C_X2(c), C_X1(c), C_T5(c), s->p); // t3 = x1 * A = B = x1'
sb_fe_mod_sub(C_T7(c), C_Y2(c), C_Y1(c), s->p); // t7 = y2 - y1
sb_fe_mod_add(C_T5(c), C_X2(c), C_T6(c), s->p); // t5 = B + C
sb_fe_mod_sub(C_T6(c), C_T6(c), C_X2(c),
s->p); // t6 = C - B = (x2 - x1)^3 = (Z' / Z)^3
sb_fe_mont_mult(C_Y2(c), C_Y1(c), C_T6(c),
s->p); // y1' = y1 * (Z' / Z)^3 = E
sb_fe_mont_square(C_X1(c), C_T7(c), s->p); // t1 = (y2 - y1)^2 = D
sb_fe_mod_sub(C_X1(c), C_X1(c), C_T5(c), s->p); // x3 = D - B - C
sb_fe_mod_sub(C_T6(c), C_X2(c), C_X1(c), s->p); // t6 = B - x3
sb_fe_mont_mult(C_Y1(c), C_T7(c), C_T6(c),
s->p); // t4 = (y2 - y1) * (B - x3)
sb_fe_mod_sub(C_Y1(c), C_Y1(c), C_Y2(c),
s->p); // y3 = (y2 - y1) * (B - x3) - E
}
// Co-Z addition with update, with Z-update computation
// Sets t6 to x2 - x1 before calling sb_sw_point_co_z_add_update_zup
// Cost: 6MM + 7A
static inline void
sb_sw_point_co_z_add_update(sb_sw_context_t c[static const 1],
const sb_sw_curve_t s[static const 1])
{
sb_fe_mod_sub(C_T6(c), C_X2(c), C_X1(c), s->p); // t6 = x2 - x1 = Z' / Z
sb_sw_point_co_z_add_update_zup(c, s);
}
// Co-Z conjugate addition with update, with Z-update computation
// Input: P = (x1, y1), Q = (x2, y2) in co-Z, with x2 - x1 in t6
// Output: P + Q = (x3, y3) in (x1, y1), P - Q = in (x2, y2), P' in (t6, t7)
// with Z' = Z * (x2 - x1)
// or: P = P + Q, Q = P - Q
// Uses: t5, t6, t7, t8
// Cost: 8MM + 11A (6MM + 7A for addition-with-update + 2MM + 4A)
static void sb_sw_point_co_z_conj_add(sb_sw_context_t c[static const 1],
const sb_sw_curve_t s[static const 1])
{
sb_fe_mod_add(C_T8(c), C_Y1(c), C_Y2(c), s->p); // t8 = y1 + y2
sb_sw_point_co_z_add_update(c, s); // t5 = B + C
*C_T6(c) = *C_X2(c);
*C_T7(c) = *C_Y2(c);
sb_fe_mont_square(C_X2(c), C_T8(c), s->p); // t6 = (y1 + y2)^2 = F
sb_fe_mod_sub(C_X2(c), C_X2(c), C_T5(c), s->p); // t6 = F - (B + C) = x3'
sb_fe_mod_sub(C_T5(c), C_X2(c), C_T6(c), s->p); // t5 = x3' - B
sb_fe_mont_mult(C_Y2(c), C_T8(c), C_T5(c),
s->p); // t2 = (y2 + y1) * (x3' - B)
sb_fe_mod_sub(C_Y2(c), C_Y2(c), C_T7(c),
s->p); // y3' = (y2 + y1) * (x3' - B) - E
}
// Regularize the bit count of the scalar by adding CURVE_N or 2 * CURVE_N
// The resulting scalar will have P256_BITS + 1 bits, with the highest bit set
// This enables the Montgomery ladder to start at (1P, 2P) instead of (0P, 1P).
// The resulting scalar k is always >= N + R (where R is 2^256 mod N) and
// < 2N + R.
// To see how this works, consider an input scalar of R: the first addition
// produces N + (2^256 - N) = 2^256 and overflows; therefore the resulting
// scalar will be N + R, and this is the lowest scalar that produces
// overflow on the first addition. Now consider an input scalar of R - 1:
// the first addition produces N + (2^256 - N - 1) = 2^256 - 1 which does
// not overflow; hence a second addition is necessary. This is the largest
// scalar which requires two additions.
static void sb_sw_regularize_scalar(sb_fe_t scalar[static const 1],
sb_sw_context_t c[static const 1],
const sb_sw_curve_t s[static const 1])
{
const sb_word_t c_1 = sb_fe_add(C_T5(c), scalar, &s->n->p);
sb_fe_add(scalar, C_T5(c), &s->n->p);
sb_fe_ctswap(c_1, scalar, C_T5(c));
}
static void
sb_sw_point_mult(sb_sw_context_t m[static const 1],
const sb_fe_t point[static const 2],
const sb_sw_curve_t s[static const 1])
{
// Input scalars MUST always be checked for validity
// (k is reduced and ∉ {-2, -1, 0, 1} mod N).
sb_sw_regularize_scalar(MULT_K(m), m, s);
// Throughout the ladder, (x1, y1) is (X0 * R, Y0 * R)
// (x2, y2) is (X1 * R, Y1 * R)
// This enables montgomery multiplies to be used in the ladder without
// explicit multiplies by R^2 mod P
*C_X2(m) = point[0];
*C_Y2(m) = point[1];
sb_sw_point_initial_double(m, s);
// The following applies a Z update of iz * R^-1.
sb_fe_mont_square(C_T7(m), MULT_Z(m), s->p); // t7 = z^2
sb_fe_mont_mult(C_T6(m), MULT_Z(m), C_T7(m), s->p); // t6 = z^3
*C_T5(m) = *C_X1(m);
sb_fe_mont_mult(C_X1(m), C_T5(m), C_T7(m), s->p); // x z^2
*C_T5(m) = *C_Y1(m);
sb_fe_mont_mult(C_Y1(m), C_T5(m), C_T6(m), s->p); // y z^3
*C_T5(m) = *C_X2(m);
sb_fe_mont_mult(C_X2(m), C_T5(m), C_T7(m), s->p); // x z^2
*C_T5(m) = *C_Y2(m);
sb_fe_mont_mult(C_Y2(m), C_T5(m), C_T6(m), s->p); // y z^3
// (x1 * R^-1, y1 * R^-1) = R0, (x2 * R^-1, y2 * R^-1) = R1
// R1 - R0 = P' for some Z
// To show that the ladder is complete for scalars ∉ {-2, -1, 0, 1}, let P = p * G
// R1 = 2 * p * G
// R0 = p * G
// It is easy to see that in a prime-order group, neither R1 nor R0 is
// the point at infinity at the beginning of the algorithm assuming nonzero p.
// In other words, every point on the curve is a generator.
// Through the ladder, at the end of each ladder step, we have:
// R0 = k[256..i] * P
// R1 = R0 + P
// where k[256..i] is the 256th through i_th bit of `k` inclusive
// The beginning of the loop is the end of the first ladder step (i = 256).
// Each ladder step computes the sum of R0 and R1, and one point doubling.
// The point doubling formula does not have exceptional cases, so we must
// consider point additions by zero and inadvertent point doublings.
// (Additions of -P and P would produce zero, which reduces to the case
// of addition by zero.) Point doublings do not occur simply because R0 +
// (R0 + P) is never a doubling operation.
// R0 = k[256..i] * P is the point at infinity if k[256..i] is zero.
// k[256] is 1 and N is 256 bits long. Therefore, k[256..i] is nonzero
// and less than N for all i > 1.
// It remains to consider the case of k[256..1] = N and
// k[256..0] = 2N. If k[256..1] is N, then k[256..0] is 2N or 2N + 1.
// Because the original input scalar was reduced, this only occurs with
// an input scalar of 0 or 1.
// R1 = (k[256..i] + 1) * P is zero if k[256..i] + 1 is zero.
// N is 256 bits long. For i > 1, k[256..i] is at most 255 bits long and therefore
// less than N - 1. It remains to consider k[256..1] = N - 1 and k[256..0] = 2N - 1.
// If k[256..1] is N - 1, then k[256..0] is 2N - 2 or 2N - 1.
// Because the input scalar was reduced, this only occurs with an input
// scalar of -2 or -1.
// The following intermediaries are generated:
// (2 * k[256..i] + 1) * P, P, and -P
// Because the order of the group is prime, it is easy to see that
// k[256..i] * P = 0 iff k[256..i] is 0 for nonzero p.
// What about (2 * k[256..i] + 1) * P?
// 2 * k[256..i] + 1 must be zero.
// For i > 2, 2 * k[256..i] is at most 255 bits long and thus
// less than N - 1. It remains to consider 2 * k[256..2] = N - 1,
// 2 * k[256..1] = N - 1, and 2 * k[256..0] = N - 1.
// If 2 * k[256..2] = N - 1, then k[256..2] = (N - 1) / 2.
// k[256..1] is then N - 1 or N, and k[256..0] is 2N - 2, 2N - 1, N, or N + 1.
// Thus, this occurs only if k ∈ { -2, -1, 0, 1 }.
// If 2 * k[256..1] = N - 1, then k[256..1] is (N - 1) / 2.
// k[256..0] is then N - 1 or N, which only occurs if k ∈ { -1, 0 }.
// Thus, for reduced inputs ∉ {-2, -1, 0, 1} the Montgomery ladder
// is non-exceptional for our short Weierstrass curves.
// 14MM + 18A per bit
// c.f. Table 1 in Rivain 2011 showing 9M + 5S + 18A
sb_word_t swap = 0;
for (size_t i = SB_FE_BITS - 1; i > 0; i--) {
const sb_word_t b = sb_fe_test_bit(MULT_K(m), i);
// if swap is 0: (x2, y2) = R0; (x1, y1) = R1
// if swap is 1: (x2, y2) = R1; (x1, y1) = R0
// swap iff bit is set:
// (x1, y1) = R_b; (x2, y2) = R_{1-b}
swap ^= b;
sb_fe_ctswap(swap, C_X1(m), C_X2(m));
sb_fe_ctswap(swap, C_Y1(m), C_Y2(m));
swap = b;
// our scalar 'k' is a 257-bit integer
// R0 = k[256..(i+1)] * P
// at the beginning of the loop, when i is 255:
// R0 = k[256..256] * P = 1 * P
// R1 = R0 + P = (k[256..(i+1)] + 1) * P
// When k[i] is 0:
// (x1, y1) = k[256..(i+1)] * P
// (x2, y2) = (k[256..(i+1)] + 1) * P
// When k[i] is 1:
// (x1, y1) = (k[256..(i+1)] + 1) * P
// (x2, y2) = k[256..(i+1)] * P
// R_b = R_b + R_{1-b}; R_{1-b} = R_{b} - R{1-b}
sb_sw_point_co_z_conj_add(m, s); // 6MM + 7A
// (x1, y1) = (2 * k[256..(i+1)] + 1 ) * P
// if k[i] is 0:
// (x2, y2) = -1 * P
// if k[i] is 1:
// (x2, y2) = 1 * P
// R_b = R_b + R_{1-b}; R_{1-b} = R_b'
sb_sw_point_co_z_add_update(m, s); // 8MM + 11A
// if k[i] is 0:
// (x1, y1) is 2 * k[256..(i+1)] * P = k[256..i] * P
// (x2, y2) is (2 * k[256..(i+1)] + 1 ) * P = (k[256..i] + 1) * P
// if k[i] is 1:
// (x1, y1) is (2 * k[256..(i+1)] + 2) * P = (k[256..i] + 1) * P
// (x2, y2) is (2 * k[256..(i+1)] + 1 ) * P = k[256..i] * P
// R_swap is k[256..i] * P
// R_!swap is (k[256..i] + 1) * P
}
const sb_word_t b = sb_fe_test_bit(MULT_K(m), 0);
// (x1, y1) = R0; (x2, y2) = R1
// swap iff bit is set:
swap ^= b;
sb_fe_ctswap(swap, C_X1(m), C_X2(m));
sb_fe_ctswap(swap, C_Y1(m), C_Y2(m));
// (x1, y1) = R_b; (x2, y2) = R_{1-b}
// here the logical meaning of the registers swaps!
sb_sw_point_co_z_conj_add(m, s);
// (x1, y1) = R_{1-b}, (x2, y2) = R_b
// if b is 1, swap the registers
sb_fe_ctswap(b, C_X1(m), C_X2(m));
sb_fe_ctswap(b, C_Y1(m), C_Y2(m));
// (x1, y1) = R1; (x2, y2) = R0
// Compute final Z^-1
sb_fe_mod_sub(C_T8(m), C_X1(m), C_X2(m), s->p); // X1 - X0
// if b is 1, swap the registers back
sb_fe_ctswap(b, C_X1(m), C_X2(m));
sb_fe_ctswap(b, C_Y1(m), C_Y2(m));
// (x1, y1) = R_{1-b}, (x2, y2) = R_b
sb_fe_mont_mult(C_T5(m), C_T8(m), C_Y2(m), s->p);
// t5 = Y_b * (X_1 - X_0)
sb_fe_mont_mult(C_T8(m), C_T5(m), &point[0], s->p);
// t8 = t5 * x_P = x_P * Y_b * (X_1 - X_0)
sb_fe_mod_inv_r(C_T8(m), C_T5(m), C_T6(m), s->p);
// t8 = 1 / (x_P * Y_b * (X_1 - X_0))
sb_fe_mont_mult(C_T5(m), C_T8(m), &point[1], s->p);
// t5 = yP / (x_P * Y_b * (X_1 - X_0))
sb_fe_mont_mult(C_T8(m), C_T5(m), C_X2(m), s->p);
// t8 = (X_b * y_P) / (x_P * Y_b * (X_1 - X_0))
// = final Z^-1
// (x1, y1) = R_{1-b}, (x2, y2) = R_b
sb_sw_point_co_z_add_update(m, s);
// the logical meaning of the registers is reversed
// (x1, y1) = R_b, (x2, y2) = R_{1-b}
// if b is 0, swap the registers
sb_fe_ctswap((b ^ (sb_word_t) 1), C_X1(m), C_X2(m));
sb_fe_ctswap((b ^ (sb_word_t) 1), C_Y1(m), C_Y2(m));
// (x1, y1) = R1; (x2, y2) = R0
// t8 = Z^-1 * R
// x2 = X0 * Z^2 * R
// y2 = Y0 * Z^3 * R
sb_fe_mont_square(C_T5(m), C_T8(m), s->p); // t5 = Z^-2 * R
sb_fe_mont_mult(C_T6(m), C_T5(m), C_T8(m), s->p); // t6 = Z^-3 * R
sb_fe_mont_mult(C_T7(m), C_T5(m), C_X2(m), s->p); // t7 = X0 * Z^-2 * R
sb_fe_mont_reduce(C_X1(m), C_T7(m), s->p); // Montgomery reduce to x1
sb_fe_mont_mult(C_T7(m), C_T6(m), C_Y2(m), s->p); // t7 = Y0 * Z^-3 * R
sb_fe_mont_reduce(C_Y1(m), C_T7(m), s->p); // Montgomery reduce to y1
sb_fe_sub(MULT_K(m), MULT_K(m), &s->n->p); // subtract off the overflow
sb_fe_mod_sub(MULT_K(m), MULT_K(m), &s->n->p,
s->n); // reduce to restore original scalar
}
// Multiplication-addition using Shamir's trick to produce k_1 * P + k_2 * Q
// sb_sw_point_mult_add_z_update computes the new Z and then performs co-Z
// point addition at a cost of 7MM + 7A
static void sb_sw_point_mult_add_z_update(sb_sw_context_t q[static const 1],
const sb_sw_curve_t s[static const 1])
{
sb_fe_mod_sub(C_T6(q), C_X2(q), C_X1(q), s->p); // t6 = x2 - x1 = Z' / Z
sb_fe_mont_mult(C_T5(q), C_T6(q), MULT_Z(q), s->p); // updated Z
*MULT_Z(q) = *C_T5(q);
sb_sw_point_co_z_add_update_zup(q, s);
}
// sb_sw_point_mult_add_apply_z applies a Z value to the selected point
// (H, P + H, G + H, or P + G + H) at a cost of 4MM
static void sb_sw_point_mult_add_apply_z(sb_sw_context_t q[static const 1],
const sb_sw_curve_t s[static const 1])
{
sb_fe_mont_square(C_T6(q), MULT_Z(q), s->p); // Z^2
sb_fe_mont_mult(C_T7(q), C_X2(q), C_T6(q), s->p);
*C_X2(q) = *C_T7(q);
sb_fe_mont_mult(C_T7(q), C_T6(q), MULT_Z(q), s->p); // Z^3
sb_fe_mont_mult(C_T6(q), C_Y2(q), C_T7(q), s->p);
*C_Y2(q) = *C_T6(q);
}
// sb_sw_point_mult_add_select selects the point to conjugate-add to the
// running total based on the bits of the given input scalars
static void sb_sw_point_mult_add_select(const sb_word_t bp, const sb_word_t bg,
sb_sw_context_t q[static const 1],
const sb_sw_curve_t s[static const 1])
{
// select a point S for conjugate addition with R
// if bp = 0 and bg = 0, select h
// if bp = 0 and bg = 1, select g + h
// if bp = 1 and bg = 0, select p + h
// if bp = 1 and bg = 1, select p + g + h
*C_X2(q) = s->h_r[0];
*C_Y2(q) = s->h_r[1];
*C_T5(q) = s->g_h_r[0];
*C_T6(q) = s->g_h_r[1];
sb_fe_ctswap(bg, C_X2(q), C_T5(q));
sb_fe_ctswap(bg, C_Y2(q), C_T6(q));
*C_T5(q) = MULT_POINT(q)[0];
*C_T6(q) = MULT_POINT(q)[1];
sb_fe_ctswap(bp, C_X2(q), C_T5(q));
sb_fe_ctswap(bp, C_Y2(q), C_T6(q));
*C_T5(q) = MULT_ADD_PG(q)[0];
*C_T6(q) = MULT_ADD_PG(q)[1];
sb_fe_ctswap(bp & bg, C_X2(q), C_T5(q));
sb_fe_ctswap(bp & bg, C_Y2(q), C_T6(q));
sb_sw_point_mult_add_apply_z(q, s);
}
// Signature verification uses a regular double-and-add algorithm with Shamir's
// trick for dual scalar-basepoint multiplication. Because adding O (the
// point at infinity) is an exceptional case in the standard formulae for
// point addition on short Weierstrass curves, each iteration adds an
// additional point H. Due to regularization of the input scalars,
// the algorithm starts with bit 256 in both k_p and k_g set, so the initial
// value is P + G + H, and at the end of the loop, (2^257 - 1) * H has been
// added, producing k_p * P + k_g * G + (2^257 - 1) * H. To correct for this,
// one could subtract the extra multiple of H at the end of the algorithm,
// but instead H has been chosen so that we can easily adjust k_g before the
// multiplication instead. Let H be (2^257 - 1)^-1 * G. Then compute:
// k_p * P + (k_g - 1) * G + (2^257 - 1) * H
// = k_p * P + (k_g - 1) * G + (2^257 - 1) * (2^257 - 1)^-1 * G
// = k_p * P + (k_g - 1) * G + G
// = k_p * P + k_g * G
// The algorithm is as follows:
// Given inputs k_p, P, k_g on some curve with base point G, and let H as
// above, with G + H precomputed
// 1. Compute P + H and P + G + H
// Let S(b_p, b_g) be: H if b_p == 0 && b_g == 0
// P + H if b_p == 1 && b_g == 0
// G + H if b_p == 0 && b_g == 1
// P + G + H if b_p == 1 && b_g == 1
// 2. k_g := k_g - 1
// 3. k_p := regularize(k_p)
// 4. k_g := regularize(k_g)
// 5. R := P + G + H
// 6. R := 2 * R
// 7. R := R + S(k_p_255, k_g_255)
// 8. for i from 254 downto 0:
// 8.1. R' := R + S(k_p_i, k_g_i)
// 8.2. R := R + R'
// 9. return R
// Produces kp * P + kg * G in (x1, y1) with Z * R in t5
static void sb_sw_point_mult_add_z(sb_sw_context_t q[static const 1],
const sb_sw_curve_t s[static const 1])
{
sb_fe_t* const kp = MULT_K(q);
sb_fe_t* const kg = MULT_ADD_KG(q);
// Subtract one from kg to account for the addition of (2^257 - 1) * H = G
sb_fe_sub(kg, kg, &SB_FE_ONE);
// Regularize the input scalars so the ladder starts at P + G + H
sb_sw_regularize_scalar(kp, q, s);
sb_sw_regularize_scalar(kg, q, s);
// multiply (x, y) of P by R
sb_fe_mont_mult(C_X1(q), &MULT_POINT(q)[0], &s->p->r2_mod_p, s->p);
MULT_POINT(q)[0] = *C_X1(q);
sb_fe_mont_mult(C_Y1(q), &MULT_POINT(q)[1], &s->p->r2_mod_p, s->p);
MULT_POINT(q)[1] = *C_Y1(q);
*C_X2(q) = s->h_r[0];
*C_Y2(q) = s->h_r[1];
// Save initial Z in T8 until it can be applied
*C_T8(q) = *MULT_Z(q);
// P and H are in affine coordinates, so our current Z is one (R in
// Montgomery domain)
*MULT_Z(q) = s->p->r_mod_p;
// (x1, x2) = P + H; (x2, y2) = P'
sb_sw_point_mult_add_z_update(q, s);
// Apply Z to G before co-Z addition of (P + H) and G
*C_X2(q) = s->g_r[0];
*C_Y2(q) = s->g_r[1];
sb_sw_point_mult_add_apply_z(q, s);
// (x1, x2) = P + G + H; (x2, y2) = P + H
sb_sw_point_mult_add_z_update(q, s);
// Invert Z and multiply so that P + H and P + G + H are in affine
// coordinates
*C_T5(q) = *MULT_Z(q); // t5 = Z * R
sb_fe_mod_inv_r(C_T5(q), C_T6(q), C_T7(q), s->p); // t5 = Z^-1 * R
sb_fe_mont_square(C_T6(q), C_T5(q), s->p); // t6 = Z^-2 * R
sb_fe_mont_mult(C_T7(q), C_T5(q), C_T6(q), s->p); // t7 = Z^-3 * R
// Apply Z to P + H
sb_fe_mont_mult(&MULT_POINT(q)[0], C_X2(q), C_T6(q), s->p);
sb_fe_mont_mult(&MULT_POINT(q)[1], C_Y2(q), C_T7(q), s->p);
// Apply Z to P + G + H
sb_fe_mont_mult(&MULT_ADD_PG(q)[0], C_X1(q), C_T6(q), s->p);
sb_fe_mont_mult(&MULT_ADD_PG(q)[1], C_Y1(q), C_T7(q), s->p);
// Computation begins with R = P + G + H due to regularization of the
// scalars. If bit 255 of kp and kpg are both 1, this would lead to a
// point doubling!
// Avoid the inadvertent doubling in the first bit, so that the regular
// ladder can start at 2 * (P + G + H) + S
*C_X2(q) = MULT_ADD_PG(q)[0];
*C_Y2(q) = MULT_ADD_PG(q)[1];
sb_sw_point_initial_double(q, s);
// 2 * (P + G + H) is now in (x2, y2); Z is in t5
// apply initial Z
*MULT_Z(q) = *C_T8(q);
sb_sw_point_mult_add_apply_z(q, s);
// z coordinate of (x2, y2) is now iz * t5
sb_fe_mont_mult(C_T6(q), MULT_Z(q), C_T5(q), s->p);
*MULT_Z(q) = *C_T6(q);
// move 2 * (P + G + H) to (x1, y1)
*C_X1(q) = *C_X2(q);
*C_Y1(q) = *C_Y2(q);
// 14MM + 14A + 4MM co-Z update = 18MM + 14A per bit
// Note that mixed Jacobian-affine doubling-addition can be done in 18MM.
// Assuming a Hamming weight of ~128 on both scalars and 8MM doubling, the
// expected performance of a conditional Jacobian double-and-add
// implementation would be (3/4 * 18MM) + (1/4 * 8MM) = 15.5MM/bit
// The algorithm used here is regular and reuses the existing co-Z addition
// operation. Conventional wisdom says that signature verification does
// not need to be constant time; however, it's not clear to me that this
// holds in all cases. For instance, an embedded system might leak
// information about its firmware version by the amount of time that it
// takes to verify a signature on boot.
// If you want a variable-time ladder, consider using Algorithms 14 and
// 17 from Rivain 2011 instead.
// Note that this algorithm may also not be SPA- or DPA-resistant, as H,
// P + H, G + H, and P + G + H are stored and used in affine coordinates,
// so the co-Z update of these variables might be detectable even with
// Z blinding. If this matters for signature verification in your
// application, please contact the authors for commercial support.
for (size_t i = SB_FE_BITS - 1; i < SB_FE_BITS; i--) {
const sb_word_t bp = sb_fe_test_bit(kp, i);
const sb_word_t bg = sb_fe_test_bit(kg, i);
sb_sw_point_mult_add_select(bp, bg, q, s);
// (x1, y1) = (R + S), (x2, y2) = R'
sb_sw_point_mult_add_z_update(q, s);
// The initial point has already been doubled
if (i < SB_FE_BITS - 1) {
// R := (R + S) + R = 2 * R + S
sb_sw_point_mult_add_z_update(q, s);
}
}
*C_T6(q) = *C_X1(q);
sb_fe_mont_reduce(C_X1(q), C_T6(q), s->p);
*C_T6(q) = *C_Y1(q);
sb_fe_mont_reduce(C_Y1(q), C_T6(q), s->p);
*C_T5(q) = *MULT_Z(q);
}
#ifdef SB_TEST
// Test that A * (B * G) + C * G = (A * B + C) * G
static _Bool test_sw_point_mult_add(const sb_fe_t* const ka,
const sb_fe_t* const kb,
const sb_fe_t* const kc,
const sb_sw_curve_t* const s)
{
sb_sw_context_t m;
memset(&m, 0, sizeof(m));
sb_fe_t kabc;
sb_fe_mont_mult(C_T5(&m), ka, kb, s->n);
sb_fe_mont_mult(&kabc, C_T5(&m), &s->n->r2_mod_p, s->n);
sb_fe_mod_add(&kabc, &kabc, kc, s->n);
*MULT_Z(&m) = SB_FE_ONE;
*MULT_K(&m) = *kb;
sb_sw_point_mult(&m, s->g_r, s);
sb_fe_t pb[] = { *C_X1(&m), *C_Y1(&m) };
*MULT_K(&m) = kabc;
sb_sw_point_mult(&m, s->g_r, s);
sb_fe_t pabc[] = { *C_X1(&m), *C_Y1(&m) };
sb_sw_context_t q;
memset(&q, 0, sizeof(q));
*MULT_Z(&q) = SB_FE_ONE;
MULT_POINT(&q)[0] = pb[0];
MULT_POINT(&q)[1] = pb[1];
*MULT_K(&q) = *ka;
*MULT_ADD_KG(&q) = *kc;
// A * (B * G) + C * G = (A * B + C) * G
sb_sw_point_mult_add_z(&q, s);
// put pabc in co-Z with the result
sb_fe_mont_square(C_T6(&q), C_T5(&q), s->p); // t6 = Z^2 * R
sb_fe_mont_mult(C_T7(&q), C_T6(&q), C_T5(&q), s->p); // t7 = Z^3 * R
sb_fe_mont_mult(C_X2(&q), C_T6(&q), &pabc[0], s->p); // x2 = x * Z^2
sb_fe_mont_mult(C_Y2(&q), C_T7(&q), &pabc[1], s->p); // y2 = y * Z^3
SB_TEST_ASSERT(
sb_fe_equal(C_X1(&q), C_X2(&q)) && sb_fe_equal(C_Y1(&q), C_Y2(&q)));
return 1;
}
static _Bool generate_fe(sb_fe_t* const fe, sb_hmac_drbg_state_t* const drbg)
{
sb_single_t s;
SB_TEST_ASSERT_SUCCESS(sb_hmac_drbg_generate(drbg, s.bytes, SB_ELEM_BYTES));
sb_fe_from_bytes(fe, s.bytes, SB_DATA_ENDIAN_BIG);
return 1;
}
_Bool sb_test_sw_point_mult_add(void)
{
sb_fe_t ka = SB_FE_CONST(0, 0, 0, 3);
sb_fe_t kb = SB_FE_CONST(0, 0, 0, 4);
sb_fe_t kc = SB_FE_CONST(0, 0, 0, 6);
SB_TEST_ASSERT(test_sw_point_mult_add(&ka, &kb, &kc, &SB_CURVE_P256));
SB_TEST_ASSERT(test_sw_point_mult_add(&ka, &kb, &kc, &SB_CURVE_SECP256K1));
return 1;
}
_Bool sb_test_sw_point_mult_add_rand(void)
{
sb_fe_t ka, kb, kc;
sb_hmac_drbg_state_t drbg;
memset(&drbg, 0, sizeof(drbg));
for (size_t i = 0; i < 64; i++) {
SB_TEST_ASSERT(generate_fe(&ka, &drbg));
SB_TEST_ASSERT(generate_fe(&kb, &drbg));
SB_TEST_ASSERT(generate_fe(&kc, &drbg));
SB_TEST_ASSERT(test_sw_point_mult_add(&ka, &kb, &kc, &SB_CURVE_P256));
SB_TEST_ASSERT(
test_sw_point_mult_add(&ka, &kb, &kc, &SB_CURVE_SECP256K1));
drbg.reseed_counter = 1;
}
return 1;
}
#endif
// Given a point context with x in *C_X1(c), computes
// y^2 = x^3 + a * x + b in *C_Y1(c)
static void sb_sw_curve_y2(sb_sw_context_t c[static const 1],
const sb_sw_curve_t s[static const 1])
{
sb_fe_mont_mult(C_T5(c), C_X1(c), &s->p->r2_mod_p, s->p); // t5 = x * R
sb_fe_mont_mult(C_T6(c), C_T5(c), C_X1(c), s->p); // t6 = x^2
sb_fe_mod_sub(C_T6(c), C_T6(c), &s->minus_a, s->p); // t6 = x^2 + a
sb_fe_mont_mult(C_Y1(c), C_T5(c), C_T6(c),
s->p); // y1 = (x^2 + a) * x * R * R^-1 = x^3 + a * x
sb_fe_mod_add(C_Y1(c), C_Y1(c), &s->b, s->p); // y1 = y^2 = x^3 + a * x + b
}
// Note that this assumes reduced input, not quasi-reduced input!
static _Bool
sb_sw_point_valid(const sb_fe_t point[static const 2],
sb_sw_context_t c[static const 1],
const sb_sw_curve_t s[static const 1])
{
// The point at infinity is not valid.
if (sb_fe_equal(&point[0], &SB_FE_ZERO) &&
sb_fe_equal(&point[1], &SB_FE_ZERO)) {
return 0;
}
// Unreduced points are not valid.
if (!(sb_fe_lt(&point[0], &s->p->p) &&
sb_fe_lt(&point[1], &s->p->p))) {
return 0;
}
// Verify y^2 = x^3 + ax + b
sb_fe_mont_square(C_T5(c), &point[1], s->p); // t5 = y^2 * R^-1
sb_fe_mont_mult(C_Y2(c), C_T5(c), &s->p->r2_mod_p, s->p); // y2 = y^2
*C_X1(c) = point[0];
sb_sw_curve_y2(c, s);
// clang HATES cast to _Bool
return sb_fe_equal(C_Y1(c), C_Y2(c)) != 0;
}
// A scalar is valid if it is reduced and ∉ {-2, -1, 0, 1} mod N
// Valid scalars are not modified, but invalid scalars may be reduced
static _Bool
sb_sw_scalar_valid(sb_fe_t* const k, const sb_sw_curve_t s[static const 1])
{
_Bool r = 1;
r &= sb_fe_lt(k, &s->n->p);
r &= !sb_fe_equal(k, &SB_FE_ONE); // 1
r &= !sb_fe_equal(k, &SB_FE_ZERO); // 0
sb_fe_sub(k, &s->n->p, k); // -k
r &= !sb_fe_equal(k, &SB_FE_ONE); // -1
sb_fe_mod_sub(k, k, &SB_FE_ONE, s->n); // (-k) - 1
r &= !sb_fe_equal(k, &SB_FE_ONE); // -2
sb_fe_mod_add(k, k, &SB_FE_ONE, s->n); // -k
sb_fe_sub(k, &s->n->p, k); // k
return r;
}
#ifdef SB_TEST
static _Bool test_h(const sb_sw_curve_t* s)
{
sb_sw_context_t m;
memset(&m, 0, sizeof(m));
*MULT_Z(&m) = SB_FE_ONE;
*MULT_K(&m) = (sb_fe_t) SB_FE_CONST(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF);
sb_fe_sub(MULT_K(&m), MULT_K(&m), &s->n->p);
sb_fe_sub(MULT_K(&m), MULT_K(&m), &s->n->p);
const sb_fe_t h_inv = *MULT_K(&m);
sb_fe_mont_mult(C_T5(&m), MULT_K(&m), &s->n->r2_mod_p,
s->n);
sb_fe_mod_inv_r(C_T5(&m), C_T6(&m), C_T7(&m), s->n);
sb_fe_mont_mult(MULT_K(&m), C_T5(&m), &SB_FE_ONE, s->n);
sb_sw_point_mult(&m, s->g_r, s);
sb_fe_mont_mult(&MULT_POINT(&m)[0], C_X1(&m), &s->p->r2_mod_p, s->p);
sb_fe_mont_mult(&MULT_POINT(&m)[1], C_Y1(&m), &s->p->r2_mod_p, s->p);
SB_TEST_ASSERT(sb_fe_equal(&MULT_POINT(&m)[0], &s->h_r[0]));
SB_TEST_ASSERT(sb_fe_equal(&MULT_POINT(&m)[1], &s->h_r[1]));
*MULT_K(&m) = h_inv;
sb_sw_point_mult(&m, MULT_POINT(&m), s);
sb_fe_mont_mult(C_X2(&m), &s->g_r[0], &SB_FE_ONE, s->p);
sb_fe_mont_mult(C_Y2(&m), &s->g_r[1], &SB_FE_ONE, s->p);
SB_TEST_ASSERT(sb_fe_equal(C_X1(&m), C_X2(&m)));
SB_TEST_ASSERT(sb_fe_equal(C_Y1(&m), C_Y2(&m)));
return 1;
}
_Bool sb_test_sw_h(void)
{
SB_TEST_ASSERT(test_h(&SB_CURVE_P256));
SB_TEST_ASSERT(test_h(&SB_CURVE_SECP256K1));
return 1;
}
// The following scalars cause exceptions in the ladder and are NOT valid.
_Bool sb_test_exceptions(void)
{
sb_sw_context_t m;
memset(&m, 0, sizeof(m));
*MULT_Z(&m) = SB_FE_ONE;
// Exceptions produce P, not zero, due to ZVA countermeasures
#define EX_ZERO(c) ((c).p->p)
#define TEST_EX() do { \
SB_TEST_ASSERT(!sb_sw_scalar_valid(MULT_K(&m), &SB_CURVE_P256)); \
sb_sw_point_mult(&m, SB_CURVE_P256.g_r, &SB_CURVE_P256); \
SB_TEST_ASSERT(sb_fe_equal(C_X1(&m), &EX_ZERO(SB_CURVE_P256)) && \
sb_fe_equal(C_Y1(&m), &EX_ZERO(SB_CURVE_P256))); \
} while (0)
// k = 1
*MULT_K(&m) = SB_FE_ONE;
TEST_EX();
// k = 0
*MULT_K(&m) = SB_CURVE_P256.n->p;
TEST_EX();
// k = -1
*MULT_K(&m) = SB_CURVE_P256.n->p;
sb_fe_sub(MULT_K(&m), MULT_K(&m), &SB_FE_ONE);
TEST_EX();
// k = -2
*MULT_K(&m) = SB_CURVE_P256.n->p;
sb_fe_sub(MULT_K(&m), MULT_K(&m), &SB_FE_ONE);
sb_fe_sub(MULT_K(&m), MULT_K(&m), &SB_FE_ONE);
TEST_EX();
return 1;
}
#endif
// Places (r, s) into (x2, y2)
static _Bool
sb_sw_sign(sb_sw_context_t g[static const 1],
const sb_sw_curve_t s[static const 1])
{
_Bool res = 1;
sb_sw_point_mult(g, s->g_r, s);
// This is used to quasi-reduce x1 modulo the curve N:
*C_X2(g) = *C_X1(g);
sb_fe_qr(C_X2(g), 0, s->n);
res &= !sb_fe_equal(C_X2(g), &s->n->p);
sb_fe_mont_mult(C_T7(g), MULT_K(g), &s->n->r2_mod_p, s->n); // t7 = k * R
sb_fe_mod_inv_r(C_T7(g), C_T5(g), C_T6(g), s->n); // t7 = k^-1 * R
sb_fe_mont_mult(C_T6(g), SIGN_PRIVATE(g), &s->n->r2_mod_p,
s->n); // t6 = d_A * R
sb_fe_mont_mult(C_T5(g), C_X2(g), C_T6(g), s->n); // t5 = r * d_A
sb_fe_mod_add(C_T5(g), C_T5(g), SIGN_MESSAGE(g), s->n); // t5 = z + r * d_A
sb_fe_mont_mult(C_Y2(g), C_T5(g), C_T7(g),
s->n); // y2 = k^-1 * R * (z + r * d_A) * R^-1 mod N
// mont_mul produces quasi-reduced output
res &= !sb_fe_equal(C_Y2(g), &s->n->p);
return res;
}
static _Bool sb_sw_verify(sb_sw_context_t v[static const 1],
const sb_sw_curve_t s[static const 1])
{
_Bool res = 1;
res &= sb_sw_scalar_valid(VERIFY_QR(v), s);
res &= sb_sw_scalar_valid(VERIFY_QS(v), s);
sb_fe_mont_mult(C_T5(v), VERIFY_QS(v), &s->n->r2_mod_p, s->n); // t5 = s * R
sb_fe_mod_inv_r(C_T5(v), C_T6(v), C_T7(v), s->n); // t5 = s^-1 * R
sb_fe_mont_mult(MULT_ADD_KG(v), VERIFY_MESSAGE(v), C_T5(v),
s->n); // k_G = m * s^-1
sb_fe_mont_mult(MULT_K(v), VERIFY_QR(v), C_T5(v), s->n); // k_P = r * s^-1
sb_sw_point_mult_add_z(v, s);
// This happens when p is some multiple of g that occurs within
// the ladder, such that additions inadvertently produce a point
// doubling. When that occurs, the private scalar that generated p is
// also obvious, so this is bad news. Don't do this.
res &= !(sb_fe_equal(C_X1(v), &s->p->p) & sb_fe_equal(C_Y1(v), &s->p->p));
_Bool ver = 0;
// qr ==? x mod N, but we don't have x, just x * z^2
// Given that qr is reduced, if it is >= P - N, then it can be used directly
// if it is < P - N, then we need to try to see if the original value was
// qr or qr + N
// Try directly first:
sb_fe_mont_square(C_T6(v), C_T5(v), s->p); // t6 = Z^2 * R
sb_fe_mont_mult(C_T7(v), VERIFY_QR(v), C_T6(v), s->p);
ver |= sb_fe_equal(C_T7(v), C_X1(v));
// If that didn't work, and qr < P - N, then we need to compare
// (qr + N) * z^2 against x * z^2
// Note that this code is probably never used because this situation occurs
// with very low probability (<2^-128)
sb_fe_mod_add(C_T5(v), VERIFY_QR(v), &s->n->p, s->p);
sb_fe_mont_mult(C_T7(v), VERIFY_QR(v), C_T6(v), s->p);
sb_fe_sub(C_T5(v), &s->p->p, &s->n->p); // t5 = P - N
ver |= (sb_fe_lt(VERIFY_QR(v), C_T5(v)) &
sb_fe_equal(C_T7(v), C_X1(v)));
return res & ver;
}
static sb_error_t sb_sw_curve_from_id(const sb_sw_curve_t** const s,
sb_sw_curve_id_t const curve)
{
switch (curve) {
#if SB_SW_P256_SUPPORT
case SB_SW_CURVE_P256: {
*s = &SB_CURVE_P256;
return 0;
}
#endif
#if SB_SW_SECP256K1_SUPPORT
case SB_SW_CURVE_SECP256K1: {
*s = &SB_CURVE_SECP256K1;
return 0;
}
#endif
#ifdef SB_TEST
case SB_SW_CURVE_INVALID:
break;
#endif
}
// Huh?
*s = NULL;
return SB_ERROR_CURVE_INVALID;
}
// a Z value is invalid if it is zero, since the only point with Z = 0 is the
// point at infinity. Note that this is not expected to ever occur!
static sb_error_t sb_sw_z_valid(const sb_fe_t z[static const 1],
const sb_sw_curve_t s[static const 1])