ethereum.forks.bpo5.vm.precompiled_contracts.bls12_381.bls12_381_g2ethereum.forks.amsterdam.vm.precompiled_contracts.bls12_381.bls12_381_g2

Ethereum Virtual Machine (EVM) BLS12 381 G2 CONTRACTS.

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

Introduction

Implementation of pre-compiles in G2 (curve over base prime field).

LENGTH_PER_PAIR

36
LENGTH_PER_PAIR = 288

bls12_g2_add

The bls12_381 G2 point addition precompile.

Parameters

evm : The current EVM frame.

Raises

InvalidParameter If the input length is invalid.

def bls12_g2_add(​evm: Evm​) -> None:
40
    <snip>
54
    data = evm.message.data
54
    data = evm.call_data
55
    if len(data) != 512:
56
        raise InvalidParameter("Invalid Input Length")
57
58
    # GAS
59
    charge_gas(evm, GasCosts.PRECOMPILE_BLS_G2ADD)
60
61
    # OPERATION
62
    p1 = bytes_to_g2(buffer_read(data, U256(0), U256(256)))
63
    p2 = bytes_to_g2(buffer_read(data, U256(256), U256(256)))
62
    p1 = unpad_g2(data[:256])
63
    p2 = unpad_g2(data[256:512])
64
65
    result = bls12_add(p1, p2)
65
    try:
66
        raw = g2_add(p1, p2)
67
    except InvalidInputError as e:
68
        raise InvalidParameter(str(e)) from e
69
67
    evm.output = g2_to_bytes(result)
70
    evm.output = pad_g2(raw)

bls12_g2_msm

The bls12_381 G2 multi-scalar multiplication precompile. Note: This uses the naive approach to multi-scalar multiplication which is not suitably optimized for production clients. Clients are required to implement a more efficient algorithm such as the Pippenger algorithm.

Parameters

evm : The current EVM frame.

Raises

InvalidParameter If the input length is invalid.

def bls12_g2_msm(​evm: Evm​) -> None:
74
    <snip>
89
    data = evm.message.data
92
    data = evm.call_data
93
    if len(data) == 0 or len(data) % LENGTH_PER_PAIR != 0:
94
        raise InvalidParameter("Invalid Input Length")
95
96
    # GAS
97
    k = len(data) // LENGTH_PER_PAIR
98
    if k <= 128:
99
        discount = Uint(G2_K_DISCOUNT[k - 1])
100
    else:
101
        discount = Uint(G2_MAX_DISCOUNT)
102
100
    gas_cost = Uint(k) * GasCosts.PRECOMPILE_BLS_G2MUL * discount // MULTIPLIER
103
    gas_cost = ExecutionGas(
104
        Uint(k) * GasCosts.PRECOMPILE_BLS_G2MUL * discount // MULTIPLIER
105
    )
106
    charge_gas(evm, gas_cost)
107
103
    # OPERATION
104
    for i in range(k):
105
        start_index = i * LENGTH_PER_PAIR
106
        end_index = start_index + LENGTH_PER_PAIR
107
108
        p, m = decode_g2_scalar_pair(data[start_index:end_index])
109
        product = bls12_multiply(p, m)
110
111
        if i == 0:
112
            result = product
113
        else:
114
            result = bls12_add(result, product)
108
    # OPERATION
109
    points = []
110
    scalars = []
111
    for i in range(k):
112
        start = i * LENGTH_PER_PAIR
113
        points.append(unpad_g2(data[start : start + 256]))
114
        scalars.append(data[start + 256 : start + LENGTH_PER_PAIR])
115
116
    evm.output = g2_to_bytes(result)
116
    try:
117
        raw = g2_msm(points, scalars)
118
    except InvalidInputError as e:
119
        raise InvalidParameter(str(e)) from e
120
121
    evm.output = pad_g2(raw)

bls12_map_fp2_to_g2

Precompile to map field element to G2.

Parameters

evm : The current EVM frame.

Raises

InvalidParameter If the input length is invalid.

def bls12_map_fp2_to_g2(​evm: Evm​) -> None:
125
    <snip>
134
    data = evm.message.data
139
    data = evm.call_data
140
    if len(data) != 128:
141
        raise InvalidParameter("Invalid Input Length")
142
143
    # GAS
144
    charge_gas(evm, GasCosts.PRECOMPILE_BLS_G2MAP)
145
146
    # OPERATION
142
    field_element = bytes_to_fq2(data)
143
    assert isinstance(field_element, FQ2)
147
    fp2 = unpad_fp(data[:64]) + unpad_fp(data[64:])
148
145
    fp2 = bytes_to_fq2(data)
146
    g2_3d = clear_cofactor_G2(map_to_curve_G2(fp2))
149
    try:
150
        raw = map_fp2_to_g2(fp2)
151
    except InvalidInputError as e:
152
        raise InvalidParameter(str(e)) from e
153
148
    evm.output = g2_to_bytes(g2_3d)
154
    evm.output = pad_g2(raw)