1
+ // Copyright © 2023 ZeroPass <[email protected] >
2
+ // Author: Crt Vavros
3
+ #pragma once
4
+ #include < ack/ec.hpp>
5
+ #include < ack/ec_curve.hpp>
6
+ #include < ack/ecdsa.hpp>
7
+ #include < ack/keccak.hpp>
8
+ #include < ack/sha.hpp>
9
+ #include < ack/utils.hpp>
10
+ #include < ack/tests/ecdsa_test_utils.hpp>
11
+ #include < ack/tests/utils.hpp>
12
+
13
+ #include < eosio/crypto.hpp>
14
+ #include < eosio/tester.hpp>
15
+
16
+
17
+ namespace ack ::tests {
18
+ EOSIO_TEST_BEGIN ( ecdsa_misc_test )
19
+ constexpr auto & curve = ack::ec_curve::secp256r1;
20
+ using point_type = std::remove_cvref_t <decltype(curve)>::point_type;
21
+ using int_type = std::remove_cvref_t <decltype(curve)>::int_type;
22
+
23
+ constexpr auto q = curve.make_point(
24
+ " e424dc61d4bb3cb7ef4344a7f8957a0c5134e16f7a67c074f82e6e12f49abf3c" ,
25
+ " 970eed7aa2bc48651545949de1dddaf0127e5965ac85d1243d6f60e7dfaee927"
26
+ );
27
+
28
+ constexpr auto h = from_hex( " d1b8ef21eb4182ee270638061063a3f3c16c114e33937f69fb232cc833965a94" );
29
+ auto md = hash256( h );
30
+ constexpr int_type r = " bf96b99aa49c705c910be33142017c642ff540c76349b9dab72f981fd9347f4f" ;
31
+ constexpr int_type s = " 17c55095819089c2e03b9cd415abdf12444e323075d98f31920b9e0f57ec871c" ;
32
+ constexpr size_t recid = 1 ;
33
+
34
+ // Misc ECDSA key recovery tests
35
+ {
36
+ // Test recovery succeeds
37
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, r, s, recid, /* verify=*/ true ), q )
38
+
39
+ // Test passing too big recid results in point at infinity
40
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, r, s, /* recid=*/ 4 , /* verify=*/ true ).is_identity (), true )
41
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, r, s, /* recid=*/ 4 , /* verify=*/ true ).is_identity (), true )
42
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, r, s, /* recid=*/ 0xff , /* verify=*/ true ).is_identity (), true )
43
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, r, s, /* recid=*/ 0xff , /* verify=*/ true ).is_identity (), true )
44
+
45
+ // Test passing too small and too big r
46
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, int_type ( 0 ), s, recid, /* verify=*/ true ).is_identity (), true )
47
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, int_type ( 0 ), s, recid, /* verify=*/ true ).is_identity (), true )
48
+
49
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, curve.n , s, recid, /* verify=*/ true ).is_identity (), true )
50
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, curve.n , s, recid, /* verify=*/ true ).is_identity (), true )
51
+
52
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, curve.n + 1 , s, recid, /* verify=*/ true ).is_identity (), true )
53
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, curve.n + 1 , s, recid, /* verify=*/ true ).is_identity (), true )
54
+
55
+ // Test passing too small and too big s
56
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, r, int_type ( 0 ), recid, /* verify=*/ true ).is_identity (), true )
57
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, r, int_type ( 0 ), recid, /* verify=*/ true ).is_identity (), true )
58
+
59
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, r, curve.n , recid, /* verify=*/ true ).is_identity (), true )
60
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, r, curve.n , recid, /* verify=*/ true ).is_identity (), true )
61
+
62
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, r, curve.n + 1 , recid, /* verify=*/ true ).is_identity (), true )
63
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, r, curve.n + 1 , recid, /* verify=*/ true ).is_identity (), true )
64
+
65
+ // Test passing too big r when recovering second key
66
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, curve.p_minus_n , s, 2 , /* verify=*/ true ).is_identity (), true )
67
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, curve.p_minus_n , s, 2 , /* verify=*/ true ).is_identity (), true )
68
+
69
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), h, curve.p_minus_n + 1 , s, 2 , /* verify=*/ true ).is_identity (), true )
70
+ REQUIRE_EQUAL ( ecdsa_recover ( q.curve (), md, curve.p_minus_n + 1 , s, 2 , /* verify=*/ true ).is_identity (), true )
71
+ }
72
+
73
+ // Misc ECDSA sigver tests
74
+ {
75
+ // Test verification ECDSA signature succeeds
76
+ REQUIRE_EQUAL ( ecdsa_verify ( q, h, r, s ), true )
77
+ REQUIRE_EQUAL ( ecdsa_verify ( q, md, r, s ), true )
78
+ assert_ecdsa ( q, h, r, s,
79
+ " ECDSA signature verification failed"
80
+ );
81
+ assert_ecdsa ( q, md, r, s,
82
+ " ECDSA signature verification failed"
83
+ );
84
+
85
+ // Test verification fails when passing identity q
86
+ REQUIRE_EQUAL ( ecdsa_verify ( point_type{}, h, r, s ), false )
87
+ REQUIRE_EQUAL ( ecdsa_verify ( point_type{}, md, r, s ), false )
88
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
89
+ assert_ecdsa ( point_type{}, h, r, s,
90
+ " ECDSA signature verification failed"
91
+ );
92
+ })
93
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
94
+ assert_ecdsa ( point_type{}, md, r, s,
95
+ " ECDSA signature verification failed"
96
+ );
97
+ })
98
+
99
+ // Test passing too small and too big r
100
+ REQUIRE_EQUAL ( ecdsa_verify ( q, h, int_type ( 0 ), s ), false )
101
+ REQUIRE_EQUAL ( ecdsa_verify ( q, md, int_type ( 0 ), s ), false )
102
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
103
+ assert_ecdsa ( q, h, int_type ( 0 ), s,
104
+ " ECDSA signature verification failed"
105
+ );
106
+ })
107
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
108
+ assert_ecdsa ( q, md, int_type ( 0 ), s,
109
+ " ECDSA signature verification failed"
110
+ );
111
+ })
112
+
113
+ REQUIRE_EQUAL ( ecdsa_verify ( q, h, curve.n , s ), false )
114
+ REQUIRE_EQUAL ( ecdsa_verify ( q, md, curve.n , s ), false )
115
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
116
+ assert_ecdsa ( q, h, curve.n , s,
117
+ " ECDSA signature verification failed"
118
+ );
119
+ })
120
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
121
+ assert_ecdsa ( q, md, curve.n , s,
122
+ " ECDSA signature verification failed"
123
+ );
124
+ })
125
+
126
+ REQUIRE_EQUAL ( ecdsa_verify ( q, h, curve.n + 1 , s ), false )
127
+ REQUIRE_EQUAL ( ecdsa_verify ( q, md, curve.n + 1 , s ), false )
128
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
129
+ assert_ecdsa ( q, h, curve.n + 1 , s,
130
+ " ECDSA signature verification failed"
131
+ );
132
+ })
133
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
134
+ assert_ecdsa ( q, md, curve.n + 1 , s,
135
+ " ECDSA signature verification failed"
136
+ );
137
+ })
138
+
139
+ // Test passing too small and too big s
140
+ REQUIRE_EQUAL ( ecdsa_verify ( q, h, r, int_type ( 0 ) ), false )
141
+ REQUIRE_EQUAL ( ecdsa_verify ( q, md, r, int_type ( 0 ) ), false )
142
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
143
+ assert_ecdsa ( q, h, r, int_type ( 0 ),
144
+ " ECDSA signature verification failed"
145
+ );
146
+ })
147
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
148
+ assert_ecdsa ( q, md, r, int_type ( 0 ),
149
+ " ECDSA signature verification failed"
150
+ );
151
+ })
152
+
153
+ REQUIRE_EQUAL ( ecdsa_verify ( q, h, r, curve.n ), false )
154
+ REQUIRE_EQUAL ( ecdsa_verify ( q, md, r, curve.n ), false )
155
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
156
+ assert_ecdsa ( q, h, r, curve.n ,
157
+ " ECDSA signature verification failed"
158
+ );
159
+ })
160
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
161
+ assert_ecdsa ( q, md, r, curve.n ,
162
+ " ECDSA signature verification failed"
163
+ );
164
+ })
165
+
166
+ REQUIRE_EQUAL ( ecdsa_verify ( q, h, r, curve.n + 1 ), false )
167
+ REQUIRE_EQUAL ( ecdsa_verify ( q, md, r, curve.n + 1 ), false )
168
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
169
+ assert_ecdsa ( q, h, r, curve.n + 1 ,
170
+ " ECDSA signature verification failed"
171
+ );
172
+ })
173
+ REQUIRE_ASSERT ( " ECDSA signature verification failed" , [&]() {
174
+ assert_ecdsa ( q, md, r, curve.n + 1 ,
175
+ " ECDSA signature verification failed"
176
+ );
177
+ })
178
+ }
179
+ EOSIO_TEST_END
180
+ }
0 commit comments