forked from ckormanyos/wide-integer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example013_ecdsa_sign_verify.cpp
974 lines (779 loc) · 47.4 KB
/
example013_ecdsa_sign_verify.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
///////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2023. //
// Distributed under the Boost Software License, //
// Version 1.0. (See accompanying file LICENSE_1_0.txt //
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
///////////////////////////////////////////////////////////////////
// This work uses (significantly) translated and modified parts
// of andreacorbellini/ecc
// see also: https://github.com/andreacorbellini/ecc
// and also: https://github.com/andreacorbellini/ecc/blob/master/scripts/ecdsa.py
// Full original andreacorbellini/ecc copyright information follows.
/*----------------------------------------------------------------------------
The MIT License (MIT)
Copyright (c) 2015 Andrea Corbellini
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------*/
// For algorithm description of ECDSA, please consult also:
// D. Hankerson, A. Menezes, S. Vanstone, "Guide to Elliptic
// Curve Cryptography", Springer 2004, Chapter 4, in particular
// Algorithm 4.24 (keygen on page 180), and Algorithms 4.29 and 4.30.
// Complete descriptions of sign/verify are featured on page 184.
// For another algorithm description of ECDSA,
// see also: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-5.pdf
// For algorithm description of SHA-2 HASH-256,
// see also: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
// The SHA-2 HASH-256 implementation has been taken (with slight modification)
// from: https://github.com/imahjoub/hash_sha256
#include <algorithm>
#include <cstdint>
#include <random>
#include <string>
#include <utility>
#include <vector>
#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>
namespace example013_ecdsa
{
#if defined(WIDE_INTEGER_NAMESPACE)
using WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::fill_unsafe;
#else
using ::math::wide_integer::detail::fill_unsafe;
#endif
class hash_sha256
{
public:
#if defined(WIDE_INTEGER_NAMESPACE)
using result_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::array_detail::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(32))>;
#else
using result_type = ::math::wide_integer::detail::array_detail::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(32))>;
#endif
// LCOV_EXCL_START
constexpr hash_sha256() = default;
constexpr hash_sha256(const hash_sha256&) = default;
constexpr hash_sha256(hash_sha256&&) noexcept = default;
~hash_sha256() = default;
constexpr auto operator=(hash_sha256&&) noexcept -> hash_sha256& = default;
constexpr auto operator=(const hash_sha256&) -> hash_sha256& = default;
// LCOV_EXCL_STOP
constexpr auto hash(const std::uint8_t* msg, const size_t length) -> result_type
{
init();
update(msg, length);
return finalize();
}
constexpr void init()
{
my_datalen = static_cast<std::uint32_t>(UINT8_C(0));
my_bitlen = static_cast<std::uint64_t>(UINT8_C(0));
transform_context[static_cast<std::size_t>(UINT8_C(0))] = static_cast<std::uint32_t>(UINT32_C(0x6A09E667));
transform_context[static_cast<std::size_t>(UINT8_C(1))] = static_cast<std::uint32_t>(UINT32_C(0xBB67AE85));
transform_context[static_cast<std::size_t>(UINT8_C(2))] = static_cast<std::uint32_t>(UINT32_C(0x3C6EF372));
transform_context[static_cast<std::size_t>(UINT8_C(3))] = static_cast<std::uint32_t>(UINT32_C(0xA54FF53A));
transform_context[static_cast<std::size_t>(UINT8_C(4))] = static_cast<std::uint32_t>(UINT32_C(0x510E527F));
transform_context[static_cast<std::size_t>(UINT8_C(5))] = static_cast<std::uint32_t>(UINT32_C(0x9B05688C));
transform_context[static_cast<std::size_t>(UINT8_C(6))] = static_cast<std::uint32_t>(UINT32_C(0x1F83D9AB));
transform_context[static_cast<std::size_t>(UINT8_C(7))] = static_cast<std::uint32_t>(UINT32_C(0x5BE0CD19));
}
constexpr void update(const std::uint8_t* msg, const size_t length)
{
for (auto i = static_cast<std::size_t>(UINT8_C(0)); i < length; ++i)
{
my_data[my_datalen] = msg[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-bounds-constant-array-index)
my_datalen++;
if(my_datalen == static_cast<std::uint32_t>(UINT8_C(64)))
{
// LCOV_EXCL_START
sha256_transform();
my_datalen = static_cast<std::uint32_t>(UINT8_C(0));
my_bitlen = static_cast<std::uint64_t>(my_bitlen + static_cast<std::uint_fast16_t>(UINT16_C(512)));
// LCOV_EXCL_STOP
}
}
}
constexpr auto finalize() -> result_type
{
result_type hash_result { };
auto hash_index = static_cast<std::size_t>(my_datalen);
my_data[hash_index] = static_cast<std::uint8_t>(UINT8_C(0x80)); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-bounds-constant-array-index)
++hash_index;
// Pad whatever data is left in the buffer.
if(my_datalen < static_cast<std::uint32_t>(UINT8_C(56U)))
{
fill_unsafe((my_data.begin() + hash_index), (my_data.begin() + static_cast<std::size_t>(UINT8_C(56))), static_cast<std::uint8_t>(UINT8_C(0)));
}
else
{
// LCOV_EXCL_START
fill_unsafe((my_data.begin() + hash_index), my_data.end(), static_cast<std::uint8_t>(UINT8_C(0)));
sha256_transform();
fill_unsafe(my_data.begin(), my_data.begin() + static_cast<std::size_t>(UINT8_C(56)), static_cast<std::uint8_t>(UINT8_C(0)));
// LCOV_EXCL_STOP
}
// Append to the padding the total message length (in bits) and subsequently transform.
my_bitlen =
static_cast<std::uint64_t>
(
my_bitlen
+ static_cast<std::uint64_t>
(
static_cast<std::uint64_t>(my_datalen) * static_cast<std::uint8_t>(UINT8_C(8))
)
);
my_data[static_cast<std::size_t>(UINT8_C(63))] = static_cast<std::uint8_t>(my_bitlen >> static_cast<unsigned>(UINT8_C( 0)));
my_data[static_cast<std::size_t>(UINT8_C(62))] = static_cast<std::uint8_t>(my_bitlen >> static_cast<unsigned>(UINT8_C( 8)));
my_data[static_cast<std::size_t>(UINT8_C(61))] = static_cast<std::uint8_t>(my_bitlen >> static_cast<unsigned>(UINT8_C(16)));
my_data[static_cast<std::size_t>(UINT8_C(60))] = static_cast<std::uint8_t>(my_bitlen >> static_cast<unsigned>(UINT8_C(24)));
my_data[static_cast<std::size_t>(UINT8_C(59))] = static_cast<std::uint8_t>(my_bitlen >> static_cast<unsigned>(UINT8_C(32)));
my_data[static_cast<std::size_t>(UINT8_C(58))] = static_cast<std::uint8_t>(my_bitlen >> static_cast<unsigned>(UINT8_C(40)));
my_data[static_cast<std::size_t>(UINT8_C(57))] = static_cast<std::uint8_t>(my_bitlen >> static_cast<unsigned>(UINT8_C(48)));
my_data[static_cast<std::size_t>(UINT8_C(56))] = static_cast<std::uint8_t>(my_bitlen >> static_cast<unsigned>(UINT8_C(56)));
sha256_transform();
// Since this implementation uses little endian byte ordering and SHA uses big endian,
// reverse all the bytes when copying the final transform_context to the output hash.
constexpr auto conversion_scale =
static_cast<std::size_t>
(
std::numeric_limits<typename transform_context_type::value_type>::digits
/ std::numeric_limits<std::uint8_t>::digits
);
for(auto output_index = static_cast<std::size_t>(UINT8_C(0));
#if defined(WIDE_INTEGER_NAMESPACE)
output_index < WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::array_detail::tuple_size<result_type>::value;
#else
output_index < ::math::wide_integer::detail::array_detail::tuple_size<result_type>::value;
#endif
++output_index)
{
const auto right_shift_amount =
static_cast<std::size_t>
(
static_cast<std::size_t>
(
static_cast<std::size_t>
(
static_cast<std::size_t>(conversion_scale - static_cast<std::size_t>(UINT8_C(1)))
- static_cast<std::size_t>(output_index % conversion_scale)
)
* static_cast<std::size_t>(UINT8_C(8))
)
);
hash_result[output_index] =
static_cast<std::uint8_t>
(
transform_context[(output_index / conversion_scale)] >> right_shift_amount
);
}
return hash_result;
}
private:
#if defined(WIDE_INTEGER_NAMESPACE)
using transform_context_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::array_detail::array<std::uint32_t, static_cast<std::size_t>(UINT8_C(8))>;
using data_array_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::array_detail::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(64))>;
#else
using transform_context_type = ::math::wide_integer::detail::array_detail::array<std::uint32_t, static_cast<std::size_t>(UINT8_C(8))>;
using data_array_type = ::math::wide_integer::detail::array_detail::array<std::uint8_t, static_cast<std::size_t>(UINT8_C(64))>;
#endif
std::uint32_t my_datalen { }; // NOLINT(readability-identifier-naming)
std::uint64_t my_bitlen { }; // NOLINT(readability-identifier-naming)
data_array_type my_data { }; // NOLINT(readability-identifier-naming)
transform_context_type transform_context { }; // NOLINT(readability-identifier-naming)
constexpr auto sha256_transform() -> void
{
#if defined(WIDE_INTEGER_NAMESPACE)
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::array_detail::array<std::uint32_t, static_cast<std::size_t>(UINT8_C(64))> m { };
#else
::math::wide_integer::detail::array_detail::array<std::uint32_t, static_cast<std::size_t>(UINT8_C(64))> m { };
#endif
for(auto i = static_cast<std::size_t>(UINT8_C(0)), j = static_cast<std::size_t>(UINT8_C(0));
i < static_cast<std::size_t>(UINT8_C(16));
++i, j = static_cast<std::size_t>(j + static_cast<std::size_t>(UINT8_C(4))))
{
m[i] = // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-bounds-constant-array-index)
static_cast<std::uint32_t>
(
static_cast<std::uint32_t>(static_cast<std::uint32_t>(my_data[j + static_cast<std::size_t>(UINT8_C(0))]) << static_cast<unsigned>(UINT8_C(24))) // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-bounds-constant-array-index)
| static_cast<std::uint32_t>(static_cast<std::uint32_t>(my_data[j + static_cast<std::size_t>(UINT8_C(1))]) << static_cast<unsigned>(UINT8_C(16))) // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-bounds-constant-array-index)
| static_cast<std::uint32_t>(static_cast<std::uint32_t>(my_data[j + static_cast<std::size_t>(UINT8_C(2))]) << static_cast<unsigned>(UINT8_C( 8))) // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-bounds-constant-array-index)
| static_cast<std::uint32_t>(static_cast<std::uint32_t>(my_data[j + static_cast<std::size_t>(UINT8_C(3))]) << static_cast<unsigned>(UINT8_C( 0))) // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-bounds-constant-array-index)
);
}
for(auto i = static_cast<std::size_t>(UINT8_C(16)) ; i < static_cast<std::size_t>(UINT8_C(64)); ++i)
{
m[i] = ssig1(m[i - static_cast<std::size_t>(UINT8_C(2))]) + m[i - static_cast<std::size_t>(UINT8_C(7))] + ssig0(m[i - static_cast<std::size_t>(UINT8_C(15))]) + m[i - static_cast<std::size_t>(UINT8_C(16))]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-bounds-constant-array-index)
}
#if defined(WIDE_INTEGER_NAMESPACE)
constexpr WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::array_detail::array<std::uint32_t, 64U> transform_constants =
#else
constexpr ::math::wide_integer::detail::array_detail::array<std::uint32_t, 64U> transform_constants =
#endif
{
static_cast<std::uint32_t>(UINT32_C(0x428A2F98)), static_cast<std::uint32_t>(UINT32_C(0x71374491)), static_cast<std::uint32_t>(UINT32_C(0xB5C0FBCF)), static_cast<std::uint32_t>(UINT32_C(0xE9B5DBA5)),
static_cast<std::uint32_t>(UINT32_C(0x3956C25B)), static_cast<std::uint32_t>(UINT32_C(0x59F111F1)), static_cast<std::uint32_t>(UINT32_C(0x923F82A4)), static_cast<std::uint32_t>(UINT32_C(0xAB1C5ED5)),
static_cast<std::uint32_t>(UINT32_C(0xD807AA98)), static_cast<std::uint32_t>(UINT32_C(0x12835B01)), static_cast<std::uint32_t>(UINT32_C(0x243185BE)), static_cast<std::uint32_t>(UINT32_C(0x550C7DC3)),
static_cast<std::uint32_t>(UINT32_C(0x72BE5D74)), static_cast<std::uint32_t>(UINT32_C(0x80DEB1FE)), static_cast<std::uint32_t>(UINT32_C(0x9BDC06A7)), static_cast<std::uint32_t>(UINT32_C(0xC19BF174)),
static_cast<std::uint32_t>(UINT32_C(0xE49B69C1)), static_cast<std::uint32_t>(UINT32_C(0xEFBE4786)), static_cast<std::uint32_t>(UINT32_C(0x0FC19DC6)), static_cast<std::uint32_t>(UINT32_C(0x240CA1CC)),
static_cast<std::uint32_t>(UINT32_C(0x2DE92C6F)), static_cast<std::uint32_t>(UINT32_C(0x4A7484AA)), static_cast<std::uint32_t>(UINT32_C(0x5CB0A9DC)), static_cast<std::uint32_t>(UINT32_C(0x76F988DA)),
static_cast<std::uint32_t>(UINT32_C(0x983E5152)), static_cast<std::uint32_t>(UINT32_C(0xA831C66D)), static_cast<std::uint32_t>(UINT32_C(0xB00327C8)), static_cast<std::uint32_t>(UINT32_C(0xBF597FC7)),
static_cast<std::uint32_t>(UINT32_C(0xC6E00BF3)), static_cast<std::uint32_t>(UINT32_C(0xD5A79147)), static_cast<std::uint32_t>(UINT32_C(0x06CA6351)), static_cast<std::uint32_t>(UINT32_C(0x14292967)),
static_cast<std::uint32_t>(UINT32_C(0x27B70A85)), static_cast<std::uint32_t>(UINT32_C(0x2E1B2138)), static_cast<std::uint32_t>(UINT32_C(0x4D2C6DFC)), static_cast<std::uint32_t>(UINT32_C(0x53380D13)),
static_cast<std::uint32_t>(UINT32_C(0x650A7354)), static_cast<std::uint32_t>(UINT32_C(0x766A0ABB)), static_cast<std::uint32_t>(UINT32_C(0x81C2C92E)), static_cast<std::uint32_t>(UINT32_C(0x92722C85)),
static_cast<std::uint32_t>(UINT32_C(0xA2BFE8A1)), static_cast<std::uint32_t>(UINT32_C(0xA81A664B)), static_cast<std::uint32_t>(UINT32_C(0xC24B8B70)), static_cast<std::uint32_t>(UINT32_C(0xC76C51A3)),
static_cast<std::uint32_t>(UINT32_C(0xD192E819)), static_cast<std::uint32_t>(UINT32_C(0xD6990624)), static_cast<std::uint32_t>(UINT32_C(0xF40E3585)), static_cast<std::uint32_t>(UINT32_C(0x106AA070)),
static_cast<std::uint32_t>(UINT32_C(0x19A4C116)), static_cast<std::uint32_t>(UINT32_C(0x1E376C08)), static_cast<std::uint32_t>(UINT32_C(0x2748774C)), static_cast<std::uint32_t>(UINT32_C(0x34B0BCB5)),
static_cast<std::uint32_t>(UINT32_C(0x391C0CB3)), static_cast<std::uint32_t>(UINT32_C(0x4ED8AA4A)), static_cast<std::uint32_t>(UINT32_C(0x5B9CCA4F)), static_cast<std::uint32_t>(UINT32_C(0x682E6FF3)),
static_cast<std::uint32_t>(UINT32_C(0x748F82EE)), static_cast<std::uint32_t>(UINT32_C(0x78A5636F)), static_cast<std::uint32_t>(UINT32_C(0x84C87814)), static_cast<std::uint32_t>(UINT32_C(0x8CC70208)),
static_cast<std::uint32_t>(UINT32_C(0x90BEFFFA)), static_cast<std::uint32_t>(UINT32_C(0xA4506CEB)), static_cast<std::uint32_t>(UINT32_C(0xBEF9A3F7)), static_cast<std::uint32_t>(UINT32_C(0xC67178F2))
};
transform_context_type state = transform_context;
for(auto i = static_cast<std::size_t>(UINT8_C(0)); i < static_cast<std::size_t>(UINT8_C(64)); ++i)
{
const auto tmp1 =
static_cast<std::uint32_t>
(
state[static_cast<std::size_t>(UINT8_C(7))]
+ bsig1(state[static_cast<std::size_t>(UINT8_C(4))])
+ ch(state[static_cast<std::size_t>(UINT8_C(4))], state[static_cast<std::size_t>(UINT8_C(5))], state[static_cast<std::size_t>(UINT8_C(6))])
+ transform_constants[i] // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
+ m[i] // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
);
const auto tmp2 =
static_cast<std::uint32_t>
(
bsig0(state[static_cast<std::size_t>(UINT8_C(0))])
+ maj(state[static_cast<std::size_t>(UINT8_C(0))], state[static_cast<std::size_t>(UINT8_C(1))], state[static_cast<std::size_t>(UINT8_C(2))])
);
state[static_cast<std::size_t>(UINT8_C(7))] = state[static_cast<std::size_t>(UINT8_C(6))];
state[static_cast<std::size_t>(UINT8_C(6))] = state[static_cast<std::size_t>(UINT8_C(5))];
state[static_cast<std::size_t>(UINT8_C(5))] = state[static_cast<std::size_t>(UINT8_C(4))];
state[static_cast<std::size_t>(UINT8_C(4))] = state[static_cast<std::size_t>(UINT8_C(3))] + tmp1;
state[static_cast<std::size_t>(UINT8_C(3))] = state[static_cast<std::size_t>(UINT8_C(2))];
state[static_cast<std::size_t>(UINT8_C(2))] = state[static_cast<std::size_t>(UINT8_C(1))];
state[static_cast<std::size_t>(UINT8_C(1))] = state[static_cast<std::size_t>(UINT8_C(0))];
state[static_cast<std::size_t>(UINT8_C(0))] = static_cast<std::uint32_t>(tmp1 + tmp2);
}
transform_context[static_cast<std::size_t>(UINT8_C(0))] += state[static_cast<std::size_t>(UINT8_C(0))];
transform_context[static_cast<std::size_t>(UINT8_C(1))] += state[static_cast<std::size_t>(UINT8_C(1))];
transform_context[static_cast<std::size_t>(UINT8_C(2))] += state[static_cast<std::size_t>(UINT8_C(2))];
transform_context[static_cast<std::size_t>(UINT8_C(3))] += state[static_cast<std::size_t>(UINT8_C(3))];
transform_context[static_cast<std::size_t>(UINT8_C(4))] += state[static_cast<std::size_t>(UINT8_C(4))];
transform_context[static_cast<std::size_t>(UINT8_C(5))] += state[static_cast<std::size_t>(UINT8_C(5))];
transform_context[static_cast<std::size_t>(UINT8_C(6))] += state[static_cast<std::size_t>(UINT8_C(6))];
transform_context[static_cast<std::size_t>(UINT8_C(7))] += state[static_cast<std::size_t>(UINT8_C(7))];
}
static constexpr auto rotl(std::uint32_t a, unsigned b) -> std::uint32_t { return (static_cast<std::uint32_t>(a << b) | static_cast<std::uint32_t>(a >> (static_cast<unsigned>(UINT8_C(32)) - b))); }
static constexpr auto rotr(std::uint32_t a, unsigned b) -> std::uint32_t { return (static_cast<std::uint32_t>(a >> b) | static_cast<std::uint32_t>(a << (static_cast<unsigned>(UINT8_C(32)) - b))); }
static constexpr auto ch (std::uint32_t x, std::uint32_t y, std::uint32_t z) -> std::uint32_t { return (static_cast<std::uint32_t>(x & y) ^ static_cast<std::uint32_t>(~x & z)); }
static constexpr auto maj(std::uint32_t x, std::uint32_t y, std::uint32_t z) -> std::uint32_t { return (static_cast<std::uint32_t>(x & y) ^ static_cast<std::uint32_t>(x & z) ^ static_cast<std::uint32_t>(y & z)); }
static constexpr auto bsig0(std::uint32_t x) -> std::uint32_t { return (rotr(x, static_cast<unsigned>(UINT8_C( 2))) ^ rotr(x, static_cast<unsigned>(UINT8_C(13))) ^ rotr(x, static_cast<unsigned>(UINT8_C(22)))); }
static constexpr auto bsig1(std::uint32_t x) -> std::uint32_t { return (rotr(x, static_cast<unsigned>(UINT8_C( 6))) ^ rotr(x, static_cast<unsigned>(UINT8_C(11))) ^ rotr(x, static_cast<unsigned>(UINT8_C(25)))); }
static constexpr auto ssig0(std::uint32_t x) -> std::uint32_t { return (rotr(x, static_cast<unsigned>(UINT8_C( 7))) ^ rotr(x, static_cast<unsigned>(UINT8_C(18))) ^ (x >> static_cast<unsigned>(UINT8_C( 3)))); }
static constexpr auto ssig1(std::uint32_t x) -> std::uint32_t { return (rotr(x, static_cast<unsigned>(UINT8_C(17))) ^ rotr(x, static_cast<unsigned>(UINT8_C(19))) ^ (x >> static_cast<unsigned>(UINT8_C(10)))); }
};
template<const unsigned CurveBits,
typename LimbType,
const char* CoordX,
const char* CoordY>
struct ecc_point
{
#if defined(WIDE_INTEGER_NAMESPACE)
using uint_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::uintwide_t<static_cast<WIDE_INTEGER_NAMESPACE::math::wide_integer::size_t>(CurveBits), LimbType, void, false>;
#else
using uint_type = ::math::wide_integer::uintwide_t<static_cast<::math::wide_integer::size_t>(CurveBits), LimbType, void, false>;
#endif
#if defined(WIDE_INTEGER_NAMESPACE)
using double_sint_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::uintwide_t<static_cast<WIDE_INTEGER_NAMESPACE::math::wide_integer::size_t>(std::numeric_limits<uint_type>::digits * static_cast<int>(INT8_C(2))), LimbType, void, true>;
#else
using double_sint_type = ::math::wide_integer::uintwide_t<static_cast<::math::wide_integer::size_t>(std::numeric_limits<uint_type>::digits * static_cast<int>(INT8_C(2))), LimbType, void, true>;
#endif
static_assert(static_cast<unsigned>(std::numeric_limits<uint_type>::digits) == CurveBits,
"Error: Wrong number of bits in the smallest unsigned type of the point");
using limb_type = typename uint_type::limb_type;
using point_type =
struct point_type
{
constexpr point_type(double_sint_type x = static_cast<double_sint_type>(static_cast<unsigned>(UINT8_C(0))), // NOLINT(google-explicit-constructor,hicpp-explicit-conversions,bugprone-easily-swappable-parameters)
double_sint_type y = static_cast<double_sint_type>(static_cast<unsigned>(UINT8_C(0)))) noexcept
: my_x(x),
my_y(y) { } // LCOV_EXCL_LINE
double_sint_type my_x; // NOLINT(misc-non-private-member-variables-in-classes)
double_sint_type my_y; // NOLINT(misc-non-private-member-variables-in-classes)
};
};
template<const unsigned CurveBits,
typename LimbType,
const char* CurveName,
const char* FieldCharacteristicP,
const char* CurveCoefficientA,
const char* CurveCoefficientB,
const char* CoordGx,
const char* CoordGy,
const char* SubGroupOrderN,
const int SubGroupCoFactorH>
struct elliptic_curve : public ecc_point<CurveBits, LimbType, CoordGx, CoordGy> // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
{
using base_class_type = ecc_point<CurveBits, LimbType, CoordGx, CoordGy>; // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
using point_type = typename base_class_type::point_type;
using uint_type = typename base_class_type::uint_type;
using double_sint_type = typename base_class_type::double_sint_type;
using limb_type = typename base_class_type::limb_type;
using keypair_type = std::pair<uint_type, std::pair<uint_type, uint_type>>;
#if defined(WIDE_INTEGER_NAMESPACE)
using quadruple_sint_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::uintwide_t<static_cast<WIDE_INTEGER_NAMESPACE::math::wide_integer::size_t>(std::numeric_limits<uint_type>::digits * static_cast<int>(INT8_C(4))), limb_type, void, true>;
#else
using quadruple_sint_type = ::math::wide_integer::uintwide_t<static_cast<::math::wide_integer::size_t>(std::numeric_limits<uint_type>::digits * static_cast<int>(INT8_C(4))), limb_type, void, true>;
#endif
#if defined(WIDE_INTEGER_NAMESPACE)
using sexatuple_sint_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::uintwide_t<static_cast<WIDE_INTEGER_NAMESPACE::math::wide_integer::size_t>(std::numeric_limits<uint_type>::digits * static_cast<int>(INT8_C(6))), limb_type, void, true>;
#else
using sexatuple_sint_type = ::math::wide_integer::uintwide_t<static_cast<::math::wide_integer::size_t>(std::numeric_limits<uint_type>::digits * static_cast<int>(INT8_C(6))), limb_type, void, true>;
#endif
#if defined(WIDE_INTEGER_NAMESPACE)
using duodectuple_sint_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::uintwide_t<static_cast<WIDE_INTEGER_NAMESPACE::math::wide_integer::size_t>(std::numeric_limits<uint_type>::digits * static_cast<int>(INT8_C(12))), limb_type, void, true>;
#else
using duodectuple_sint_type = ::math::wide_integer::uintwide_t<static_cast<::math::wide_integer::size_t>(std::numeric_limits<uint_type>::digits * static_cast<int>(INT8_C(12))), limb_type, void, true>;
#endif
static constexpr auto curve_p () noexcept -> double_sint_type { return double_sint_type(FieldCharacteristicP); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
static constexpr auto curve_a () noexcept -> double_sint_type { return double_sint_type(CurveCoefficientA); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
static constexpr auto curve_b () noexcept -> double_sint_type { return double_sint_type(CurveCoefficientB); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
static constexpr auto curve_gx() noexcept -> double_sint_type { return double_sint_type(CoordGx); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
static constexpr auto curve_gy() noexcept -> double_sint_type { return double_sint_type(CoordGy); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
static constexpr auto curve_n () noexcept -> double_sint_type { return double_sint_type(SubGroupOrderN); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
static auto inverse_mod(const double_sint_type& k, const double_sint_type& p) -> double_sint_type // NOLINT(misc-no-recursion)
{
// Returns the inverse of k modulo p.
// This function returns the only integer x such that (x * k) % p == 1.
// k must be non-zero and p must be a prime.
if(k == 0)
{
// Error: Division by zero.
return 0; // LCOV_EXCL_LINE
}
if(k < 0)
{
// k ** -1 = p - (-k) ** -1 (mod p)
return p - inverse_mod(-k, p);
}
// Extended Euclidean algorithm.
auto s = double_sint_type(static_cast<unsigned>(UINT8_C(0)));
auto old_s = double_sint_type(static_cast<unsigned>(UINT8_C(1)));
auto r = p;
auto old_r = k;
while(r != 0U) // NOLINT(altera-id-dependent-backward-branch)
{
const auto quotient = divmod(old_r, r).first;
const auto tmp_r = r; r = old_r - (quotient * r); old_r = tmp_r;
const auto tmp_s = s; s = old_s - (quotient * s); old_s = tmp_s;
}
return divmod(old_s, p).second;
}
// Functions that work on curve points
static auto is_on_curve(const point_type& point) -> bool
{
// Returns True if the given point lies on the elliptic curve.
if((point.my_x == 0) && (point.my_y == 0))
{
// None represents the point at infinity.
return true; // LCOV_EXCL_LINE
}
// Test the condition:
// (y * y - x * x * x - curve.a * x -curve.b) % curve.p == 0
const auto num =
quadruple_sint_type
(
(quadruple_sint_type(point.my_y) * quadruple_sint_type(point.my_y))
- (quadruple_sint_type(point.my_x) * (quadruple_sint_type(point.my_x) * quadruple_sint_type(point.my_x)))
- (quadruple_sint_type(point.my_x) * quadruple_sint_type(curve_a()))
- quadruple_sint_type(curve_b())
);
const auto divmod_result = divmod(num, quadruple_sint_type(curve_p())).second;
return (divmod_result == 0);
}
// LCOV_EXCL_START
static constexpr auto point_neg(const point_type& point) -> point_type
{
// Returns the negation of the point on the curve (i.e., -point).
return
{
((point.my_x == 0) && (point.my_y == 0))
? point_type(0)
: point_type
{
point.my_x,
-divmod(point.my_y, curve_p()).second
}
};
}
// LCOV_EXCL_STOP
static auto point_add(const point_type& point1, const point_type& point2) -> point_type
{
// Returns the result of (point1 + point2) according to the group law.
const auto& x1 = point1.my_x; const auto& y1 = point1.my_y;
const auto& x2 = point2.my_x; const auto& y2 = point2.my_y;
if((x1 == 0) && (y1 == 0))
{
// 0 + point2 = point2
return point_type(point2);
}
if((x2 == 0) && (y2 == 0))
{
// point1 + 0 = point1
return point_type(point1); // LCOV_EXCL_LINE
}
if((x1 == x2) && (y1 != y2))
{
// point1 + (-point1) = 0
return point_type { }; // LCOV_EXCL_LINE
}
// Differentiate the cases (point1 == point2) and (point1 != point2).
const auto m =
quadruple_sint_type
(
(x1 == x2)
? (quadruple_sint_type(x1) * quadruple_sint_type(x1) * 3 + quadruple_sint_type(curve_a())) * quadruple_sint_type(inverse_mod(y1 * 2, curve_p()))
: quadruple_sint_type(y1 - y2) * quadruple_sint_type(inverse_mod(x1 - x2, curve_p()))
);
const auto x3 =
duodectuple_sint_type
(
duodectuple_sint_type(m) * duodectuple_sint_type(m) - duodectuple_sint_type(x1 + x2)
);
auto y3 =
duodectuple_sint_type
(
duodectuple_sint_type(y1) + duodectuple_sint_type(m) * (x3 - duodectuple_sint_type(x1))
);
// Negate y3 for the modulus operation below.
y3.negate();
return
{
double_sint_type(divmod(x3, duodectuple_sint_type(curve_p())).second),
double_sint_type(divmod(y3, duodectuple_sint_type(curve_p())).second)
};
}
static auto scalar_mult(const double_sint_type& k, const point_type& point) -> point_type // NOLINT(misc-no-recursion)
{
// Returns k * point computed using the double and point_add algorithm.
if(((k % curve_n()) == 0) || ((point.my_x == 0) && (point.my_y == 0)))
{
return point_type { }; // LCOV_EXCL_LINE
}
if(k < 0)
{
// k * point = -k * (-point)
return scalar_mult(-k, point_neg(point)); // LCOV_EXCL_LINE
}
point_type result { };
point_type addend = point;
double_sint_type k_val(k);
while(k_val != 0) // NOLINT(altera-id-dependent-backward-branch)
{
const auto lo_bit =
static_cast<unsigned>
(
static_cast<unsigned>(k_val) & static_cast<unsigned>(UINT8_C(1))
);
if(lo_bit != static_cast<unsigned>(UINT8_C(0)))
{
// Add.
result = point_add(result, addend);
}
// Double.
addend = point_add(addend, addend);
k_val >>= static_cast<unsigned>(UINT8_C(1));
}
return result;
}
template<typename UnknownWideUintType>
static auto get_pseudo_random_uint(const UnknownWideUintType& a = (std::numeric_limits<UnknownWideUintType>::min)(),
const UnknownWideUintType& b = (std::numeric_limits<UnknownWideUintType>::max)()) -> UnknownWideUintType
{
using local_wide_unsigned_integer_type = UnknownWideUintType;
#if defined(WIDE_INTEGER_NAMESPACE)
using local_distribution_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::uniform_int_distribution<local_wide_unsigned_integer_type::my_width2, typename local_wide_unsigned_integer_type::limb_type>;
#else
using local_distribution_type = ::math::wide_integer::uniform_int_distribution<local_wide_unsigned_integer_type::my_width2, typename local_wide_unsigned_integer_type::limb_type>;
#endif
using local_random_engine_type = std::linear_congruential_engine<std::uint32_t, UINT32_C(48271), UINT32_C(0), UINT32_C(2147483647)>;
using local_random_device_type = std::random_device;
local_random_device_type dev;
const auto seed_value = static_cast<typename local_random_engine_type::result_type>(dev());
local_random_engine_type generator(seed_value);
local_distribution_type dist { a, b };
const auto unsigned_pseudo_random_value = dist(generator);
return unsigned_pseudo_random_value;
}
static auto make_keypair(const uint_type* p_uint_seed = nullptr) -> keypair_type
{
// This subroutine generate a random private-public key pair.
// The input parameter p_uint_seed can, however, be used to
// provide a fixed-input value for the private key.
// Also be sure to limit to random.randrange(1, curve.n).
const auto private_key =
uint_type
(
(p_uint_seed == nullptr)
? get_pseudo_random_uint<uint_type>(uint_type { static_cast<unsigned>(UINT8_C(1)) }, curve_n())
: *p_uint_seed
);
const auto public_key = scalar_mult(private_key, { curve_gx(), curve_gy() } );
return
{
private_key,
{
uint_type { public_key.my_x },
uint_type { public_key.my_y }
}
};
}
template<typename MsgIteratorType>
static auto hash_message(MsgIteratorType msg_first, MsgIteratorType msg_last) -> uint_type
{
// This subroutine returns the hash of the message (msg), where
// the type of the hash is 256-bit SHA2, as implenebted locally above.
// For those interested in the general case of ECC, a larger/smaller
// bit-length hash needs to be left/right shifted for cases when there
// are different hash/curve bit-lengths (as specified in FIPS 180).
const auto message { std::vector<std::uint8_t>(msg_first, msg_last) };
using hash_type = hash_sha256;
hash_type hash_object;
const auto hash_result = hash_object.hash(message.data(), message.size());
const auto z =
[&hash_result]()
{
auto u = uint_type { };
static_cast<void>(import_bits(u, hash_result.cbegin(), hash_result.cend()));
return u;
}();
return z;
}
template<typename MsgIteratorType>
static auto sign_message(const uint_type& private_key,
MsgIteratorType msg_first,
MsgIteratorType msg_last,
const uint_type* p_uint_seed = nullptr) -> std::pair<uint_type, uint_type>
{
const auto z = sexatuple_sint_type(hash_message(msg_first, msg_last));
double_sint_type r { };
double_sint_type s { };
const auto n = sexatuple_sint_type(curve_n());
const auto pk = sexatuple_sint_type(private_key);
while((r == 0) || (s == 0)) // NOLINT(altera-id-dependent-backward-branch)
{
const uint_type
uk
{
(p_uint_seed == nullptr) ? std::move(get_pseudo_random_uint<uint_type>()) : *p_uint_seed
};
const double_sint_type k { uk };
const point_type pt { scalar_mult(k, { curve_gx(), curve_gy() } ) };
r = divmod(pt.my_x, curve_n()).second;
const sexatuple_sint_type
num
{
(sexatuple_sint_type(z) + (sexatuple_sint_type(r) * pk))
* sexatuple_sint_type(inverse_mod(k, curve_n()))
};
s = double_sint_type { divmod(num, n).second };
}
return
{
uint_type(r),
uint_type(s)
};
}
template<typename MsgIteratorType>
static auto verify_signature(const std::pair<uint_type, uint_type>& pub,
MsgIteratorType msg_first,
MsgIteratorType msg_last,
const std::pair<uint_type, uint_type>& sig) -> bool
{
const sexatuple_sint_type w(inverse_mod(sig.second, curve_n()));
const sexatuple_sint_type n(curve_n());
const auto z = hash_message(msg_first, msg_last);
const double_sint_type u1(divmod(sexatuple_sint_type(z) * w, n).second);
const double_sint_type u2(divmod(sexatuple_sint_type(sig.first) * w, n).second);
const auto pt =
point_add
(
scalar_mult(u1, { curve_gx(), curve_gy() } ),
scalar_mult(u2, { pub.first, pub.second } )
);
return
(
divmod(double_sint_type(sig.first), curve_n()).second == divmod(pt.my_x, curve_n()).second
);
}
};
constexpr char CurveName [] = "secp256k1"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,cppcoreguidelines-pro-bounds-array-to-pointer-decay,modernize-avoid-c-arrays)
constexpr char FieldCharacteristicP[] = "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,cppcoreguidelines-pro-bounds-array-to-pointer-decay,modernize-avoid-c-arrays)
constexpr char CurveCoefficientA [] = "0x0"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,cppcoreguidelines-pro-bounds-array-to-pointer-decay,modernize-avoid-c-arrays)
constexpr char CurveCoefficientB [] = "0x7"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,cppcoreguidelines-pro-bounds-array-to-pointer-decay,modernize-avoid-c-arrays)
constexpr char BasePointGx [] = "0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,cppcoreguidelines-pro-bounds-array-to-pointer-decay,modernize-avoid-c-arrays)
constexpr char BasePointGy [] = "0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,cppcoreguidelines-pro-bounds-array-to-pointer-decay,modernize-avoid-c-arrays)
constexpr char SubGroupOrderN [] = "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,cppcoreguidelines-pro-bounds-array-to-pointer-decay,modernize-avoid-c-arrays)
constexpr auto SubGroupCoFactorH = static_cast<int>(INT8_C(1));
} // namespace example013_ecdsa
#if defined(WIDE_INTEGER_NAMESPACE)
auto WIDE_INTEGER_NAMESPACE::math::wide_integer::example013_ecdsa_sign_verify() -> bool
#else
auto ::math::wide_integer::example013_ecdsa_sign_verify() -> bool
#endif
{
auto result_is_ok = true;
using elliptic_curve_type =
example013_ecdsa::elliptic_curve<static_cast<unsigned>(UINT16_C(256)),
std::uint32_t,
example013_ecdsa::CurveName, // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
example013_ecdsa::FieldCharacteristicP, // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
example013_ecdsa::CurveCoefficientA, // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
example013_ecdsa::CurveCoefficientB, // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
example013_ecdsa::BasePointGx, // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
example013_ecdsa::BasePointGy, // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
example013_ecdsa::SubGroupOrderN, // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
example013_ecdsa::SubGroupCoFactorH>;
static_assert(elliptic_curve_type::curve_p() == elliptic_curve_type::double_sint_type(example013_ecdsa::FieldCharacteristicP), // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
"Error: Elliptic curve Field Characteristic p seems to be incorrect");
static_assert(elliptic_curve_type::curve_a() == elliptic_curve_type::double_sint_type(example013_ecdsa::CurveCoefficientA), // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
"Error: Elliptic curve curve coefficient a seems to be incorrect");
static_assert(elliptic_curve_type::curve_b() == elliptic_curve_type::double_sint_type(example013_ecdsa::CurveCoefficientB), // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
"Error: Elliptic curve curve coefficient b seems to be incorrect");
static_assert(elliptic_curve_type::curve_gx() == elliptic_curve_type::double_sint_type(example013_ecdsa::BasePointGx), // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
"Error: Elliptic curve base-point Gx seems to be incorrect");
static_assert(elliptic_curve_type::curve_gy() == elliptic_curve_type::double_sint_type(example013_ecdsa::BasePointGy), // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
"Error: Elliptic curve base-point Gx seems to be incorrect");
static_assert(elliptic_curve_type::curve_n() == elliptic_curve_type::double_sint_type(example013_ecdsa::SubGroupOrderN), // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
"Error: Elliptic curve Sub-Group Order seems to be incorrect");
// Declare the message "Hello!" as an array of chars.
#if defined(WIDE_INTEGER_NAMESPACE)
constexpr WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::array_detail::array<char, static_cast<std::size_t>(UINT8_C(6))> msg_as_array { 'H', 'e', 'l', 'l', 'o', '!' };
#else
constexpr ::math::wide_integer::detail::array_detail::array<char, static_cast<std::size_t>(UINT8_C(6))> msg_as_array { 'H', 'e', 'l', 'l', 'o', '!' };
#endif
// Get the message to sign as a string and ensure that it is "Hello!".
const auto msg_as_string = std::string(msg_as_array.cbegin(), msg_as_array.cend());
const auto result_msg_as_string_is_ok = (msg_as_string == "Hello!");
result_is_ok = (result_msg_as_string_is_ok && result_is_ok);
{
// Test the hash SHA-2 HASH-256 implementation.
const auto hash_result = elliptic_curve_type::hash_message(msg_as_array.cbegin(), msg_as_array.cend());
const auto result_hash_is_ok =
(
hash_result == elliptic_curve_type::uint_type("0x334D016F755CD6DC58C53A86E183882F8EC14F52FB05345887C8A5EDD42C87B7")
);
result_is_ok = (result_hash_is_ok && result_is_ok);
}
{
// Test ECC key generation, sign and verify. In this case we use random (but pre-defined seeds
// for both keygen as well as signing.
const auto seed_keygen = elliptic_curve_type::uint_type("0xC6455BF2F380F6B81F5FD1A1DBC2392B3783ED1E7D91B62942706E5584BA0B92");
const auto keypair = elliptic_curve_type::make_keypair(&seed_keygen);
const auto result_is_on_curve_is_ok =
elliptic_curve_type::is_on_curve
(
{
std::get<1>(keypair).first,
std::get<1>(keypair).second
}
);
const auto result_private_is_ok = (std::get<0>(keypair) == "0xC6455BF2F380F6B81F5FD1A1DBC2392B3783ED1E7D91B62942706E5584BA0B92");
const auto result_public_x_is_ok = (std::get<1>(keypair).first == "0xC6235629F157690E1DF37248256C4FB7EFF073D0250F5BD85DF40B9E127A8461");
const auto result_public_y_is_ok = (std::get<1>(keypair).second == "0xCBAA679F07F9B98F915C1FB7D85A379D0559A9EEE6735B1BE0CE0E2E2B2E94DE");
const auto result_keygen_is_ok =
(
result_private_is_ok
&& result_public_x_is_ok
&& result_public_y_is_ok
);
result_is_ok = (result_is_on_curve_is_ok && result_keygen_is_ok && result_is_ok);
const auto priv = elliptic_curve_type::uint_type("0x6F73D8E95D6DDBF0EB352A9F0B2CE91931511EDAF9AC8F128D5A4F877C4F0450");
const auto sig =
elliptic_curve_type::sign_message(std::get<0>(keypair), msg_as_string.cbegin(), msg_as_string.cend(), &priv);
const auto result_sig_is_ok =
(
sig == std::make_pair
(
elliptic_curve_type::uint_type("0x65717A860F315A21E6E23CDE411C8940DE42A69D8AB26C2465902BE8F3B75E7B"),
elliptic_curve_type::uint_type("0xDB8B8E75A7B0C2F0D9EB8DBF1B5236EDEB89B2116F5AEBD40E770F8CCC3D6605")
)
);
result_is_ok = (result_sig_is_ok && result_is_ok);
const auto result_verify_is_ok =
elliptic_curve_type::verify_signature
(
std::get<1>(keypair),
msg_as_string.cbegin(),
msg_as_string.cend(),
sig
);
result_is_ok = (result_verify_is_ok && result_is_ok);
}
{
// We will now test 3 more successful keygen, sign, verify sequences.
for(auto count = static_cast<unsigned>(UINT8_C(0));
count < static_cast<unsigned>(UINT8_C(3));
++count)
{
const auto keypair = elliptic_curve_type::make_keypair();
const auto msg_str_append_index = msg_as_string + std::to_string(count);
const auto sig =
elliptic_curve_type::sign_message
(
std::get<0>(keypair),
msg_str_append_index.cbegin(),
msg_str_append_index.cend()
);
const auto result_verify_is_ok =
elliptic_curve_type::verify_signature
(
std::get<1>(keypair),
msg_str_append_index.cbegin(),
msg_str_append_index.cend(),
sig
);
result_is_ok = (result_verify_is_ok && result_is_ok);
}
}
{
// We will now test keygen, sign, and a (failing!) verify sequence,
// where the message being verified has been artificially modified
// and signature verification is expected to fail.
const auto keypair = elliptic_curve_type::make_keypair();
const auto sig =
elliptic_curve_type::sign_message(std::get<0>(keypair), msg_as_string.cbegin(), msg_as_string.cend());
const auto msg_str_to_fail = msg_as_string + "x";
const auto result_verify_expected_fail_is_ok =
(!elliptic_curve_type::verify_signature(std::get<1>(keypair), msg_str_to_fail.cbegin(), msg_str_to_fail.cend(), sig));
result_is_ok = (result_verify_expected_fail_is_ok && result_is_ok);
}
return result_is_ok;
}
// Enable this if you would like to activate this main() as a standalone example.
#if defined(WIDE_INTEGER_STANDALONE_EXAMPLE013_ECDSA_SIGN_VERIFY)
#include <iomanip>
#include <iostream>
auto main() -> int
{
#if defined(WIDE_INTEGER_NAMESPACE)
const auto result_is_ok = WIDE_INTEGER_NAMESPACE::math::wide_integer::example013_ecdsa_sign_verify();
#else
const auto result_is_ok = ::math::wide_integer::example013_ecdsa_sign_verify();
#endif
std::cout << "result_is_ok: " << std::boolalpha << result_is_ok << std::endl;
return (result_is_ok ? 0 : -1);
}
#endif