ethereum.forks.bpo5.vm.precompiled_contracts.bls12_381.bls12_381_pairingethereum.forks.amsterdam.vm.precompiled_contracts.bls12_381.bls12_381_pairing

Ethereum Virtual Machine (EVM) BLS12 381 PAIRING PRE-COMPILE.

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Implementation of the BLS12 381 pairing pre-compile.

bls12_pairing

The bls12_381 pairing precompile.

Parameters

evm : The current EVM frame.

Raises

InvalidParameter If the input length is invalid or if the subgroup check fails.

def bls12_pairing(​evm: Evm​) -> None:
26
    <snip>
39
    data = evm.message.data
40
    data = evm.call_data
41
    if len(data) == 0 or len(data) % 384 != 0:
42
        raise InvalidParameter("Invalid Input Length")
43
44
    # GAS
45
    k = len(data) // 384
45
    gas_cost = Uint(32600 * k + 37700)
46
    gas_cost = ExecutionGas(Uint(32600 * k + 37700))
47
    charge_gas(evm, gas_cost)
48
49
    # OPERATION
49
    result = FQ12.one()
50
    g1_points = []
51
    g2_points = []
52
    for i in range(k):
51
        g1_start = Uint(384 * i)
52
        g2_start = Uint(384 * i + 128)
53
        g1_start = 384 * i
54
        g2_start = 384 * i + 128
55
54
        g1_slice = data[g1_start : g1_start + Uint(128)]
55
        g1_point = bytes_to_g1(bytes(g1_slice))
56
        if not is_inf(bls12_multiply(g1_point, curve_order)):
57
            raise InvalidParameter("Subgroup check failed for G1 point.")
58
59
        g2_slice = data[g2_start : g2_start + Uint(256)]
60
        g2_point = bytes_to_g2(bytes(g2_slice))
61
        if not is_inf(bls12_multiply(g2_point, curve_order)):
62
            raise InvalidParameter("Subgroup check failed for G2 point.")
63
64
        result *= pairing(g2_point, g1_point)
56
        g1_points.append(unpad_g1(data[g1_start : g1_start + 128]))
57
        g2_points.append(unpad_g2(data[g2_start : g2_start + 256]))
58
66
    if result == FQ12.one():
59
    try:
60
        result = pairing_check(g1_points, g2_points)
61
    except InvalidInputError as e:
62
        raise InvalidParameter(str(e)) from e
63
64
    if result:
65
        evm.output = b"\x00" * 31 + b"\x01"
66
    else:
67
        evm.output = b"\x00" * 32