Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Jul 16, 2024
1 parent 3a95717 commit 9e04273
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion blockset-lib/src/secp256k1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
],
);
}
}

0 comments on commit 9e04273

Please sign in to comment.