use eigensdk::crypto_bls::{BlsG1Point, BlsG2Point};
use eigensdk::services_blsaggregation::bls_aggregation_service_response::BlsAggregationServiceResponse;
pub trait ContractG1Point: Sized + Clone + Send + Sync + 'static {
type X;
type Y;
fn new(x: Self::X, y: Self::Y) -> Self;
}
pub trait ContractG2Point: Sized + Clone + Send + Sync + 'static {
type X;
type Y;
fn new(x: Self::X, y: Self::Y) -> Self;
}
pub trait NonSignerStakesAndSignature<G1, G2>: Sized + Clone + Send + Sync + 'static
where
G1: ContractG1Point,
G2: ContractG2Point,
{
#[allow(clippy::too_many_arguments)]
fn new(
non_signer_pubkeys: Vec<G1>,
non_signer_quorum_bitmap_indices: Vec<u32>,
quorum_apks: Vec<G1>,
apk_g2: G2,
sigma: G1,
quorum_apk_indices: Vec<u32>,
total_stake_indices: Vec<u32>,
non_signer_stake_indices: Vec<Vec<u32>>,
) -> Self;
}
pub fn convert_to_contract_g1<G1: ContractG1Point>(
point: &BlsG1Point,
convert_g1: impl FnOnce(&BlsG1Point) -> (G1::X, G1::Y),
) -> G1 {
let (x, y) = convert_g1(point);
G1::new(x, y)
}
pub fn convert_to_contract_g2<G2: ContractG2Point>(
point: &BlsG2Point,
convert_g2: impl FnOnce(&BlsG2Point) -> (G2::X, G2::Y),
) -> G2 {
let (x, y) = convert_g2(point);
G2::new(x, y)
}
pub fn convert_aggregation_response<G1, G2, NSS>(
response: &BlsAggregationServiceResponse,
convert_g1: impl Fn(&BlsG1Point) -> (G1::X, G1::Y) + Copy,
convert_g2: impl Fn(&BlsG2Point) -> (G2::X, G2::Y),
) -> NSS
where
G1: ContractG1Point,
G2: ContractG2Point,
NSS: NonSignerStakesAndSignature<G1, G2>,
{
let non_signer_pubkeys = response
.non_signers_pub_keys_g1
.iter()
.map(|pk| convert_to_contract_g1(pk, convert_g1))
.collect();
let quorum_apks = response
.quorum_apks_g1
.iter()
.map(|pk| convert_to_contract_g1(pk, convert_g1))
.collect();
let apk_g2 = convert_to_contract_g2(&response.signers_apk_g2, convert_g2);
let sigma = convert_to_contract_g1(&response.signers_agg_sig_g1.g1_point(), convert_g1);
NSS::new(
non_signer_pubkeys,
response.non_signer_quorum_bitmap_indices.clone(),
quorum_apks,
apk_g2,
sigma,
response.quorum_apk_indices.clone(),
response.total_stake_indices.clone(),
response.non_signer_stake_indices.clone(),
)
}