-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.rs
416 lines (345 loc) · 15.4 KB
/
integration_test.rs
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
#![cfg(feature = "speculos-tests")]
mod test_common;
use common::{BigIntOperator, HashId};
use hex_literal::hex;
use sha2::Digest;
#[tokio::test]
#[rustfmt::skip]
async fn test_big_num() {
let mut setup = test_common::setup().await;
let zero_large = [0u8; 64].to_vec();
let minus_one_large = [0xffu8; 64].to_vec();
let one_large = {
let mut one = [0u8; 64];
one[63] = 1;
one.to_vec()
};
// additions
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Add, &hex!("77989873"), &hex!("a4589234"), &[]).await.unwrap(),
hex!("1bf12aa7")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Add, &hex!("47989873"), &hex!("a4589234"), &[]).await.unwrap(),
hex!("ebf12aa7")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Add, &hex!("ffffffff"), &hex!("00000001"), &[]).await.unwrap(),
hex!("00000000")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Add, &minus_one_large, &one_large, &[]).await.unwrap(),
zero_large
);
// subtractions
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Sub, &hex!("a4589234"), &hex!("77989873"), &[]).await.unwrap(),
hex!("2cbff9c1")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Sub, &hex!("77989873"), &hex!("a4589234"), &[]).await.unwrap(),
hex!("d340063f")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Sub, &hex!("00000000"), &hex!("00000001"), &[]).await.unwrap(),
hex!("ffffffff")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Sub, &zero_large, &one_large, &[]).await.unwrap(),
minus_one_large
);
}
#[tokio::test]
#[rustfmt::skip]
async fn test_big_num_mod() {
let mut setup = test_common::setup().await;
let M: Vec<u8> = hex!("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f").to_vec();
let M2: Vec<u8> = hex!("3d4b0f9e4e4d5b6e5e5d6e7e8e8d9e9e8e8d9e9e8e8d9e9e8e8d9e9e8e8d9e9d").to_vec();
let zero: Vec<u8> = hex!("0000000000000000000000000000000000000000000000000000000000000000").to_vec();
let one: Vec<u8> = hex!("0000000000000000000000000000000000000000000000000000000000000001").to_vec();
// addition
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Add, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("7390984098209380980948098230840982340294098092384092834923840923"), &M).await.unwrap(),
hex!("15d7f1c4cab897b32c12c8a1b638879e023d5a9d8ca0da88d89bdb53a7b61679")
);
// subtraction
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Sub, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("7390984098209380980948098230840982340294098092384092834923840923"), &M).await.unwrap(),
hex!("2eb6c1439a7770b1fc00388eb1d77f8afdd55575799fb6185776d4c060ae0062")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Sub, &hex!("7390984098209380980948098230840982340294098092384092834923840923"), &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &M).await.unwrap(),
hex!("d1493ebc65888f4e03ffc7714e288075022aaa8a866049e7a8892b3e9f51fbcd")
);
// multiplication
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Mul, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &zero, &M).await.unwrap(),
zero
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Mul, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &one, &M).await.unwrap(),
hex!("a247598432980432940980983408039480095809832048509809580984320985")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Mul, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("7390984098209380980948098230840982340294098092384092834923840923"), &M).await.unwrap(),
hex!("2d5daeb3ed823bef5a4480a2c5aa0708e8e37ed7302d2b21c9b442b244d48ce6")
);
// TODO: disabled until speculos is fixed as per https://github.com/LedgerHQ/speculos/pull/52
// powers: 0
// assert_eq!(
// setup.client.bignum_operation(BigIntOperator::Pow, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("00"), &M2).await.unwrap(),
// one
// );
// assert_eq!(
// setup.client.bignum_operation(BigIntOperator::Pow, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &zero, &M2).await.unwrap(),
// one
// );
// powers: 1
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Pow, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &one, &M).await.unwrap(),
hex!("a247598432980432940980983408039480095809832048509809580984320985")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Pow, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("01"), &M).await.unwrap(),
hex!("a247598432980432940980983408039480095809832048509809580984320985")
);
// powers: 2
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Pow, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("02"), &M2).await.unwrap(),
hex!("2378a937274b6304f12d26e7170d5d757087246a2db3d5c776faf10984d3331b")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Pow, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("00000002"), &M2).await.unwrap(),
hex!("2378a937274b6304f12d26e7170d5d757087246a2db3d5c776faf10984d3331b")
);
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Pow, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("0000000000000000000000000000000000000000000000000000000000000002"), &M2).await.unwrap(),
hex!("2378a937274b6304f12d26e7170d5d757087246a2db3d5c776faf10984d3331b")
);
// powers: large
assert_eq!(
setup.client.bignum_operation(BigIntOperator::Pow, &hex!("a247598432980432940980983408039480095809832048509809580984320985"), &hex!("22e0b80916f2f35efab04d6d61155f9d1aa9f8f0dff2a2b656cdee1bb7b6dcd722e0b80916f2f35efab04d6d61155f9d1aa9f8f0dff2a2b656cdee1bb7b6dcd7"), &M2).await.unwrap(),
hex!("3c0baee8c4e2f7220615013d7402fa5e69e43bc10e55500a5af4f8b966658846")
);
}
#[tokio::test]
async fn test_ripemd160() {
let mut setup = test_common::setup().await;
#[rustfmt::skip]
let testcases: Vec<(Vec<u8>, Vec<u8>)> = vec![
(hex!("").to_vec(), hex!("9c1185a5c5e9fc54612808977ee8f548b2258d31").to_vec()),
(hex!("54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67").to_vec(), hex!("37f332f68db77bd9d7edd4969571ad671cf9dd3b").to_vec()),
];
for (input, expected) in testcases {
assert_eq!(
setup.client.hash(HashId::Ripemd160, &input).await.unwrap(),
expected
);
}
}
#[tokio::test]
async fn test_sha256() {
let mut setup = test_common::setup().await;
#[rustfmt::skip]
let testcases: Vec<(Vec<u8>, Vec<u8>)> = vec![
(hex!("").to_vec(), hex!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855").to_vec()),
(hex!("54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67").to_vec(), hex!("d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592").to_vec()),
];
for (input, expected) in testcases {
assert_eq!(
setup.client.hash(HashId::Sha256, &input).await.unwrap(),
expected
);
}
}
#[tokio::test]
async fn test_sha512() {
let mut setup = test_common::setup().await;
#[rustfmt::skip]
let testcases: Vec<(Vec<u8>, Vec<u8>)> = vec![
(hex!("").to_vec(), hex!("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e").to_vec()),
(hex!("54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67").to_vec(), hex!("07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6").to_vec()),
];
for (input, expected) in testcases {
assert_eq!(
setup.client.hash(HashId::Sha512, &input).await.unwrap(),
expected
);
}
}
#[tokio::test]
async fn test_secp256k1_get_master_fingerprint() {
let mut setup = test_common::setup().await;
assert_eq!(
setup
.client
.get_master_fingerprint(common::Curve::Secp256k1)
.await
.unwrap(),
hex!("f5acc2fd").to_vec()
);
}
#[tokio::test]
async fn test_secp256k1_derive_hd_node() {
let mut setup = test_common::setup().await;
let test_cases: Vec<(Vec<u32>, ([u8; 32], [u8; 32]))> = vec![
(
vec![],
(
hex!("eb473a0fa0af5031f14db9fe7c37bb8416a4ff01bb69dae9966dc83b5e5bf921"),
hex!("34ac5d784ebb4df4727bcddf6a6743f5d5d46d83dd74aa825866390c694f2938"),
),
),
(
vec![0x8000002c, 0x80000000, 0x80000001, 0, 3],
(
hex!("6da5f32f47232b3b9b2d6b59b802e2b313afa7cbda242f73da607139d8e04989"),
hex!("239841e64103fd024b01283e752a213fee1a8969f6825204ee3617a45c5e4a91"),
),
),
];
for (path, (exp_chaincode, exp_privkey)) in test_cases {
let res = setup
.client
.derive_hd_node(common::Curve::Secp256k1, path)
.await
.unwrap();
assert_eq!(res.len(), exp_chaincode.len() + exp_privkey.len());
let chaincode = &res[0..exp_chaincode.len()];
let privkey = &res[exp_chaincode.len()..];
assert_eq!(exp_chaincode, chaincode);
assert_eq!(exp_privkey, privkey);
}
}
#[tokio::test]
async fn test_secp256k1_point_add() {
let mut setup = test_common::setup().await;
let p = hex!("04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a");
let q = hex!("04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672");
let res = setup
.client
.ecpoint_add(common::Curve::Secp256k1, &p, &q)
.await
.unwrap();
assert_eq!(
res,
hex!("042f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4d8ac222636e5e3d6d4dba9dda6c9c426f788271bab0d6840dca87d3aa6ac62d6")
);
}
#[tokio::test]
async fn test_secp256k1_point_scalarmul() {
let mut setup = test_common::setup().await;
let p = hex!("0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8");
let k = hex!("22445566778899aabbccddeeff0011223344556677889900aabbccddeeff0011");
let res = setup
.client
.ecpoint_scalarmult(common::Curve::Secp256k1, &p, &k)
.await
.unwrap();
assert_eq!(
res,
hex!("042748bce8ffc3f815e69e594ae974be5e9a3be69a233d5557ea9c92b71d69367b747206115143153c85f3e8bb94d392bd955d36f1f0204921e6dd7684e81bdaab")
);
}
#[tokio::test]
async fn test_secp256k1_ecdsa_sign() {
let mut setup = test_common::setup().await;
let msg =
"If you don't believe me or don't get it, I don't have time to try to convince you, sorry.";
// compute the sha256 hash using the sha2 crate
let msg_hash = sha2::Sha256::digest(msg.as_bytes()).to_vec();
let privkey = hex!("4242424242424242424242424242424242424242424242424242424242424242");
let result = setup
.client
.ecdsa_sign(common::Curve::Secp256k1, &privkey, &msg_hash)
.await
.unwrap();
let expected_signature = hex!("304402201bbd5947e4a9cdf85d6efb0aeecdfa8c179480a1b972a3dd8b277a78a409dcdf022064c812320ad4f0ae5a3fa1ef5d66ef70c78922bd9d9e30b224d1b38671a3291b");
// signature is deterministic per RFC6979
assert_eq!(result, expected_signature);
}
#[tokio::test]
async fn test_secp256k1_ecdsa_verify() {
let mut setup = test_common::setup().await;
let msg =
"If you don't believe me or don't get it, I don't have time to try to convince you, sorry.";
// compute the sha256 hash using the sha2 crate
let msg_hash = sha2::Sha256::digest(msg.as_bytes()).to_vec();
let pubkey = hex!("0424653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c119fc5009a032aa9fe47f5e149bb8442f71f884ccb516590686d8ff6ab91c613");
let signature = hex!("304402201bbd5947e4a9cdf85d6efb0aeecdfa8c179480a1b972a3dd8b277a78a409dcdf022064c812320ad4f0ae5a3fa1ef5d66ef70c78922bd9d9e30b224d1b38671a3291b");
let result = setup
.client
.ecdsa_verify(common::Curve::Secp256k1, &pubkey, &msg_hash, &signature)
.await
.unwrap();
assert_eq!(result, vec![1]);
let sig_wrong = {
let mut sig = signature.clone();
sig[16] ^= 0x01;
sig
};
let result = setup
.client
.ecdsa_verify(common::Curve::Secp256k1, &pubkey, &msg_hash, &sig_wrong)
.await
.unwrap();
assert_eq!(result, vec![0]);
}
#[tokio::test]
async fn test_secp256k1_schnorr_sign() {
let mut setup = test_common::setup().await;
let msg =
"If you don't believe me or don't get it, I don't have time to try to convince you, sorry.";
let privkey = hex!("4242424242424242424242424242424242424242424242424242424242424242");
let result = setup
.client
.schnorr_sign(common::Curve::Secp256k1, &privkey, &msg.as_bytes())
.await
.unwrap();
// Schnorr signature using BIP340 is not deterministic. Check that the returned signature is valid instead
assert_eq!(result.len(), 64);
println!("Signature: {:?}", result);
let signature = k256::schnorr::Signature::try_from(result.as_slice()).unwrap();
// verify that the signature is valid using the k256 crate
let pubkey = k256::schnorr::VerifyingKey::from_bytes(&hex!(
"24653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c"
))
.unwrap();
// verify_raw uses unhashed messages; normally one wouldn't reaaly use this
pubkey.verify_raw(msg.as_bytes(), &signature).unwrap();
}
#[tokio::test]
async fn test_secp256k1_schnorr_verify() {
let mut setup = test_common::setup().await;
let msg =
"If you don't believe me or don't get it, I don't have time to try to convince you, sorry.";
let pubkey = hex!("0424653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1cee603aff65fcd55601b80a1eb6447bbd08e077b334ae9a6f97927008546e361c");
let signature = hex!("54a2a499ce77edc2599c3fdb99b66d461230165776abf6efebe3a86cb6b3a88e8bf4a388ff3fe1e424a907974826a991b2bb497691d055da66b1b5ba12bb67cc");
let result = setup
.client
.schnorr_verify(
common::Curve::Secp256k1,
&pubkey,
&msg.as_bytes(),
&signature,
)
.await
.unwrap();
assert_eq!(result, vec![1]);
let sig_wrong = {
let mut sig = signature.clone();
sig[16] ^= 0x01;
sig
};
let result = setup
.client
.schnorr_verify(
common::Curve::Secp256k1,
&pubkey,
&msg.as_bytes(),
&sig_wrong,
)
.await
.unwrap();
assert_eq!(result, vec![0]);
}