diff --git a/crates/precompile/src/bls12_381/g1.rs b/crates/precompile/src/bls12_381/g1.rs index 28e5a845d4..163afb3e5b 100644 --- a/crates/precompile/src/bls12_381/g1.rs +++ b/crates/precompile/src/bls12_381/g1.rs @@ -1,8 +1,7 @@ +use super::utils::{fp_to_bytes, remove_padding, PADDED_FP_LENGTH}; use blst::{blst_fp_from_bendian, blst_p1_affine, blst_p1_affine_in_g1}; use revm_primitives::{Bytes, PrecompileError}; -use super::utils::{fp_to_bytes, remove_padding, PADDED_FP_LENGTH}; - /// Length of each of the elements in a g1 operation input. pub(super) const G1_INPUT_ITEM_LENGTH: usize = 128; /// Output length of a g1 operation. @@ -20,10 +19,10 @@ pub(super) fn encode_g1_point(input: *const blst_p1_affine) -> Bytes { } /// Extracts a G1 point in Affine format from a 128 byte slice representation. -pub(super) fn extract_g1_input(input: &[u8]) -> Result<*const blst_p1_affine, PrecompileError> { +pub(super) fn extract_g1_input(input: &[u8]) -> Result { if input.len() != G1_INPUT_ITEM_LENGTH { return Err(PrecompileError::Other(format!( - "Input should be {G1_INPUT_ITEM_LENGTH} bits, was {}", + "Input should be {G1_INPUT_ITEM_LENGTH} bytes, was {}", input.len() ))); } @@ -39,10 +38,8 @@ pub(super) fn extract_g1_input(input: &[u8]) -> Result<*const blst_p1_affine, Pr } // SAFETY: out is a blst value. - unsafe { - if !blst_p1_affine_in_g1(&out) { - return Err(PrecompileError::Other("Element not in G1".to_string())); - } + if unsafe { !blst_p1_affine_in_g1(&out) } { + return Err(PrecompileError::Other("Element not in G1".to_string())); } - Ok(&mut out as *const _) + Ok(out) } diff --git a/crates/precompile/src/bls12_381/g1_add.rs b/crates/precompile/src/bls12_381/g1_add.rs index e3adba016a..358ab0cc0b 100644 --- a/crates/precompile/src/bls12_381/g1_add.rs +++ b/crates/precompile/src/bls12_381/g1_add.rs @@ -1,12 +1,10 @@ +use super::g1::{encode_g1_point, extract_g1_input, G1_INPUT_ITEM_LENGTH}; +use crate::{u64_to_address, PrecompileWithAddress}; use blst::{ blst_p1, blst_p1_add_or_double_affine, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, }; use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; -use crate::{u64_to_address, PrecompileWithAddress}; - -use super::g1::{encode_g1_point, extract_g1_input, 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), Precompile::Standard(g1_add)); @@ -30,31 +28,25 @@ fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if input.len() != INPUT_LENGTH { return Err(PrecompileError::Other(format!( - "G1ADD Input should be {INPUT_LENGTH} bits, was {}", + "G1ADD input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); } - let a_aff = extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH])?; - let b_aff = extract_g1_input(&input[G1_INPUT_ITEM_LENGTH..])?; + let a_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH])?; + let b_aff = &extract_g1_input(&input[G1_INPUT_ITEM_LENGTH..])?; let mut b = blst_p1::default(); // SAFETY: b and b_aff are blst values. - unsafe { - blst_p1_from_affine(&mut b, b_aff); - } + 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); - } + 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); - } + unsafe { blst_p1_to_affine(&mut p_aff, &p) }; let out = encode_g1_point(&p_aff); Ok((BASE_GAS_FEE, out)) diff --git a/crates/precompile/src/bls12_381/g1_msm.rs b/crates/precompile/src/bls12_381/g1_msm.rs index a339d86039..c02f055bb2 100644 --- a/crates/precompile/src/bls12_381/g1_msm.rs +++ b/crates/precompile/src/bls12_381/g1_msm.rs @@ -1,14 +1,12 @@ -use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; - -use crate::{u64_to_address, PrecompileWithAddress}; - use super::{ g1::{encode_g1_point, extract_g1_input, G1_INPUT_ITEM_LENGTH}, g1_mul, msm::msm_required_gas, utils::{extract_scalar_input, NBITS, SCALAR_LENGTH}, }; +use crate::{u64_to_address, PrecompileWithAddress}; +use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -43,15 +41,12 @@ fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let mut g1_points: Vec = Vec::with_capacity(k); let mut scalars: Vec = Vec::with_capacity(k * SCALAR_LENGTH); for i in 0..k { - let p0_aff = extract_g1_input( + let p0_aff = &extract_g1_input( &input[i * g1_mul::INPUT_LENGTH..i * g1_mul::INPUT_LENGTH + G1_INPUT_ITEM_LENGTH], )?; let mut p0 = blst_p1::default(); // SAFETY: p0 and p0_aff are blst values. - unsafe { - blst_p1_from_affine(&mut p0, p0_aff); - } - + unsafe { blst_p1_from_affine(&mut p0, p0_aff) }; g1_points.push(p0); scalars.extend_from_slice( @@ -68,9 +63,7 @@ fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let mut multiexp_aff = blst_p1_affine::default(); // SAFETY: multiexp_aff and multiexp are blst values. - unsafe { - blst_p1_to_affine(&mut multiexp_aff, &multiexp); - } + unsafe { blst_p1_to_affine(&mut multiexp_aff, &multiexp) }; let out = encode_g1_point(&multiexp_aff); Ok((required_gas, out)) diff --git a/crates/precompile/src/bls12_381/g1_mul.rs b/crates/precompile/src/bls12_381/g1_mul.rs index 171de875c8..8982cb5896 100644 --- a/crates/precompile/src/bls12_381/g1_mul.rs +++ b/crates/precompile/src/bls12_381/g1_mul.rs @@ -1,12 +1,10 @@ -use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_mult, blst_p1_to_affine}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; - -use crate::{u64_to_address, PrecompileWithAddress}; - use super::{ g1::{encode_g1_point, extract_g1_input, G1_INPUT_ITEM_LENGTH}, utils::{extract_scalar_input, NBITS}, }; +use crate::{u64_to_address, PrecompileWithAddress}; +use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_mult, blst_p1_to_affine}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MUL precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -31,30 +29,24 @@ pub fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult { } if input.len() != INPUT_LENGTH { return Err(PrecompileError::Other(format!( - "G1MUL Input should be {INPUT_LENGTH} bits, was {}", + "G1MUL input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); } - let p0_aff = extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH])?; + let p0_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH])?; let mut p0 = blst_p1::default(); // SAFETY: p0 and p0_aff are blst values. - unsafe { - blst_p1_from_affine(&mut p0, p0_aff); - } + unsafe { blst_p1_from_affine(&mut p0, p0_aff) }; let input_scalar0 = extract_scalar_input(&input[G1_INPUT_ITEM_LENGTH..])?; let mut p = blst_p1::default(); // SAFETY: input_scalar0.b has fixed size, p and p0 are blst values. - unsafe { - blst_p1_mult(&mut p, &p0, input_scalar0.b.as_ptr(), NBITS); - } + unsafe { blst_p1_mult(&mut p, &p0, input_scalar0.b.as_ptr(), NBITS) }; 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); - } + unsafe { blst_p1_to_affine(&mut p_aff, &p) }; let out = encode_g1_point(&p_aff); Ok((BASE_GAS_FEE, out)) diff --git a/crates/precompile/src/bls12_381/g2.rs b/crates/precompile/src/bls12_381/g2.rs index 0c861e7f7d..538bc5beec 100644 --- a/crates/precompile/src/bls12_381/g2.rs +++ b/crates/precompile/src/bls12_381/g2.rs @@ -1,45 +1,41 @@ +use super::utils::{fp_to_bytes, remove_padding, FP_LENGTH, PADDED_FP_LENGTH}; use blst::{blst_fp_from_bendian, blst_p2_affine, blst_p2_affine_in_g2}; use revm_primitives::{Bytes, PrecompileError}; -use super::utils::{fp_to_bytes, remove_padding, FP_LENGTH, 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 a byte slice with padded elements. -pub(super) fn encode_g2_point(input: *const blst_p2_affine) -> Bytes { +pub(super) fn encode_g2_point(input: &blst_p2_affine) -> Bytes { let mut out = vec![0u8; G2_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[0]); - fp_to_bytes( - &mut out[PADDED_FP_LENGTH..2 * PADDED_FP_LENGTH], - &(*input).x.fp[1], - ); - fp_to_bytes( - &mut out[2 * PADDED_FP_LENGTH..3 * PADDED_FP_LENGTH], - &(*input).y.fp[0], - ); - fp_to_bytes( - &mut out[3 * PADDED_FP_LENGTH..4 * PADDED_FP_LENGTH], - &(*input).y.fp[1], - ); - } + fp_to_bytes(&mut out[..PADDED_FP_LENGTH], &input.x.fp[0]); + fp_to_bytes( + &mut out[PADDED_FP_LENGTH..2 * PADDED_FP_LENGTH], + &input.x.fp[1], + ); + fp_to_bytes( + &mut out[2 * PADDED_FP_LENGTH..3 * PADDED_FP_LENGTH], + &input.y.fp[0], + ); + fp_to_bytes( + &mut out[3 * PADDED_FP_LENGTH..4 * PADDED_FP_LENGTH], + &input.y.fp[1], + ); out.into() } /// Extracts a G2 point in Affine format from a 256 byte slice representation. -pub(super) fn extract_g2_input(input: &[u8]) -> Result<*const blst_p2_affine, PrecompileError> { +pub(super) fn extract_g2_input(input: &[u8]) -> Result { if input.len() != G2_INPUT_ITEM_LENGTH { return Err(PrecompileError::Other(format!( - "Input should be {G2_INPUT_ITEM_LENGTH} bits, was {}", + "Input should be {G2_INPUT_ITEM_LENGTH} bytes, was {}", input.len() ))); } - let mut input_fps: [[u8; FP_LENGTH]; 4] = [[0; FP_LENGTH]; 4]; + let mut input_fps: [&[u8; FP_LENGTH]; 4] = [&[0; FP_LENGTH]; 4]; for i in 0..4 { input_fps[i] = remove_padding(&input[i * PADDED_FP_LENGTH..(i + 1) * PADDED_FP_LENGTH])?; } @@ -54,11 +50,9 @@ pub(super) fn extract_g2_input(input: &[u8]) -> Result<*const blst_p2_affine, Pr } // SAFETY: out is a blst value. - unsafe { - if !blst_p2_affine_in_g2(&out) { - return Err(PrecompileError::Other("Element not in G2".to_string())); - } + if unsafe { !blst_p2_affine_in_g2(&out) } { + return Err(PrecompileError::Other("Element not in G2".to_string())); } - Ok(&mut out as *const _) + Ok(out) } diff --git a/crates/precompile/src/bls12_381/g2_add.rs b/crates/precompile/src/bls12_381/g2_add.rs index 2d7e95edf6..28ca3f819f 100644 --- a/crates/precompile/src/bls12_381/g2_add.rs +++ b/crates/precompile/src/bls12_381/g2_add.rs @@ -1,12 +1,10 @@ +use super::g2::{encode_g2_point, extract_g2_input, G2_INPUT_ITEM_LENGTH}; +use crate::{u64_to_address, PrecompileWithAddress}; use blst::{ blst_p2, blst_p2_add_or_double_affine, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, }; use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; -use crate::{u64_to_address, PrecompileWithAddress}; - -use super::g2::{encode_g2_point, extract_g2_input, 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), Precompile::Standard(g2_add)); @@ -31,31 +29,25 @@ fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult { if input.len() != INPUT_LENGTH { return Err(PrecompileError::Other(format!( - "G2ADD Input should be {INPUT_LENGTH} bits, was {}", + "G2ADD input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); } - let a_aff = extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH])?; - let b_aff = extract_g2_input(&input[G2_INPUT_ITEM_LENGTH..])?; + let a_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH])?; + let b_aff = &extract_g2_input(&input[G2_INPUT_ITEM_LENGTH..])?; let mut b = blst_p2::default(); // SAFETY: b and b_aff are blst values. - unsafe { - blst_p2_from_affine(&mut b, b_aff); - } + unsafe { blst_p2_from_affine(&mut b, b_aff) }; let mut p = blst_p2::default(); // SAFETY: p, b and a_aff are blst values. - unsafe { - blst_p2_add_or_double_affine(&mut p, &b, a_aff); - } + unsafe { blst_p2_add_or_double_affine(&mut p, &b, a_aff) }; let mut p_aff = blst_p2_affine::default(); // SAFETY: p_aff and p are blst values. - unsafe { - blst_p2_to_affine(&mut p_aff, &p); - } + unsafe { blst_p2_to_affine(&mut p_aff, &p) }; let out = encode_g2_point(&p_aff); Ok((BASE_GAS_FEE, out)) diff --git a/crates/precompile/src/bls12_381/g2_msm.rs b/crates/precompile/src/bls12_381/g2_msm.rs index 8ea82c2024..a17c5c47de 100644 --- a/crates/precompile/src/bls12_381/g2_msm.rs +++ b/crates/precompile/src/bls12_381/g2_msm.rs @@ -1,14 +1,12 @@ -use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; - -use crate::{u64_to_address, PrecompileWithAddress}; - use super::{ g2::{encode_g2_point, extract_g2_input, G2_INPUT_ITEM_LENGTH}, g2_mul, msm::msm_required_gas, utils::{extract_scalar_input, NBITS, SCALAR_LENGTH}, }; +use crate::{u64_to_address, PrecompileWithAddress}; +use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MSM precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -43,14 +41,12 @@ fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let mut g2_points: Vec = Vec::with_capacity(k); let mut scalars: Vec = Vec::with_capacity(k * SCALAR_LENGTH); for i in 0..k { - let p0_aff = extract_g2_input( + let p0_aff = &extract_g2_input( &input[i * g2_mul::INPUT_LENGTH..i * g2_mul::INPUT_LENGTH + G2_INPUT_ITEM_LENGTH], )?; let mut p0 = blst_p2::default(); // SAFETY: p0 and p0_aff are blst values. - unsafe { - blst_p2_from_affine(&mut p0, p0_aff); - } + unsafe { blst_p2_from_affine(&mut p0, p0_aff) }; g2_points.push(p0); @@ -68,9 +64,7 @@ fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult { let mut multiexp_aff = blst_p2_affine::default(); // SAFETY: multiexp_aff and multiexp are blst values. - unsafe { - blst_p2_to_affine(&mut multiexp_aff, &multiexp); - } + unsafe { blst_p2_to_affine(&mut multiexp_aff, &multiexp) }; let out = encode_g2_point(&multiexp_aff); Ok((required_gas, out)) diff --git a/crates/precompile/src/bls12_381/g2_mul.rs b/crates/precompile/src/bls12_381/g2_mul.rs index be3f4f9f14..d7d6883b23 100644 --- a/crates/precompile/src/bls12_381/g2_mul.rs +++ b/crates/precompile/src/bls12_381/g2_mul.rs @@ -1,12 +1,10 @@ -use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_mult, blst_p2_to_affine}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; - -use crate::{u64_to_address, PrecompileWithAddress}; - use super::{ g2::{encode_g2_point, extract_g2_input, G2_INPUT_ITEM_LENGTH}, utils::{extract_scalar_input, NBITS}, }; +use crate::{u64_to_address, PrecompileWithAddress}; +use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_mult, blst_p2_to_affine}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MUL precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -31,30 +29,24 @@ fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult { } if input.len() != INPUT_LENGTH { return Err(PrecompileError::Other(format!( - "G2MUL Input should be {INPUT_LENGTH} bits, was {}", + "G2MUL input should be {INPUT_LENGTH} bytes, was {}", input.len() ))); } - let p0_aff = extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH])?; + let p0_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH])?; let mut p0 = blst_p2::default(); // SAFETY: p0 and p0_aff are blst values. - unsafe { - blst_p2_from_affine(&mut p0, p0_aff); - } + unsafe { blst_p2_from_affine(&mut p0, p0_aff) }; let input_scalar0 = extract_scalar_input(&input[G2_INPUT_ITEM_LENGTH..])?; let mut p = blst_p2::default(); // SAFETY: input_scalar0.b has fixed size, p and p0 are blst values. - unsafe { - blst_p2_mult(&mut p, &p0, input_scalar0.b.as_ptr(), NBITS); - } + unsafe { blst_p2_mult(&mut p, &p0, input_scalar0.b.as_ptr(), NBITS) }; let mut p_aff = blst_p2_affine::default(); // SAFETY: p_aff and p are blst values. - unsafe { - blst_p2_to_affine(&mut p_aff, &p); - } + unsafe { blst_p2_to_affine(&mut p_aff, &p) }; let out = encode_g2_point(&p_aff); Ok((BASE_GAS_FEE, out)) 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 6448dda6f6..4615ffd862 100644 --- a/crates/precompile/src/bls12_381/map_fp2_to_g2.rs +++ b/crates/precompile/src/bls12_381/map_fp2_to_g2.rs @@ -1,16 +1,14 @@ +use super::{ + g2::encode_g2_point, + utils::{remove_padding, PADDED_FP2_LENGTH, PADDED_FP_LENGTH}, +}; +use crate::{u64_to_address, PrecompileWithAddress}; use blst::{ blst_fp, blst_fp2, blst_fp_from_bendian, blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine, }; use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; -use crate::{u64_to_address, PrecompileWithAddress}; - -use super::{ - g2::encode_g2_point, - utils::{remove_padding, PADDED_FP2_LENGTH, 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), Precompile::Standard(map_fp2_to_g2)); @@ -30,7 +28,7 @@ fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { if input.len() != PADDED_FP2_LENGTH { return Err(PrecompileError::Other(format!( - "MAP_FP2_TO_G2 Input should be {PADDED_FP2_LENGTH} bits, was {}", + "MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}", input.len() ))); } @@ -42,28 +40,20 @@ fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult { let mut fp_x = blst_fp::default(); let mut fp_y = blst_fp::default(); // SAFETY: input_p0_x has fixed length, fp_x is a blst value. - unsafe { - blst_fp_from_bendian(&mut fp_x, input_p0_x.as_ptr()); - } + unsafe { blst_fp_from_bendian(&mut fp_x, input_p0_x.as_ptr()) }; // SAFETY: input_p0_y has fixed length, fp_y is a blst value. - unsafe { - blst_fp_from_bendian(&mut fp_y, input_p0_y.as_ptr()); - } + unsafe { blst_fp_from_bendian(&mut fp_y, input_p0_y.as_ptr()) }; fp2.fp[0] = fp_x; fp2.fp[1] = fp_y; let mut p = blst_p2::default(); // SAFETY: p and fp2 are blst values. - unsafe { - // third argument is unused if null. - blst_map_to_g2(&mut p, &fp2, std::ptr::null()); - } + // third argument is unused if null. + unsafe { blst_map_to_g2(&mut p, &fp2, core::ptr::null()) }; let mut p_aff = blst_p2_affine::default(); // SAFETY: p_aff and p are blst values. - unsafe { - blst_p2_to_affine(&mut p_aff, &p); - } + unsafe { blst_p2_to_affine(&mut p_aff, &p) }; let out = encode_g2_point(&p_aff); Ok((BASE_GAS_FEE, out)) 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 f0d273f3bf..b161f7d397 100644 --- a/crates/precompile/src/bls12_381/map_fp_to_g1.rs +++ b/crates/precompile/src/bls12_381/map_fp_to_g1.rs @@ -1,14 +1,12 @@ -use blst::{ - blst_fp, blst_fp_from_bendian, blst_map_to_g1, blst_p1, blst_p1_affine, blst_p1_to_affine, -}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; - -use crate::{u64_to_address, PrecompileWithAddress}; - use super::{ g1::encode_g1_point, utils::{remove_padding, PADDED_FP_LENGTH}, }; +use crate::{u64_to_address, PrecompileWithAddress}; +use blst::{ + blst_fp, blst_fp_from_bendian, blst_map_to_g1, blst_p1, blst_p1_affine, blst_p1_to_affine, +}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP_TO_G1 precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -28,7 +26,7 @@ fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult { if input.len() != PADDED_FP_LENGTH { return Err(PrecompileError::Other(format!( - "MAP_FP_TO_G1 Input should be {PADDED_FP_LENGTH} bits, was {}", + "MAP_FP_TO_G1 input should be {PADDED_FP_LENGTH} bytes, was {}", input.len() ))); } @@ -38,22 +36,16 @@ fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult { let mut fp = blst_fp::default(); // SAFETY: input_p0 has fixed length, fp is a blst value. - unsafe { - blst_fp_from_bendian(&mut fp, input_p0.as_ptr()); - } + unsafe { blst_fp_from_bendian(&mut fp, input_p0.as_ptr()) }; let mut p = blst_p1::default(); // SAFETY: p and fp are blst values. - unsafe { - // third argument is unused if null. - blst_map_to_g1(&mut p, &fp, std::ptr::null()); - } + // third argument is unused if null. + unsafe { blst_map_to_g1(&mut p, &fp, core::ptr::null()) }; 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); - } + unsafe { blst_p1_to_affine(&mut p_aff, &p) }; let out = encode_g1_point(&p_aff); Ok((MAP_FP_TO_G1_BASE, out)) diff --git a/crates/precompile/src/bls12_381/msm.rs b/crates/precompile/src/bls12_381/msm.rs index e26ea3ebb2..9ddeedc015 100644 --- a/crates/precompile/src/bls12_381/msm.rs +++ b/crates/precompile/src/bls12_381/msm.rs @@ -1,7 +1,8 @@ /// Amount used to calculate the multi-scalar-multiplication discount. const MSM_MULTIPLIER: u64 = 1000; + /// Table of gas discounts for multi-scalar-multiplication operations. -const MSM_DISCOUNT_TABLE: [u64; 128] = [ +static MSM_DISCOUNT_TABLE: [u16; 128] = [ 1200, 888, 764, 641, 594, 547, 500, 453, 438, 423, 408, 394, 379, 364, 349, 334, 330, 326, 322, 318, 314, 310, 306, 302, 298, 294, 289, 285, 281, 277, 273, 269, 268, 266, 265, 263, 262, 260, 259, 257, 256, 254, 253, 251, 250, 248, 247, 245, 244, 242, 241, 239, 238, 236, 235, 233, 232, @@ -18,11 +19,8 @@ pub(super) fn msm_required_gas(k: usize, multiplication_cost: u64) -> u64 { return 0; } - let discount = if k < MSM_DISCOUNT_TABLE.len() { - MSM_DISCOUNT_TABLE[k - 1] - } else { - MSM_DISCOUNT_TABLE[MSM_DISCOUNT_TABLE.len() - 1] - }; + let index = core::cmp::min(k - 1, MSM_DISCOUNT_TABLE.len() - 1); + let discount = MSM_DISCOUNT_TABLE[index] as u64; (k as u64 * discount * multiplication_cost) / MSM_MULTIPLIER } diff --git a/crates/precompile/src/bls12_381/pairing.rs b/crates/precompile/src/bls12_381/pairing.rs index 2cd5cf9a6d..fb83e44807 100644 --- a/crates/precompile/src/bls12_381/pairing.rs +++ b/crates/precompile/src/bls12_381/pairing.rs @@ -1,12 +1,10 @@ -use blst::{blst_final_exp, blst_fp12, blst_fp12_is_one, blst_fp12_mul, blst_miller_loop}; -use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult, B256}; - -use crate::{u64_to_address, PrecompileWithAddress}; - use super::{ g1::{extract_g1_input, G1_INPUT_ITEM_LENGTH}, g2::{extract_g2_input, G2_INPUT_ITEM_LENGTH}, }; +use crate::{u64_to_address, PrecompileWithAddress}; +use blst::{blst_final_exp, blst_fp12, blst_fp12_is_one, blst_fp12_mul, blst_miller_loop}; +use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult, B256}; /// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile. pub const PRECOMPILE: PrecompileWithAddress = @@ -49,9 +47,9 @@ fn pairing(input: &Bytes, gas_limit: u64) -> PrecompileResult { let mut acc = blst_fp12::default(); for i in 0..k { let p1_aff = - extract_g1_input(&input[i * INPUT_LENGTH..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH])?; + &extract_g1_input(&input[i * INPUT_LENGTH..i * INPUT_LENGTH + G1_INPUT_ITEM_LENGTH])?; - let p2_aff = extract_g2_input( + 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], )?; diff --git a/crates/precompile/src/bls12_381/utils.rs b/crates/precompile/src/bls12_381/utils.rs index 38673d66b7..3617a752c3 100644 --- a/crates/precompile/src/bls12_381/utils.rs +++ b/crates/precompile/src/bls12_381/utils.rs @@ -19,47 +19,41 @@ pub(super) fn fp_to_bytes(out: &mut [u8], input: *const blst_fp) { if out.len() != PADDED_FP_LENGTH { return; } - for item in out.iter_mut().take(PADDING_LENGTH) { - *item = 0; - } + 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(out[PADDING_LENGTH..].as_mut_ptr(), input); - } + 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> { +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 {PADDED_FP_LENGTH} bits, was {}", + "Padded input should be {PADDED_FP_LENGTH} bytes, was {}", input.len() ))); } - if !input.iter().take(PADDING_LENGTH).all(|&x| x == 0) { + let (padding, unpadded) = input.split_at(PADDING_LENGTH); + if !padding.iter().all(|&x| x == 0) { return Err(PrecompileError::Other(format!( "{PADDING_LENGTH} top bytes of input are not zero", ))); } - - let sliced = &input[PADDING_LENGTH..PADDED_FP_LENGTH]; - <[u8; FP_LENGTH]>::try_from(sliced).map_err(|e| PrecompileError::Other(format!("{e}"))) + Ok(unpadded.try_into().unwrap()) } /// Extracts an Scalar from a 32 byte slice representation. pub(super) fn extract_scalar_input(input: &[u8]) -> Result { if input.len() != SCALAR_LENGTH { return Err(PrecompileError::Other(format!( - "Input should be {SCALAR_LENGTH} bits, was {}", + "Input should be {SCALAR_LENGTH} bytes, was {}", input.len() ))); } let mut out = blst_scalar::default(); // SAFETY: input length is checked previously, out is a blst value. - unsafe { - blst_scalar_from_bendian(&mut out, input.as_ptr()); - } + unsafe { blst_scalar_from_bendian(&mut out, input.as_ptr()) }; Ok(out) }