2022-07-16 21:45:41 +00:00
|
|
|
use ethereum_serai::crypto::*;
|
|
|
|
use frost::curve::Secp256k1;
|
|
|
|
use k256::{
|
|
|
|
elliptic_curve::{bigint::ArrayEncoding, ops::Reduce, sec1::ToEncodedPoint},
|
|
|
|
ProjectivePoint, Scalar, U256,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ecrecover() {
|
|
|
|
use k256::ecdsa::{
|
|
|
|
recoverable::Signature,
|
|
|
|
signature::{Signer, Verifier},
|
|
|
|
SigningKey, VerifyingKey,
|
|
|
|
};
|
2022-07-27 09:43:23 +00:00
|
|
|
use rand_core::OsRng;
|
2022-07-16 21:45:41 +00:00
|
|
|
|
|
|
|
let private = SigningKey::random(&mut OsRng);
|
|
|
|
let public = VerifyingKey::from(&private);
|
|
|
|
|
2023-01-01 09:18:23 +00:00
|
|
|
const MESSAGE: &[u8] = b"Hello, World!";
|
2022-07-16 21:45:41 +00:00
|
|
|
let sig: Signature = private.sign(MESSAGE);
|
|
|
|
public.verify(MESSAGE, &sig).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
ecrecover(hash_to_scalar(MESSAGE), sig.as_ref()[64], *sig.r(), *sig.s()).unwrap(),
|
|
|
|
address(&ProjectivePoint::from(public))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signing() {
|
|
|
|
use frost::{
|
|
|
|
algorithm::Schnorr,
|
|
|
|
tests::{algorithm_machines, key_gen, sign},
|
|
|
|
};
|
2022-07-27 09:43:23 +00:00
|
|
|
use rand_core::OsRng;
|
2022-07-16 21:45:41 +00:00
|
|
|
|
|
|
|
let keys = key_gen::<_, Secp256k1>(&mut OsRng);
|
|
|
|
let _group_key = keys[&1].group_key();
|
|
|
|
|
2023-01-01 09:18:23 +00:00
|
|
|
const MESSAGE: &[u8] = b"Hello, World!";
|
2022-07-16 21:45:41 +00:00
|
|
|
|
2022-12-09 00:04:35 +00:00
|
|
|
let algo = Schnorr::<Secp256k1, EthereumHram>::new();
|
2022-07-16 21:45:41 +00:00
|
|
|
let _sig = sign(
|
|
|
|
&mut OsRng,
|
2023-01-01 09:18:23 +00:00
|
|
|
algo,
|
2022-12-09 00:04:35 +00:00
|
|
|
keys.clone(),
|
2022-07-16 21:45:41 +00:00
|
|
|
algorithm_machines(&mut OsRng, Schnorr::<Secp256k1, EthereumHram>::new(), &keys),
|
|
|
|
MESSAGE,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ecrecover_hack() {
|
|
|
|
use frost::{
|
|
|
|
algorithm::Schnorr,
|
|
|
|
tests::{algorithm_machines, key_gen, sign},
|
|
|
|
};
|
2022-07-27 09:43:23 +00:00
|
|
|
use rand_core::OsRng;
|
2022-07-16 21:45:41 +00:00
|
|
|
|
|
|
|
let keys = key_gen::<_, Secp256k1>(&mut OsRng);
|
|
|
|
let group_key = keys[&1].group_key();
|
|
|
|
let group_key_encoded = group_key.to_encoded_point(true);
|
|
|
|
let group_key_compressed = group_key_encoded.as_ref();
|
|
|
|
let group_key_x = Scalar::from_uint_reduced(U256::from_be_slice(&group_key_compressed[1 .. 33]));
|
|
|
|
|
2023-01-01 09:18:23 +00:00
|
|
|
const MESSAGE: &[u8] = b"Hello, World!";
|
2022-07-16 21:45:41 +00:00
|
|
|
let hashed_message = keccak256(MESSAGE);
|
2022-08-30 06:13:53 +00:00
|
|
|
let chain_id = U256::ONE;
|
2022-07-16 21:45:41 +00:00
|
|
|
|
|
|
|
let full_message = &[chain_id.to_be_byte_array().as_slice(), &hashed_message].concat();
|
|
|
|
|
2022-12-09 00:04:35 +00:00
|
|
|
let algo = Schnorr::<Secp256k1, EthereumHram>::new();
|
2022-07-16 21:45:41 +00:00
|
|
|
let sig = sign(
|
|
|
|
&mut OsRng,
|
2022-12-09 00:04:35 +00:00
|
|
|
algo.clone(),
|
|
|
|
keys.clone(),
|
|
|
|
algorithm_machines(&mut OsRng, algo, &keys),
|
2022-07-16 21:45:41 +00:00
|
|
|
full_message,
|
|
|
|
);
|
|
|
|
|
|
|
|
let (sr, er) =
|
|
|
|
preprocess_signature_for_ecrecover(hashed_message, &sig.R, sig.s, &group_key, chain_id);
|
|
|
|
let q = ecrecover(sr, group_key_compressed[0] - 2, group_key_x, er).unwrap();
|
|
|
|
assert_eq!(q, address(&sig.R));
|
|
|
|
}
|