From 17d33500c87366924f8613f6e3263bec8250512c Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 01:00:56 +0530 Subject: [PATCH 01/68] Update bls12_381.rs --- crates/precompile/src/bls12_381.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381.rs b/crates/precompile/src/bls12_381.rs index aa47c90124..683c0f1423 100644 --- a/crates/precompile/src/bls12_381.rs +++ b/crates/precompile/src/bls12_381.rs @@ -1,18 +1,27 @@ use crate::PrecompileWithAddress; mod g1; +#[cfg(feature = "blst")] pub mod g1_add; +#[cfg(feature = "blst")] pub mod g1_msm; mod g2; +#[cfg(feature = "blst")] pub mod g2_add; +#[cfg(feature = "blst")] pub mod g2_msm; +#[cfg(feature = "blst")] pub mod map_fp2_to_g2; +#[cfg(feature = "blst")] pub mod map_fp_to_g1; -pub mod msm; +#[cfg(feature = "blst")] pub mod pairing; mod utils; +pub mod reuse_const; +pub mod msm; /// Returns the BLS12-381 precompiles with their addresses. +#[cfg(feature = "blst")] pub fn precompiles() -> impl Iterator { [ g1_add::PRECOMPILE, From 6723031e1d368bed848d002eaa886e387b63a759 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:36:01 +0530 Subject: [PATCH 02/68] Update g1.rs --- crates/precompile/src/bls12_381/g1.rs | 123 +++++++++----------------- 1 file changed, 40 insertions(+), 83 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1.rs b/crates/precompile/src/bls12_381/g1.rs index 912141f7e0..2aa7a7ad8e 100644 --- a/crates/precompile/src/bls12_381/g1.rs +++ b/crates/precompile/src/bls12_381/g1.rs @@ -1,96 +1,53 @@ -use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding, PADDED_FP_LENGTH}; -use crate::PrecompileError; -use blst::{blst_p1_affine, blst_p1_affine_in_g1, blst_p1_affine_on_curve}; +use super::g1::{encode_g1_point, extract_g1_input}; +use crate::{u64_to_address, PrecompileWithAddress}; +use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; +use blst::{ + blst_p1, blst_p1_add_or_double_affine, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, +}; use primitives::Bytes; +use crate::bls12_381::bls12_381_const::{G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH,G1_INPUT_ITEM_LENGTH}; -/// Length of each of the elements in a g1 operation input. -pub(super) const G1_INPUT_ITEM_LENGTH: usize = 128; +/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile. +pub const PRECOMPILE: PrecompileWithAddress = + PrecompileWithAddress(u64_to_address(G1_ADD_ADDRESS), g1_add); -/// Output length of a g1 operation. -const G1_OUTPUT_LENGTH: usize = 128; -/// Encodes a G1 point in affine format into byte slice with padded elements. -pub(super) fn encode_g1_point(input: *const blst_p1_affine) -> Bytes { - let mut out = vec![0u8; G1_OUTPUT_LENGTH]; - // SAFETY: Out comes from fixed length array, input is a blst value. - unsafe { - fp_to_bytes(&mut out[..PADDED_FP_LENGTH], &(*input).x); - fp_to_bytes(&mut out[PADDED_FP_LENGTH..], &(*input).y); +/// G1 addition call expects `256` bytes as an input that is interpreted as byte +/// concatenation of two G1 points (`128` bytes each). +/// Output is an encoding of addition operation result - single G1 point (`128` +/// bytes). +/// See also: +pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { + if G1_ADD_BASE_GAS_FEE > gas_limit { + return Err(PrecompileError::OutOfGas.into()); } - out.into() -} - -/// Returns a `blst_p1_affine` from the provided byte slices, which represent the x and y -/// affine coordinates of the point. -/// -/// If the x or y coordinate do not represent a canonical field element, an error is returned. -/// -/// See [fp_from_bendian] for more information. -pub(super) fn decode_and_check_g1( - p0_x: &[u8; 48], - p0_y: &[u8; 48], -) -> Result { - let out = blst_p1_affine { - x: fp_from_bendian(p0_x)?, - y: fp_from_bendian(p0_y)?, - }; - - Ok(out) -} -/// Extracts a G1 point in Affine format from a 128 byte slice representation. -/// -/// **Note**: This function will perform a G1 subgroup check if `subgroup_check` is set to `true`. -pub(super) fn extract_g1_input( - input: &[u8], - subgroup_check: bool, -) -> Result { - if input.len() != G1_INPUT_ITEM_LENGTH { + if input.len() != G1_ADD_INPUT_LENGTH { return Err(PrecompileError::Other(format!( - "Input should be {G1_INPUT_ITEM_LENGTH} bytes, was {}", + "G1ADD input should be {G1_ADD_INPUT_LENGTH} bytes, was {}", input.len() - ))); + )) + .into()); } - let input_p0_x = remove_padding(&input[..PADDED_FP_LENGTH])?; - let input_p0_y = remove_padding(&input[PADDED_FP_LENGTH..G1_INPUT_ITEM_LENGTH])?; - let out = decode_and_check_g1(input_p0_x, input_p0_y)?; + // NB: There is no subgroup check for the G1 addition precompile. + // + // So we set the subgroup checks here to `false` + let a_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], false)?; + let b_aff = &extract_g1_input(&input[G1_INPUT_ITEM_LENGTH..], false)?; - if subgroup_check { - // NB: Subgroup checks - // - // Scalar multiplications, MSMs and pairings MUST perform a subgroup check. - // - // Implementations SHOULD use the optimized subgroup check method: - // - // https://eips.ethereum.org/assets/eip-2537/fast_subgroup_checks - // - // On any input that fail the subgroup check, the precompile MUST return an error. - // - // As endomorphism acceleration requires input on the correct subgroup, implementers MAY - // use endomorphism acceleration. - if unsafe { !blst_p1_affine_in_g1(&out) } { - return Err(PrecompileError::Other("Element not in G1".to_string())); - } - } else { - // From EIP-2537: - // - // Error cases: - // - // * An input is neither a point on the G1 elliptic curve nor the infinity point - // - // NB: There is no subgroup check for the G1 addition precompile. - // - // We use blst_p1_affine_on_curve instead of blst_p1_affine_in_g1 because the latter performs - // the subgroup check. - // - // SAFETY: Out is a blst value. - if unsafe { !blst_p1_affine_on_curve(&out) } { - return Err(PrecompileError::Other( - "Element not on G1 curve".to_string(), - )); - } - } + let mut b = blst_p1::default(); + // SAFETY: `b` and `b_aff` are blst values. + unsafe { blst_p1_from_affine(&mut b, b_aff) }; + + let mut p = blst_p1::default(); + // SAFETY: `p`, `b` and `a_aff` are blst values. + unsafe { blst_p1_add_or_double_affine(&mut p, &b, a_aff) }; + + let mut p_aff = blst_p1_affine::default(); + // SAFETY: `p_aff` and `p`` are blst values. + unsafe { blst_p1_to_affine(&mut p_aff, &p) }; - Ok(out) + let out = encode_g1_point(&p_aff); + Ok(PrecompileOutput::new(G1_ADD_BASE_GAS_FEE, out)) } From 771feed316e2db00a17ff3a9ccecfd150246b550 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:36:32 +0530 Subject: [PATCH 03/68] Update g1_add.rs --- crates/precompile/src/bls12_381/g1_add.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index eb5228743a..2aa7a7ad8e 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -1,21 +1,16 @@ -use super::g1::{encode_g1_point, extract_g1_input, G1_INPUT_ITEM_LENGTH}; +use super::g1::{encode_g1_point, extract_g1_input}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{ blst_p1, blst_p1_add_or_double_affine, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, }; use primitives::Bytes; +use crate::bls12_381::bls12_381_const::{G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH,G1_INPUT_ITEM_LENGTH}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile. pub const PRECOMPILE: PrecompileWithAddress = - PrecompileWithAddress(u64_to_address(ADDRESS), g1_add); -/// BLS12_G1ADD precompile address. -pub const ADDRESS: u64 = 0x0b; -/// Base gas fee for BLS12-381 g1_add operation. -const BASE_GAS_FEE: u64 = 375; + PrecompileWithAddress(u64_to_address(G1_ADD_ADDRESS), g1_add); -/// Input length of g1_add operation. -const INPUT_LENGTH: usize = 256; /// G1 addition call expects `256` bytes as an input that is interpreted as byte /// concatenation of two G1 points (`128` bytes each). @@ -23,13 +18,13 @@ const INPUT_LENGTH: usize = 256; /// bytes). /// See also: pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { - if BASE_GAS_FEE > gas_limit { + if G1_ADD_BASE_GAS_FEE > gas_limit { return Err(PrecompileError::OutOfGas.into()); } - if input.len() != INPUT_LENGTH { + if input.len() != G1_ADD_INPUT_LENGTH { return Err(PrecompileError::Other(format!( - "G1ADD input should be {INPUT_LENGTH} bytes, was {}", + "G1ADD input should be {G1_ADD_INPUT_LENGTH} bytes, was {}", input.len() )) .into()); @@ -54,5 +49,5 @@ pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { unsafe { blst_p1_to_affine(&mut p_aff, &p) }; let out = encode_g1_point(&p_aff); - Ok(PrecompileOutput::new(BASE_GAS_FEE, out)) + Ok(PrecompileOutput::new(G1_ADD_BASE_GAS_FEE, out)) } From 46393d7ca557f56ecad1947f87a7989e96263fa2 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:36:54 +0530 Subject: [PATCH 04/68] Update g1.rs --- crates/precompile/src/bls12_381/g1.rs | 120 +++++++++++++++++--------- 1 file changed, 79 insertions(+), 41 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1.rs b/crates/precompile/src/bls12_381/g1.rs index 2aa7a7ad8e..72df5f5938 100644 --- a/crates/precompile/src/bls12_381/g1.rs +++ b/crates/precompile/src/bls12_381/g1.rs @@ -1,53 +1,91 @@ -use super::g1::{encode_g1_point, extract_g1_input}; -use crate::{u64_to_address, PrecompileWithAddress}; -use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; -use blst::{ - blst_p1, blst_p1_add_or_double_affine, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, -}; +use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; +use crate::PrecompileError; +use blst::{blst_p1_affine, blst_p1_affine_in_g1, blst_p1_affine_on_curve}; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH,G1_INPUT_ITEM_LENGTH}; +use crate::bls12_381::bls12_381_const::{G1_OUTPUT_LENGTH, G1_INPUT_ITEM_LENGTH,UTILS_PADDED_FP_LENGTH}; -/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile. -pub const PRECOMPILE: PrecompileWithAddress = - PrecompileWithAddress(u64_to_address(G1_ADD_ADDRESS), g1_add); +/// Encodes a G1 point in affine format into byte slice with padded elements. +pub(super) fn encode_g1_point(input: *const blst_p1_affine) -> Bytes { + let mut out = vec![0u8; G1_OUTPUT_LENGTH]; + // SAFETY: Out comes from fixed length array, input is a blst value. + unsafe { + fp_to_bytes(&mut out[..UTILS_PADDED_FP_LENGTH], &(*input).x); + fp_to_bytes(&mut out[UTILS_PADDED_FP_LENGTH..], &(*input).y); + } + out.into() +} +/// Returns a `blst_p1_affine` from the provided byte slices, which represent the x and y +/// affine coordinates of the point. +/// +/// If the x or y coordinate do not represent a canonical field element, an error is returned. +/// +/// See [fp_from_bendian] for more information. +pub(super) fn decode_and_check_g1( + p0_x: &[u8; 48], + p0_y: &[u8; 48], +) -> Result { + let out = blst_p1_affine { + x: fp_from_bendian(p0_x)?, + y: fp_from_bendian(p0_y)?, + }; -/// G1 addition call expects `256` bytes as an input that is interpreted as byte -/// concatenation of two G1 points (`128` bytes each). -/// Output is an encoding of addition operation result - single G1 point (`128` -/// bytes). -/// See also: -pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { - if G1_ADD_BASE_GAS_FEE > gas_limit { - return Err(PrecompileError::OutOfGas.into()); - } + Ok(out) +} - if input.len() != G1_ADD_INPUT_LENGTH { +/// Extracts a G1 point in Affine format from a 128 byte slice representation. +/// +/// **Note**: This function will perform a G1 subgroup check if `subgroup_check` is set to `true`. +pub(super) fn extract_g1_input( + input: &[u8], + subgroup_check: bool, +) -> Result { + if input.len() != G1_INPUT_ITEM_LENGTH { return Err(PrecompileError::Other(format!( - "G1ADD input should be {G1_ADD_INPUT_LENGTH} bytes, was {}", + "Input should be {G1_INPUT_ITEM_LENGTH} bytes, was {}", input.len() - )) - .into()); + ))); } - // NB: There is no subgroup check for the G1 addition precompile. - // - // So we set the subgroup checks here to `false` - let a_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], false)?; - let b_aff = &extract_g1_input(&input[G1_INPUT_ITEM_LENGTH..], false)?; + let input_p0_x = remove_padding(&input[..UTILS_PADDED_FP_LENGTH])?; + let input_p0_y = remove_padding(&input[UTILS_PADDED_FP_LENGTH..G1_INPUT_ITEM_LENGTH])?; + let out = decode_and_check_g1(input_p0_x, input_p0_y)?; - let mut b = blst_p1::default(); - // SAFETY: `b` and `b_aff` are blst values. - unsafe { blst_p1_from_affine(&mut b, b_aff) }; - - let mut p = blst_p1::default(); - // SAFETY: `p`, `b` and `a_aff` are blst values. - unsafe { blst_p1_add_or_double_affine(&mut p, &b, a_aff) }; - - let mut p_aff = blst_p1_affine::default(); - // SAFETY: `p_aff` and `p`` are blst values. - unsafe { blst_p1_to_affine(&mut p_aff, &p) }; + if subgroup_check { + // NB: Subgroup checks + // + // Scalar multiplications, MSMs and pairings MUST perform a subgroup check. + // + // Implementations SHOULD use the optimized subgroup check method: + // + // https://eips.ethereum.org/assets/eip-2537/fast_subgroup_checks + // + // On any input that fail the subgroup check, the precompile MUST return an error. + // + // As endomorphism acceleration requires input on the correct subgroup, implementers MAY + // use endomorphism acceleration. + if unsafe { !blst_p1_affine_in_g1(&out) } { + return Err(PrecompileError::Other("Element not in G1".to_string())); + } + } else { + // From EIP-2537: + // + // Error cases: + // + // * An input is neither a point on the G1 elliptic curve nor the infinity point + // + // NB: There is no subgroup check for the G1 addition precompile. + // + // We use blst_p1_affine_on_curve instead of blst_p1_affine_in_g1 because the latter performs + // the subgroup check. + // + // SAFETY: Out is a blst value. + if unsafe { !blst_p1_affine_on_curve(&out) } { + return Err(PrecompileError::Other( + "Element not on G1 curve".to_string(), + )); + } + } - let out = encode_g1_point(&p_aff); - Ok(PrecompileOutput::new(G1_ADD_BASE_GAS_FEE, out)) + Ok(out) } From b3a883035af70f9ea5e3cd2eb4d15b706add14fb Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:37:15 +0530 Subject: [PATCH 05/68] Update g1_msm.rs --- crates/precompile/src/bls12_381/g1_msm.rs | 33 +++++++++-------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index 18cf51a123..fcc8ecd6d2 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -1,25 +1,18 @@ use super::{ - g1::{encode_g1_point, extract_g1_input, G1_INPUT_ITEM_LENGTH}, + g1::{encode_g1_point, extract_g1_input}, msm::msm_required_gas, - utils::{extract_scalar_input, NBITS, SCALAR_LENGTH}, + utils::extract_scalar_input }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines}; use primitives::Bytes; +use crate::bls12_381::bls12_381_const::{G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, G1_MSM_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH,UTILS_SCALAR_LENGTH, UTILS_NBITS}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = - PrecompileWithAddress(u64_to_address(ADDRESS), g1_msm); + PrecompileWithAddress(u64_to_address(G1_MSM_ADDRESS), g1_msm); -/// BLS12_G1MSM precompile address. -pub const ADDRESS: u64 = 0x0c; - -/// Base gas fee for BLS12-381 g1_mul operation. -pub const BASE_GAS_FEE: u64 = 12000; - -/// Input length of g1_mul operation. -pub const INPUT_LENGTH: usize = 160; /// Discounts table for G1 MSM as a vector of pairs `[k, discount]`. pub static DISCOUNT_TABLE: [u16; 128] = [ @@ -42,24 +35,24 @@ pub static DISCOUNT_TABLE: [u16; 128] = [ /// See also: pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); - if input_len == 0 || input_len % INPUT_LENGTH != 0 { + if input_len == 0 || input_len % G1_MSM_INPUT_LENGTH != 0 { return Err(PrecompileError::Other(format!( "G1MSM input length should be multiple of {}, was {}", - INPUT_LENGTH, input_len + G1_MSM_INPUT_LENGTH, input_len )) .into()); } - let k = input_len / INPUT_LENGTH; - let required_gas = msm_required_gas(k, &DISCOUNT_TABLE, BASE_GAS_FEE); + let k = input_len / G1_MSM_INPUT_LENGTH; + let required_gas = msm_required_gas(k, &DISCOUNT_TABLE, G1_MSM_BASE_GAS_FEE); if required_gas > gas_limit { return Err(PrecompileError::OutOfGas.into()); } let mut g1_points: Vec = Vec::with_capacity(k); - let mut scalars: Vec = Vec::with_capacity(k * SCALAR_LENGTH); + let mut scalars: Vec = Vec::with_capacity(k * UTILS_SCALAR_LENGTH); for i in 0..k { - let slice = &input[i * INPUT_LENGTH..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH]; + let slice = &input[i * G1_MSM_INPUT_LENGTH..i * G1_MSM_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH]; // BLST batch API for p1_affines blows up when you pass it a point at infinity, so we must // filter points at infinity (and their corresponding scalars) from the input. @@ -79,8 +72,8 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { scalars.extend_from_slice( &extract_scalar_input( - &input[i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH - ..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + SCALAR_LENGTH], + &input[i * G1_MSM_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + ..i * G1_MSM_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + UTILS_SCALAR_LENGTH], )? .b, ); @@ -92,7 +85,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { } let points = p1_affines::from(&g1_points); - let multiexp = points.mult(&scalars, NBITS); + let multiexp = points.mult(&scalars, UTILS_NBITS); let mut multiexp_aff = blst_p1_affine::default(); // SAFETY: `multiexp_aff` and `multiexp` are blst values. From 65b91b26d0f1c8f39ffb5926321e7ae092b0df21 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:37:37 +0530 Subject: [PATCH 06/68] Update g2.rs --- crates/precompile/src/bls12_381/g2.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2.rs b/crates/precompile/src/bls12_381/g2.rs index 5a75808b28..f3b8a5ce64 100644 --- a/crates/precompile/src/bls12_381/g2.rs +++ b/crates/precompile/src/bls12_381/g2.rs @@ -1,28 +1,24 @@ -use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding, FP_LENGTH, PADDED_FP_LENGTH}; +use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; use crate::PrecompileError; use blst::{blst_fp2, blst_p2_affine, blst_p2_affine_in_g2, blst_p2_affine_on_curve}; use primitives::Bytes; +use crate::bls12_381::bls12_381_const::{G2_OUTPUT_LENGTH,G2_INPUT_ITEM_LENGTH, UTILS_FP_LENGTH, UTILS_PADDED_FP_LENGTH}; -/// Length of each of the elements in a g2 operation input. -pub(super) const G2_INPUT_ITEM_LENGTH: usize = 256; - -/// Output length of a g2 operation. -const G2_OUTPUT_LENGTH: usize = 256; /// Encodes a G2 point in affine format into byte slice with padded elements. pub(super) fn encode_g2_point(input: &blst_p2_affine) -> Bytes { let mut out = vec![0u8; G2_OUTPUT_LENGTH]; - fp_to_bytes(&mut out[..PADDED_FP_LENGTH], &input.x.fp[0]); + fp_to_bytes(&mut out[..UTILS_PADDED_FP_LENGTH], &input.x.fp[0]); fp_to_bytes( - &mut out[PADDED_FP_LENGTH..2 * PADDED_FP_LENGTH], + &mut out[UTILS_PADDED_FP_LENGTH..2 * UTILS_PADDED_FP_LENGTH], &input.x.fp[1], ); fp_to_bytes( - &mut out[2 * PADDED_FP_LENGTH..3 * PADDED_FP_LENGTH], + &mut out[2 * UTILS_PADDED_FP_LENGTH..3 * UTILS_PADDED_FP_LENGTH], &input.y.fp[0], ); fp_to_bytes( - &mut out[3 * PADDED_FP_LENGTH..4 * PADDED_FP_LENGTH], + &mut out[3 * UTILS_PADDED_FP_LENGTH..4 * UTILS_PADDED_FP_LENGTH], &input.y.fp[1], ); out.into() @@ -69,9 +65,9 @@ pub(super) fn extract_g2_input( ))); } - let mut input_fps = [&[0; FP_LENGTH]; 4]; + let mut input_fps = [&[0; UTILS_FP_LENGTH]; 4]; for i in 0..4 { - input_fps[i] = remove_padding(&input[i * PADDED_FP_LENGTH..(i + 1) * PADDED_FP_LENGTH])?; + input_fps[i] = remove_padding(&input[i * UTILS_PADDED_FP_LENGTH..(i + 1) * UTILS_PADDED_FP_LENGTH])?; } let out = decode_and_check_g2(input_fps[0], input_fps[1], input_fps[2], input_fps[3])?; From 06b1abc3eb3ba10933e5711cb998522e7a0b6064 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:38:00 +0530 Subject: [PATCH 07/68] Update g2_add.rs --- crates/precompile/src/bls12_381/g2_add.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index 142eb7e95d..97569a8c28 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -1,21 +1,16 @@ -use super::g2::{encode_g2_point, extract_g2_input, G2_INPUT_ITEM_LENGTH}; +use super::g2::{encode_g2_point, extract_g2_input}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{ blst_p2, blst_p2_add_or_double_affine, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, }; use primitives::Bytes; +use crate::bls12_381::bls12_381_const::{G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH,G2_INPUT_ITEM_LENGTH}; + /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2ADD precompile. pub const PRECOMPILE: PrecompileWithAddress = - PrecompileWithAddress(u64_to_address(ADDRESS), g2_add); -/// BLS12_G2ADD precompile address. -pub const ADDRESS: u64 = 0x0d; -/// Base gas fee for BLS12-381 g2_add operation. -const BASE_GAS_FEE: u64 = 600; - -/// Input length of g2_add operation. -const INPUT_LENGTH: usize = 512; + PrecompileWithAddress(u64_to_address(G2_ADD_ADDRESS), g2_add); /// G2 addition call expects `512` bytes as an input that is interpreted as byte /// concatenation of two G2 points (`256` bytes each). @@ -24,13 +19,13 @@ const INPUT_LENGTH: usize = 512; /// bytes). /// See also pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { - if BASE_GAS_FEE > gas_limit { + if G2_ADD_BASE_GAS_FEE > gas_limit { return Err(PrecompileError::OutOfGas.into()); } - if input.len() != INPUT_LENGTH { + if input.len() != G2_ADD_INPUT_LENGTH { return Err(PrecompileError::Other(format!( - "G2ADD input should be {INPUT_LENGTH} bytes, was {}", + "G2ADD input should be {G2_ADD_INPUT_LENGTH} bytes, was {}", input.len() )) .into()); @@ -55,5 +50,5 @@ pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { unsafe { blst_p2_to_affine(&mut p_aff, &p) }; let out = encode_g2_point(&p_aff); - Ok(PrecompileOutput::new(BASE_GAS_FEE, out)) + Ok(PrecompileOutput::new(G2_ADD_BASE_GAS_FEE, out)) } From 8b440c719029a7b27b2bde383e12ecaf5c5552c4 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:38:17 +0530 Subject: [PATCH 08/68] Update g2_msm.rs --- crates/precompile/src/bls12_381/g2_msm.rs | 34 +++++++++-------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 9bc916f975..49f0d2c4b7 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -1,25 +1,17 @@ use super::{ - g2::{encode_g2_point, extract_g2_input, G2_INPUT_ITEM_LENGTH}, + g2::{encode_g2_point, extract_g2_input}, msm::msm_required_gas, - utils::{extract_scalar_input, NBITS, SCALAR_LENGTH}, + utils::extract_scalar_input }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; use primitives::Bytes; +use crate::bls12_381::bls12_381_const::{G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH,G2_INPUT_ITEM_LENGTH, UTILS_NBITS, UTILS_SCALAR_LENGTH}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = - PrecompileWithAddress(u64_to_address(ADDRESS), g2_msm); - -/// BLS12_G2MSM precompile address. -pub const ADDRESS: u64 = 0x0e; - -/// Base gas fee for BLS12-381 g2_mul operation. -pub const BASE_GAS_FEE: u64 = 22500; - -/// Input length of g2_mul operation. -pub const INPUT_LENGTH: usize = 288; + PrecompileWithAddress(u64_to_address(G2_ADD_ADDRESS), g2_msm); // Discounts table for G2 MSM as a vector of pairs `[k, discount]`: pub static DISCOUNT_TABLE: [u16; 128] = [ @@ -42,24 +34,24 @@ pub static DISCOUNT_TABLE: [u16; 128] = [ /// See also: pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); - if input_len == 0 || input_len % INPUT_LENGTH != 0 { + if input_len == 0 || input_len % G2_ADD_INPUT_LENGTH != 0 { return Err(PrecompileError::Other(format!( "G2MSM input length should be multiple of {}, was {}", - INPUT_LENGTH, input_len + G2_ADD_INPUT_LENGTH, input_len )) .into()); } - let k = input_len / INPUT_LENGTH; - let required_gas = msm_required_gas(k, &DISCOUNT_TABLE, BASE_GAS_FEE); + let k = input_len / G2_ADD_INPUT_LENGTH; + let required_gas = msm_required_gas(k, &DISCOUNT_TABLE, G2_ADD_BASE_GAS_FEE); if required_gas > gas_limit { return Err(PrecompileError::OutOfGas.into()); } let mut g2_points: Vec = Vec::with_capacity(k); - let mut scalars: Vec = Vec::with_capacity(k * SCALAR_LENGTH); + let mut scalars: Vec = Vec::with_capacity(k * UTILS_SCALAR_LENGTH); for i in 0..k { - let slice = &input[i * INPUT_LENGTH..i * INPUT_LENGTH + G2_INPUT_ITEM_LENGTH]; + let slice = &input[i * G2_ADD_INPUT_LENGTH..i * G2_ADD_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH]; // BLST batch API for p2_affines blows up when you pass it a point at infinity, so we must // filter points at infinity (and their corresponding scalars) from the input. if slice.iter().all(|i| *i == 0) { @@ -79,8 +71,8 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { scalars.extend_from_slice( &extract_scalar_input( - &input[i * INPUT_LENGTH + G2_INPUT_ITEM_LENGTH - ..i * INPUT_LENGTH + G2_INPUT_ITEM_LENGTH + SCALAR_LENGTH], + &input[i * G2_ADD_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH + ..i * G2_ADD_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH + UTILS_SCALAR_LENGTH], )? .b, ); @@ -92,7 +84,7 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { } let points = p2_affines::from(&g2_points); - let multiexp = points.mult(&scalars, NBITS); + let multiexp = points.mult(&scalars, UTILS_NBITS); let mut multiexp_aff = blst_p2_affine::default(); // SAFETY: `multiexp_aff` and `multiexp` are blst values. From 45d2b57ebed2d8696f6766de93d042503517f541 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:38:55 +0530 Subject: [PATCH 09/68] Update map_fp_to_g1.rs --- .../precompile/src/bls12_381/map_fp_to_g1.rs | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index 13179cd590..8991ab2ced 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -1,33 +1,28 @@ use super::{ g1::encode_g1_point, - utils::{fp_from_bendian, remove_padding, PADDED_FP_LENGTH}, + utils::{fp_from_bendian, remove_padding}, }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g1, blst_p1, blst_p1_affine, blst_p1_to_affine}; use primitives::Bytes; +use crate::bls12_381::bls12_381_const::{MAP_FP_TO_G1_ADDRESS, MAP_FP_TO_G1_BASE_GAS_FEE, UTILS_PADDED_FP_LENGTH}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP_TO_G1 precompile. pub const PRECOMPILE: PrecompileWithAddress = - PrecompileWithAddress(u64_to_address(ADDRESS), map_fp_to_g1); - -/// BLS12_MAP_FP_TO_G1 precompile address. -pub const ADDRESS: u64 = 0x10; - -/// Base gas fee for BLS12-381 map_fp_to_g1 operation. -const MAP_FP_TO_G1_BASE: u64 = 5500; + PrecompileWithAddress(u64_to_address(MAP_FP_TO_G1_ADDRESS), map_fp_to_g1); /// Field-to-curve call expects 64 bytes as an input that is interpreted as an /// element of Fp. Output of this call is 128 bytes and is an encoded G1 point. /// See also: pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult { - if MAP_FP_TO_G1_BASE > gas_limit { + if MAP_FP_TO_G1_BASE_GAS_FEE > gas_limit { return Err(PrecompileError::OutOfGas.into()); } - if input.len() != PADDED_FP_LENGTH { + if input.len() != UTILS_PADDED_FP_LENGTH { return Err(PrecompileError::Other(format!( - "MAP_FP_TO_G1 input should be {PADDED_FP_LENGTH} bytes, was {}", + "MAP_FP_TO_G1 input should be {UTILS_PADDED_FP_LENGTH} bytes, was {}", input.len() )) .into()); @@ -46,7 +41,7 @@ pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult { unsafe { blst_p1_to_affine(&mut p_aff, &p) }; let out = encode_g1_point(&p_aff); - Ok(PrecompileOutput::new(MAP_FP_TO_G1_BASE, out)) + Ok(PrecompileOutput::new(MAP_FP_TO_G1_BASE_GAS_FEE, out)) } #[cfg(test)] @@ -57,7 +52,7 @@ mod test { #[test] fn sanity_test() { let input = Bytes::from(hex!("000000000000000000000000000000006900000000000000636f6e7472616374595a603f343061cd305a03f40239f5ffff31818185c136bc2595f2aa18e08f17")); - let fail = map_fp_to_g1(&input, MAP_FP_TO_G1_BASE); + let fail = map_fp_to_g1(&input, MAP_FP_TO_G1_BASE_GAS_FEE); assert_eq!( fail, Err(PrecompileError::Other("non-canonical fp value".to_string()).into()) From 2aa936b9d2ef563735278c0b31d779dc3bb25cdd Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:39:10 +0530 Subject: [PATCH 10/68] Update map_fp2_to_g2.rs --- .../precompile/src/bls12_381/map_fp2_to_g2.rs | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 57bf76f034..dfc14febd7 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,42 +1,37 @@ use super::{ g2::check_canonical_fp2, g2::encode_g2_point, - utils::{remove_padding, PADDED_FP2_LENGTH, PADDED_FP_LENGTH}, + utils::remove_padding }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine}; use primitives::Bytes; +use crate::bls12_381::bls12_381_const::{MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, UTILS_PADDED_FP2_LENGTH, UTILS_PADDED_FP_LENGTH}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP2_TO_G2 precompile. pub const PRECOMPILE: PrecompileWithAddress = - PrecompileWithAddress(u64_to_address(ADDRESS), map_fp2_to_g2); - -/// BLS12_MAP_FP2_TO_G2 precompile address. -pub const ADDRESS: u64 = 0x11; - -/// Base gas fee for BLS12-381 map_fp2_to_g2 operation. -const BASE_GAS_FEE: u64 = 23800; + PrecompileWithAddress(u64_to_address(MAP_FP2_TO_G2_ADDRESS), map_fp2_to_g2); /// Field-to-curve call expects 128 bytes as an input that is interpreted as /// an element of Fp2. Output of this call is 256 bytes and is an encoded G2 /// point. /// See also: -pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { - if BASE_GAS_FEE > gas_limit { +pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { + if MAP_FP2_TO_G2_BASE_GAS_FEE > gas_limit { return Err(PrecompileError::OutOfGas.into()); } - if input.len() != PADDED_FP2_LENGTH { + if input.len() != UTILS_PADDED_FP2_LENGTH { return Err(PrecompileError::Other(format!( - "MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}", + "MAP_FP2_TO_G2 input should be {UTILS_PADDED_FP2_LENGTH} bytes, was {}", input.len() )) .into()); } - let input_p0_x = remove_padding(&input[..PADDED_FP_LENGTH])?; - let input_p0_y = remove_padding(&input[PADDED_FP_LENGTH..PADDED_FP2_LENGTH])?; + let input_p0_x = remove_padding(&input[..UTILS_PADDED_FP_LENGTH])?; + let input_p0_y = remove_padding(&input[UTILS_PADDED_FP_LENGTH..UTILS_PADDED_FP2_LENGTH])?; let fp2 = check_canonical_fp2(input_p0_x, input_p0_y)?; let mut p = blst_p2::default(); @@ -49,5 +44,5 @@ pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { unsafe { blst_p2_to_affine(&mut p_aff, &p) }; let out = encode_g2_point(&p_aff); - Ok(PrecompileOutput::new(BASE_GAS_FEE, out)) + Ok(PrecompileOutput::new(MAP_FP2_TO_G2_BASE_GAS_FEE, out)) } From 92752f8c5ae883bc8ba57aa43ec89830881c81a8 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:39:33 +0530 Subject: [PATCH 11/68] Update msm.rs --- crates/precompile/src/bls12_381/msm.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/precompile/src/bls12_381/msm.rs b/crates/precompile/src/bls12_381/msm.rs index 674135cd16..89da57c033 100644 --- a/crates/precompile/src/bls12_381/msm.rs +++ b/crates/precompile/src/bls12_381/msm.rs @@ -1,5 +1,4 @@ -/// Amount used to calculate the multi-scalar-multiplication discount -const MSM_MULTIPLIER: u64 = 1000; +use crate::bls12_381::bls12_381_const::MSM_MULTIPLIER; /// Implements the gas schedule for G1/G2 Multiscalar-multiplication assuming 30 /// MGas/second, see also: From 91e4e49de78f75d8c754219412f9f7deb865a7cb Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:39:51 +0530 Subject: [PATCH 12/68] Update pairing.rs --- crates/precompile/src/bls12_381/pairing.rs | 31 +++++++++------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index bb85f0414b..7434a58bb5 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -1,25 +1,18 @@ use super::{ - g1::{extract_g1_input, G1_INPUT_ITEM_LENGTH}, - g2::{extract_g2_input, G2_INPUT_ITEM_LENGTH}, + g1::extract_g1_input, + g2::extract_g2_input, }; use crate::{ u64_to_address, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress, }; use blst::{blst_final_exp, blst_fp12, blst_fp12_is_one, blst_fp12_mul, blst_miller_loop}; use primitives::{Bytes, B256}; +use crate::bls12_381::bls12_381_const::{PAIRING_ADDRESS, PAIRING_PAIRING_MULTIPLIER_BAS, PAIRING_PAIRING_OFFSET_BASE, PAIRING_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH,G2_INPUT_ITEM_LENGTH}; + /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile. pub const PRECOMPILE: PrecompileWithAddress = - PrecompileWithAddress(u64_to_address(ADDRESS), pairing); -/// BLS12_PAIRING precompile address. -pub const ADDRESS: u64 = 0x0f; - -/// Multiplier gas fee for BLS12-381 pairing operation. -const PAIRING_MULTIPLIER_BASE: u64 = 32600; -/// Offset gas fee for BLS12-381 pairing operation. -const PAIRING_OFFSET_BASE: u64 = 37700; -/// Input length of pairing operation. -const INPUT_LENGTH: usize = 384; + PrecompileWithAddress(u64_to_address(PAIRING_ADDRESS), pairing); /// Pairing call expects 384*k (k being a positive integer) bytes as an inputs /// that is interpreted as byte concatenation of k slices. Each slice has the @@ -35,15 +28,15 @@ const INPUT_LENGTH: usize = 384; /// See also: pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let input_len = input.len(); - if input_len == 0 || input_len % INPUT_LENGTH != 0 { + if input_len == 0 || input_len % PAIRING_INPUT_LENGTH != 0 { return Err(PrecompileError::Other(format!( - "Pairing input length should be multiple of {INPUT_LENGTH}, was {input_len}" + "Pairing input length should be multiple of {PAIRING_INPUT_LENGTH}, was {input_len}" )) .into()); } - let k = input_len / INPUT_LENGTH; - let required_gas: u64 = PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_OFFSET_BASE; + let k = input_len / PAIRING_INPUT_LENGTH; + let required_gas: u64 = PAIRING_PAIRING_MULTIPLIER_BAS * k as u64 + PAIRING_PAIRING_OFFSET_BASE; if required_gas > gas_limit { return Err(PrecompileError::OutOfGas.into()); } @@ -55,7 +48,7 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { // // So we set the subgroup_check flag to `true` let p1_aff = &extract_g1_input( - &input[i * INPUT_LENGTH..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH], + &input[i * PAIRING_INPUT_LENGTH..i * PAIRING_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH], true, )?; @@ -63,8 +56,8 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { // // So we set the subgroup_check flag to `true` let p2_aff = &extract_g2_input( - &input[i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH - ..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + G2_INPUT_ITEM_LENGTH], + &input[i * PAIRING_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + ..i * PAIRING_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + G2_INPUT_ITEM_LENGTH], true, )?; From 71709563b1ab112d2a847195ae95118a3697feb4 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:40:20 +0530 Subject: [PATCH 13/68] Update utils.rs --- crates/precompile/src/bls12_381/utils.rs | 40 +++++++----------------- 1 file changed, 11 insertions(+), 29 deletions(-) diff --git a/crates/precompile/src/bls12_381/utils.rs b/crates/precompile/src/bls12_381/utils.rs index 2487a2a2d8..8bbe6488f3 100644 --- a/crates/precompile/src/bls12_381/utils.rs +++ b/crates/precompile/src/bls12_381/utils.rs @@ -3,49 +3,31 @@ use blst::{ blst_bendian_from_fp, blst_fp, blst_fp_from_bendian, blst_scalar, blst_scalar_from_bendian, }; use core::cmp::Ordering; - -/// Number of bits used in the BLS12-381 curve finite field elements. -pub(super) const NBITS: usize = 256; -/// Finite field element input length. -pub(super) const FP_LENGTH: usize = 48; -/// Finite field element padded input length. -pub(super) const PADDED_FP_LENGTH: usize = 64; -/// Quadratic extension of finite field element input length. -pub(super) const PADDED_FP2_LENGTH: usize = 128; -/// Input elements padding length. -pub(super) const PADDING_LENGTH: usize = 16; -/// Scalar length. -pub(super) const SCALAR_LENGTH: usize = 32; -// Big-endian non-Montgomery form. -pub(super) const MODULUS_REPR: [u8; 48] = [ - 0x1a, 0x01, 0x11, 0xea, 0x39, 0x7f, 0xe6, 0x9a, 0x4b, 0x1b, 0xa7, 0xb6, 0x43, 0x4b, 0xac, 0xd7, - 0x64, 0x77, 0x4b, 0x84, 0xf3, 0x85, 0x12, 0xbf, 0x67, 0x30, 0xd2, 0xa0, 0xf6, 0xb0, 0xf6, 0x24, - 0x1e, 0xab, 0xff, 0xfe, 0xb1, 0x53, 0xff, 0xff, 0xb9, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xaa, 0xab, -]; +use crate::bls12_381::bls12_381_const::{UTILS_FP_LENGTH, UTILS_PADDED_FP_LENGTH, UTILS_PADDING_LENGTH, UTILS_SCALAR_LENGTH, UTILS_MODULUS_REPR}; /// Encodes a single finite field element into byte slice with padding. pub(super) fn fp_to_bytes(out: &mut [u8], input: *const blst_fp) { - if out.len() != PADDED_FP_LENGTH { + if out.len() != UTILS_PADDED_FP_LENGTH { return; } - let (padding, rest) = out.split_at_mut(PADDING_LENGTH); + let (padding, rest) = out.split_at_mut(UTILS_PADDING_LENGTH); padding.fill(0); // SAFETY: Out length is checked previously, `input` is a blst value. unsafe { blst_bendian_from_fp(rest.as_mut_ptr(), input) }; } /// Removes zeros with which the precompile inputs are left padded to 64 bytes. -pub(super) fn remove_padding(input: &[u8]) -> Result<&[u8; FP_LENGTH], PrecompileError> { - if input.len() != PADDED_FP_LENGTH { +pub(super) fn remove_padding(input: &[u8]) -> Result<&[u8; UTILS_FP_LENGTH], PrecompileError> { + if input.len() != UTILS_PADDED_FP_LENGTH { return Err(PrecompileError::Other(format!( - "Padded input should be {PADDED_FP_LENGTH} bytes, was {}", + "Padded input should be {UTILS_PADDED_FP_LENGTH} bytes, was {}", input.len() ))); } - let (padding, unpadded) = input.split_at(PADDING_LENGTH); + let (padding, unpadded) = input.split_at(UTILS_PADDING_LENGTH); if !padding.iter().all(|&x| x == 0) { return Err(PrecompileError::Other(format!( - "{PADDING_LENGTH} top bytes of input are not zero", + "{UTILS_PADDING_LENGTH} top bytes of input are not zero", ))); } Ok(unpadded.try_into().unwrap()) @@ -62,9 +44,9 @@ pub(super) fn remove_padding(input: &[u8]) -> Result<&[u8; FP_LENGTH], Precompil /// * The corresponding integer is not required to be less than or equal than main subgroup order /// `q`. pub(super) fn extract_scalar_input(input: &[u8]) -> Result { - if input.len() != SCALAR_LENGTH { + if input.len() != UTILS_SCALAR_LENGTH { return Err(PrecompileError::Other(format!( - "Input should be {SCALAR_LENGTH} bytes, was {}", + "Input should be {UTILS_SCALAR_LENGTH} bytes, was {}", input.len() ))); } @@ -84,7 +66,7 @@ pub(super) fn extract_scalar_input(input: &[u8]) -> Result bool { - for (i, modulo) in input.iter().zip(MODULUS_REPR.iter()) { + for (i, modulo) in input.iter().zip(UTILS_MODULUS_REPR.iter()) { match i.cmp(modulo) { Ordering::Greater => return false, Ordering::Less => return true, From e14174b095e2a8e8657fd292618d1a6cc44f1f4d Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:40:52 +0530 Subject: [PATCH 14/68] Add files via upload --- .../src/bls12_381/bls12_381_const.rs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 crates/precompile/src/bls12_381/bls12_381_const.rs diff --git a/crates/precompile/src/bls12_381/bls12_381_const.rs b/crates/precompile/src/bls12_381/bls12_381_const.rs new file mode 100644 index 0000000000..93455ec5e4 --- /dev/null +++ b/crates/precompile/src/bls12_381/bls12_381_const.rs @@ -0,0 +1,68 @@ +use crate::PrecompileWithAddress; + +use crate::bls12_381::g1_add; +use crate::bls12_381::g1_msm; +use crate::bls12_381::g2_add; +use crate::bls12_381::g2_msm; +use crate::bls12_381::pairing; +use crate::bls12_381::map_fp_to_g1; +use crate::bls12_381::map_fp2_to_g2; + +pub const G1_ADD_ADDRESS: u64 = 0x0b; +pub const G1_ADD_BASE_GAS_FEE: u64 = 375; +pub const G1_ADD_INPUT_LENGTH: usize = 256; +pub const G1_MSM_ADDRESS: u64=0x0c; +pub const G1_MSM_BASE_GAS_FEE: u64 = 1200; +pub const G1_MSM_INPUT_LENGTH: usize = 160; +pub const G1_OUTPUT_LENGTH: usize = 128; +pub const G1_INPUT_ITEM_LENGTH: usize = 128; +pub const G2_ADD_ADDRESS: u64 = 0x0d; +pub const G2_ADD_BASE_GAS_FEE: u64 = 600; +pub const G2_ADD_INPUT_LENGTH: usize = 512; +pub const G2_MSM_ADDRESS: u64 = 0x0e; +pub const G2_MSM_BASE_GAS_FEE: u64 = 22500; +pub const G2_MSM_INPUT_LENGTH: usize = 288; +pub const G2_OUTPUT_LENGTH: usize = 256; +pub const G2_INPUT_ITEM_LENGTH: usize = 256; +pub const PAIRING_ADDRESS: u64 = 0x0f; +pub const PAIRING_PAIRING_MULTIPLIER_BAS: u64 = 32600; +pub const PAIRING_PAIRING_OFFSET_BASE: u64 = 37700; +pub const PAIRING_INPUT_LENGTH: usize = 384; +pub const MAP_FP_TO_G1_ADDRESS: u64 = 0x10; +pub const MAP_FP_TO_G1_BASE_GAS_FEE: u64 = 5500; +pub const MAP_FP2_TO_G2_ADDRESS: u64 = 0x11; +pub const MAP_FP2_TO_G2_BASE_GAS_FEE: u64 = 0x23800; +pub const MSM_MULTIPLIER: u64 = 1000; +/// Number of bits used in the BLS12-381 curve finite field elements. +pub const UTILS_NBITS: usize = 256; +/// Finite field element input length. +pub const UTILS_FP_LENGTH: usize = 48; +/// Finite field element padded input length. +pub const UTILS_PADDED_FP_LENGTH: usize = 64; +/// Quadratic extension of finite field element input length. +pub const UTILS_PADDED_FP2_LENGTH: usize = 128; +/// Input elements padding length. +pub const UTILS_PADDING_LENGTH: usize = 16; +/// Scalar length. +pub const UTILS_SCALAR_LENGTH: usize = 32; +// Big-endian non-Montgomery form. +pub const UTILS_MODULUS_REPR: [u8; 48] = [ + 0x1a, 0x01, 0x11, 0xea, 0x39, 0x7f, 0xe6, 0x9a, 0x4b, 0x1b, 0xa7, 0xb6, 0x43, 0x4b, 0xac, 0xd7, + 0x64, 0x77, 0x4b, 0x84, 0xf3, 0x85, 0x12, 0xbf, 0x67, 0x30, 0xd2, 0xa0, 0xf6, 0xb0, 0xf6, 0x24, + 0x1e, 0xab, 0xff, 0xfe, 0xb1, 0x53, 0xff, 0xff, 0xb9, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xaa, 0xab, +]; + +#[cfg(feature = "blst")] +pub fn precompiles() -> impl Iterator { + + [ + g1_add::PRECOMPILE, + g1_msm::PRECOMPILE, + g2_add::PRECOMPILE, + g2_msm::PRECOMPILE, + pairing::PRECOMPILE, + map_fp_to_g1::PRECOMPILE, + map_fp2_to_g2::PRECOMPILE, + ] + .into_iter() +} From 5ed8fb4dcd99ce02c6a7a7928adf22be6a0674fb Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 15 Feb 2025 16:56:25 +0530 Subject: [PATCH 15/68] Update bls12_381.rs --- crates/precompile/src/bls12_381.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381.rs b/crates/precompile/src/bls12_381.rs index 683c0f1423..2c0a8c6c16 100644 --- a/crates/precompile/src/bls12_381.rs +++ b/crates/precompile/src/bls12_381.rs @@ -17,7 +17,7 @@ pub mod map_fp_to_g1; #[cfg(feature = "blst")] pub mod pairing; mod utils; -pub mod reuse_const; +pub mod bls12_381_const; pub mod msm; /// Returns the BLS12-381 precompiles with their addresses. From 7e45d7c6f21664d67e78d13cf3d423b417ce4438 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:55:01 +0530 Subject: [PATCH 16/68] Update bls12_381_const.rs --- .../src/bls12_381/bls12_381_const.rs | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/crates/precompile/src/bls12_381/bls12_381_const.rs b/crates/precompile/src/bls12_381/bls12_381_const.rs index 93455ec5e4..d6a0b999c3 100644 --- a/crates/precompile/src/bls12_381/bls12_381_const.rs +++ b/crates/precompile/src/bls12_381/bls12_381_const.rs @@ -1,17 +1,7 @@ -use crate::PrecompileWithAddress; - -use crate::bls12_381::g1_add; -use crate::bls12_381::g1_msm; -use crate::bls12_381::g2_add; -use crate::bls12_381::g2_msm; -use crate::bls12_381::pairing; -use crate::bls12_381::map_fp_to_g1; -use crate::bls12_381::map_fp2_to_g2; - pub const G1_ADD_ADDRESS: u64 = 0x0b; pub const G1_ADD_BASE_GAS_FEE: u64 = 375; pub const G1_ADD_INPUT_LENGTH: usize = 256; -pub const G1_MSM_ADDRESS: u64=0x0c; +pub const G1_MSM_ADDRESS: u64 = 0x0c; pub const G1_MSM_BASE_GAS_FEE: u64 = 1200; pub const G1_MSM_INPUT_LENGTH: usize = 160; pub const G1_OUTPUT_LENGTH: usize = 128; @@ -34,35 +24,40 @@ pub const MAP_FP2_TO_G2_ADDRESS: u64 = 0x11; pub const MAP_FP2_TO_G2_BASE_GAS_FEE: u64 = 0x23800; pub const MSM_MULTIPLIER: u64 = 1000; /// Number of bits used in the BLS12-381 curve finite field elements. -pub const UTILS_NBITS: usize = 256; +pub const NBITS: usize = 256; /// Finite field element input length. -pub const UTILS_FP_LENGTH: usize = 48; +pub const FP_LENGTH: usize = 48; /// Finite field element padded input length. -pub const UTILS_PADDED_FP_LENGTH: usize = 64; +pub const PADDED_FP_LENGTH: usize = 64; /// Quadratic extension of finite field element input length. -pub const UTILS_PADDED_FP2_LENGTH: usize = 128; +pub const PADDED_FP2_LENGTH: usize = 128; /// Input elements padding length. -pub const UTILS_PADDING_LENGTH: usize = 16; +pub const PADDING_LENGTH: usize = 16; /// Scalar length. -pub const UTILS_SCALAR_LENGTH: usize = 32; +pub const SCALAR_LENGTH: usize = 32; // Big-endian non-Montgomery form. -pub const UTILS_MODULUS_REPR: [u8; 48] = [ +pub const MODULUS_REPR: [u8; 48] = [ 0x1a, 0x01, 0x11, 0xea, 0x39, 0x7f, 0xe6, 0x9a, 0x4b, 0x1b, 0xa7, 0xb6, 0x43, 0x4b, 0xac, 0xd7, 0x64, 0x77, 0x4b, 0x84, 0xf3, 0x85, 0x12, 0xbf, 0x67, 0x30, 0xd2, 0xa0, 0xf6, 0xb0, 0xf6, 0x24, 0x1e, 0xab, 0xff, 0xfe, 0xb1, 0x53, 0xff, 0xff, 0xb9, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xaa, 0xab, ]; - -#[cfg(feature = "blst")] -pub fn precompiles() -> impl Iterator { - - [ - g1_add::PRECOMPILE, - g1_msm::PRECOMPILE, - g2_add::PRECOMPILE, - g2_msm::PRECOMPILE, - pairing::PRECOMPILE, - map_fp_to_g1::PRECOMPILE, - map_fp2_to_g2::PRECOMPILE, - ] - .into_iter() -} +/// Discounts table for G1 MSM as a vector of pairs `[k, discount]`. +pub static DISCOUNT_TABLE_G1_MSM: [u16; 128] = [ + 1000, 949, 848, 797, 764, 750, 738, 728, 719, 712, 705, 698, 692, 687, 682, 677, 673, 669, 665, + 661, 658, 654, 651, 648, 645, 642, 640, 637, 635, 632, 630, 627, 625, 623, 621, 619, 617, 615, + 613, 611, 609, 608, 606, 604, 603, 601, 599, 598, 596, 595, 593, 592, 591, 589, 588, 586, 585, + 584, 582, 581, 580, 579, 577, 576, 575, 574, 573, 572, 570, 569, 568, 567, 566, 565, 564, 563, + 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 551, 550, 549, 548, 547, 547, 546, 545, + 544, 543, 542, 541, 540, 540, 539, 538, 537, 536, 536, 535, 534, 533, 532, 532, 531, 530, 529, + 528, 528, 527, 526, 525, 525, 524, 523, 522, 522, 521, 520, 520, 519, +]; +// Discounts table for G2 MSM as a vector of pairs `[k, discount]`: +pub static DISCOUNT_TABLE_G2_MSM: [u16; 128] = [ + 1000, 1000, 923, 884, 855, 832, 812, 796, 782, 770, 759, 749, 740, 732, 724, 717, 711, 704, + 699, 693, 688, 683, 679, 674, 670, 666, 663, 659, 655, 652, 649, 646, 643, 640, 637, 634, 632, + 629, 627, 624, 622, 620, 618, 615, 613, 611, 609, 607, 606, 604, 602, 600, 598, 597, 595, 593, + 592, 590, 589, 587, 586, 584, 583, 582, 580, 579, 578, 576, 575, 574, 573, 571, 570, 569, 568, + 567, 566, 565, 563, 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 552, 551, 550, 549, + 548, 547, 546, 545, 545, 544, 543, 542, 541, 541, 540, 539, 538, 537, 537, 536, 535, 535, 534, + 533, 532, 532, 531, 530, 530, 529, 528, 528, 527, 526, 526, 525, 524, 524, +]; From 88eb122ae1346582ed8b839938f8e65a45420357 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:56:02 +0530 Subject: [PATCH 17/68] Update g1.rs --- crates/precompile/src/bls12_381/g1.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1.rs b/crates/precompile/src/bls12_381/g1.rs index 72df5f5938..db4b0d7fb6 100644 --- a/crates/precompile/src/bls12_381/g1.rs +++ b/crates/precompile/src/bls12_381/g1.rs @@ -1,16 +1,18 @@ use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; +use crate::bls12_381::bls12_381_const::{ + G1_INPUT_ITEM_LENGTH, G1_OUTPUT_LENGTH, PADDED_FP_LENGTH, +}; use crate::PrecompileError; use blst::{blst_p1_affine, blst_p1_affine_in_g1, blst_p1_affine_on_curve}; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{G1_OUTPUT_LENGTH, G1_INPUT_ITEM_LENGTH,UTILS_PADDED_FP_LENGTH}; /// Encodes a G1 point in affine format into byte slice with padded elements. pub(super) fn encode_g1_point(input: *const blst_p1_affine) -> Bytes { let mut out = vec![0u8; G1_OUTPUT_LENGTH]; // SAFETY: Out comes from fixed length array, input is a blst value. unsafe { - fp_to_bytes(&mut out[..UTILS_PADDED_FP_LENGTH], &(*input).x); - fp_to_bytes(&mut out[UTILS_PADDED_FP_LENGTH..], &(*input).y); + fp_to_bytes(&mut out[..PADDED_FP_LENGTH], &(*input).x); + fp_to_bytes(&mut out[PADDED_FP_LENGTH..], &(*input).y); } out.into() } @@ -47,8 +49,8 @@ pub(super) fn extract_g1_input( ))); } - let input_p0_x = remove_padding(&input[..UTILS_PADDED_FP_LENGTH])?; - let input_p0_y = remove_padding(&input[UTILS_PADDED_FP_LENGTH..G1_INPUT_ITEM_LENGTH])?; + let input_p0_x = remove_padding(&input[..PADDED_FP_LENGTH])?; + let input_p0_y = remove_padding(&input[PADDED_FP_LENGTH..G1_INPUT_ITEM_LENGTH])?; let out = decode_and_check_g1(input_p0_x, input_p0_y)?; if subgroup_check { From fa790458f9291917c971c02e28ca8f74e9dd603e Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:56:23 +0530 Subject: [PATCH 18/68] Update g1_add.rs --- crates/precompile/src/bls12_381/g1_add.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index 2aa7a7ad8e..ce0070640f 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -1,17 +1,18 @@ use super::g1::{encode_g1_point, extract_g1_input}; +use crate::bls12_381::bls12_381_const::{ + G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH, +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{ blst_p1, blst_p1_add_or_double_affine, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, }; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH,G1_INPUT_ITEM_LENGTH}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile. pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(u64_to_address(G1_ADD_ADDRESS), g1_add); - /// G1 addition call expects `256` bytes as an input that is interpreted as byte /// concatenation of two G1 points (`128` bytes each). /// Output is an encoding of addition operation result - single G1 point (`128` From 5a3f7d18baf655b4c7d39258bb5fa345805f70df Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:56:46 +0530 Subject: [PATCH 19/68] Update g1_msm.rs --- crates/precompile/src/bls12_381/g1_msm.rs | 27 ++++++++--------------- 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index fcc8ecd6d2..de966fa6ce 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -1,30 +1,21 @@ use super::{ g1::{encode_g1_point, extract_g1_input}, msm::msm_required_gas, - utils::extract_scalar_input + utils::extract_scalar_input, +}; +use crate::bls12_381::bls12_381_const::{ + G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, G1_MSM_INPUT_LENGTH, NBITS, + SCALAR_LENGTH,DISCOUNT_TABLE_G1_MSM }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines}; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, G1_MSM_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH,UTILS_SCALAR_LENGTH, UTILS_NBITS}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(u64_to_address(G1_MSM_ADDRESS), g1_msm); - -/// Discounts table for G1 MSM as a vector of pairs `[k, discount]`. -pub static DISCOUNT_TABLE: [u16; 128] = [ - 1000, 949, 848, 797, 764, 750, 738, 728, 719, 712, 705, 698, 692, 687, 682, 677, 673, 669, 665, - 661, 658, 654, 651, 648, 645, 642, 640, 637, 635, 632, 630, 627, 625, 623, 621, 619, 617, 615, - 613, 611, 609, 608, 606, 604, 603, 601, 599, 598, 596, 595, 593, 592, 591, 589, 588, 586, 585, - 584, 582, 581, 580, 579, 577, 576, 575, 574, 573, 572, 570, 569, 568, 567, 566, 565, 564, 563, - 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 551, 550, 549, 548, 547, 547, 546, 545, - 544, 543, 542, 541, 540, 540, 539, 538, 537, 536, 536, 535, 534, 533, 532, 532, 531, 530, 529, - 528, 528, 527, 526, 525, 525, 524, 523, 522, 522, 521, 520, 520, 519, -]; - /// Implements EIP-2537 G1MSM precompile. /// G1 multi-scalar-multiplication call expects `160*k` bytes as an input that is interpreted /// as byte concatenation of `k` slices each of them being a byte concatenation @@ -44,13 +35,13 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { } let k = input_len / G1_MSM_INPUT_LENGTH; - let required_gas = msm_required_gas(k, &DISCOUNT_TABLE, G1_MSM_BASE_GAS_FEE); + let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G1_MSM, G1_MSM_BASE_GAS_FEE); if required_gas > gas_limit { return Err(PrecompileError::OutOfGas.into()); } let mut g1_points: Vec = Vec::with_capacity(k); - let mut scalars: Vec = Vec::with_capacity(k * UTILS_SCALAR_LENGTH); + let mut scalars: Vec = Vec::with_capacity(k * SCALAR_LENGTH); for i in 0..k { let slice = &input[i * G1_MSM_INPUT_LENGTH..i * G1_MSM_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH]; @@ -73,7 +64,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { scalars.extend_from_slice( &extract_scalar_input( &input[i * G1_MSM_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH - ..i * G1_MSM_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + UTILS_SCALAR_LENGTH], + ..i * G1_MSM_INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + SCALAR_LENGTH], )? .b, ); @@ -85,7 +76,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { } let points = p1_affines::from(&g1_points); - let multiexp = points.mult(&scalars, UTILS_NBITS); + let multiexp = points.mult(&scalars, NBITS); let mut multiexp_aff = blst_p1_affine::default(); // SAFETY: `multiexp_aff` and `multiexp` are blst values. From 9bf0f5fb944e1e4fce7780d842f942b77c92626f Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:57:13 +0530 Subject: [PATCH 20/68] Update g2.rs --- crates/precompile/src/bls12_381/g2.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2.rs b/crates/precompile/src/bls12_381/g2.rs index f3b8a5ce64..545582e7a9 100644 --- a/crates/precompile/src/bls12_381/g2.rs +++ b/crates/precompile/src/bls12_381/g2.rs @@ -1,24 +1,25 @@ use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; +use crate::bls12_381::bls12_381_const::{ + G2_INPUT_ITEM_LENGTH, G2_OUTPUT_LENGTH, FP_LENGTH, PADDED_FP_LENGTH, +}; use crate::PrecompileError; use blst::{blst_fp2, blst_p2_affine, blst_p2_affine_in_g2, blst_p2_affine_on_curve}; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{G2_OUTPUT_LENGTH,G2_INPUT_ITEM_LENGTH, UTILS_FP_LENGTH, UTILS_PADDED_FP_LENGTH}; - /// Encodes a G2 point in affine format into byte slice with padded elements. pub(super) fn encode_g2_point(input: &blst_p2_affine) -> Bytes { let mut out = vec![0u8; G2_OUTPUT_LENGTH]; - fp_to_bytes(&mut out[..UTILS_PADDED_FP_LENGTH], &input.x.fp[0]); + fp_to_bytes(&mut out[..PADDED_FP_LENGTH], &input.x.fp[0]); fp_to_bytes( - &mut out[UTILS_PADDED_FP_LENGTH..2 * UTILS_PADDED_FP_LENGTH], + &mut out[PADDED_FP_LENGTH..2 * PADDED_FP_LENGTH], &input.x.fp[1], ); fp_to_bytes( - &mut out[2 * UTILS_PADDED_FP_LENGTH..3 * UTILS_PADDED_FP_LENGTH], + &mut out[2 * PADDED_FP_LENGTH..3 * PADDED_FP_LENGTH], &input.y.fp[0], ); fp_to_bytes( - &mut out[3 * UTILS_PADDED_FP_LENGTH..4 * UTILS_PADDED_FP_LENGTH], + &mut out[3 * PADDED_FP_LENGTH..4 * PADDED_FP_LENGTH], &input.y.fp[1], ); out.into() @@ -65,9 +66,10 @@ pub(super) fn extract_g2_input( ))); } - let mut input_fps = [&[0; UTILS_FP_LENGTH]; 4]; + let mut input_fps = [&[0; FP_LENGTH]; 4]; for i in 0..4 { - input_fps[i] = remove_padding(&input[i * UTILS_PADDED_FP_LENGTH..(i + 1) * UTILS_PADDED_FP_LENGTH])?; + input_fps[i] = + remove_padding(&input[i * PADDED_FP_LENGTH..(i + 1) * PADDED_FP_LENGTH])?; } let out = decode_and_check_g2(input_fps[0], input_fps[1], input_fps[2], input_fps[3])?; From 5f414ea6ea227f5e1321e5de118b284875c567ef Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:57:35 +0530 Subject: [PATCH 21/68] Update g2_add.rs --- crates/precompile/src/bls12_381/g2_add.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index 97569a8c28..980261a9e4 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -1,12 +1,13 @@ use super::g2::{encode_g2_point, extract_g2_input}; +use crate::bls12_381::bls12_381_const::{ + G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH, G2_INPUT_ITEM_LENGTH, +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{ blst_p2, blst_p2_add_or_double_affine, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, }; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH,G2_INPUT_ITEM_LENGTH}; - /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2ADD precompile. pub const PRECOMPILE: PrecompileWithAddress = From 96da3a13a431686114dd4c7e1d5a5bedbf050c54 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:58:00 +0530 Subject: [PATCH 22/68] Update g2_msm.rs --- crates/precompile/src/bls12_381/g2_msm.rs | 26 ++++++++--------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 49f0d2c4b7..71d74d12c5 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -1,29 +1,21 @@ use super::{ g2::{encode_g2_point, extract_g2_input}, msm::msm_required_gas, - utils::extract_scalar_input + utils::extract_scalar_input, +}; +use crate::bls12_381::bls12_381_const::{ + G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH, G2_INPUT_ITEM_LENGTH, NBITS, + SCALAR_LENGTH,DISCOUNT_TABLE_G2_MSM }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH,G2_INPUT_ITEM_LENGTH, UTILS_NBITS, UTILS_SCALAR_LENGTH}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(u64_to_address(G2_ADD_ADDRESS), g2_msm); -// Discounts table for G2 MSM as a vector of pairs `[k, discount]`: -pub static DISCOUNT_TABLE: [u16; 128] = [ - 1000, 1000, 923, 884, 855, 832, 812, 796, 782, 770, 759, 749, 740, 732, 724, 717, 711, 704, - 699, 693, 688, 683, 679, 674, 670, 666, 663, 659, 655, 652, 649, 646, 643, 640, 637, 634, 632, - 629, 627, 624, 622, 620, 618, 615, 613, 611, 609, 607, 606, 604, 602, 600, 598, 597, 595, 593, - 592, 590, 589, 587, 586, 584, 583, 582, 580, 579, 578, 576, 575, 574, 573, 571, 570, 569, 568, - 567, 566, 565, 563, 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 552, 551, 550, 549, - 548, 547, 546, 545, 545, 544, 543, 542, 541, 541, 540, 539, 538, 537, 537, 536, 535, 535, 534, - 533, 532, 532, 531, 530, 530, 529, 528, 528, 527, 526, 526, 525, 524, 524, -]; - /// Implements EIP-2537 G2MSM precompile. /// G2 multi-scalar-multiplication call expects `288*k` bytes as an input that is interpreted /// as byte concatenation of `k` slices each of them being a byte concatenation @@ -43,13 +35,13 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { } let k = input_len / G2_ADD_INPUT_LENGTH; - let required_gas = msm_required_gas(k, &DISCOUNT_TABLE, G2_ADD_BASE_GAS_FEE); + let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G2_MSM, G2_ADD_BASE_GAS_FEE); if required_gas > gas_limit { return Err(PrecompileError::OutOfGas.into()); } let mut g2_points: Vec = Vec::with_capacity(k); - let mut scalars: Vec = Vec::with_capacity(k * UTILS_SCALAR_LENGTH); + let mut scalars: Vec = Vec::with_capacity(k * SCALAR_LENGTH); for i in 0..k { let slice = &input[i * G2_ADD_INPUT_LENGTH..i * G2_ADD_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH]; // BLST batch API for p2_affines blows up when you pass it a point at infinity, so we must @@ -72,7 +64,7 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { scalars.extend_from_slice( &extract_scalar_input( &input[i * G2_ADD_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH - ..i * G2_ADD_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH + UTILS_SCALAR_LENGTH], + ..i * G2_ADD_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH + SCALAR_LENGTH], )? .b, ); @@ -84,7 +76,7 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { } let points = p2_affines::from(&g2_points); - let multiexp = points.mult(&scalars, UTILS_NBITS); + let multiexp = points.mult(&scalars, NBITS); let mut multiexp_aff = blst_p2_affine::default(); // SAFETY: `multiexp_aff` and `multiexp` are blst values. From da24a29b8c4a52d1727a54c622997f38ebe15b2c Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:58:30 +0530 Subject: [PATCH 23/68] Update map_fp2_to_g2.rs --- .../precompile/src/bls12_381/map_fp2_to_g2.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index dfc14febd7..f6d1d3f619 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,13 +1,12 @@ -use super::{ - g2::check_canonical_fp2, - g2::encode_g2_point, - utils::remove_padding +use super::{g2::check_canonical_fp2, g2::encode_g2_point, utils::remove_padding}; +use crate::bls12_381::bls12_381_const::{ + MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, + PADDED_FP_LENGTH, }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine}; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, UTILS_PADDED_FP2_LENGTH, UTILS_PADDED_FP_LENGTH}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP2_TO_G2 precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -17,21 +16,21 @@ pub const PRECOMPILE: PrecompileWithAddress = /// an element of Fp2. Output of this call is 256 bytes and is an encoded G2 /// point. /// See also: -pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { +pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { if MAP_FP2_TO_G2_BASE_GAS_FEE > gas_limit { return Err(PrecompileError::OutOfGas.into()); } - if input.len() != UTILS_PADDED_FP2_LENGTH { + if input.len() != PADDED_FP2_LENGTH { return Err(PrecompileError::Other(format!( - "MAP_FP2_TO_G2 input should be {UTILS_PADDED_FP2_LENGTH} bytes, was {}", + "MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}", input.len() )) .into()); } - let input_p0_x = remove_padding(&input[..UTILS_PADDED_FP_LENGTH])?; - let input_p0_y = remove_padding(&input[UTILS_PADDED_FP_LENGTH..UTILS_PADDED_FP2_LENGTH])?; + let input_p0_x = remove_padding(&input[..PADDED_FP_LENGTH])?; + let input_p0_y = remove_padding(&input[PADDED_FP_LENGTH..PADDED_FP2_LENGTH])?; let fp2 = check_canonical_fp2(input_p0_x, input_p0_y)?; let mut p = blst_p2::default(); From 37adee4a36c44e16ac7d50e27ccfa4bdfc6fd1b0 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:59:00 +0530 Subject: [PATCH 24/68] Update map_fp_to_g1.rs --- crates/precompile/src/bls12_381/map_fp_to_g1.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index 8991ab2ced..8b1a3afcd8 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -2,11 +2,13 @@ use super::{ g1::encode_g1_point, utils::{fp_from_bendian, remove_padding}, }; +use crate::bls12_381::bls12_381_const::{ + MAP_FP_TO_G1_ADDRESS, MAP_FP_TO_G1_BASE_GAS_FEE, PADDED_FP_LENGTH, +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g1, blst_p1, blst_p1_affine, blst_p1_to_affine}; use primitives::Bytes; -use crate::bls12_381::bls12_381_const::{MAP_FP_TO_G1_ADDRESS, MAP_FP_TO_G1_BASE_GAS_FEE, UTILS_PADDED_FP_LENGTH}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP_TO_G1 precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -20,9 +22,9 @@ pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult { return Err(PrecompileError::OutOfGas.into()); } - if input.len() != UTILS_PADDED_FP_LENGTH { + if input.len() != PADDED_FP_LENGTH { return Err(PrecompileError::Other(format!( - "MAP_FP_TO_G1 input should be {UTILS_PADDED_FP_LENGTH} bytes, was {}", + "MAP_FP_TO_G1 input should be {PADDED_FP_LENGTH} bytes, was {}", input.len() )) .into()); From 66a8485fed7f8ac10b7483977c2444152d4bc435 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:59:24 +0530 Subject: [PATCH 25/68] Update pairing.rs --- crates/precompile/src/bls12_381/pairing.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index 7434a58bb5..90f197aa7f 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -1,14 +1,13 @@ -use super::{ - g1::extract_g1_input, - g2::extract_g2_input, +use super::{g1::extract_g1_input, g2::extract_g2_input}; +use crate::bls12_381::bls12_381_const::{ + G1_INPUT_ITEM_LENGTH, G2_INPUT_ITEM_LENGTH, PAIRING_ADDRESS, PAIRING_INPUT_LENGTH, + PAIRING_PAIRING_MULTIPLIER_BAS, PAIRING_PAIRING_OFFSET_BASE, }; use crate::{ u64_to_address, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress, }; use blst::{blst_final_exp, blst_fp12, blst_fp12_is_one, blst_fp12_mul, blst_miller_loop}; use primitives::{Bytes, B256}; -use crate::bls12_381::bls12_381_const::{PAIRING_ADDRESS, PAIRING_PAIRING_MULTIPLIER_BAS, PAIRING_PAIRING_OFFSET_BASE, PAIRING_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH,G2_INPUT_ITEM_LENGTH}; - /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile. pub const PRECOMPILE: PrecompileWithAddress = From 31fa51e2d586abee9993c9020ca0467ec34e443f Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:59:41 +0530 Subject: [PATCH 26/68] Update utils.rs --- crates/precompile/src/bls12_381/utils.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/precompile/src/bls12_381/utils.rs b/crates/precompile/src/bls12_381/utils.rs index 8bbe6488f3..06dca747fa 100644 --- a/crates/precompile/src/bls12_381/utils.rs +++ b/crates/precompile/src/bls12_381/utils.rs @@ -1,33 +1,35 @@ +use crate::bls12_381::bls12_381_const::{ + FP_LENGTH, MODULUS_REPR, PADDED_FP_LENGTH, PADDING_LENGTH,SCALAR_LENGTH, +}; use crate::PrecompileError; use blst::{ blst_bendian_from_fp, blst_fp, blst_fp_from_bendian, blst_scalar, blst_scalar_from_bendian, }; use core::cmp::Ordering; -use crate::bls12_381::bls12_381_const::{UTILS_FP_LENGTH, UTILS_PADDED_FP_LENGTH, UTILS_PADDING_LENGTH, UTILS_SCALAR_LENGTH, UTILS_MODULUS_REPR}; /// Encodes a single finite field element into byte slice with padding. pub(super) fn fp_to_bytes(out: &mut [u8], input: *const blst_fp) { - if out.len() != UTILS_PADDED_FP_LENGTH { + if out.len() != PADDED_FP_LENGTH { return; } - let (padding, rest) = out.split_at_mut(UTILS_PADDING_LENGTH); + let (padding, rest) = out.split_at_mut(PADDING_LENGTH); padding.fill(0); // SAFETY: Out length is checked previously, `input` is a blst value. unsafe { blst_bendian_from_fp(rest.as_mut_ptr(), input) }; } /// Removes zeros with which the precompile inputs are left padded to 64 bytes. -pub(super) fn remove_padding(input: &[u8]) -> Result<&[u8; UTILS_FP_LENGTH], PrecompileError> { - if input.len() != UTILS_PADDED_FP_LENGTH { +pub(super) fn remove_padding(input: &[u8]) -> Result<&[u8; FP_LENGTH], PrecompileError> { + if input.len() != PADDED_FP_LENGTH { return Err(PrecompileError::Other(format!( - "Padded input should be {UTILS_PADDED_FP_LENGTH} bytes, was {}", + "Padded input should be {PADDED_FP_LENGTH} bytes, was {}", input.len() ))); } - let (padding, unpadded) = input.split_at(UTILS_PADDING_LENGTH); + let (padding, unpadded) = input.split_at(PADDING_LENGTH); if !padding.iter().all(|&x| x == 0) { return Err(PrecompileError::Other(format!( - "{UTILS_PADDING_LENGTH} top bytes of input are not zero", + "{PADDING_LENGTH} top bytes of input are not zero", ))); } Ok(unpadded.try_into().unwrap()) @@ -44,9 +46,9 @@ pub(super) fn remove_padding(input: &[u8]) -> Result<&[u8; UTILS_FP_LENGTH], Pre /// * The corresponding integer is not required to be less than or equal than main subgroup order /// `q`. pub(super) fn extract_scalar_input(input: &[u8]) -> Result { - if input.len() != UTILS_SCALAR_LENGTH { + if input.len() != SCALAR_LENGTH { return Err(PrecompileError::Other(format!( - "Input should be {UTILS_SCALAR_LENGTH} bytes, was {}", + "Input should be {SCALAR_LENGTH} bytes, was {}", input.len() ))); } @@ -66,7 +68,7 @@ pub(super) fn extract_scalar_input(input: &[u8]) -> Result bool { - for (i, modulo) in input.iter().zip(UTILS_MODULUS_REPR.iter()) { + for (i, modulo) in input.iter().zip(MODULUS_REPR.iter()) { match i.cmp(modulo) { Ordering::Greater => return false, Ordering::Less => return true, From 4090e87eccd5646b74e9bea6e24fb491975a028b Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:49:58 +0530 Subject: [PATCH 27/68] Delete crates/precompile/src/bls12_381/bls12_381_const.rs --- .../src/bls12_381/bls12_381_const.rs | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 crates/precompile/src/bls12_381/bls12_381_const.rs diff --git a/crates/precompile/src/bls12_381/bls12_381_const.rs b/crates/precompile/src/bls12_381/bls12_381_const.rs deleted file mode 100644 index d6a0b999c3..0000000000 --- a/crates/precompile/src/bls12_381/bls12_381_const.rs +++ /dev/null @@ -1,63 +0,0 @@ -pub const G1_ADD_ADDRESS: u64 = 0x0b; -pub const G1_ADD_BASE_GAS_FEE: u64 = 375; -pub const G1_ADD_INPUT_LENGTH: usize = 256; -pub const G1_MSM_ADDRESS: u64 = 0x0c; -pub const G1_MSM_BASE_GAS_FEE: u64 = 1200; -pub const G1_MSM_INPUT_LENGTH: usize = 160; -pub const G1_OUTPUT_LENGTH: usize = 128; -pub const G1_INPUT_ITEM_LENGTH: usize = 128; -pub const G2_ADD_ADDRESS: u64 = 0x0d; -pub const G2_ADD_BASE_GAS_FEE: u64 = 600; -pub const G2_ADD_INPUT_LENGTH: usize = 512; -pub const G2_MSM_ADDRESS: u64 = 0x0e; -pub const G2_MSM_BASE_GAS_FEE: u64 = 22500; -pub const G2_MSM_INPUT_LENGTH: usize = 288; -pub const G2_OUTPUT_LENGTH: usize = 256; -pub const G2_INPUT_ITEM_LENGTH: usize = 256; -pub const PAIRING_ADDRESS: u64 = 0x0f; -pub const PAIRING_PAIRING_MULTIPLIER_BAS: u64 = 32600; -pub const PAIRING_PAIRING_OFFSET_BASE: u64 = 37700; -pub const PAIRING_INPUT_LENGTH: usize = 384; -pub const MAP_FP_TO_G1_ADDRESS: u64 = 0x10; -pub const MAP_FP_TO_G1_BASE_GAS_FEE: u64 = 5500; -pub const MAP_FP2_TO_G2_ADDRESS: u64 = 0x11; -pub const MAP_FP2_TO_G2_BASE_GAS_FEE: u64 = 0x23800; -pub const MSM_MULTIPLIER: u64 = 1000; -/// Number of bits used in the BLS12-381 curve finite field elements. -pub const NBITS: usize = 256; -/// Finite field element input length. -pub const FP_LENGTH: usize = 48; -/// Finite field element padded input length. -pub const PADDED_FP_LENGTH: usize = 64; -/// Quadratic extension of finite field element input length. -pub const PADDED_FP2_LENGTH: usize = 128; -/// Input elements padding length. -pub const PADDING_LENGTH: usize = 16; -/// Scalar length. -pub const SCALAR_LENGTH: usize = 32; -// Big-endian non-Montgomery form. -pub const MODULUS_REPR: [u8; 48] = [ - 0x1a, 0x01, 0x11, 0xea, 0x39, 0x7f, 0xe6, 0x9a, 0x4b, 0x1b, 0xa7, 0xb6, 0x43, 0x4b, 0xac, 0xd7, - 0x64, 0x77, 0x4b, 0x84, 0xf3, 0x85, 0x12, 0xbf, 0x67, 0x30, 0xd2, 0xa0, 0xf6, 0xb0, 0xf6, 0x24, - 0x1e, 0xab, 0xff, 0xfe, 0xb1, 0x53, 0xff, 0xff, 0xb9, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xaa, 0xab, -]; -/// Discounts table for G1 MSM as a vector of pairs `[k, discount]`. -pub static DISCOUNT_TABLE_G1_MSM: [u16; 128] = [ - 1000, 949, 848, 797, 764, 750, 738, 728, 719, 712, 705, 698, 692, 687, 682, 677, 673, 669, 665, - 661, 658, 654, 651, 648, 645, 642, 640, 637, 635, 632, 630, 627, 625, 623, 621, 619, 617, 615, - 613, 611, 609, 608, 606, 604, 603, 601, 599, 598, 596, 595, 593, 592, 591, 589, 588, 586, 585, - 584, 582, 581, 580, 579, 577, 576, 575, 574, 573, 572, 570, 569, 568, 567, 566, 565, 564, 563, - 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 551, 550, 549, 548, 547, 547, 546, 545, - 544, 543, 542, 541, 540, 540, 539, 538, 537, 536, 536, 535, 534, 533, 532, 532, 531, 530, 529, - 528, 528, 527, 526, 525, 525, 524, 523, 522, 522, 521, 520, 520, 519, -]; -// Discounts table for G2 MSM as a vector of pairs `[k, discount]`: -pub static DISCOUNT_TABLE_G2_MSM: [u16; 128] = [ - 1000, 1000, 923, 884, 855, 832, 812, 796, 782, 770, 759, 749, 740, 732, 724, 717, 711, 704, - 699, 693, 688, 683, 679, 674, 670, 666, 663, 659, 655, 652, 649, 646, 643, 640, 637, 634, 632, - 629, 627, 624, 622, 620, 618, 615, 613, 611, 609, 607, 606, 604, 602, 600, 598, 597, 595, 593, - 592, 590, 589, 587, 586, 584, 583, 582, 580, 579, 578, 576, 575, 574, 573, 571, 570, 569, 568, - 567, 566, 565, 563, 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 552, 551, 550, 549, - 548, 547, 546, 545, 545, 544, 543, 542, 541, 541, 540, 539, 538, 537, 537, 536, 535, 535, 534, - 533, 532, 532, 531, 530, 530, 529, 528, 528, 527, 526, 526, 525, 524, 524, -]; From f6dc7a5a3b0bc499ea53c0629656c7479c0029b8 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:50:38 +0530 Subject: [PATCH 28/68] Update lib.rs --- crates/precompile/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 91053c921d..838a6ea50f 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -22,6 +22,7 @@ pub mod secp256k1; #[cfg(feature = "secp256r1")] pub mod secp256r1; pub mod utilities; +pub mod bls12_381_const; pub use interface::*; #[cfg(all(feature = "c-kzg", feature = "kzg-rs"))] From 1073fec14766caf35af96d83b0e51202ef720d04 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:51:15 +0530 Subject: [PATCH 29/68] Add files via upload --- crates/precompile/src/bls12_381_const.rs | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 crates/precompile/src/bls12_381_const.rs diff --git a/crates/precompile/src/bls12_381_const.rs b/crates/precompile/src/bls12_381_const.rs new file mode 100644 index 0000000000..fea9967b04 --- /dev/null +++ b/crates/precompile/src/bls12_381_const.rs @@ -0,0 +1,63 @@ +pub const G1_ADD_ADDRESS: u64 = 0x0b; +pub const G1_ADD_BASE_GAS_FEE: u64 = 375; +pub const G1_ADD_INPUT_LENGTH: usize = 256; +pub const G1_MSM_ADDRESS: u64 = 0x0c; +pub const G1_MSM_BASE_GAS_FEE: u64 = 1200; +pub const G1_MSM_INPUT_LENGTH: usize = 160; +pub const G1_OUTPUT_LENGTH: usize = 128; +pub const G1_INPUT_ITEM_LENGTH: usize = 128; +pub const G2_ADD_ADDRESS: u64 = 0x0d; +pub const G2_ADD_BASE_GAS_FEE: u64 = 600; +pub const G2_ADD_INPUT_LENGTH: usize = 512; +pub const G2_MSM_ADDRESS: u64 = 0x0e; +pub const G2_MSM_BASE_GAS_FEE: u64 = 22500; +pub const G2_MSM_INPUT_LENGTH: usize = 288; +pub const G2_OUTPUT_LENGTH: usize = 256; +pub const G2_INPUT_ITEM_LENGTH: usize = 256; +pub const PAIRING_ADDRESS: u64 = 0x0f; +pub const PAIRING_PAIRING_MULTIPLIER_BAS: u64 = 32600; +pub const PAIRING_PAIRING_OFFSET_BASE: u64 = 37700; +pub const PAIRING_INPUT_LENGTH: usize = 384; +pub const MAP_FP_TO_G1_ADDRESS: u64 = 0x10; +pub const MAP_FP_TO_G1_BASE_GAS_FEE: u64 = 5500; +pub const MAP_FP2_TO_G2_ADDRESS: u64 = 0x11; +pub const MAP_FP2_TO_G2_BASE_GAS_FEE: u64 = 0x23800; +pub const MSM_MULTIPLIER: u64 = 1000; +/// Number of bits used in the BLS12-381 curve finite field elements. +pub const NBITS: usize = 256; +/// Finite field element input length. +pub const FP_LENGTH: usize = 48; +/// Finite field element padded input length. +pub const PADDED_FP_LENGTH: usize = 64; +/// Quadratic extension of finite field element input length. +pub const PADDED_FP2_LENGTH: usize = 128; +/// Input elements padding length. +pub const PADDING_LENGTH: usize = 16; +/// Scalar length. +pub const SCALAR_LENGTH: usize = 32; +// Big-endian non-Montgomery form. +pub const MODULUS_REPR: [u8; 48] = [ + 0x1a, 0x01, 0x11, 0xea, 0x39, 0x7f, 0xe6, 0x9a, 0x4b, 0x1b, 0xa7, 0xb6, 0x43, 0x4b, 0xac, 0xd7, + 0x64, 0x77, 0x4b, 0x84, 0xf3, 0x85, 0x12, 0xbf, 0x67, 0x30, 0xd2, 0xa0, 0xf6, 0xb0, 0xf6, 0x24, + 0x1e, 0xab, 0xff, 0xfe, 0xb1, 0x53, 0xff, 0xff, 0xb9, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xaa, 0xab, +]; +/// Discounts table for G1 MSM as a vector of pairs `[k, discount]`. +pub static DISCOUNT_TABLE_G1_MSM: [u16; 128] = [ + 1000, 949, 848, 797, 764, 750, 738, 728, 719, 712, 705, 698, 692, 687, 682, 677, 673, 669, 665, + 661, 658, 654, 651, 648, 645, 642, 640, 637, 635, 632, 630, 627, 625, 623, 621, 619, 617, 615, + 613, 611, 609, 608, 606, 604, 603, 601, 599, 598, 596, 595, 593, 592, 591, 589, 588, 586, 585, + 584, 582, 581, 580, 579, 577, 576, 575, 574, 573, 572, 570, 569, 568, 567, 566, 565, 564, 563, + 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 551, 550, 549, 548, 547, 547, 546, 545, + 544, 543, 542, 541, 540, 540, 539, 538, 537, 536, 536, 535, 534, 533, 532, 532, 531, 530, 529, + 528, 528, 527, 526, 525, 525, 524, 523, 522, 522, 521, 520, 520, 519, +]; +// Discounts table for G2 MSM as a vector of pairs `[k, discount]`: +pub static DISCOUNT_TABLE_G2_MSM: [u16; 128] = [ + 1000, 1000, 923, 884, 855, 832, 812, 796, 782, 770, 759, 749, 740, 732, 724, 717, 711, 704, + 699, 693, 688, 683, 679, 674, 670, 666, 663, 659, 655, 652, 649, 646, 643, 640, 637, 634, 632, + 629, 627, 624, 622, 620, 618, 615, 613, 611, 609, 607, 606, 604, 602, 600, 598, 597, 595, 593, + 592, 590, 589, 587, 586, 584, 583, 582, 580, 579, 578, 576, 575, 574, 573, 571, 570, 569, 568, + 567, 566, 565, 563, 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 552, 551, 550, 549, + 548, 547, 546, 545, 545, 544, 543, 542, 541, 541, 540, 539, 538, 537, 537, 536, 535, 535, 534, + 533, 532, 532, 531, 530, 530, 529, 528, 528, 527, 526, 526, 525, 524, 524, +]; \ No newline at end of file From f2bb09661ca0ff50677354da04ab1c75d1d76083 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:52:25 +0530 Subject: [PATCH 30/68] Update utils.rs --- crates/precompile/src/bls12_381/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/utils.rs b/crates/precompile/src/bls12_381/utils.rs index 06dca747fa..3d4cc78b68 100644 --- a/crates/precompile/src/bls12_381/utils.rs +++ b/crates/precompile/src/bls12_381/utils.rs @@ -1,4 +1,4 @@ -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ FP_LENGTH, MODULUS_REPR, PADDED_FP_LENGTH, PADDING_LENGTH,SCALAR_LENGTH, }; use crate::PrecompileError; From cdce5bac507e64b34a153347e2e5defa291fa8d8 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:53:30 +0530 Subject: [PATCH 31/68] Update pairing.rs --- crates/precompile/src/bls12_381/pairing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index 90f197aa7f..1fa1f19fd3 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -1,5 +1,5 @@ use super::{g1::extract_g1_input, g2::extract_g2_input}; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ G1_INPUT_ITEM_LENGTH, G2_INPUT_ITEM_LENGTH, PAIRING_ADDRESS, PAIRING_INPUT_LENGTH, PAIRING_PAIRING_MULTIPLIER_BAS, PAIRING_PAIRING_OFFSET_BASE, }; From 19e7ce69276b5fc9a4cc89804dabbdf064acf749 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:54:19 +0530 Subject: [PATCH 32/68] Update msm.rs --- crates/precompile/src/bls12_381/msm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/msm.rs b/crates/precompile/src/bls12_381/msm.rs index 89da57c033..b72fd90dcd 100644 --- a/crates/precompile/src/bls12_381/msm.rs +++ b/crates/precompile/src/bls12_381/msm.rs @@ -1,4 +1,4 @@ -use crate::bls12_381::bls12_381_const::MSM_MULTIPLIER; +use crate::bls12_381_const::MSM_MULTIPLIER; /// Implements the gas schedule for G1/G2 Multiscalar-multiplication assuming 30 /// MGas/second, see also: From b259b1e27248b84fc4f864dbed8a9db231cda1c4 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:54:36 +0530 Subject: [PATCH 33/68] Update map_fp_to_g1.rs --- crates/precompile/src/bls12_381/map_fp_to_g1.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index 8b1a3afcd8..0e19034347 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -2,7 +2,7 @@ use super::{ g1::encode_g1_point, utils::{fp_from_bendian, remove_padding}, }; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ MAP_FP_TO_G1_ADDRESS, MAP_FP_TO_G1_BASE_GAS_FEE, PADDED_FP_LENGTH, }; use crate::{u64_to_address, PrecompileWithAddress}; From 9860c79a3317edd99886525cf366d01f044e4ee3 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:54:54 +0530 Subject: [PATCH 34/68] Update map_fp2_to_g2.rs --- crates/precompile/src/bls12_381/map_fp2_to_g2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index f6d1d3f619..d88c576a9e 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,5 +1,5 @@ use super::{g2::check_canonical_fp2, g2::encode_g2_point, utils::remove_padding}; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH, }; From 04b32b62ce481290c2265c9662883c2e08931f63 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:55:12 +0530 Subject: [PATCH 35/68] Update g2_msm.rs --- crates/precompile/src/bls12_381/g2_msm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 71d74d12c5..78211e03c2 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -3,7 +3,7 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH, G2_INPUT_ITEM_LENGTH, NBITS, SCALAR_LENGTH,DISCOUNT_TABLE_G2_MSM }; From 0c8aeea29f01ef04e7a00b4c21ad0000175efe8d Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:55:33 +0530 Subject: [PATCH 36/68] Update g2_add.rs --- crates/precompile/src/bls12_381/g2_add.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index 980261a9e4..70355579be 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -1,5 +1,5 @@ use super::g2::{encode_g2_point, extract_g2_input}; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH, G2_INPUT_ITEM_LENGTH, }; use crate::{u64_to_address, PrecompileWithAddress}; From c828bf9df2b5bc4a67c5cde68fbb04ea6fb030d0 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:55:51 +0530 Subject: [PATCH 37/68] Update g2.rs --- crates/precompile/src/bls12_381/g2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g2.rs b/crates/precompile/src/bls12_381/g2.rs index 545582e7a9..828842955f 100644 --- a/crates/precompile/src/bls12_381/g2.rs +++ b/crates/precompile/src/bls12_381/g2.rs @@ -1,5 +1,5 @@ use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ G2_INPUT_ITEM_LENGTH, G2_OUTPUT_LENGTH, FP_LENGTH, PADDED_FP_LENGTH, }; use crate::PrecompileError; From 1dc038fb2b8f7929e9411e77cc843e31d5fe64d4 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:56:20 +0530 Subject: [PATCH 38/68] Update g1_msm.rs --- crates/precompile/src/bls12_381/g1_msm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index de966fa6ce..6a4f0e3300 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -3,7 +3,7 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH,DISCOUNT_TABLE_G1_MSM }; From 2ef3aaea82dfb2433d32d2a0692242f81c7d9012 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:56:35 +0530 Subject: [PATCH 39/68] Update g1_add.rs --- crates/precompile/src/bls12_381/g1_add.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index ce0070640f..8607fa66eb 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -1,5 +1,5 @@ use super::g1::{encode_g1_point, extract_g1_input}; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH, }; use crate::{u64_to_address, PrecompileWithAddress}; From c3c91c30eb5cdf32a1a2f0c3676c39f1fca21fbd Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:56:50 +0530 Subject: [PATCH 40/68] Update g1.rs --- crates/precompile/src/bls12_381/g1.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g1.rs b/crates/precompile/src/bls12_381/g1.rs index db4b0d7fb6..210b01f0a3 100644 --- a/crates/precompile/src/bls12_381/g1.rs +++ b/crates/precompile/src/bls12_381/g1.rs @@ -1,5 +1,5 @@ use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; -use crate::bls12_381::bls12_381_const::{ +use crate::bls12_381_const::{ G1_INPUT_ITEM_LENGTH, G1_OUTPUT_LENGTH, PADDED_FP_LENGTH, }; use crate::PrecompileError; From f5e0d122e826f91aae78205fdbfd1bcd59c06b4e Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 18 Feb 2025 10:57:59 +0530 Subject: [PATCH 41/68] Update bls12_381.rs --- crates/precompile/src/bls12_381.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/crates/precompile/src/bls12_381.rs b/crates/precompile/src/bls12_381.rs index 2c0a8c6c16..dc3f665292 100644 --- a/crates/precompile/src/bls12_381.rs +++ b/crates/precompile/src/bls12_381.rs @@ -1,27 +1,18 @@ use crate::PrecompileWithAddress; mod g1; -#[cfg(feature = "blst")] pub mod g1_add; -#[cfg(feature = "blst")] pub mod g1_msm; mod g2; -#[cfg(feature = "blst")] pub mod g2_add; -#[cfg(feature = "blst")] pub mod g2_msm; -#[cfg(feature = "blst")] pub mod map_fp2_to_g2; -#[cfg(feature = "blst")] pub mod map_fp_to_g1; -#[cfg(feature = "blst")] pub mod pairing; mod utils; -pub mod bls12_381_const; pub mod msm; /// Returns the BLS12-381 precompiles with their addresses. -#[cfg(feature = "blst")] pub fn precompiles() -> impl Iterator { [ g1_add::PRECOMPILE, From 0405e7f8309d56e64460e1630f8a3abeda1daba9 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:25:22 +0530 Subject: [PATCH 42/68] Update g1.rs --- crates/precompile/src/bls12_381/g1.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1.rs b/crates/precompile/src/bls12_381/g1.rs index 210b01f0a3..27d3db0bdf 100644 --- a/crates/precompile/src/bls12_381/g1.rs +++ b/crates/precompile/src/bls12_381/g1.rs @@ -1,7 +1,5 @@ use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; -use crate::bls12_381_const::{ - G1_INPUT_ITEM_LENGTH, G1_OUTPUT_LENGTH, PADDED_FP_LENGTH, -}; +use crate::bls12_381_const::{G1_INPUT_ITEM_LENGTH, G1_OUTPUT_LENGTH, PADDED_FP_LENGTH}; use crate::PrecompileError; use blst::{blst_p1_affine, blst_p1_affine_in_g1, blst_p1_affine_on_curve}; use primitives::Bytes; From ba1c27ebf073b419fad12d1ab5c7674fdba04601 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:29:56 +0530 Subject: [PATCH 43/68] Update g1_msm.rs --- crates/precompile/src/bls12_381/g1_msm.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index 6a4f0e3300..07181f341d 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -3,10 +3,8 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381_const::{ - G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, G1_MSM_INPUT_LENGTH, NBITS, - SCALAR_LENGTH,DISCOUNT_TABLE_G1_MSM -}; +use crate::bls12_381_const::{DISCOUNT_TABLE_G1_MSM, G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, ++ G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines}; From bc47a3e2372ae167834f77af370734902e00021b Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:30:30 +0530 Subject: [PATCH 44/68] Update g2.rs --- crates/precompile/src/bls12_381/g2.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2.rs b/crates/precompile/src/bls12_381/g2.rs index 828842955f..45ec5a7be0 100644 --- a/crates/precompile/src/bls12_381/g2.rs +++ b/crates/precompile/src/bls12_381/g2.rs @@ -1,7 +1,5 @@ use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; -use crate::bls12_381_const::{ - G2_INPUT_ITEM_LENGTH, G2_OUTPUT_LENGTH, FP_LENGTH, PADDED_FP_LENGTH, -}; ++use crate::bls12_381_const::{FP_LENGTH, G2_INPUT_ITEM_LENGTH, G2_OUTPUT_LENGTH, PADDED_FP_LENGTH}; use crate::PrecompileError; use blst::{blst_fp2, blst_p2_affine, blst_p2_affine_in_g2, blst_p2_affine_on_curve}; use primitives::Bytes; From d1cee003b0516e6993823ad711803fe57c569ede Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:31:55 +0530 Subject: [PATCH 45/68] Update g2.rs --- crates/precompile/src/bls12_381/g2.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2.rs b/crates/precompile/src/bls12_381/g2.rs index 45ec5a7be0..98c6a24cb1 100644 --- a/crates/precompile/src/bls12_381/g2.rs +++ b/crates/precompile/src/bls12_381/g2.rs @@ -66,8 +66,7 @@ pub(super) fn extract_g2_input( let mut input_fps = [&[0; FP_LENGTH]; 4]; for i in 0..4 { - input_fps[i] = - remove_padding(&input[i * PADDED_FP_LENGTH..(i + 1) * PADDED_FP_LENGTH])?; + input_fps[i] = remove_padding(&input[i * PADDED_FP_LENGTH..(i + 1) * PADDED_FP_LENGTH])?; } let out = decode_and_check_g2(input_fps[0], input_fps[1], input_fps[2], input_fps[3])?; From 791f2e0c610140ef02d62cb94af5bd02a0ea7966 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:33:59 +0530 Subject: [PATCH 46/68] Update g2_msm.rs --- crates/precompile/src/bls12_381/g2_msm.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 78211e03c2..b1124e3c3a 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -3,10 +3,7 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381_const::{ - G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH, G2_INPUT_ITEM_LENGTH, NBITS, - SCALAR_LENGTH,DISCOUNT_TABLE_G2_MSM -}; +use crate::bls12_381_const::{DISCOUNT_TABLE_G2_MSM, G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH,G2_INPUT_ITEM_LENGTH, NBITS, SCALAR_LENGTH}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; From e13e418c90d64b77beff74574252ce0db37bed8a Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:36:08 +0530 Subject: [PATCH 47/68] Update g1_msm.rs --- crates/precompile/src/bls12_381/g1_msm.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index 07181f341d..a58fd6e07c 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -3,8 +3,7 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381_const::{DISCOUNT_TABLE_G1_MSM, G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, -+ G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH}; +use crate::bls12_381_const::{DISCOUNT_TABLE_G1_MSM, G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE,G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines}; From 25d447d3278d4565e0d005b2e5564186177f9516 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:38:26 +0530 Subject: [PATCH 48/68] Update g2.rs --- crates/precompile/src/bls12_381/g2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g2.rs b/crates/precompile/src/bls12_381/g2.rs index 98c6a24cb1..38159cf073 100644 --- a/crates/precompile/src/bls12_381/g2.rs +++ b/crates/precompile/src/bls12_381/g2.rs @@ -1,5 +1,5 @@ use super::utils::{fp_from_bendian, fp_to_bytes, remove_padding}; -+use crate::bls12_381_const::{FP_LENGTH, G2_INPUT_ITEM_LENGTH, G2_OUTPUT_LENGTH, PADDED_FP_LENGTH}; +use crate::bls12_381_const::{FP_LENGTH, G2_INPUT_ITEM_LENGTH, G2_OUTPUT_LENGTH, PADDED_FP_LENGTH}; use crate::PrecompileError; use blst::{blst_fp2, blst_p2_affine, blst_p2_affine_in_g2, blst_p2_affine_on_curve}; use primitives::Bytes; From 7547528707ebab58b15c627960d0c89cd0709eb9 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:38:58 +0530 Subject: [PATCH 49/68] Update g1_add.rs --- crates/precompile/src/bls12_381/g1_add.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index 8607fa66eb..2698e2fb16 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -1,7 +1,5 @@ use super::g1::{encode_g1_point, extract_g1_input}; -use crate::bls12_381_const::{ - G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH, -}; +use crate::bls12_381_const::{G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{ From 80fbd53095f71c653b2e5228fe995028718e59ee Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:42:44 +0530 Subject: [PATCH 50/68] Update g1_add.rs --- crates/precompile/src/bls12_381/g1_add.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index 2698e2fb16..8607fa66eb 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -1,5 +1,7 @@ use super::g1::{encode_g1_point, extract_g1_input}; -use crate::bls12_381_const::{G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH}; +use crate::bls12_381_const::{ + G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH, +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{ From 7af1926d58d9045bfe8a72a2bc2b44d8c1b01f7e Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:43:16 +0530 Subject: [PATCH 51/68] Update g1_msm.rs --- crates/precompile/src/bls12_381/g1_msm.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index a58fd6e07c..a5a05bfe1a 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -3,7 +3,10 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381_const::{DISCOUNT_TABLE_G1_MSM, G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE,G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH}; +use crate::bls12_381_const::{ + DISCOUNT_TABLE_G1_MSM, G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, + G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH, +} use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines}; From 708bbc0782f03e6509520557fa8a412f8aaa1228 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:43:28 +0530 Subject: [PATCH 52/68] Update g1_add.rs --- crates/precompile/src/bls12_381/g1_add.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index 8607fa66eb..cd8965a128 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -1,7 +1,7 @@ use super::g1::{encode_g1_point, extract_g1_input}; use crate::bls12_381_const::{ G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH, -}; +} use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{ From 72376e6064a6c756337f7c4ac99aa7c97df6ac08 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:44:56 +0530 Subject: [PATCH 53/68] Update g2_msm.rs --- crates/precompile/src/bls12_381/g2_msm.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index b1124e3c3a..5580ff8aad 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -3,7 +3,10 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381_const::{DISCOUNT_TABLE_G2_MSM, G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH,G2_INPUT_ITEM_LENGTH, NBITS, SCALAR_LENGTH}; +use crate::bls12_381_const::{ + DISCOUNT_TABLE_G1_MSM, G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, + G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH, +} use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; From 629b0706d3440d1fb4a3d8a81ef3337b54459188 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:46:18 +0530 Subject: [PATCH 54/68] Update map_fp2_to_g2.rs --- crates/precompile/src/bls12_381/map_fp2_to_g2.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index d88c576a9e..2caa87d103 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,7 +1,6 @@ use super::{g2::check_canonical_fp2, g2::encode_g2_point, utils::remove_padding}; use crate::bls12_381_const::{ - MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, - PADDED_FP_LENGTH, + MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; From b280c3579783a135f5753c3ee8855137f405e342 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:46:56 +0530 Subject: [PATCH 55/68] Update map_fp_to_g1.rs --- crates/precompile/src/bls12_381/map_fp_to_g1.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp_to_g1.rs b/crates/precompile/src/bls12_381/map_fp_to_g1.rs index 0e19034347..90f6f12ca0 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -2,9 +2,7 @@ use super::{ g1::encode_g1_point, utils::{fp_from_bendian, remove_padding}, }; -use crate::bls12_381_const::{ - MAP_FP_TO_G1_ADDRESS, MAP_FP_TO_G1_BASE_GAS_FEE, PADDED_FP_LENGTH, -}; +use crate::bls12_381_const::{MAP_FP_TO_G1_ADDRESS, MAP_FP_TO_G1_BASE_GAS_FEE, PADDED_FP_LENGTH}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g1, blst_p1, blst_p1_affine, blst_p1_to_affine}; From ca89ecf81890383ccd56713d9126024409489bb6 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:47:41 +0530 Subject: [PATCH 56/68] Update utils.rs --- crates/precompile/src/bls12_381/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/utils.rs b/crates/precompile/src/bls12_381/utils.rs index 3d4cc78b68..b7d884ad54 100644 --- a/crates/precompile/src/bls12_381/utils.rs +++ b/crates/precompile/src/bls12_381/utils.rs @@ -1,5 +1,5 @@ use crate::bls12_381_const::{ - FP_LENGTH, MODULUS_REPR, PADDED_FP_LENGTH, PADDING_LENGTH,SCALAR_LENGTH, + FP_LENGTH, MODULUS_REPR, PADDED_FP_LENGTH, PADDING_LENGTH, SCALAR_LENGTH, }; use crate::PrecompileError; use blst::{ From a9b0845c603029954c1d14e793420d14db9aa60b Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:54:39 +0530 Subject: [PATCH 57/68] Update bls12_381.rs --- crates/precompile/src/bls12_381.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381.rs b/crates/precompile/src/bls12_381.rs index dc3f665292..aa47c90124 100644 --- a/crates/precompile/src/bls12_381.rs +++ b/crates/precompile/src/bls12_381.rs @@ -8,9 +8,9 @@ pub mod g2_add; pub mod g2_msm; pub mod map_fp2_to_g2; pub mod map_fp_to_g1; +pub mod msm; pub mod pairing; mod utils; -pub mod msm; /// Returns the BLS12-381 precompiles with their addresses. pub fn precompiles() -> impl Iterator { From 1149445434a62e0e8a035692770138673f2efc5e Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 11:59:03 +0530 Subject: [PATCH 58/68] Update g1_add.rs --- crates/precompile/src/bls12_381/g1_add.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index cd8965a128..8607fa66eb 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -1,7 +1,7 @@ use super::g1::{encode_g1_point, extract_g1_input}; use crate::bls12_381_const::{ G1_ADD_ADDRESS, G1_ADD_BASE_GAS_FEE, G1_ADD_INPUT_LENGTH, G1_INPUT_ITEM_LENGTH, -} +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{ From 681a35bf7d5d5e2a71372a5000e13581128dd4d2 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:39:26 +0530 Subject: [PATCH 59/68] Update g1_msm.rs --- crates/precompile/src/bls12_381/g1_msm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index a5a05bfe1a..ed390d7f69 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -6,7 +6,7 @@ use super::{ use crate::bls12_381_const::{ DISCOUNT_TABLE_G1_MSM, G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH, -} +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines}; From 451581ff8e5a6080212869174d8c20ed51fd6856 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:46:57 +0530 Subject: [PATCH 60/68] Update g2_msm.rs --- crates/precompile/src/bls12_381/g2_msm.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 5580ff8aad..bcc30add37 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -3,10 +3,8 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381_const::{ - DISCOUNT_TABLE_G1_MSM, G1_INPUT_ITEM_LENGTH, G1_MSM_ADDRESS, G1_MSM_BASE_GAS_FEE, - G1_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH, -} +use crate::bls12_381_const::{G2_ADD_ADDRESS, G2_ADD_INPUT_LENGTH, G2_ADD_BASE_GAS_FEE, G2_INPUT_ITEM_LENGTH, + DISCOUNT_TABLE_G2_MSM, NBITS, SCALAR_LENGTH,} use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; From b4931e5eadf49a39f1d518654ec5f8a2458779be Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:48:29 +0530 Subject: [PATCH 61/68] Update g2_msm.rs --- crates/precompile/src/bls12_381/g2_msm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index bcc30add37..9097b5b3f9 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -4,7 +4,7 @@ use super::{ utils::extract_scalar_input, }; use crate::bls12_381_const::{G2_ADD_ADDRESS, G2_ADD_INPUT_LENGTH, G2_ADD_BASE_GAS_FEE, G2_INPUT_ITEM_LENGTH, - DISCOUNT_TABLE_G2_MSM, NBITS, SCALAR_LENGTH,} + DISCOUNT_TABLE_G2_MSM, NBITS, SCALAR_LENGTH,}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; From c9b400137460311c702c81856c12c1da5f204ffa Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:50:47 +0530 Subject: [PATCH 62/68] Update g2_msm.rs --- crates/precompile/src/bls12_381/g2_msm.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 9097b5b3f9..de5848b1bf 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -3,8 +3,10 @@ use super::{ msm::msm_required_gas, utils::extract_scalar_input, }; -use crate::bls12_381_const::{G2_ADD_ADDRESS, G2_ADD_INPUT_LENGTH, G2_ADD_BASE_GAS_FEE, G2_INPUT_ITEM_LENGTH, - DISCOUNT_TABLE_G2_MSM, NBITS, SCALAR_LENGTH,}; +use crate::bls12_381_const::{ + DISCOUNT_TABLE_G2_MSM, G2_ADD_ADDRESS, G2_ADD_BASE_GAS_FEE, G2_ADD_INPUT_LENGTH, + G2_INPUT_ITEM_LENGTH, NBITS, SCALAR_LENGTH, +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; From 2ad5dedd1b007e0112c8dc4b5e8b6f5bdc14658c Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:52:29 +0530 Subject: [PATCH 63/68] Update bls12_381_const.rs --- crates/precompile/src/bls12_381_const.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381_const.rs b/crates/precompile/src/bls12_381_const.rs index fea9967b04..9ed067cb3f 100644 --- a/crates/precompile/src/bls12_381_const.rs +++ b/crates/precompile/src/bls12_381_const.rs @@ -60,4 +60,4 @@ pub static DISCOUNT_TABLE_G2_MSM: [u16; 128] = [ 567, 566, 565, 563, 562, 561, 560, 559, 558, 557, 556, 555, 554, 553, 552, 552, 551, 550, 549, 548, 547, 546, 545, 545, 544, 543, 542, 541, 541, 540, 539, 538, 537, 537, 536, 535, 535, 534, 533, 532, 532, 531, 530, 530, 529, 528, 528, 527, 526, 526, 525, 524, 524, -]; \ No newline at end of file +]; From 22f9e489e935f844a04767e5b748fc0adfb8cda3 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:53:18 +0530 Subject: [PATCH 64/68] Update lib.rs --- crates/precompile/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index 838a6ea50f..66faee445c 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -11,6 +11,7 @@ extern crate alloc as std; pub mod blake2; #[cfg(feature = "blst")] pub mod bls12_381; +pub mod bls12_381_const; pub mod bn128; pub mod hash; pub mod identity; @@ -22,7 +23,6 @@ pub mod secp256k1; #[cfg(feature = "secp256r1")] pub mod secp256r1; pub mod utilities; -pub mod bls12_381_const; pub use interface::*; #[cfg(all(feature = "c-kzg", feature = "kzg-rs"))] From 3643dce892924d70fd6252ea81ad7bee09d240ea Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:54:29 +0530 Subject: [PATCH 65/68] Update map_fp2_to_g2.rs --- crates/precompile/src/bls12_381/map_fp2_to_g2.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 2caa87d103..451a282945 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,7 +1,7 @@ use super::{g2::check_canonical_fp2, g2::encode_g2_point, utils::remove_padding}; -use crate::bls12_381_const::{ - MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH -}; + use crate::bls12_381_const::{ + MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH, + }; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine}; From f6a1e801c379f8787c5df85b1b480489773e7e65 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:56:36 +0530 Subject: [PATCH 66/68] Update map_fp2_to_g2.rs --- crates/precompile/src/bls12_381/map_fp2_to_g2.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 451a282945..735c49efa9 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,7 +1,7 @@ use super::{g2::check_canonical_fp2, g2::encode_g2_point, utils::remove_padding}; - use crate::bls12_381_const::{ - MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH, - }; +use crate::bls12_381_const::{ + MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH, +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine}; From 6fff63957664496e581cb40c3b93dfa403c718c0 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:58:16 +0530 Subject: [PATCH 67/68] Update map_fp2_to_g2.rs --- crates/precompile/src/bls12_381/map_fp2_to_g2.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index 735c49efa9..f938bb51ea 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,7 +1,5 @@ use super::{g2::check_canonical_fp2, g2::encode_g2_point, utils::remove_padding}; -use crate::bls12_381_const::{ - MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH, -}; +use crate::bls12_381_const::{MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH,}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine}; From ec26a8653b5cb7da9c81222bdf219cadcfcd4ffb Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:59:49 +0530 Subject: [PATCH 68/68] Update map_fp2_to_g2.rs --- crates/precompile/src/bls12_381/map_fp2_to_g2.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs index f938bb51ea..926400329e 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,5 +1,7 @@ use super::{g2::check_canonical_fp2, g2::encode_g2_point, utils::remove_padding}; -use crate::bls12_381_const::{MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH,}; +use crate::bls12_381_const::{ + MAP_FP2_TO_G2_ADDRESS, MAP_FP2_TO_G2_BASE_GAS_FEE, PADDED_FP2_LENGTH, PADDED_FP_LENGTH, +}; use crate::{u64_to_address, PrecompileWithAddress}; use crate::{PrecompileError, PrecompileOutput, PrecompileResult}; use blst::{blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine};