Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup rust BLS example #11

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/bls-multisig/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ alloy = { version = "0.4", features = [
"contract",
"sol-types",
"node-bindings",
"rpc-types",
"getrandom",
] }
tokio = { version = "1", features = ["full"] }
blst = "0.3"
Expand Down
150 changes: 99 additions & 51 deletions examples/bls-multisig/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use alloy::{primitives::U256, providers::ProviderBuilder, sol, sol_types::SolValue};
use blst::min_pk::{AggregateSignature, SecretKey, Signature};
use rand::RngCore;
use BLS::G2Point;
use alloy::{
network::TransactionBuilder,
primitives::{Address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
sol,
sol_types::SolValue,
};
use blst::min_pk::{AggregateSignature, PublicKey, SecretKey, Signature};
use rand::{seq::IteratorRandom, RngCore};

sol! {
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -10,50 +16,29 @@ sol! {
"../../../out/BLSMultisig.sol/BLSMultisig.json"
}

impl From<[u8; 96]> for BLS::G1Point {
fn from(value: [u8; 96]) -> Self {
let mut data = [0u8; 128];
data[16..64].copy_from_slice(&value[0..48]);
data[80..128].copy_from_slice(&value[48..96]);

BLS::G1Point::abi_decode(&data, false).unwrap()
}
}

impl From<[u8; 192]> for BLS::G2Point {
fn from(value: [u8; 192]) -> Self {
let mut data = [0u8; 256];
data[16..64].copy_from_slice(&value[48..96]);
data[80..128].copy_from_slice(&value[0..48]);
data[144..192].copy_from_slice(&value[144..192]);
data[208..256].copy_from_slice(&value[96..144]);

BLS::G2Point::abi_decode(&data, false).unwrap()
}
}

/// Generates `num` BLS keys and returns them as a tuple of secret keys and public keys, sorted by public key.
/// Generates `num` BLS keys and returns them as a tuple of private and public keys
fn generate_keys(num: usize) -> (Vec<SecretKey>, Vec<BLS::G1Point>) {
let mut rng = rand::thread_rng();
let mut keys = Vec::with_capacity(num);

let mut public = Vec::with_capacity(num);
let mut private = Vec::with_capacity(num);

for _ in 0..num {
let mut ikm = [0u8; 32];
rng.fill_bytes(&mut ikm);

let sk = SecretKey::key_gen(&ikm, &[]).unwrap();
let pk: BLS::G1Point = sk.sk_to_pk().serialize().into();
let pk = BLS::G1Point::from(sk.sk_to_pk());

keys.push((sk, pk));
public.push(pk);
private.push(sk);
}

keys.sort_by(|(_, pk1), (_, pk2)| pk1.cmp(pk2));

keys.into_iter().unzip()
(private, public)
}

/// Signs a message with the provided keys and returns the aggregated signature.
fn sign_message(keys: &[SecretKey], msg: &[u8]) -> G2Point {
fn sign_message(keys: &[&SecretKey], msg: &[u8]) -> BLS::G2Point {
let mut sigs = Vec::new();

// create individual signatures
Expand All @@ -62,39 +47,102 @@ fn sign_message(keys: &[SecretKey], msg: &[u8]) -> G2Point {
sigs.push(sig);
}

let agg_sig = Signature::from_aggregate(
// aggregate
Signature::from_aggregate(
&AggregateSignature::aggregate(sigs.iter().collect::<Vec<_>>().as_slice(), false).unwrap(),
);

agg_sig.serialize().into()
)
.into()
}

#[tokio::main]
pub async fn main() {
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Spawn Anvil node and connect to it
let provider = ProviderBuilder::new().on_anvil_with_config(|config| config.arg("--alphanet"));

let (keys, signers) = generate_keys(100);

let multisig = BLSMultisig::deploy(provider, signers.clone(), U256::from(1))
.await
.unwrap();

let operation = BLSMultisig::Operation::default();

// Generate 100 BLS keys
let (private_keys, public_keys) = generate_keys(100);

// Deploy multisig contract, configuring generated keys as signers and requiring threshold of 50
let multisig = BLSMultisig::deploy(&provider, public_keys.clone(), U256::from(50)).await?;

// Fund multisig with some ETH
provider
.send_transaction(
TransactionRequest::default()
.to(*multisig.address())
.with_value(U256::from(1_000_000_000_000_000_000u128)),
)
.await?
.watch()
.await?;

// Operation which will transfer 1 ETH to a random address
let operation = BLSMultisig::Operation {
to: Address::random(),
value: U256::from(1_000_000_000_000_000_000u128),
nonce: multisig.nonce().call().await?._0,
data: Default::default(),
};

// Choose 50 random signers to sign the operation
let (keys, signers): (Vec<_>, Vec<_>) = {
let mut pairs = private_keys
.iter()
.zip(public_keys.clone())
.choose_multiple(&mut rand::thread_rng(), 50);

// contract requires signers to be sorted by public key
pairs.sort_by(|(_, pk1), (_, pk2)| pk1.cmp(pk2));

pairs.into_iter().unzip()
};

// Sign abi-encoded operation with the chosen keys
let signature = sign_message(&keys, &operation.abi_encode());

// Send the signed operation to the contract along with the list of signers
let receipt = multisig
.verifyAndExecute(BLSMultisig::SignedOperation {
operation: operation.clone(),
signers,
signature,
})
.send()
.await
.unwrap()
.await?
.get_receipt()
.await
.unwrap();
.await?;

// Assert that the transaction was successful and that recipient has received the funds
assert!(receipt.status());
assert!(provider.get_balance(operation.to).await? > U256::ZERO);

Ok(())
}

/// Converts a blst [`PublicKey`] to a [`BLS::G1Point`] which can be passed to the contract
impl From<PublicKey> for BLS::G1Point {
fn from(value: PublicKey) -> Self {
let serialized = value.serialize();

let mut data = [0u8; 128];
data[16..64].copy_from_slice(&serialized[0..48]);
data[80..128].copy_from_slice(&serialized[48..96]);

BLS::G1Point::abi_decode(&data, false).unwrap()
}
}

/// Converts a blst [`Signature`] to a [`BLS::G2Point`] which can be passed to the contract
impl From<Signature> for BLS::G2Point {
fn from(value: Signature) -> Self {
let serialized = value.serialize();

let mut data = [0u8; 256];
data[16..64].copy_from_slice(&serialized[48..96]);
data[80..128].copy_from_slice(&serialized[0..48]);
data[144..192].copy_from_slice(&serialized[144..192]);
data[208..256].copy_from_slice(&serialized[96..144]);

BLS::G2Point::abi_decode(&data, false).unwrap()
}
}