Skip to content

Commit 0c56a0e

Browse files
committed
keystore: port _test_keystore_secp256k1_schnorr_sign to Rust
1 parent 718cfda commit 0c56a0e

File tree

2 files changed

+44
-56
lines changed

2 files changed

+44
-56
lines changed

src/rust/bitbox02/src/keystore.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,50 @@ mod tests {
401401
.is_ok());
402402
}
403403

404+
#[test]
405+
fn test_secp256k1_schnorr_sign() {
406+
mock_unlocked_using_mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", "");
407+
let keypath = [86 + HARDENED, 0 + HARDENED, 0 + HARDENED, 0, 0];
408+
let msg = [0x88u8; 32];
409+
410+
let expected_pubkey = {
411+
let pubkey =
412+
hex::decode("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115")
413+
.unwrap();
414+
secp256k1::XOnlyPublicKey::from_slice(&pubkey).unwrap()
415+
};
416+
417+
// Test without tweak
418+
crate::random::mock_reset();
419+
let sig = secp256k1_schnorr_sign(&keypath, &msg, None).unwrap();
420+
let secp = secp256k1::Secp256k1::new();
421+
assert!(secp
422+
.verify_schnorr(
423+
&secp256k1::schnorr::Signature::from_slice(&sig).unwrap(),
424+
&secp256k1::Message::from_digest_slice(&msg).unwrap(),
425+
&expected_pubkey
426+
)
427+
.is_ok());
428+
429+
// Test with tweak
430+
crate::random::mock_reset();
431+
let tweak = {
432+
let tweak =
433+
hex::decode("a39fb163dbd9b5e0840af3cc1ee41d5b31245c5dd8d6bdc3d026d09b8964997c")
434+
.unwrap();
435+
secp256k1::Scalar::from_be_bytes(tweak.try_into().unwrap()).unwrap()
436+
};
437+
let (tweaked_pubkey, _) = expected_pubkey.add_tweak(&secp, &tweak).unwrap();
438+
let sig = secp256k1_schnorr_sign(&keypath, &msg, Some(&tweak.to_be_bytes())).unwrap();
439+
assert!(secp
440+
.verify_schnorr(
441+
&secp256k1::schnorr::Signature::from_slice(&sig).unwrap(),
442+
&secp256k1::Message::from_digest_slice(&msg).unwrap(),
443+
&tweaked_pubkey
444+
)
445+
.is_ok());
446+
}
447+
404448
#[test]
405449
fn test_secp256k1_nonce_commit() {
406450
lock();

test/unit-test/test_keystore.c

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,6 @@ static void _test_keystore_create_and_store_seed(void** state)
140140
}
141141
}
142142

143-
static void _mock_with_mnemonic(const char* mnemonic, const char* passphrase)
144-
{
145-
uint8_t seed[32] = {0};
146-
size_t seed_len;
147-
assert_true(keystore_bip39_mnemonic_to_seed(mnemonic, seed, &seed_len));
148-
149-
_mock_unlocked(seed, seed_len, NULL);
150-
assert_true(keystore_unlock_bip39(passphrase));
151-
}
152-
153143
// This tests that `secp256k1_schnorrsig_sign()` is the correct function to be used for schnorr sigs
154144
// in taproot. It is a separate test because there are test vectors available for this which cannot
155145
// be made to work with `keystore_secp256k1_schnorr_bip86_sign()`.
@@ -208,59 +198,13 @@ static void _test_secp256k1_schnorr_sign(void** state)
208198
}
209199
}
210200

211-
static void _test_keystore_secp256k1_schnorr_sign(void** state)
212-
{
213-
_mock_with_mnemonic(
214-
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon "
215-
"about",
216-
"");
217-
const uint32_t keypath[] = {
218-
86 + BIP32_INITIAL_HARDENED_CHILD,
219-
0 + BIP32_INITIAL_HARDENED_CHILD,
220-
0 + BIP32_INITIAL_HARDENED_CHILD,
221-
0,
222-
0,
223-
};
224-
struct ext_key xpub = {0};
225-
assert_true(keystore_get_xpub(keypath, 5, &xpub));
226-
227-
uint8_t msg[32] = {0};
228-
memset(msg, 0x88, sizeof(msg));
229-
uint8_t sig[64] = {0};
230-
uint8_t mock_aux_rand[32] = {0};
231-
232-
// Test without tweak
233-
will_return(__wrap_random_32_bytes, mock_aux_rand);
234-
assert_true(keystore_secp256k1_schnorr_sign(keypath, 5, msg, NULL, sig));
235-
const secp256k1_context* ctx = wally_get_secp_context();
236-
secp256k1_pubkey pubkey = {0};
237-
assert_true(secp256k1_ec_pubkey_parse(ctx, &pubkey, xpub.pub_key, sizeof(xpub.pub_key)));
238-
secp256k1_xonly_pubkey xonly_pubkey = {0};
239-
assert_true(secp256k1_xonly_pubkey_from_pubkey(ctx, &xonly_pubkey, NULL, &pubkey));
240-
assert_true(secp256k1_schnorrsig_verify(ctx, sig, msg, sizeof(msg), &xonly_pubkey));
241-
242-
// Test with tweak
243-
const uint8_t tweak[32] =
244-
"\xa3\x9f\xb1\x63\xdb\xd9\xb5\xe0\x84\x0a\xf3\xcc\x1e\xe4\x1d\x5b\x31\x24\x5c\x5d\xd8\xd6"
245-
"\xbd\xc3\xd0\x26\xd0\x9b\x89\x64\x99\x7c";
246-
will_return(__wrap_random_32_bytes, mock_aux_rand);
247-
assert_true(keystore_secp256k1_schnorr_sign(keypath, 5, msg, tweak, sig));
248-
secp256k1_pubkey tweaked_pubkey = {0};
249-
assert_true(secp256k1_xonly_pubkey_tweak_add(ctx, &tweaked_pubkey, &xonly_pubkey, tweak));
250-
secp256k1_xonly_pubkey tweaked_xonly_pubkey = {0};
251-
assert_true(
252-
secp256k1_xonly_pubkey_from_pubkey(ctx, &tweaked_xonly_pubkey, NULL, &tweaked_pubkey));
253-
assert_true(secp256k1_schnorrsig_verify(ctx, sig, msg, sizeof(msg), &tweaked_xonly_pubkey));
254-
}
255-
256201
int main(void)
257202
{
258203
mock_memory_set_salt_root(_salt_root);
259204

260205
const struct CMUnitTest tests[] = {
261206
cmocka_unit_test(_test_keystore_create_and_store_seed),
262207
cmocka_unit_test(_test_secp256k1_schnorr_sign),
263-
cmocka_unit_test(_test_keystore_secp256k1_schnorr_sign),
264208
};
265209
return cmocka_run_group_tests(tests, NULL, NULL);
266210
}

0 commit comments

Comments
 (0)