Skip to content

Commit 81d3b17

Browse files
committed
Simplify precompile interface: remove gas_limit from PrecompileOutput, flatten PrecompileError
Remove gas_limit field from PrecompileOutput (only gas_used needed) and eliminate the PrecompileError wrapper layer so errors are returned directly as the enum variant without .into() conversion.
1 parent 033d0ef commit 81d3b17

17 files changed

Lines changed: 76 additions & 175 deletions

crates/precompile/src/blake2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ pub const FUN: Precompile = Precompile::new(PrecompileId::Blake2F, crate::u64_to
1515
/// [4 bytes for rounds][64 bytes for h][128 bytes for m][8 bytes for t_0][8 bytes for t_1][1 byte for f]
1616
pub fn run(input: &[u8], gas_limit: u64) -> PrecompileResult {
1717
if input.len() != INPUT_LENGTH {
18-
return Err(PrecompileError::Blake2WrongLength.into());
18+
return Err(PrecompileError::Blake2WrongLength);
1919
}
2020

2121
// Parse number of rounds (4 bytes)
2222
let rounds = u32::from_be_bytes(input[..4].try_into().unwrap());
2323
let gas_used = rounds as u64 * F_ROUND;
2424
if gas_used > gas_limit {
25-
return Err(PrecompileError::OutOfGas.into());
25+
return Err(PrecompileError::OutOfGas);
2626
}
2727

2828
// Parse final block flag
2929
let f = match input[212] {
3030
0 => false,
3131
1 => true,
32-
_ => return Err(PrecompileError::Blake2WrongFinalIndicatorFlag.into()),
32+
_ => return Err(PrecompileError::Blake2WrongFinalIndicatorFlag),
3333
};
3434

3535
// Parse state vector h (8 × u64)
@@ -61,7 +61,7 @@ pub fn run(input: &[u8], gas_limit: u64) -> PrecompileResult {
6161
out[i..i + 8].copy_from_slice(&h.to_le_bytes());
6262
}
6363

64-
Ok(PrecompileOutput::new(gas_limit, gas_used, out.into()))
64+
Ok(PrecompileOutput::new(gas_used, out.into()))
6565
}
6666

6767
/// Blake2 algorithm

crates/precompile/src/bls12_381/g1_add.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub const PRECOMPILE: Precompile =
1616
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g1-addition>
1717
pub fn g1_add(input: &[u8], gas_limit: u64) -> PrecompileResult {
1818
if G1_ADD_BASE_GAS_FEE > gas_limit {
19-
return Err(PrecompileError::OutOfGas.into());
19+
return Err(PrecompileError::OutOfGas);
2020
}
2121

2222
if input.len() != G1_ADD_INPUT_LENGTH {
23-
return Err(PrecompileError::Bls12381G1AddInputLength.into());
23+
return Err(PrecompileError::Bls12381G1AddInputLength);
2424
}
2525

2626
// Extract coordinates from padded input
@@ -36,7 +36,6 @@ pub fn g1_add(input: &[u8], gas_limit: u64) -> PrecompileResult {
3636
let padded_result = pad_g1_point(&unpadded_result);
3737

3838
Ok(PrecompileOutput::new(
39-
gas_limit,
4039
G1_ADD_BASE_GAS_FEE,
4140
padded_result.into(),
4241
))

crates/precompile/src/bls12_381/g1_msm.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ pub const PRECOMPILE: Precompile =
2727
pub fn g1_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
2828
let input_len = input.len();
2929
if input_len == 0 || !input_len.is_multiple_of(G1_MSM_INPUT_LENGTH) {
30-
return Err(PrecompileError::Bls12381G1MsmInputLength.into());
30+
return Err(PrecompileError::Bls12381G1MsmInputLength);
3131
}
3232

3333
let k = input_len / G1_MSM_INPUT_LENGTH;
3434
let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G1_MSM, G1_MSM_BASE_GAS_FEE);
3535
if required_gas > gas_limit {
36-
return Err(PrecompileError::OutOfGas.into());
36+
return Err(PrecompileError::OutOfGas);
3737
}
3838

3939
let mut valid_pairs_iter = (0..k).map(|i| {
@@ -54,11 +54,7 @@ pub fn g1_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
5454
// Pad the result for EVM compatibility
5555
let padded_result = pad_g1_point(&unpadded_result);
5656

57-
Ok(PrecompileOutput::new(
58-
gas_limit,
59-
required_gas,
60-
padded_result.into(),
61-
))
57+
Ok(PrecompileOutput::new(required_gas, padded_result.into()))
6258
}
6359

6460
#[cfg(test)]
@@ -70,6 +66,6 @@ mod test {
7066
fn bls_g1multiexp_g1_not_on_curve_but_in_subgroup() {
7167
let input = Bytes::from(hex!("000000000000000000000000000000000a2833e497b38ee3ca5c62828bf4887a9f940c9e426c7890a759c20f248c23a7210d2432f4c98a514e524b5184a0ddac00000000000000000000000000000000150772d56bf9509469f9ebcd6e47570429fd31b0e262b66d512e245c38ec37255529f2271fd70066473e393a8bead0c30000000000000000000000000000000000000000000000000000000000000000"));
7268
let fail = g1_msm(&input, G1_MSM_BASE_GAS_FEE);
73-
assert_eq!(fail, Err(PrecompileError::Bls12381G1NotOnCurve.into()));
69+
assert_eq!(fail, Err(PrecompileError::Bls12381G1NotOnCurve));
7470
}
7571
}

crates/precompile/src/bls12_381/g2_add.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ pub const PRECOMPILE: Precompile =
1717
/// See also <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-addition>
1818
pub fn g2_add(input: &[u8], gas_limit: u64) -> PrecompileResult {
1919
if G2_ADD_BASE_GAS_FEE > gas_limit {
20-
return Err(PrecompileError::OutOfGas.into());
20+
return Err(PrecompileError::OutOfGas);
2121
}
2222

2323
if input.len() != G2_ADD_INPUT_LENGTH {
24-
return Err(PrecompileError::Bls12381G2AddInputLength.into());
24+
return Err(PrecompileError::Bls12381G2AddInputLength);
2525
}
2626

2727
// Extract coordinates from padded input
@@ -37,7 +37,6 @@ pub fn g2_add(input: &[u8], gas_limit: u64) -> PrecompileResult {
3737
let padded_result = pad_g2_point(&unpadded_result);
3838

3939
Ok(PrecompileOutput::new(
40-
gas_limit,
4140
G2_ADD_BASE_GAS_FEE,
4241
padded_result.into(),
4342
))

crates/precompile/src/bls12_381/g2_msm.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub const PRECOMPILE: Precompile =
2424
pub fn g2_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
2525
let input_len = input.len();
2626
if input_len == 0 || !input_len.is_multiple_of(G2_MSM_INPUT_LENGTH) {
27-
return Err(PrecompileError::Bls12381G2MsmInputLength.into());
27+
return Err(PrecompileError::Bls12381G2MsmInputLength);
2828
}
2929

3030
let k = input_len / G2_MSM_INPUT_LENGTH;
3131
let required_gas = msm_required_gas(k, &DISCOUNT_TABLE_G2_MSM, G2_MSM_BASE_GAS_FEE);
3232
if required_gas > gas_limit {
33-
return Err(PrecompileError::OutOfGas.into());
33+
return Err(PrecompileError::OutOfGas);
3434
}
3535

3636
let mut valid_pairs_iter = (0..k).map(|i| {
@@ -50,9 +50,5 @@ pub fn g2_msm(input: &[u8], gas_limit: u64) -> PrecompileResult {
5050
// Pad the result for EVM compatibility
5151
let padded_result = pad_g2_point(&unpadded_result);
5252

53-
Ok(PrecompileOutput::new(
54-
gas_limit,
55-
required_gas,
56-
padded_result.into(),
57-
))
53+
Ok(PrecompileOutput::new(required_gas, padded_result.into()))
5854
}

crates/precompile/src/bls12_381/map_fp2_to_g2.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ pub const PRECOMPILE: Precompile = Precompile::new(
2020
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-mapping-fp2-element-to-g2-point>
2121
pub fn map_fp2_to_g2(input: &[u8], gas_limit: u64) -> PrecompileResult {
2222
if MAP_FP2_TO_G2_BASE_GAS_FEE > gas_limit {
23-
return Err(PrecompileError::OutOfGas.into());
23+
return Err(PrecompileError::OutOfGas);
2424
}
2525

2626
if input.len() != PADDED_FP2_LENGTH {
27-
return Err(PrecompileError::Bls12381MapFp2ToG2InputLength.into());
27+
return Err(PrecompileError::Bls12381MapFp2ToG2InputLength);
2828
}
2929

3030
let input_p0_x = remove_fp_padding(&input[..PADDED_FP_LENGTH])?;
@@ -36,7 +36,6 @@ pub fn map_fp2_to_g2(input: &[u8], gas_limit: u64) -> PrecompileResult {
3636
let padded_result = pad_g2_point(&unpadded_result);
3737

3838
Ok(PrecompileOutput::new(
39-
gas_limit,
4039
MAP_FP2_TO_G2_BASE_GAS_FEE,
4140
padded_result.into(),
4241
))

crates/precompile/src/bls12_381/map_fp_to_g1.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ pub const PRECOMPILE: Precompile = Precompile::new(
1717
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-mapping-fp-element-to-g1-point>
1818
pub fn map_fp_to_g1(input: &[u8], gas_limit: u64) -> PrecompileResult {
1919
if MAP_FP_TO_G1_BASE_GAS_FEE > gas_limit {
20-
return Err(PrecompileError::OutOfGas.into());
20+
return Err(PrecompileError::OutOfGas);
2121
}
2222

2323
if input.len() != PADDED_FP_LENGTH {
24-
return Err(PrecompileError::Bls12381MapFpToG1InputLength.into());
24+
return Err(PrecompileError::Bls12381MapFpToG1InputLength);
2525
}
2626

2727
let input_p0 = remove_fp_padding(input)?;
@@ -32,7 +32,6 @@ pub fn map_fp_to_g1(input: &[u8], gas_limit: u64) -> PrecompileResult {
3232
let padded_result = pad_g1_point(&unpadded_result);
3333

3434
Ok(PrecompileOutput::new(
35-
gas_limit,
3635
MAP_FP_TO_G1_BASE_GAS_FEE,
3736
padded_result.into(),
3837
))
@@ -47,6 +46,6 @@ mod test {
4746
fn sanity_test() {
4847
let input = Bytes::from(hex!("000000000000000000000000000000006900000000000000636f6e7472616374595a603f343061cd305a03f40239f5ffff31818185c136bc2595f2aa18e08f17"));
4948
let fail = map_fp_to_g1(&input, MAP_FP_TO_G1_BASE_GAS_FEE);
50-
assert_eq!(fail, Err(PrecompileError::NonCanonicalFp.into()));
49+
assert_eq!(fail, Err(PrecompileError::NonCanonicalFp));
5150
}
5251
}

crates/precompile/src/bls12_381/pairing.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ pub const PRECOMPILE: Precompile =
3232
pub fn pairing(input: &[u8], gas_limit: u64) -> PrecompileResult {
3333
let input_len = input.len();
3434
if input_len == 0 || !input_len.is_multiple_of(PAIRING_INPUT_LENGTH) {
35-
return Err(PrecompileError::Bls12381PairingInputLength.into());
35+
return Err(PrecompileError::Bls12381PairingInputLength);
3636
}
3737

3838
let k = input_len / PAIRING_INPUT_LENGTH;
3939
let required_gas: u64 = PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_OFFSET_BASE;
4040
if required_gas > gas_limit {
41-
return Err(PrecompileError::OutOfGas.into());
41+
return Err(PrecompileError::OutOfGas);
4242
}
4343

4444
// Collect pairs of points for the pairing check
@@ -59,7 +59,6 @@ pub fn pairing(input: &[u8], gas_limit: u64) -> PrecompileResult {
5959
let result = if result { 1 } else { 0 };
6060

6161
Ok(PrecompileOutput::new(
62-
gas_limit,
6362
required_gas,
6463
B256::with_last_byte(result).into(),
6564
))

crates/precompile/src/bn254.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub const PAIR_ELEMENT_LEN: usize = G1_LEN + G2_LEN;
154154
/// Run the Bn254 add precompile
155155
pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult {
156156
if gas_cost > gas_limit {
157-
return Err(PrecompileError::OutOfGas.into());
157+
return Err(PrecompileError::OutOfGas);
158158
}
159159

160160
let input = right_pad::<ADD_INPUT_LEN>(input);
@@ -163,13 +163,13 @@ pub fn run_add(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult
163163
let p2_bytes = &input[G1_LEN..];
164164
let output = crypto().bn254_g1_add(p1_bytes, p2_bytes)?;
165165

166-
Ok(PrecompileOutput::new(gas_limit, gas_cost, output.into()))
166+
Ok(PrecompileOutput::new(gas_cost, output.into()))
167167
}
168168

169169
/// Run the Bn254 mul precompile
170170
pub fn run_mul(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult {
171171
if gas_cost > gas_limit {
172-
return Err(PrecompileError::OutOfGas.into());
172+
return Err(PrecompileError::OutOfGas);
173173
}
174174

175175
let input = right_pad::<MUL_INPUT_LEN>(input);
@@ -178,7 +178,7 @@ pub fn run_mul(input: &[u8], gas_cost: u64, gas_limit: u64) -> PrecompileResult
178178
let scalar_bytes = &input[G1_LEN..G1_LEN + SCALAR_LEN];
179179
let output = crypto().bn254_g1_mul(point_bytes, scalar_bytes)?;
180180

181-
Ok(PrecompileOutput::new(gas_limit, gas_cost, output.into()))
181+
Ok(PrecompileOutput::new(gas_cost, output.into()))
182182
}
183183

184184
/// Run the Bn254 pair precompile
@@ -190,11 +190,11 @@ pub fn run_pair(
190190
) -> PrecompileResult {
191191
let gas_used = (input.len() / PAIR_ELEMENT_LEN) as u64 * pair_per_point_cost + pair_base_cost;
192192
if gas_used > gas_limit {
193-
return Err(PrecompileError::OutOfGas.into());
193+
return Err(PrecompileError::OutOfGas);
194194
}
195195

196196
if !input.len().is_multiple_of(PAIR_ELEMENT_LEN) {
197-
return Err(PrecompileError::Bn254PairLength.into());
197+
return Err(PrecompileError::Bn254PairLength);
198198
}
199199

200200
let elements = input.len() / PAIR_ELEMENT_LEN;
@@ -217,7 +217,6 @@ pub fn run_pair(
217217

218218
let pairing_result = crypto().bn254_pairing_check(&points)?;
219219
Ok(PrecompileOutput::new(
220-
gas_limit,
221220
gas_used,
222221
bool_to_bytes32(pairing_result),
223222
))
@@ -288,7 +287,7 @@ mod tests {
288287

289288
let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 499);
290289

291-
assert!(matches!(res, Err(ref f) if f.error == PrecompileError::OutOfGas));
290+
assert!(matches!(res, Err(ref f) if *f == PrecompileError::OutOfGas));
292291

293292
// No input test
294293
let input = [0u8; 0];
@@ -315,7 +314,7 @@ mod tests {
315314
let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, 500);
316315
assert!(matches!(
317316
res,
318-
Err(ref f) if f.error == PrecompileError::Bn254AffineGFailedToCreate
317+
Err(ref f) if *f == PrecompileError::Bn254AffineGFailedToCreate
319318
));
320319
}
321320

@@ -348,7 +347,7 @@ mod tests {
348347
.unwrap();
349348

350349
let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 39_999);
351-
assert!(matches!(res, Err(ref f) if f.error == PrecompileError::OutOfGas));
350+
assert!(matches!(res, Err(ref f) if *f == PrecompileError::OutOfGas));
352351

353352
// Zero multiplication test
354353
let input = hex::decode(
@@ -392,7 +391,7 @@ mod tests {
392391
let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, 40_000);
393392
assert!(matches!(
394393
res,
395-
Err(ref f) if f.error == PrecompileError::Bn254AffineGFailedToCreate
394+
Err(ref f) if *f == PrecompileError::Bn254AffineGFailedToCreate
396395
));
397396
}
398397

@@ -451,7 +450,7 @@ mod tests {
451450
BYZANTIUM_PAIR_BASE,
452451
259_999,
453452
);
454-
assert!(matches!(res, Err(ref f) if f.error == PrecompileError::OutOfGas));
453+
assert!(matches!(res, Err(ref f) if *f == PrecompileError::OutOfGas));
455454

456455
// No input test
457456
let input = [0u8; 0];
@@ -488,7 +487,7 @@ mod tests {
488487
);
489488
assert!(matches!(
490489
res,
491-
Err(ref f) if f.error == PrecompileError::Bn254AffineGFailedToCreate
490+
Err(ref f) if *f == PrecompileError::Bn254AffineGFailedToCreate
492491
));
493492

494493
// Invalid input length
@@ -507,7 +506,7 @@ mod tests {
507506
BYZANTIUM_PAIR_BASE,
508507
260_000,
509508
);
510-
assert!(matches!(res, Err(ref f) if f.error == PrecompileError::Bn254PairLength));
509+
assert!(matches!(res, Err(ref f) if *f == PrecompileError::Bn254PairLength));
511510

512511
// Test with point at infinity - should return true (identity element)
513512
// G1 point at infinity (0,0) followed by a valid G2 point

crates/precompile/src/hash.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ pub const RIPEMD160: Precompile = Precompile::new(
2525
pub fn sha256_run(input: &[u8], gas_limit: u64) -> PrecompileResult {
2626
let cost = calc_linear_cost(input.len(), 60, 12);
2727
if cost > gas_limit {
28-
Err(PrecompileError::OutOfGas.into())
28+
Err(PrecompileError::OutOfGas)
2929
} else {
3030
let output = crypto().sha256(input);
31-
Ok(PrecompileOutput::new(
32-
gas_limit,
33-
cost,
34-
output.to_vec().into(),
35-
))
31+
Ok(PrecompileOutput::new(cost, output.to_vec().into()))
3632
}
3733
}
3834

@@ -45,13 +41,9 @@ pub fn sha256_run(input: &[u8], gas_limit: u64) -> PrecompileResult {
4541
pub fn ripemd160_run(input: &[u8], gas_limit: u64) -> PrecompileResult {
4642
let gas_used = calc_linear_cost(input.len(), 600, 120);
4743
if gas_used > gas_limit {
48-
Err(PrecompileError::OutOfGas.into())
44+
Err(PrecompileError::OutOfGas)
4945
} else {
5046
let output = crypto().ripemd160(input);
51-
Ok(PrecompileOutput::new(
52-
gas_limit,
53-
gas_used,
54-
output.to_vec().into(),
55-
))
47+
Ok(PrecompileOutput::new(gas_used, output.to_vec().into()))
5648
}
5749
}

0 commit comments

Comments
 (0)