Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Error if CKZG or BLS features are disabled, but are called #1448

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/precompile/src/blake2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let input = &input[..];

if input.len() != INPUT_LENGTH {
return Err(Error::Blake2WrongLength);
return PrecompileResult::err(Error::Blake2WrongLength);
}

let f = match input[212] {
1 => true,
0 => false,
_ => return Err(Error::Blake2WrongFinalIndicatorFlag),
_ => return PrecompileResult::err(Error::Blake2WrongFinalIndicatorFlag),
};

// rounds 4 bytes
let rounds = u32::from_be_bytes(input[..4].try_into().unwrap()) as usize;
let gas_used = rounds as u64 * F_ROUND;
if gas_used > gas_limit {
return Err(Error::OutOfGas);
return PrecompileResult::err(Error::OutOfGas);
}

let mut h = [0u64; 8];
Expand All @@ -51,7 +51,7 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
out[i..i + 8].copy_from_slice(&h.to_le_bytes());
}

Ok((gas_used, out.into()))
PrecompileResult::ok(gas_used, out.into())
}

pub mod algo {
Expand Down
47 changes: 27 additions & 20 deletions crates/precompile/src/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,40 @@ mod test {
#[case] precompile: fn(input: &Bytes, gas_limit: u64) -> PrecompileResult,
#[case] file_name: &str,
) {
let test_vectors = load_test_vectors(format!("test-vectors/{file_name}"))
.unwrap_or_else(|e| panic!("Failed to load test vectors from {file_name}: {e}"));
let test_vectors = load_test_vectors(format!("test-vectors/{}", file_name))
.unwrap_or_else(|e| panic!("Failed to load test vectors from {}: {}", file_name, e));
rupam-04 marked this conversation as resolved.
Show resolved Hide resolved

for vector in test_vectors.0 {
let test_name = format!("{file_name}/{}", vector.name);
let input = Bytes::from_hex(vector.input.clone()).unwrap_or_else(|e| {
let test_name = format!("{}/{}", file_name, vector.name);
rupam-04 marked this conversation as resolved.
Show resolved Hide resolved
let input = Bytes::from_hex(&vector.input).unwrap_or_else(|e| {
panic!(
"could not deserialize input {} as hex in {test_name}: {e}",
&vector.input
"could not deserialize input {} as hex in {}: {}",
vector.input, test_name, e
)
});
let target_gas: u64 = 30_000_000;
let res = precompile(&input, target_gas);
if vector.error.unwrap_or_default() {
assert!(res.is_err(), "expected error didn't happen in {test_name}");
} else {
let (actual_gas, actual_output) =
res.unwrap_or_else(|e| panic!("precompile call failed for {test_name}: {e}"));
assert_eq!(
vector.gas, actual_gas,
"expected gas: {}, actual gas: {} in {test_name}",
vector.gas, actual_gas
);
let expected_output = Bytes::from_hex(vector.expected).unwrap();
assert_eq!(
expected_output, actual_output,
"expected output: {expected_output}, actual output: {actual_output} in {test_name}");
match res {
PrecompileResult::Error { .. } if vector.error.unwrap_or_default() => {
// Test passed, it was expected to fail
}
PrecompileResult::Ok {
gas_used: actual_gas,
output: actual_output,
} => {
assert_eq!(
vector.gas, actual_gas,
"expected gas: {}, actual gas: {} in {}",
vector.gas, actual_gas, test_name
);
let expected_output = Bytes::from_hex(&vector.expected).unwrap();
assert_eq!(
expected_output, actual_output,
"expected output: {:?}, actual output: {:?} in {}",
expected_output, actual_output, test_name
);
}
_ => panic!("unexpected result in {}", test_name),
rupam-04 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/g1_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const INPUT_LENGTH: usize = 256;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g1-addition>
pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return PrecompileResult::err(PrecompileError::OutOfGas);
}

if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
return PrecompileResult::err(PrecompileError::Other(format!(
"G1ADD input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
Expand All @@ -36,8 +36,8 @@ pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
// 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 a_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], false).unwrap();
let b_aff = &extract_g1_input(&input[G1_INPUT_ITEM_LENGTH..], false).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why unwrap here?

Copy link
Author

@rupam-04 rupam-04 May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I use expect, then what panic msg should I give? Or should I use something else altogether?


let mut b = blst_p1::default();
// SAFETY: b and b_aff are blst values.
Expand All @@ -52,5 +52,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((BASE_GAS_FEE, out))
PrecompileResult::ok(BASE_GAS_FEE, out)
}
13 changes: 7 additions & 6 deletions crates/precompile/src/bls12_381/g1_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const ADDRESS: u64 = 0x0d;
pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let input_len = input.len();
if input_len == 0 || input_len % g1_mul::INPUT_LENGTH != 0 {
return Err(PrecompileError::Other(format!(
return PrecompileResult::err(PrecompileError::Other(format!(
"G1MSM input length should be multiple of {}, was {}",
g1_mul::INPUT_LENGTH,
input_len
Expand All @@ -35,7 +35,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let k = input_len / g1_mul::INPUT_LENGTH;
let required_gas = msm_required_gas(k, g1_mul::BASE_GAS_FEE);
if required_gas > gas_limit {
return Err(PrecompileError::OutOfGas);
return PrecompileResult::err(PrecompileError::OutOfGas);
}

let mut g1_points: Vec<blst_p1> = Vec::with_capacity(k);
Expand All @@ -53,7 +53,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
// NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
//
// So we set the subgroup_check flag to `true`
let p0_aff = &extract_g1_input(slice, true)?;
let p0_aff = &extract_g1_input(slice, true).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why unwrap?


let mut p0 = blst_p1::default();
// SAFETY: p0 and p0_aff are blst values.
Expand All @@ -64,14 +64,15 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
&extract_scalar_input(
&input[i * g1_mul::INPUT_LENGTH + G1_INPUT_ITEM_LENGTH
..i * g1_mul::INPUT_LENGTH + G1_INPUT_ITEM_LENGTH + SCALAR_LENGTH],
)?
)
.unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap?

.b,
);
}

// return infinity point if all points are infinity
if g1_points.is_empty() {
return Ok((required_gas, [0; 128].into()));
return PrecompileResult::ok(required_gas, [0; 128].into());
}

let points = p1_affines::from(&g1_points);
Expand All @@ -82,5 +83,5 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut multiexp_aff, &multiexp) };

let out = encode_g1_point(&multiexp_aff);
Ok((required_gas, out))
PrecompileResult::ok(required_gas, out)
}
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/g1_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub(super) const INPUT_LENGTH: usize = 160;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g1-multiplication>
pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return PrecompileResult::err(PrecompileError::OutOfGas);
}
if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
return PrecompileResult::err(PrecompileError::Other(format!(
"G1MUL input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
Expand All @@ -37,12 +37,12 @@ pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
// NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
//
// So we set the subgroup_check flag to `true`
let p0_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], true)?;
let p0_aff = &extract_g1_input(&input[..G1_INPUT_ITEM_LENGTH], true).unwrap();
let mut p0 = blst_p1::default();
// SAFETY: p0 and p0_aff are blst values.
unsafe { blst_p1_from_affine(&mut p0, p0_aff) };

let input_scalar0 = extract_scalar_input(&input[G1_INPUT_ITEM_LENGTH..])?;
let input_scalar0 = extract_scalar_input(&input[G1_INPUT_ITEM_LENGTH..]).unwrap();

let mut p = blst_p1::default();
// SAFETY: input_scalar0.b has fixed size, p and p0 are blst values.
Expand All @@ -52,5 +52,5 @@ pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut p_aff, &p) };

let out = encode_g1_point(&p_aff);
Ok((BASE_GAS_FEE, out))
PrecompileResult::ok(BASE_GAS_FEE, out)
}
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/g2_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const INPUT_LENGTH: usize = 512;
/// See also <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-addition>
pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return PrecompileResult::err(PrecompileError::OutOfGas);
}

if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
return PrecompileResult::err(PrecompileError::Other(format!(
"G2ADD input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
Expand All @@ -37,8 +37,8 @@ pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
// NB: There is no subgroup check for the G2 addition precompile.
//
// So we set the subgroup checks here to `false`
let a_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH], false)?;
let b_aff = &extract_g2_input(&input[G2_INPUT_ITEM_LENGTH..], false)?;
let a_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH], false).unwrap();
let b_aff = &extract_g2_input(&input[G2_INPUT_ITEM_LENGTH..], false).unwrap();

let mut b = blst_p2::default();
// SAFETY: b and b_aff are blst values.
Expand All @@ -53,5 +53,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((BASE_GAS_FEE, out))
PrecompileResult::ok(BASE_GAS_FEE, out)
}
13 changes: 7 additions & 6 deletions crates/precompile/src/bls12_381/g2_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const ADDRESS: u64 = 0x10;
pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let input_len = input.len();
if input_len == 0 || input_len % g2_mul::INPUT_LENGTH != 0 {
return Err(PrecompileError::Other(format!(
return PrecompileResult::err(PrecompileError::Other(format!(
"G2MSM input length should be multiple of {}, was {}",
g2_mul::INPUT_LENGTH,
input_len
Expand All @@ -35,7 +35,7 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let k = input_len / g2_mul::INPUT_LENGTH;
let required_gas = msm_required_gas(k, g2_mul::BASE_GAS_FEE);
if required_gas > gas_limit {
return Err(PrecompileError::OutOfGas);
return PrecompileResult::err(PrecompileError::OutOfGas);
}

let mut g2_points: Vec<blst_p2> = Vec::with_capacity(k);
Expand All @@ -52,7 +52,7 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
// NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
//
// So we set the subgroup_check flag to `true`
let p0_aff = &extract_g2_input(slice, true)?;
let p0_aff = &extract_g2_input(slice, true).unwrap();

let mut p0 = blst_p2::default();
// SAFETY: p0 and p0_aff are blst values.
Expand All @@ -64,14 +64,15 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
&extract_scalar_input(
&input[i * g2_mul::INPUT_LENGTH + G2_INPUT_ITEM_LENGTH
..i * g2_mul::INPUT_LENGTH + G2_INPUT_ITEM_LENGTH + SCALAR_LENGTH],
)?
)
.unwrap()
.b,
);
}

// return infinity point if all points are infinity
if g2_points.is_empty() {
return Ok((required_gas, [0; 256].into()));
return PrecompileResult::ok(required_gas, [0; 256].into());
}

let points = p2_affines::from(&g2_points);
Expand All @@ -82,5 +83,5 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p2_to_affine(&mut multiexp_aff, &multiexp) };

let out = encode_g2_point(&multiexp_aff);
Ok((required_gas, out))
PrecompileResult::ok(required_gas, out)
}
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/g2_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub(super) const INPUT_LENGTH: usize = 288;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-multiplication>
pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return PrecompileResult::err(PrecompileError::OutOfGas);
}
if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
return PrecompileResult::err(PrecompileError::Other(format!(
"G2MUL input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
Expand All @@ -37,12 +37,12 @@ pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
// NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
//
// So we set the subgroup_check flag to `true`
let p0_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH], true)?;
let p0_aff = &extract_g2_input(&input[..G2_INPUT_ITEM_LENGTH], true).unwrap();
let mut p0 = blst_p2::default();
// SAFETY: p0 and p0_aff are blst values.
unsafe { blst_p2_from_affine(&mut p0, p0_aff) };

let input_scalar0 = extract_scalar_input(&input[G2_INPUT_ITEM_LENGTH..])?;
let input_scalar0 = extract_scalar_input(&input[G2_INPUT_ITEM_LENGTH..]).unwrap();

let mut p = blst_p2::default();
// SAFETY: input_scalar0.b has fixed size, p and p0 are blst values.
Expand All @@ -52,5 +52,5 @@ pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p2_to_affine(&mut p_aff, &p) };

let out = encode_g2_point(&p_aff);
Ok((BASE_GAS_FEE, out))
PrecompileResult::ok(BASE_GAS_FEE, out)
}
10 changes: 5 additions & 5 deletions crates/precompile/src/bls12_381/map_fp2_to_g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ const BASE_GAS_FEE: u64 = 75000;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-mapping-fp2-element-to-g2-point>
pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return PrecompileResult::err(PrecompileError::OutOfGas);
}

if input.len() != PADDED_FP2_LENGTH {
return Err(PrecompileError::Other(format!(
return PrecompileResult::err(PrecompileError::Other(format!(
"MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}",
input.len()
)));
}

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[..PADDED_FP_LENGTH]).unwrap();
let input_p0_y = remove_padding(&input[PADDED_FP_LENGTH..PADDED_FP2_LENGTH]).unwrap();

let mut fp2 = blst_fp2::default();
let mut fp_x = blst_fp::default();
Expand All @@ -56,5 +56,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((BASE_GAS_FEE, out))
PrecompileResult::ok(BASE_GAS_FEE, out)
}
8 changes: 4 additions & 4 deletions crates/precompile/src/bls12_381/map_fp_to_g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ const MAP_FP_TO_G1_BASE: u64 = 5500;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-mapping-fp-element-to-g1-point>
pub(super) fn map_fp_to_g1(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if MAP_FP_TO_G1_BASE > gas_limit {
return Err(PrecompileError::OutOfGas);
return PrecompileResult::err(PrecompileError::OutOfGas);
}

if input.len() != PADDED_FP_LENGTH {
return Err(PrecompileError::Other(format!(
return PrecompileResult::err(PrecompileError::Other(format!(
"MAP_FP_TO_G1 input should be {PADDED_FP_LENGTH} bytes, was {}",
input.len()
)));
}

let input_p0 = remove_padding(input)?;
let input_p0 = remove_padding(input).unwrap();

let mut fp = blst_fp::default();

Expand All @@ -48,5 +48,5 @@ 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((MAP_FP_TO_G1_BASE, out))
PrecompileResult::ok(MAP_FP_TO_G1_BASE, out)
}
Loading
Loading