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

bls crate #69

Merged
merged 16 commits into from
Aug 13, 2024
46 changes: 22 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ members = [
"crates/crypto/bn254/",
"crates/utils/",
"crates/crypto/bls/",
"crates/crypto/keystore/",
"crates/eigen-cli/",
"crates/metrics/collectors/economic/",
"crates/metrics/collectors/rpc_calls/",
Expand Down Expand Up @@ -63,7 +62,6 @@ eigen-client-fireblocks = { path = "crates/chainio/clients/fireblocks" }
eigen-contract-bindings = { path = "crates/contracts/bindings/" }
eigen-crypto-bls = { path = "crates/crypto/bls/" }
eigen-crypto-bn254 = { path = "crates/crypto/bn254/" }
eigen-crypto-keystore = { path = "crates/crypto/keystore/" }
eigen-metrics = { version = "0.0.1-alpha", path = "crates/metrics/" }
eigen-metrics-collectors-economic = { path = "crates/metrics/collectors/economic" }
eigen-metrics-collectors-rpc-calls = { path = "crates/metrics/collectors/rpc_calls" }
Expand Down Expand Up @@ -94,14 +92,14 @@ reth = { git = "https://github.com/paradigmxyz/reth" }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.121"
syn = "2.0"
testcontainers = "0.20.1"
thiserror = "1.0"
tokio = { version = "1.37.0", features = ["test-util", "full", "sync"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3", features = ["json"] }
url = "2.5.2"

#misc
testcontainers = "0.20.1"
rust-bls-bn254 = {git = "https://github.com/Layr-Labs/rust-bls-bn254.git", rev = "bd712a7", features = ["std"] }
#misc
parking_lot = "0.12"

#alloy
Expand Down
1 change: 0 additions & 1 deletion crates/chainio/clients/avsregistry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ eigen-types.workspace = true
eigen-crypto-bls.workspace = true
ark-ff.workspace = true
eigen-client-elcontracts.workspace = true
eigen-chainio-utils.workspace = true
eigen-utils.workspace = true
eigen-logging.workspace = true
ark-bn254 = "0.4.0"
Expand Down
11 changes: 11 additions & 0 deletions crates/chainio/clients/avsregistry/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy_contract::Error as AlloyError;
use eigen_client_elcontracts::error::ElContractsError;
use eigen_crypto_bls::error::BlsError;
use thiserror::Error;

/// Error returned by AvsRegistry
Expand Down Expand Up @@ -136,10 +137,20 @@ pub enum AvsRegistryError {
/// ElContractsError compatibility
#[error("ElContractsError: {0}")]
ElContractsError(String),

/// BlsError compatibility
#[error("BlsError :{0}")]
BlsError(String),
}

impl From<ElContractsError> for AvsRegistryError {
fn from(err: ElContractsError) -> Self {
AvsRegistryError::ElContractsError(err.to_string())
}
}

impl From<BlsError> for AvsRegistryError {
fn from(err: BlsError) -> Self {
AvsRegistryError::BlsError(err.to_string())
}
}
142 changes: 61 additions & 81 deletions crates/chainio/clients/avsregistry/src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use alloy_signer::SignerSync;
use alloy_signer::Signer;
use alloy_signer_local::PrivateKeySigner;
use ark_bn254::G1Projective;
use eigen_chainio_utils::{
convert_bn254_to_ark, convert_to_bn254_g1_point, convert_to_bn254_g2_point,
};
use eigen_client_elcontracts::reader::ELChainReader;
use eigen_logging::tracing_logger::TracingLogger;
use std::str::FromStr;

use eigen_utils::binding::{
BLSApkRegistry::{G1Point, PubkeyRegistrationParams},
RegistryCoordinator::{
self, G1Point as RegistryG1Point, G2Point as RegistryG2Point,
PubkeyRegistrationParams as RegistryPubkeyRegistrationParams,
},
use eigen_utils::binding::RegistryCoordinator::{
self, G1Point as RegistryG1Point, G2Point as RegistryG2Point, PubkeyRegistrationParams,
};
use std::str::FromStr;

use alloy_primitives::{Address, Bytes, FixedBytes, TxHash, U256};
use eigen_crypto_bls::attestation::KeyPair;
use eigen_crypto_bls::{
alloy_g1_point_to_g1_affine, convert_to_g1_point, convert_to_g2_point, BlsKeyPair,
};
use tracing::info;
use RegistryCoordinator::SignatureWithSaltAndExpiry;

Expand Down Expand Up @@ -126,15 +119,14 @@ impl AvsRegistryChainWriter {
/// Register operator in quorum with avs registry coordinator
pub async fn register_operator_in_quorum_with_avs_registry_coordinator(
&self,
bls_key_pair: KeyPair,
bls_key_pair: BlsKeyPair,
operator_to_avs_registration_sig_salt: FixedBytes<32>,
operator_to_avs_registration_sig_expiry: U256,
quorum_numbers: Bytes,
socket: String,
) -> Result<TxHash, AvsRegistryError> {
let provider = get_signer(self.signer.clone(), &self.provider);
let wallet = PrivateKeySigner::from_str(&self.signer).expect("failed to generate wallet ");

// tracing info
info!(avs_service_manager = %self.service_manager_addr, operator= %wallet.address(),quorum_numbers = ?quorum_numbers,"quorum_numbers,registering operator with the AVS's registry coordinator");
let contract_registry_coordinator =
Expand All @@ -149,85 +141,73 @@ impl AvsRegistryChainWriter {
let RegistryCoordinator::pubkeyRegistrationMessageHashReturn {
_0: g1_hashes_msg_to_sign,
} = g1_hashes_msg_to_sign_return;
let signed_msg = convert_to_bn254_g1_point(
bls_key_pair
.sign_hashes_to_curve_message(G1Projective::from(
convert_bn254_to_ark(G1Point {
X: g1_hashes_msg_to_sign.X,
Y: g1_hashes_msg_to_sign.Y,
})
.point,
))
.sig(),
);
let sig = bls_key_pair
.sign_hashed_to_curve_message(alloy_g1_point_to_g1_affine(
g1_hashes_msg_to_sign,
))
.g1_point();
let alloy_g1_point_signed_msg = convert_to_g1_point(sig.g1())?;

let g1_pubkey_bn254 = convert_to_bn254_g1_point(bls_key_pair.get_pub_key_g1());
let g2_projective = bls_key_pair
.get_pub_key_g2()
.expect("Failed to get g2 projective");
let g1_pub_key_bn254 = convert_to_g1_point(bls_key_pair.public_key().g1())?;

let g2_pubkey_bn254 = convert_to_bn254_g2_point(g2_projective);
let g2_pub_key_bn254 = convert_to_g2_point(bls_key_pair.public_key_g2().g2())?;

let pub_key_reg_params = PubkeyRegistrationParams {
pubkeyRegistrationSignature: signed_msg,
pubkeyG1: g1_pubkey_bn254,
pubkeyG2: g2_pubkey_bn254,
pubkeyRegistrationSignature: alloy_g1_point_signed_msg,
pubkeyG1: g1_pub_key_bn254,
pubkeyG2: g2_pub_key_bn254,
};

let msg_to_sign_result = self
let msg_to_sign = self
.el_reader
.calculate_operator_avs_registration_digest_hash(
wallet.address(),
self.service_manager_addr,
operator_to_avs_registration_sig_salt,
operator_to_avs_registration_sig_expiry,
)
.await;

match msg_to_sign_result {
Ok(msg_to_sign) => {
let operator_signature = wallet
.sign_message_sync(msg_to_sign.as_slice())
.expect("failed to sign message");

let operator_signature_with_salt_and_expiry = SignatureWithSaltAndExpiry {
signature: operator_signature.as_bytes().into(),
salt: operator_to_avs_registration_sig_salt,
expiry: operator_to_avs_registration_sig_expiry,
};

let contract_call = contract_registry_coordinator.registerOperator(
quorum_numbers.clone(),
socket,
RegistryPubkeyRegistrationParams {
pubkeyRegistrationSignature: RegistryG1Point {
X: pub_key_reg_params.pubkeyRegistrationSignature.X,
Y: pub_key_reg_params.pubkeyRegistrationSignature.Y,
},
pubkeyG1: RegistryG1Point {
X: pub_key_reg_params.pubkeyG1.X,
Y: pub_key_reg_params.pubkeyG1.Y,
},
pubkeyG2: RegistryG2Point {
X: pub_key_reg_params.pubkeyG2.X,
Y: pub_key_reg_params.pubkeyG2.Y,
},
},
operator_signature_with_salt_and_expiry,
);

let tx_call = contract_call.gas(2000000);
let tx_result = tx_call.send().await;

match tx_result {
Ok(tx) => {
info!(tx_hash = ?tx,"succesfully deregistered operator with the AVS's registry coordinator" );
return Ok(*tx.tx_hash());
}
Err(e) => Err(AvsRegistryError::AlloyContractError(e)),
}
.await?;

let operator_signature = wallet
.sign_hash(&msg_to_sign)
.await
.expect("failed to sign message");

let operator_signature_with_salt_and_expiry = SignatureWithSaltAndExpiry {
signature: operator_signature.as_bytes().into(),
salt: operator_to_avs_registration_sig_salt,
expiry: operator_to_avs_registration_sig_expiry,
};

let contract_call = contract_registry_coordinator.registerOperator(
quorum_numbers.clone(),
socket,
PubkeyRegistrationParams {
pubkeyRegistrationSignature: RegistryG1Point {
X: pub_key_reg_params.pubkeyRegistrationSignature.X,
Y: pub_key_reg_params.pubkeyRegistrationSignature.Y,
},
pubkeyG1: RegistryG1Point {
X: pub_key_reg_params.pubkeyG1.X,
Y: pub_key_reg_params.pubkeyG1.Y,
},
pubkeyG2: RegistryG2Point {
X: pub_key_reg_params.pubkeyG2.X,
Y: pub_key_reg_params.pubkeyG2.Y,
},
},
operator_signature_with_salt_and_expiry,
);

let tx_call = contract_call.gas(2000000);
let tx_result = tx_call.send().await;
supernovahs marked this conversation as resolved.
Show resolved Hide resolved

match tx_result {
Ok(tx) => {
info!(tx_hash = ?tx,"succesfully deregistered operator with the AVS's registry coordinator" );
Ok(*tx.tx_hash())
supernovahs marked this conversation as resolved.
Show resolved Hide resolved
}
Err(e) => Err(AvsRegistryError::ElContractsError(e.to_string())),
Err(e) => Err(AvsRegistryError::AlloyContractError(e)),
}
}
Err(_) => Err(AvsRegistryError::PubKeyRegistrationMessageHash),
Expand Down
4 changes: 2 additions & 2 deletions crates/chainio/clients/elcontracts/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ impl ELChainWriter {
Some(metadata) => {
let contract_call =
contract_delegation_manager.registerAsOperator(op_details, metadata);
contract_call.gas(130000)
contract_call.gas(300000)
supernovahs marked this conversation as resolved.
Show resolved Hide resolved
}
None => {
let contract_call =
contract_delegation_manager.registerAsOperator(op_details, "".to_string());
contract_call.gas(130000)
contract_call.gas(300000)
}
};
let binding_tx_result = binding.send().await;
Expand Down
Loading
Loading