mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
Make the bitcoin Algorithm test a unit test
This commit is contained in:
@@ -14,3 +14,6 @@ pub(crate) mod crypto;
|
||||
pub mod wallet;
|
||||
/// A minimal asynchronous Bitcoin RPC client.
|
||||
pub mod rpc;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
47
coins/bitcoin/src/tests/crypto.rs
Normal file
47
coins/bitcoin/src/tests/crypto.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use rand_core::OsRng;
|
||||
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use secp256k1::{SECP256K1, Message};
|
||||
|
||||
use k256::Scalar;
|
||||
use transcript::{Transcript, RecommendedTranscript};
|
||||
use frost::{
|
||||
curve::Secp256k1,
|
||||
Participant,
|
||||
tests::{algorithm_machines, key_gen, sign},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
bitcoin::hashes::{Hash as HashTrait, sha256::Hash},
|
||||
crypto::{x_only, make_even, Schnorr},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_algorithm() {
|
||||
let mut keys = key_gen::<_, Secp256k1>(&mut OsRng);
|
||||
const MESSAGE: &[u8] = b"Hello, World!";
|
||||
|
||||
for (_, keys) in keys.iter_mut() {
|
||||
let (_, offset) = make_even(keys.group_key());
|
||||
*keys = keys.offset(Scalar::from(offset));
|
||||
}
|
||||
|
||||
let algo =
|
||||
Schnorr::<RecommendedTranscript>::new(RecommendedTranscript::new(b"bitcoin-serai sign test"));
|
||||
let sig = sign(
|
||||
&mut OsRng,
|
||||
algo.clone(),
|
||||
keys.clone(),
|
||||
algorithm_machines(&mut OsRng, algo, &keys),
|
||||
&Sha256::digest(MESSAGE),
|
||||
);
|
||||
|
||||
SECP256K1
|
||||
.verify_schnorr(
|
||||
&sig,
|
||||
&Message::from(Hash::hash(MESSAGE)),
|
||||
&x_only(&keys[&Participant::new(1).unwrap()].group_key()),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
1
coins/bitcoin/src/tests/mod.rs
Normal file
1
coins/bitcoin/src/tests/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
mod crypto;
|
||||
Reference in New Issue
Block a user