2022-07-16 21:45:41 +00:00
|
|
|
use std::{convert::TryFrom, sync::Arc, time::Duration};
|
|
|
|
|
2022-08-30 06:13:53 +00:00
|
|
|
use rand_core::OsRng;
|
|
|
|
|
|
|
|
use k256::{elliptic_curve::bigint::ArrayEncoding, U256};
|
|
|
|
|
|
|
|
use ethers::{
|
|
|
|
prelude::*,
|
|
|
|
utils::{keccak256, Anvil, AnvilInstance},
|
|
|
|
};
|
|
|
|
|
|
|
|
use frost::{
|
|
|
|
curve::Secp256k1,
|
|
|
|
algorithm::Schnorr as Algo,
|
|
|
|
tests::{key_gen, algorithm_machines, sign},
|
|
|
|
};
|
|
|
|
|
|
|
|
use ethereum_serai::{
|
|
|
|
crypto,
|
|
|
|
contract::{Schnorr, call_verify, deploy_schnorr_verifier_contract},
|
|
|
|
};
|
|
|
|
|
|
|
|
async fn deploy_test_contract(
|
|
|
|
) -> (u32, AnvilInstance, Schnorr<SignerMiddleware<Provider<Http>, LocalWallet>>) {
|
2022-07-16 21:45:41 +00:00
|
|
|
let anvil = Anvil::new().spawn();
|
2022-08-30 06:13:53 +00:00
|
|
|
|
2022-07-16 21:45:41 +00:00
|
|
|
let wallet: LocalWallet = anvil.keys()[0].clone().into();
|
|
|
|
let provider =
|
|
|
|
Provider::<Http>::try_from(anvil.endpoint()).unwrap().interval(Duration::from_millis(10u64));
|
2022-08-30 06:13:53 +00:00
|
|
|
let chain_id = provider.get_chainid().await.unwrap().as_u32();
|
|
|
|
let client = Arc::new(SignerMiddleware::new_with_provider_chain(provider, wallet).await.unwrap());
|
2022-07-16 21:45:41 +00:00
|
|
|
|
2022-08-30 06:13:53 +00:00
|
|
|
(chain_id, anvil, deploy_schnorr_verifier_contract(client).await.unwrap())
|
2022-07-16 21:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2022-08-30 06:13:53 +00:00
|
|
|
async fn test_deploy_contract() {
|
|
|
|
deploy_test_contract().await;
|
|
|
|
}
|
2022-07-16 21:45:41 +00:00
|
|
|
|
2022-08-30 06:13:53 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_ecrecover_hack() {
|
|
|
|
let (chain_id, _anvil, contract) = deploy_test_contract().await;
|
|
|
|
let chain_id = U256::from(chain_id);
|
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
|
|
|
let hashed_message = keccak256(MESSAGE);
|
|
|
|
|
|
|
|
let full_message = &[chain_id.to_be_byte_array().as_slice(), &hashed_message].concat();
|
|
|
|
|
2022-12-09 00:04:35 +00:00
|
|
|
let algo = Algo::<Secp256k1, crypto::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 mut processed_sig =
|
|
|
|
crypto::process_signature_for_contract(hashed_message, &sig.R, sig.s, &group_key, chain_id);
|
|
|
|
|
|
|
|
call_verify(&contract, &processed_sig).await.unwrap();
|
|
|
|
|
|
|
|
// test invalid signature fails
|
|
|
|
processed_sig.message[0] = 0;
|
2022-08-30 06:13:53 +00:00
|
|
|
assert!(call_verify(&contract, &processed_sig).await.is_err());
|
2022-07-16 21:45:41 +00:00
|
|
|
}
|