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

feat: Add method in KeyPair : FromString and tests #11

Merged
merged 2 commits into from
May 22, 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
1 change: 1 addition & 0 deletions 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/crypto/bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ eigen-crypto-bn254.workspace = true
thiserror.workspace = true
ark-ec = "0.4.2"
alloy-primitives.workspace = true

hex = "0.4.3"
[dev-dependencies]
rand = "0.8.4"
tokio = { workspace = true, features = ["full"] }
62 changes: 58 additions & 4 deletions crates/crypto/bls/src/attestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use ark_ec::{
pairing::{prepare_g1, prepare_g2, Pairing},
AffineRepr, CurveGroup,
};
use ark_ff::{BigInteger256, Field, One, Zero};
use ark_ff::{BigInteger256, Field, Fp256, One, PrimeField};
use eigen_crypto_bn254::utils::{
get_g2_generator, mul_by_generator_g1, mul_by_generator_g2, u256_to_bigint256,
};
use std::ops::Neg;
use hex::FromHex;
use std::ops::{Add, Mul};
use std::{fmt::Write, io::Read};
pub fn new_fp_element(x: BigInteger256) -> Fq {
Fq::from(x)
}
use ark_ff::PrimeField;

fn new_fp2_element(a: BigInteger256, b: BigInteger256) -> Fq2 {
Fq2::new(Fq::from(a), Fq::from(b))
Expand Down Expand Up @@ -96,6 +96,12 @@ impl KeyPair {
}
}

pub fn from_string(s: String) -> Result<Self, BlsError> {
let bigint_key = hex_string_to_biginteger256(&s);
let key = Fr::from(bigint_key);
KeyPair::new(key)
}

pub fn sign_hashes_to_curve_message(&self, g1_hashes_msg: G1Projective) -> Signature {
let sig = g1_hashes_msg.mul(self.priv_key);

Expand All @@ -118,6 +124,34 @@ impl KeyPair {
}
}

pub fn bigint_to_hex(bigint: &BigInteger256) -> String {
let mut hex_string = String::new();
for part in bigint.0.iter().rev() {
write!(&mut hex_string, "{:016x}", part).unwrap();
}
hex_string
}

pub fn hex_string_to_biginteger256(hex_str: &str) -> BigInteger256 {
let bytes = Vec::from_hex(hex_str).unwrap();

assert!(bytes.len() <= 32, "Byte length exceeds 32 bytes");

let mut padded_bytes = vec![0u8; 32];
let start = 32 - bytes.len();
padded_bytes[start..].copy_from_slice(&bytes);

let mut limbs = [0u64; 4];
for (i, chunk) in padded_bytes.chunks(8).rev().enumerate() {
let mut array = [0u8; 8];
let len = chunk.len().min(8);
array[..len].copy_from_slice(&chunk[..len]); // Copy the bytes into the fixed-size array
limbs[i] = u64::from_be_bytes(array);
}

BigInteger256::new(limbs)
}

#[derive(Debug, Clone)]
pub struct G1Point {
pub point: G1Projective,
Expand Down Expand Up @@ -190,8 +224,8 @@ impl G1Point {
mod tests {
use super::*;
use ark_ff::UniformRand;
use ark_ff::{BigInt, Zero};
use rand::{thread_rng, RngCore};

#[tokio::test]
async fn test_keypair_generation() {
let mut rng = thread_rng();
Expand Down Expand Up @@ -243,6 +277,7 @@ mod tests {
async fn test_signature_verification_invalid() {
let mut rng = thread_rng();
let private_key = Fr::rand(&mut rng);
println!("private key :{:?}", private_key);
let keypair = KeyPair::new(private_key).unwrap();

let mut message = [0u8; 32];
Expand All @@ -259,4 +294,23 @@ mod tests {
let different_pub_key = G2Projective::rand(&mut rng);
assert!(!signature.verify_signature(different_pub_key, &message));
}

#[tokio::test]
async fn test_keypair_from_string() {
let bigint = BigInt([
12844100841192127628,
7068359412155877604,
5417847382009744817,
1586467664616413849,
]);
let hex_string = bigint_to_hex(&bigint);
let converted_bigint = hex_string_to_biginteger256(&hex_string);
assert_eq!(bigint, converted_bigint);
let keypair_result_from_string = KeyPair::from_string(hex_string);
let keypair_result_normal = KeyPair::new(Fr::from(bigint));

let keypair_from_string = keypair_result_from_string.unwrap();
let keypair_from_new = keypair_result_normal.unwrap();
assert_eq!(keypair_from_new.priv_key, keypair_from_string.priv_key);
}
}
4 changes: 4 additions & 0 deletions crates/crypto/bls/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ pub enum BlsError {
/// Multiply private key to g1 projective
#[error("Failed to multiply by G1 Projective")]
MulByG1Projective,

/// Failed to generate keypair from private key string
#[error("Failed to generate keypair from String")]
KeyPairFromString,
}
Loading