Skip to content

Commit

Permalink
fix(blst): Fix blst wrong constants values and usage (#2102)
Browse files Browse the repository at this point in the history
* fix(blst): Fix wrong constants value and usage

* rename BAS to BASE
  • Loading branch information
rakita authored Feb 20, 2025
1 parent eeccdc3 commit ac939f6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ethereum-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
ethtests/EIPTests/StateTests/stEIP4844-blobtransactions/ \
ethtests/EIPTests/StateTests/stEOF \
tests/eof_suite/eest/state_tests \
tests/pectra/state_tests/prague/eip7623_increase_calldata_cost
tests/pectra/state_tests/prague
- name: Run EOF validation tests
run: |
cross run --target ${{matrix.target}} --profile ${{ matrix.profile }} -p revme -- eof-validation \
Expand Down
20 changes: 10 additions & 10 deletions crates/precompile/src/bls12_381/g2_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::{
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,
DISCOUNT_TABLE_G2_MSM, G2_INPUT_ITEM_LENGTH, G2_MSM_ADDRESS, G2_MSM_BASE_GAS_FEE,
G2_MSM_INPUT_LENGTH, NBITS, SCALAR_LENGTH,
};
use crate::{u64_to_address, PrecompileWithAddress};
use crate::{PrecompileError, PrecompileOutput, PrecompileResult};
Expand All @@ -14,7 +14,7 @@ use primitives::Bytes;

/// [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);
PrecompileWithAddress(u64_to_address(G2_MSM_ADDRESS), g2_msm);

/// Implements EIP-2537 G2MSM precompile.
/// G2 multi-scalar-multiplication call expects `288*k` bytes as an input that is interpreted
Expand All @@ -26,24 +26,24 @@ pub const PRECOMPILE: PrecompileWithAddress =
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-multiexponentiation>
pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let input_len = input.len();
if input_len == 0 || input_len % G2_ADD_INPUT_LENGTH != 0 {
if input_len == 0 || input_len % G2_MSM_INPUT_LENGTH != 0 {
return Err(PrecompileError::Other(format!(
"G2MSM input length should be multiple of {}, was {}",
G2_ADD_INPUT_LENGTH, input_len
G2_MSM_INPUT_LENGTH, input_len
))
.into());
}

let k = input_len / G2_ADD_INPUT_LENGTH;
let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G2_MSM, G2_ADD_BASE_GAS_FEE);
let k = input_len / G2_MSM_INPUT_LENGTH;
let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G2_MSM, G2_MSM_BASE_GAS_FEE);
if required_gas > gas_limit {
return Err(PrecompileError::OutOfGas.into());
}

let mut g2_points: Vec<blst_p2> = Vec::with_capacity(k);
let mut scalars: Vec<u8> = 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];
let slice = &input[i * G2_MSM_INPUT_LENGTH..i * G2_MSM_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) {
Expand All @@ -63,8 +63,8 @@ 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 + SCALAR_LENGTH],
&input[i * G2_MSM_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH
..i * G2_MSM_INPUT_LENGTH + G2_INPUT_ITEM_LENGTH + SCALAR_LENGTH],
)?
.b,
);
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/bls12_381/pairing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{g1::extract_g1_input, g2::extract_g2_input};
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,
PAIRING_PAIRING_MULTIPLIER_BASE, PAIRING_PAIRING_OFFSET_BASE,
};
use crate::{
u64_to_address, PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress,
Expand Down Expand Up @@ -35,7 +35,7 @@ pub(super) fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult {
}

let k = input_len / PAIRING_INPUT_LENGTH;
let required_gas: u64 = PAIRING_PAIRING_MULTIPLIER_BAS * k as u64 + PAIRING_PAIRING_OFFSET_BASE;
let required_gas: u64 = PAIRING_PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_PAIRING_OFFSET_BASE;
if required_gas > gas_limit {
return Err(PrecompileError::OutOfGas.into());
}
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/bls12_381_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ 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_MULTIPLIER_BASE: 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 MAP_FP2_TO_G2_BASE_GAS_FEE: u64 = 23800;
pub const MSM_MULTIPLIER: u64 = 1000;
/// Number of bits used in the BLS12-381 curve finite field elements.
pub const NBITS: usize = 256;
Expand Down

0 comments on commit ac939f6

Please sign in to comment.