revm_precompile/bls12_381/
pairing.rs

1//! BLS12-381 pairing precompile. More details in [`pairing`]
2use super::crypto_backend::{pairing_check, read_g1, read_g2};
3use super::utils::{remove_g1_padding, remove_g2_padding};
4use crate::bls12_381_const::{
5    PADDED_G1_LENGTH, PADDED_G2_LENGTH, PAIRING_ADDRESS, PAIRING_INPUT_LENGTH,
6    PAIRING_MULTIPLIER_BASE, PAIRING_OFFSET_BASE,
7};
8use crate::{PrecompileError, PrecompileOutput, PrecompileResult, PrecompileWithAddress};
9use primitives::B256;
10use std::vec::Vec;
11
12/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_PAIRING precompile.
13pub const PRECOMPILE: PrecompileWithAddress = PrecompileWithAddress(PAIRING_ADDRESS, pairing);
14
15/// Pairing call expects 384*k (k being a positive integer) bytes as an inputs
16/// that is interpreted as byte concatenation of k slices. Each slice has the
17/// following structure:
18///    * 128 bytes of G1 point encoding
19///    * 256 bytes of G2 point encoding
20///
21/// Each point is expected to be in the subgroup of order q.
22/// Output is 32 bytes where first 31 bytes are equal to 0x00 and the last byte
23/// is 0x01 if pairing result is equal to the multiplicative identity in a pairing
24/// target field and 0x00 otherwise.
25///
26/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-pairing>
27pub fn pairing(input: &[u8], gas_limit: u64) -> PrecompileResult {
28    let input_len = input.len();
29    if input_len == 0 || input_len % PAIRING_INPUT_LENGTH != 0 {
30        return Err(PrecompileError::Other(format!(
31            "Pairing input length should be multiple of {PAIRING_INPUT_LENGTH}, was {input_len}"
32        )));
33    }
34
35    let k = input_len / PAIRING_INPUT_LENGTH;
36    let required_gas: u64 = PAIRING_MULTIPLIER_BASE * k as u64 + PAIRING_OFFSET_BASE;
37    if required_gas > gas_limit {
38        return Err(PrecompileError::OutOfGas);
39    }
40
41    // Collect pairs of points for the pairing check
42    let mut pairs = Vec::with_capacity(k);
43    for i in 0..k {
44        let encoded_g1_element =
45            &input[i * PAIRING_INPUT_LENGTH..i * PAIRING_INPUT_LENGTH + PADDED_G1_LENGTH];
46        let encoded_g2_element = &input[i * PAIRING_INPUT_LENGTH + PADDED_G1_LENGTH
47            ..i * PAIRING_INPUT_LENGTH + PADDED_G1_LENGTH + PADDED_G2_LENGTH];
48
49        // If either the G1 or G2 element is the encoded representation
50        // of the point at infinity, then these two points are no-ops
51        // in the pairing computation.
52        //
53        // Note: we do not skip the validation of these two elements even if
54        // one of them is the point at infinity because we could have G1 be
55        // the point at infinity and G2 be an invalid element or vice versa.
56        // In that case, the precompile should error because one of the elements
57        // was invalid.
58        let g1_is_zero = encoded_g1_element.iter().all(|i| *i == 0);
59        let g2_is_zero = encoded_g2_element.iter().all(|i| *i == 0);
60
61        let [a_x, a_y] = remove_g1_padding(encoded_g1_element)?;
62        let [b_x_0, b_x_1, b_y_0, b_y_1] = remove_g2_padding(encoded_g2_element)?;
63
64        // NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
65        // extract_g1_input and extract_g2_input perform the necessary checks
66        let p1_aff = read_g1(a_x, a_y)?;
67        let p2_aff = read_g2(b_x_0, b_x_1, b_y_0, b_y_1)?;
68
69        if !g1_is_zero & !g2_is_zero {
70            pairs.push((p1_aff, p2_aff));
71        }
72    }
73    let result = if pairing_check(&pairs) { 1 } else { 0 };
74
75    Ok(PrecompileOutput::new(
76        required_gas,
77        B256::with_last_byte(result).into(),
78    ))
79}