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 : U256 bug and some bls tests #10

Merged
merged 3 commits into from
May 20, 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
6 changes: 5 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/chainio/clients/avsregistry/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl AvsRegistryChainWriter {

let g1_pubkey_bn254 = convert_to_bn254_g1_point(bls_key_pair.get_pub_key_g1());
let g2_projective = bls_key_pair
.gt_pub_key_g2()
.get_pub_key_g2()
.expect("Failed to get g2 projective");

let g2_pubkey_bn254 = convert_to_bn254_g2_point(g2_projective);
Expand Down
6 changes: 5 additions & 1 deletion crates/crypto/bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ ark-ff.workspace = true
eigen-crypto-bn254.workspace = true
thiserror.workspace = true
ark-ec = "0.4.2"
alloy-primitives.workspace = true
alloy-primitives.workspace = true

[dev-dependencies]
rand = "0.8.4"
tokio = { workspace = true, features = ["full"] }
79 changes: 77 additions & 2 deletions crates/crypto/bls/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ark_ec::{
pairing::{prepare_g1, prepare_g2, Pairing},
AffineRepr, CurveGroup,
};
use ark_ff::{BigInteger256, Field, One};
use ark_ff::{BigInteger256, Field, One, Zero};
use eigen_crypto_bn254::utils::{
get_g2_generator, mul_by_generator_g1, mul_by_generator_g2, u256_to_bigint256,
};
Expand Down Expand Up @@ -108,7 +108,7 @@ impl KeyPair {
self.pub_key
}

pub fn gt_pub_key_g2(&self) -> Result<G2Projective, BlsError> {
pub fn get_pub_key_g2(&self) -> Result<G2Projective, BlsError> {
let mul_result = mul_by_generator_g2(self.priv_key);

match mul_result {
Expand Down Expand Up @@ -185,3 +185,78 @@ impl G1Point {
)
}
}

#[cfg(test)]
mod tests {
use super::*;
use ark_ff::UniformRand;
use rand::{thread_rng, RngCore};

#[tokio::test]
async fn test_keypair_generation() {
let mut rng = thread_rng();
let private_key = Fr::rand(&mut rng);
let keypair = KeyPair::new(private_key).unwrap();
let pub_key = keypair.get_pub_key_g1();

// Check that the public key is not zero
assert_ne!(pub_key, G1Projective::zero());
}

#[tokio::test]
async fn test_signature_generation() {
let mut rng = thread_rng();
let private_key = Fr::rand(&mut rng);
let keypair = KeyPair::new(private_key).unwrap();

let message = [0u8; 32];
let msg_hash = hash_to_g1(&message);

let signature = keypair.sign_hashes_to_curve_message(msg_hash.into());

// Check that the signature is not zero
assert_ne!(signature.sig(), G1Projective::zero());
}

#[tokio::test]
async fn test_signature_verification() {
let mut rng = thread_rng();
let private_key = Fr::rand(&mut rng);
let keypair = KeyPair::new(private_key).unwrap();
let pub_key_g2 = keypair.get_pub_key_g2().unwrap();
// generate a random message
let mut message = [0u8; 32];
rng.fill_bytes(&mut message);

let msg_hash = hash_to_g1(&message);

let signature = keypair.sign_hashes_to_curve_message(msg_hash.into());

// Check that the signature is not zero
assert_ne!(signature.sig(), G1Projective::zero());

// Check that the signature verifies
assert!(signature.verify_signature(pub_key_g2, &message));
}

#[tokio::test]
async fn test_signature_verification_invalid() {
let mut rng = thread_rng();
let private_key = Fr::rand(&mut rng);
let keypair = KeyPair::new(private_key).unwrap();

let mut message = [0u8; 32];
rng.fill_bytes(&mut message);

let msg_hash = hash_to_g1(&message);

let signature = keypair.sign_hashes_to_curve_message(msg_hash.into());

// Check that the signature is not zero
assert_ne!(signature.sig(), G1Projective::zero());

// Check that the signature does not verify with a different public key
let different_pub_key = G2Projective::rand(&mut rng);
assert!(!signature.verify_signature(different_pub_key, &message));
}
}
6 changes: 5 additions & 1 deletion crates/crypto/bn254/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ ark-ff.workspace = true
ark-bn254 = "0.4.0"
ethers.workspace = true
thiserror.workspace = true
alloy-primitives.workspace = true
alloy-primitives.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["full"] }
rand = "0.8.4"
44 changes: 33 additions & 11 deletions crates/crypto/bn254/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ use ark_bn254::{Fq, Fq2, Fr, G1Affine, G1Projective, G2Affine, G2Projective};
use ark_ff::{BigInteger, BigInteger256};
use std::ops::Mul;
use std::str::FromStr;

/// Converts [U256] to [BigInteger256]
pub fn u256_to_bigint256(value: U256) -> BigInteger256 {
// Convert U256 to a byte array
let bytes = value.to_be_bytes::<32>();
// Convert the byte array to a bit array
let mut bits = [false; 256];
for (byte_idx, byte) in bytes.iter().enumerate() {
for bit_idx in 0..8 {
let bit = byte & (1 << bit_idx) != 0;
bits[byte_idx * 8 + bit_idx] = bit;
}
// Convert U256 to a big-endian byte array
let bytes: [u8; 32] = value.to_be_bytes();

// BigInteger256 expects a 4-element array of 64-bit values in little-endian order
let mut data = [0u64; 4];

// Iterate over the bytes in chunks of 8 bytes and convert to u64
for (i, chunk) in bytes.chunks(8).enumerate() {
let mut chunk_array = [0u8; 8];
chunk_array.copy_from_slice(chunk);
data[3 - i] = u64::from_be_bytes(chunk_array);
}
// Create a BigInteger256 from the byte array
BigInteger256::from_bits_be(&bits)

BigInteger256::new(data)
}

pub fn biginteger256_to_u256(bi: BigInteger256) -> U256 {
Expand Down Expand Up @@ -115,3 +118,22 @@ pub fn mul_by_generator_g2(pvt_key: Fr) -> Result<G2Projective, Bn254Err> {
pub fn verify_sig(sig: G1Affine, pub_key: G2Affine, msg: [u8; 32]) {
let g2_gen = get_g1_generator().unwrap();
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_u256_to_bigint256() {
let u256 = U256::from(123456789);
let result = u256_to_bigint256(u256);
assert_eq!(result, BigInteger256::from(123456789u32));
}

#[tokio::test]
async fn test_bigint256_to_u256() {
let bi = BigInteger256::from(123456789u32);
let result = biginteger256_to_u256(bi);
assert_eq!(result, U256::from(123456789));
}
}
5 changes: 3 additions & 2 deletions crates/services/bls_aggregation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ eigen-types.workspace = true
eigen-crypto-bls.workspace = true
eigen-services-avsregistry.workspace = true
parking_lot.workspace = true
tokio = {workspace = true, features = ["full"]}
eigen-crypto-bn254.workspace = true
alloy-primitives.workspace = true
alloy-primitives.workspace = true
tokio = { workspace = true, features = ["full"] }

Loading