From 9e04273d208f9d15a5fe30aaa25a04e8f4f0d8a9 Mon Sep 17 00:00:00 2001 From: Sergey Shandar Date: Tue, 16 Jul 2024 00:20:09 -0700 Subject: [PATCH] f --- blockset-lib/src/secp256k1/mod.rs | 69 ++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/blockset-lib/src/secp256k1/mod.rs b/blockset-lib/src/secp256k1/mod.rs index 0dcda8f2..2d1fe857 100644 --- a/blockset-lib/src/secp256k1/mod.rs +++ b/blockset-lib/src/secp256k1/mod.rs @@ -13,7 +13,7 @@ impl Order { const fn public_key(self) -> Point { point::mul(G, self) } - const fn sign(self, z: Order, k: Order) -> Signature { + pub const fn sign(self, z: Order, k: Order) -> Signature { let r = Order::new(point::mul(G, k)[0].0); let s = z.add(r.mul(self)).div(k); [r, s] @@ -27,3 +27,70 @@ const fn verify(pub_key: Point, z: Order, [r, s]: Signature) -> bool { let p = Order::new(point::add(point::mul(G, u1), point::mul(pub_key, u2))[0].0); p.eq(&r) } + +#[cfg(test)] +mod tests { + use wasm_bindgen_test::wasm_bindgen_test; + + use super::{verify, Order}; + + #[test] + #[wasm_bindgen_test] + fn test() { + let f = |p, h, k| { + let private_key = Order::new(p); + let public_key = private_key.public_key(); + let hash = Order::new(h); + let k = Order::new(k); + let signature = private_key.sign(hash, k); + let result = verify(public_key, hash, signature); + assert!(result); + let w_private_key = private_key.add(Order::_1); + let w_signature = w_private_key.sign(hash, k); + let w_result = verify(public_key, hash, w_signature); + assert!(!w_result); + }; + f( + [ + 1234567890_1234567890_1234567890_123456789, + 234567890_1234567890_1234567890_1234567890, + ], + [ + 34567890_1234567890_1234567890_1234567890, + 4567890_1234567890_1234567890_1234567890_1, + ], + [ + 567890_1234567890_1234567890_1234567890_12, + 67890_1234567890_1234567890_1234567890_123, + ], + ); + f( + [ + 7890_1234567890_1234567890_1234567890_1234, + 890_1234567890_1234567890_1234567890_12345, + ], + [ + 90_1234567890_1234567890_1234567890_123456, + 1234567890_1234567890_1234567890_123456790, + ], + [ + 1234567890_1234567890_1234567890_123456792, + 34567890_1234567890_1234567890_1234567891, + ], + ); + f( + [ + 1111111111_2222222222_3333333333_444444444, + 4444444444_5555555555_6666666666_77777777, + ], + [ + 8888888888_9999999999_0000000000_11111111, + 2222222222_3333333333_4444444444_555555555, + ], + [ + 6666666666_7777777777_8888888888_99999999, + 3333333333_4444444444_5555555555_66666666, + ], + ); + } +}