eigen_types/
operator_pubkeys.rs1use alloy::primitives::U256;
2use ark_ff::PrimeField;
3use eigen_crypto_bls::{BlsG1Point, BlsG2Point, BlsKeyPair};
4use eigen_utils::slashing::middleware::bls_apk_registry::BN254::{G1Point, G2Point};
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct OperatorPubKeys {
8 pub g1_pub_key: BlsG1Point,
9 pub g2_pub_key: BlsG2Point,
10}
11
12impl From<BlsKeyPair> for OperatorPubKeys {
13 fn from(keypair: BlsKeyPair) -> Self {
14 Self {
15 g1_pub_key: keypair.public_key(),
16 g2_pub_key: keypair.public_key_g2(),
17 }
18 }
19}
20
21impl OperatorPubKeys {
22 pub fn to_contract_public_keys(&self) -> (G1Point, G2Point) {
23 let x = self.g1_pub_key.g1().x;
24 let y = self.g1_pub_key.g1().y;
25 let g1 = G1Point {
26 X: U256::from_limbs(x.into_bigint().0),
27 Y: U256::from_limbs(y.into_bigint().0),
28 };
29
30 let x2 = self.g2_pub_key.g2().x;
31 let y2 = self.g2_pub_key.g2().y;
32 let g2 = G2Point {
33 X: [
34 U256::from_limbs(x2.c0.into_bigint().0),
35 U256::from_limbs(x2.c1.into_bigint().0),
36 ],
37 Y: [
38 U256::from_limbs(y2.c0.into_bigint().0),
39 U256::from_limbs(y2.c1.into_bigint().0),
40 ],
41 };
42 (g1, g2)
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::OperatorPubKeys;
49 use alloy::primitives::U256;
50 use ark_bn254::{G1Affine, G2Affine};
51 use eigen_crypto_bls::{BlsG1Point, BlsG2Point};
52
53 #[test]
54 fn test_operator_pub_keys() {
55 let g1_pub_key = BlsG1Point::new(G1Affine {
56 x: ark_ff::Fp::from(1),
57 y: ark_ff::Fp::from(1),
58 infinity: false,
59 });
60
61 let g2_pub_key = BlsG2Point::new(G2Affine {
62 x: ark_ff::QuadExtField {
63 c0: ark_ff::Fp::from(1),
64 c1: ark_ff::Fp::from(1),
65 },
66 y: ark_ff::QuadExtField {
67 c0: ark_ff::Fp::from(1),
68 c1: ark_ff::Fp::from(1),
69 },
70 infinity: false,
71 });
72
73 let operator1_pubkeys = OperatorPubKeys {
74 g1_pub_key,
75 g2_pub_key,
76 };
77 let (g1, g2) = operator1_pubkeys.to_contract_public_keys();
78
79 let x_contract = g1.X;
81 let y_contract = g1.Y;
82 assert_eq!(x_contract, U256::from(1));
83 assert_eq!(y_contract, U256::from(1));
84
85 let x_g2_contract_0 = g2.X[0];
87 let x_g2_contract_1 = g2.X[1];
88 let y_g2_contract_0 = g2.Y[0];
89 let y_g2_contract_1 = g2.Y[1];
90
91 assert_eq!(x_g2_contract_0, U256::from(1));
92 assert_eq!(x_g2_contract_1, U256::from(1));
93 assert_eq!(y_g2_contract_0, U256::from(1));
94 assert_eq!(y_g2_contract_1, U256::from(1));
95 }
96}