ethereum.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.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 = unpad_g2(data[:256])
63
    p2 = unpad_g2(data[256:512])
64
65
    try:
66
        raw = g2_add(p1, p2)
67
    except InvalidInputError as e:
68
        raise InvalidParameter(str(e)) from e
69
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>
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
103
    gas_cost = ExecutionGas(
104
        Uint(k) * GasCosts.PRECOMPILE_BLS_G2MUL * discount // MULTIPLIER
105
    )
106
    charge_gas(evm, gas_cost)
107
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
    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>
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
147
    fp2 = unpad_fp(data[:64]) + unpad_fp(data[64:])
148
149
    try:
150
        raw = map_fp2_to_g2(fp2)
151
    except InvalidInputError as e:
152
        raise InvalidParameter(str(e)) from e
153
154
    evm.output = pad_g2(raw)