ethereum.forks.amsterdam.vm.precompiled_contracts.bls12_381.bls12_381_g1

Ethereum Virtual Machine (EVM) BLS12 381 CONTRACTS.

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

Introduction

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

LENGTH_PER_PAIR

39
LENGTH_PER_PAIR = 160

bls12_g1_add

The bls12_381 G1 point addition precompile.

Parameters

evm : The current EVM frame.

Raises

InvalidParameter If the input length is invalid.

def bls12_g1_add(evm: Evm) -> None:
43
    <snip>
57
    data = evm.call_data
58
    if len(data) != 256:
59
        raise InvalidParameter("Invalid Input Length")
60
61
    # GAS
62
    charge_gas(evm, GasCosts.PRECOMPILE_BLS_G1ADD)
63
64
    # OPERATION
65
    p1 = bytes_to_g1(buffer_read(data, U256(0), U256(128)))
66
    p2 = bytes_to_g1(buffer_read(data, U256(128), U256(128)))
67
68
    result = bls12_add(p1, p2)
69
70
    evm.output = g1_to_bytes(result)

bls12_g1_msm

The bls12_381 G1 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_g1_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(G1_K_DISCOUNT[k - 1])
100
    else:
101
        discount = Uint(G1_MAX_DISCOUNT)
102
103
    gas_cost = ExecutionGas(
104
        Uint(k) * GasCosts.PRECOMPILE_BLS_G1MUL * discount // MULTIPLIER
105
    )
106
    charge_gas(evm, gas_cost)
107
108
    # OPERATION
109
    for i in range(k):
110
        start_index = i * LENGTH_PER_PAIR
111
        end_index = start_index + LENGTH_PER_PAIR
112
113
        p, m = decode_g1_scalar_pair(data[start_index:end_index])
114
        product = bls12_multiply(p, m)
115
116
        if i == 0:
117
            result = product
118
        else:
119
            result = bls12_add(result, product)
120
121
    evm.output = g1_to_bytes(result)

bls12_map_fp_to_g1

Precompile to map field element to G1.

Parameters

evm : The current EVM frame.

Raises

InvalidParameter If the input length is invalid.

def bls12_map_fp_to_g1(evm: Evm) -> None:
125
    <snip>
139
    data = evm.call_data
140
    if len(data) != 64:
141
        raise InvalidParameter("Invalid Input Length")
142
143
    # GAS
144
    charge_gas(evm, GasCosts.PRECOMPILE_BLS_G1MAP)
145
146
    # OPERATION
147
    fp = int.from_bytes(data, "big")
148
    if fp >= FQ.field_modulus:
149
        raise InvalidParameter("coordinate >= field modulus")
150
151
    g1_optimized_3d = clear_cofactor_G1(map_to_curve_G1(FQ(fp)))
152
    evm.output = g1_to_bytes(g1_optimized_3d)