Skip to content

Commit

Permalink
chainio/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
supernovahs committed May 9, 2024
1 parent ff556d1 commit 5c3f530
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
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.

8 changes: 4 additions & 4 deletions crates/chainio/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub fn convert_to_bn254_g1_point(g1: G1Projective) -> RegistryG1Point {
let y: BigInteger256 = g1.y.into();

RegistryG1Point {
x: biginteger256_to_u256(x),
y: biginteger256_to_u256(y),
X: biginteger256_to_u256(x),
Y: biginteger256_to_u256(y),
}
}

Expand All @@ -35,7 +35,7 @@ pub fn convert_to_bn254_g2_point(g2: G2Projective) -> RegistryG2Point {
let y_1: BigInteger256 = g2.y.c1.into();

RegistryG2Point {
x: [biginteger256_to_u256(x_0), biginteger256_to_u256(x_1)],
y: [biginteger256_to_u256(y_0), biginteger256_to_u256(y_1)],
X: [biginteger256_to_u256(x_0), biginteger256_to_u256(x_1)],
Y: [biginteger256_to_u256(y_0), biginteger256_to_u256(y_1)],
}
}
3 changes: 2 additions & 1 deletion crates/crypto/bn254/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ license-file.workspace = true
ark-ff.workspace = true
ark-bn254 = "0.4.0"
ethers.workspace = true
thiserror.workspace = true
thiserror.workspace = true
alloy-primitives.workspace = true
7 changes: 3 additions & 4 deletions crates/crypto/bn254/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::error::Bn254Err;
use ark_bn254::{Fq, Fq2, Fr, G1Affine, G1Projective, G2Affine, G2Projective};
use ark_ff::{BigInteger, BigInteger256};
use ethers::core::types::U256;
use alloy_primitives::U256;
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 mut bytes = [0u8; 32];
value.to_big_endian(&mut bytes);
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() {
Expand All @@ -23,7 +22,7 @@ pub fn u256_to_bigint256(value: U256) -> BigInteger256 {

pub fn biginteger256_to_u256(bi: BigInteger256) -> U256 {
let s = bi.to_bytes_be();
U256::from_little_endian(&s)
U256::from_be_slice(&s)
}

pub fn get_g1_generator() -> Result<G1Affine, Bn254Err> {
Expand Down

0 comments on commit 5c3f530

Please sign in to comment.