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

Fix BLS Aggregation for multiple quorums #160

Merged
merged 6 commits into from
Nov 14, 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
22 changes: 19 additions & 3 deletions crates/services/avsregistry/src/fake_avs_registry_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,28 @@ impl AvsRegistryService for FakeAvsRegistryService {
async fn get_operators_avs_state_at_block(
&self,
block_number: u32,
_quorum_nums: &[u8],
quorum_nums: &[u8],
) -> Result<HashMap<FixedBytes<32>, OperatorAvsState>, AvsRegistryError> {
self.operators
let mut operators_state = self
.operators
.get(&(block_number as u64))
.ok_or(AvsRegistryError::GetOperatorState)
.cloned()
.cloned()?;

// for each operator, update the stake_per_quorum to remove keys that are not contained in quorum_nums
for (_id, operator) in operators_state.iter_mut() {
let keys_to_remove: Vec<_> = operator
.stake_per_quorum
.keys()
.filter(|quorum_num| !quorum_nums.contains(quorum_num))
.cloned()
.collect();

for quorum_num in keys_to_remove {
operator.stake_per_quorum.remove(&quorum_num);
}
}
Ok(operators_state)
}

async fn get_quorums_avs_state_at_block(
Expand Down
79 changes: 56 additions & 23 deletions crates/services/bls_aggregation/src/bls_agg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::{FixedBytes, U256};
use ark_bn254::G2Affine;
use ark_bn254::{G1Affine, G2Affine};
use ark_ec::AffineRepr;
use eigen_crypto_bls::{BlsG1Point, BlsG2Point, Signature};
use eigen_crypto_bn254::utils::verify_message;
Expand Down Expand Up @@ -221,24 +221,26 @@ impl<A: AvsRegistryService + Send + Sync + Clone + 'static> BlsAggregatorService
operator_state: OperatorAvsState,
signed_task_digest: SignedTaskResponseDigest,
) -> &mut AggregatedOperators {
aggregated_operators.signers_agg_sig_g1 = Signature::new(
(aggregated_operators.signers_agg_sig_g1.g1_point().g1()
+ signed_task_digest.bls_signature.g1_point().g1())
.into(),
);
let operator_g2_pubkey = operator_state
.operator_info
.pub_keys
.clone()
.unwrap()
.g2_pub_key
.g2();
aggregated_operators.signers_apk_g2 =
BlsG2Point::new((aggregated_operators.signers_apk_g2.g2() + operator_g2_pubkey).into());
aggregated_operators
.signers_operator_ids_set
.insert(signed_task_digest.operator_id, true);
for (quorum_num, stake) in operator_state.stake_per_quorum.iter() {
// For each quorum the operator has stake in, we aggregate the signature and update the stake
aggregated_operators.signers_agg_sig_g1 = Signature::new(
(aggregated_operators.signers_agg_sig_g1.g1_point().g1()
+ signed_task_digest.bls_signature.g1_point().g1())
.into(),
);
aggregated_operators.signers_apk_g2 = BlsG2Point::new(
(aggregated_operators.signers_apk_g2.g2() + operator_g2_pubkey).into(),
);
aggregated_operators
.signers_total_stake_per_quorum
.entry(*quorum_num)
Expand Down Expand Up @@ -374,14 +376,30 @@ impl<A: AvsRegistryService + Send + Sync + Clone + 'static> BlsAggregatorService
)
.clone()
})
.unwrap_or(AggregatedOperators {
signers_apk_g2: BlsG2Point::new((G2Affine::zero() + operator_g2_pubkey).into()),
signers_agg_sig_g1: signed_task_digest.bls_signature.clone(),
signers_operator_ids_set: HashMap::from([(
operator_state.operator_id.into(),
true,
)]),
signers_total_stake_per_quorum: operator_state.stake_per_quorum.clone(),
.unwrap_or_else(|| {
let mut signers_apk_g2 = BlsG2Point::new(G2Affine::zero());
let mut signers_agg_sig_g1 = Signature::new(G1Affine::zero());
for _ in 0..operator_state.stake_per_quorum.len() {
// for each quorum the operator has stake in, the signature is aggregated
// see signature verification logic here:
// https://github.com/Layr-Labs/eigenlayer-middleware/blob/7d49b5181b09198ed275783453aa082bb3766990/src/BLSSignatureChecker.sol#L161-L168
signers_apk_g2 =
BlsG2Point::new((signers_apk_g2.g2() + operator_g2_pubkey).into());
signers_agg_sig_g1 = Signature::new(
(signers_agg_sig_g1.g1_point().g1()
+ signed_task_digest.bls_signature.g1_point().g1())
.into(),
);
}
AggregatedOperators {
signers_apk_g2,
signers_agg_sig_g1,
signers_operator_ids_set: HashMap::from([(
operator_state.operator_id.into(),
true,
)]),
signers_total_stake_per_quorum: operator_state.stake_per_quorum.clone(),
}
});

aggregated_operators.insert(
Expand Down Expand Up @@ -971,8 +989,14 @@ mod tests {
.unwrap();

let quorum_apks_g1 = aggregate_g1_public_keys(&test_operators);
let signers_apk_g2 = aggregate_g2_public_keys(&test_operators);
let signers_agg_sig_g1 = aggregate_g1_signatures(&[bls_sig_op_1, bls_sig_op_2]);
let signers_apk_g2 =
aggregate_g2_public_keys(&[test_operators.clone(), test_operators].concat());
let signers_agg_sig_g1 = aggregate_g1_signatures(&[
bls_sig_op_1.clone(),
bls_sig_op_1,
bls_sig_op_2.clone(),
bls_sig_op_2,
]);

let expected_agg_service_response = BlsAggregationServiceResponse {
task_index,
Expand Down Expand Up @@ -1101,9 +1125,14 @@ mod tests {
.unwrap();

let quorum_apks_g1 = aggregate_g1_public_keys(&test_operators);
let signers_apk_g2 = aggregate_g2_public_keys(&test_operators);
let signers_agg_sig_g1_task_1 =
aggregate_g1_signatures(&[bls_sig_task_1_op_1, bls_sig_task_1_op_2]);
let signers_apk_g2 =
aggregate_g2_public_keys(&[test_operators.clone(), test_operators].concat());
let signers_agg_sig_g1_task_1 = aggregate_g1_signatures(&[
bls_sig_task_1_op_1.clone(),
bls_sig_task_1_op_1,
bls_sig_task_1_op_2.clone(),
bls_sig_task_1_op_2,
]);

let expected_response_task_1 = BlsAggregationServiceResponse {
task_index: task_1_index,
Expand All @@ -1118,8 +1147,12 @@ mod tests {
non_signer_stake_indices: vec![],
};

let signers_agg_sig_g1_task_2 =
aggregate_g1_signatures(&[bls_sig_task_2_op_1, bls_sig_task_2_op_2]);
let signers_agg_sig_g1_task_2 = aggregate_g1_signatures(&[
bls_sig_task_2_op_1.clone(),
bls_sig_task_2_op_1,
bls_sig_task_2_op_2.clone(),
bls_sig_task_2_op_2,
]);

let expected_response_task_2 = BlsAggregationServiceResponse {
task_index: task_2_index,
Expand Down
Loading
Loading