forked from coderofstuff/app-kaspa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_address.c
198 lines (153 loc) · 9.28 KB
/
test_address.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
/*****************************************************************************
* MIT License
*
* Copyright (c) 2023 coderofstuff
*
* 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.
*****************************************************************************/
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdbool.h>
#include <cmocka.h>
#include "address.h"
#include "types.h"
static void test_schnorr_address_from_public_key(void **state) {
uint8_t public_key[] = {
0x21, 0xEB, 0x0C, 0x42, 0x70, 0x12, 0x8B, 0x16, 0xC9, 0x3C, 0x5F, 0x0D, 0xAC, 0x48, 0xD5, 0x60,
0x51, 0xA6, 0x23, 0x7D, 0xAE, 0x99, 0x7B, 0x58, 0x91, 0x26, 0x95, 0x05, 0x28, 0x18, 0xE3, 0x48,
0xB0, 0xA8, 0x95, 0xCB, 0xD0, 0xC9, 0x3A, 0x11, 0xEE, 0x7A, 0xFA, 0xC7, 0x45, 0x92, 0x9D, 0x96,
0xA4, 0x64, 0x2A, 0x71, 0x83, 0x1F, 0x54, 0xA7, 0x37, 0x78, 0x93, 0xAF, 0x71, 0xA2, 0xE2, 0xAE
};
uint8_t address[SCHNORR_ADDRESS_LEN + 1] = {0};
address_from_pubkey(public_key, SCHNORR, address, SCHNORR_ADDRESS_LEN);
assert_string_equal((char *) address, "kaspa:qqs7krzzwqfgk9kf830smtzg64s9rf3r0khfj76cjynf2pfgrr35saatu88xq");
uint8_t public_key2[] = {
0xFA, 0x2B, 0x85, 0x72, 0xB6, 0x18, 0x36, 0x2A, 0x26, 0x12, 0x8D, 0xB3, 0x88, 0xF0, 0x4E, 0xD1,
0xA9, 0x5C, 0xCC, 0xD8, 0xE1, 0x89, 0xF9, 0xC1, 0xBD, 0x6C, 0x57, 0x66, 0x8B, 0x11, 0xB2, 0xD7,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
uint8_t address2[SCHNORR_ADDRESS_LEN + 1] = {0};
address_from_pubkey(public_key2, SCHNORR, address2, SCHNORR_ADDRESS_LEN);
assert_string_equal((char *) address2, "kaspa:qrazhptjkcvrv23xz2xm8z8sfmg6jhxvmrscn7wph4k9we5tzxedwfxf0v6f8");
}
static void test_p2sh_address_from_public_key(void **state) {
uint8_t public_key[] = {
// OP_BLAKE2B, 0x20,
0xF3, 0x80, 0x31, 0xF6, 0x1C, 0xA2, 0x3D, 0x70, 0x84, 0x4F, 0x63, 0xA4, 0x77, 0xD0, 0x7F, 0x0B,
0x2C, 0x2D, 0xEC, 0xAB, 0x90, 0x7C, 0x2E, 0x09, 0x6E, 0x54, 0x8B, 0x0E, 0x08, 0x72, 0x1C, 0x79,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
// OP_EQUAL
};
uint8_t address[SCHNORR_ADDRESS_LEN + 1] = {0};
address_from_pubkey(public_key, P2SH, address, SCHNORR_ADDRESS_LEN);
assert_string_equal((char *) address, "kaspa:precqv0krj3r6uyyfa36ga7s0u9jct0v4wg8ctsfde2gkrsgwgw8jgxfzfc98");
}
static void test_ecdsa_address_from_public_key(void **state) {
// Even Y-coord test case
uint8_t public_key_even[] = {
0xd5, 0xfd, 0xc7, 0xad, 0x11, 0xa6, 0x5d, 0x0b, 0xbe, 0x78, 0x82, 0xfc, 0x3d, 0xbc, 0x91, 0xb5,
0x86, 0x1d, 0x18, 0x2d, 0xcc, 0xe7, 0x9f, 0x7c, 0x1b, 0xe5, 0xbf, 0xd3, 0x0a, 0x67, 0x7c, 0xd6,
0xc5, 0x62, 0xad, 0x66, 0xab, 0xcd, 0xb1, 0xec, 0x02, 0xf3, 0xe4, 0xb0, 0x7c, 0x11, 0xbc, 0x5a,
0x94, 0xa6, 0x85, 0xfe, 0xdb, 0x5d, 0x55, 0x87, 0x07, 0x6e, 0x48, 0xb1, 0x2d, 0xa6, 0xc2, 0x82
};
uint8_t address_even[ECDSA_ADDRESS_LEN + 1] = {0};
address_from_pubkey(public_key_even, ECDSA, address_even, ECDSA_ADDRESS_LEN);
assert_string_equal((char *) address_even, "kaspa:qypdtlw845g6vhgtheug9lpahjgmtpsarqkueeul0sd7t07npfnhe4s7fd82n0v");
// Odd Y-coord test case
uint8_t public_key_odd[] = {
0xe3, 0x10, 0x0d, 0x85, 0xef, 0xae, 0x93, 0xe0, 0xc2, 0xfc, 0x65, 0x4b, 0x2f, 0x0c, 0x33, 0x58,
0x4f, 0x21, 0x3a, 0x3f, 0xdf, 0xfd, 0x02, 0x3c, 0x82, 0x12, 0x77, 0xb2, 0x17, 0x89, 0xe0, 0x64,
0xe0, 0x6b, 0x45, 0x4f, 0x5c, 0xba, 0x0e, 0xff, 0x1c, 0xe8, 0x01, 0xd3, 0x83, 0x5c, 0x39, 0xec,
0x01, 0xca, 0x94, 0x9c, 0xa8, 0x77, 0xc0, 0xb5, 0x5b, 0xbd, 0xec, 0xa8, 0xff, 0x84, 0x91, 0xd9
};
uint8_t address_odd[ECDSA_ADDRESS_LEN + 1] = {0};
address_from_pubkey(public_key_odd, ECDSA, address_odd, ECDSA_ADDRESS_LEN);
assert_string_equal((char *) address_odd, "kaspa:qyp7xyqdshh6aylqct7x2je0pse4snep8glallgz8jppyaajz7y7qeq4x79fq4z");
}
static void test_invalid_type(void **state) {
// Even Y-coord test case
uint8_t public_key[1] = {0};
uint8_t address[1] = {0};
bool res = address_from_pubkey(public_key, -1, address, 1);
assert_int_equal(res, 0);
}
static void test_compress_address_schnorr(void **state) {
uint8_t public_key[] = {
0xF3, 0x80, 0x31, 0xF6, 0x1C, 0xA2, 0x3D, 0x70, 0x84, 0x4F, 0x63, 0xA4, 0x77, 0xD0, 0x7F, 0x0B,
0x2C, 0x2D, 0xEC, 0xAB, 0x90, 0x7C, 0x2E, 0x09, 0x6E, 0x54, 0x8B, 0x0E, 0x08, 0x72, 0x1C, 0x79,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
uint8_t compressed[32] = {0};
size_t result = compress_public_key(public_key, SCHNORR, compressed, sizeof(compressed));
assert_true(result > 0);
assert_memory_equal(compressed, public_key, 32);
}
static void test_compress_address_ecdsa(void **state) {
uint8_t public_key_even[] = {
0xd5, 0xfd, 0xc7, 0xad, 0x11, 0xa6, 0x5d, 0x0b, 0xbe, 0x78, 0x82, 0xfc, 0x3d, 0xbc, 0x91, 0xb5,
0x86, 0x1d, 0x18, 0x2d, 0xcc, 0xe7, 0x9f, 0x7c, 0x1b, 0xe5, 0xbf, 0xd3, 0x0a, 0x67, 0x7c, 0xd6,
0xc5, 0x62, 0xad, 0x66, 0xab, 0xcd, 0xb1, 0xec, 0x02, 0xf3, 0xe4, 0xb0, 0x7c, 0x11, 0xbc, 0x5a,
0x94, 0xa6, 0x85, 0xfe, 0xdb, 0x5d, 0x55, 0x87, 0x07, 0x6e, 0x48, 0xb1, 0x2d, 0xa6, 0xc2, 0x82
};
uint8_t compressed_even[33] = {0};
size_t result_even = compress_public_key(public_key_even, ECDSA, compressed_even, sizeof(compressed_even));
assert_true(result_even > 0);
assert_true(compressed_even[0] == 0x02);
assert_memory_equal(compressed_even +1, public_key_even, 32);
uint8_t public_key_odd[] = {
0xe3, 0x10, 0x0d, 0x85, 0xef, 0xae, 0x93, 0xe0, 0xc2, 0xfc, 0x65, 0x4b, 0x2f, 0x0c, 0x33, 0x58,
0x4f, 0x21, 0x3a, 0x3f, 0xdf, 0xfd, 0x02, 0x3c, 0x82, 0x12, 0x77, 0xb2, 0x17, 0x89, 0xe0, 0x64,
0xe0, 0x6b, 0x45, 0x4f, 0x5c, 0xba, 0x0e, 0xff, 0x1c, 0xe8, 0x01, 0xd3, 0x83, 0x5c, 0x39, 0xec,
0x01, 0xca, 0x94, 0x9c, 0xa8, 0x77, 0xc0, 0xb5, 0x5b, 0xbd, 0xec, 0xa8, 0xff, 0x84, 0x91, 0xd9
};
uint8_t compressed_odd[33] = {0};
size_t result_odd = compress_public_key(public_key_odd, ECDSA, compressed_odd, sizeof(compressed_odd));
assert_true(result_odd > 0);
assert_true(compressed_odd[0] == 0x03);
assert_memory_equal(compressed_odd + 1, public_key_odd, 32);
}
static void test_invalid_compress_address(void **state) {
uint8_t public_key[] = {
0xe3, 0x10, 0x0d, 0x85, 0xef, 0xae, 0x93, 0xe0, 0xc2, 0xfc, 0x65, 0x4b, 0x2f, 0x0c, 0x33, 0x58,
0x4f, 0x21, 0x3a, 0x3f, 0xdf, 0xfd, 0x02, 0x3c, 0x82, 0x12, 0x77, 0xb2, 0x17, 0x89, 0xe0, 0x64,
0xe0, 0x6b, 0x45, 0x4f, 0x5c, 0xba, 0x0e, 0xff, 0x1c, 0xe8, 0x01, 0xd3, 0x83, 0x5c, 0x39, 0xec,
0x01, 0xca, 0x94, 0x9c, 0xa8, 0x77, 0xc0, 0xb5, 0x5b, 0xbd, 0xec, 0xa8, 0xff, 0x84, 0x91, 0xd9
};
uint8_t compressed_too_small_schnorr[31] = {0}; // We need 32 for valid
size_t result_schnorr = compress_public_key(public_key, SCHNORR, compressed_too_small_schnorr, sizeof(compressed_too_small_schnorr));
assert_true(result_schnorr == 0);
uint8_t compressed_too_small_ecdsa[32] = {0}; // We need 33 for valid
size_t result_ecdsa = compress_public_key(public_key, ECDSA, compressed_too_small_ecdsa, sizeof(compressed_too_small_ecdsa));
assert_true(result_ecdsa == 0);
}
int main() {
const struct CMUnitTest tests[] = {cmocka_unit_test(test_schnorr_address_from_public_key),
cmocka_unit_test(test_ecdsa_address_from_public_key),
cmocka_unit_test(test_p2sh_address_from_public_key),
cmocka_unit_test(test_invalid_type),
cmocka_unit_test(test_compress_address_schnorr),
cmocka_unit_test(test_compress_address_ecdsa),
cmocka_unit_test(test_invalid_compress_address)};
return cmocka_run_group_tests(tests, NULL, NULL);
}