ethereum.forks.bpo1.vm.precompiled_contracts.bls12_381.bls12_381_g1ethereum.forks.bpo2.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

35
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:
39
    """
40
    The bls12_381 G1 point addition precompile.
41
42
    Parameters
43
    ----------
44
    evm :
45
        The current EVM frame.
46
47
    Raises
48
    ------
49
    InvalidParameter
50
        If the input length is invalid.
51
52
    """
53
    data = evm.message.data
54
    if len(data) != 256:
55
        raise InvalidParameter("Invalid Input Length")
56
57
    # GAS
61
    charge_gas(evm, GasCosts.PRECOMPILE_BLS_G1ADD)
58
    charge_gas(evm, Uint(GasCosts.PRECOMPILE_BLS_G1ADD))
59
60
    # OPERATION
61
    p1 = bytes_to_g1(buffer_read(data, U256(0), U256(128)))
62
    p2 = bytes_to_g1(buffer_read(data, U256(128), U256(128)))
63
64
    result = bls12_add(p1, p2)
65
66
    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:
70
    """
71
    The bls12_381 G1 multi-scalar multiplication precompile.
72
    Note: This uses the naive approach to multi-scalar multiplication
73
    which is not suitably optimized for production clients. Clients are
74
    required to implement a more efficient algorithm such as the Pippenger
75
    algorithm.
76
77
    Parameters
78
    ----------
79
    evm :
80
        The current EVM frame.
81
82
    Raises
83
    ------
84
    InvalidParameter
85
        If the input length is invalid.
86
87
    """
88
    data = evm.message.data
89
    if len(data) == 0 or len(data) % LENGTH_PER_PAIR != 0:
90
        raise InvalidParameter("Invalid Input Length")
91
92
    # GAS
93
    k = len(data) // LENGTH_PER_PAIR
94
    if k <= 128:
95
        discount = Uint(G1_K_DISCOUNT[k - 1])
96
    else:
97
        discount = Uint(G1_MAX_DISCOUNT)
98
99
    gas_cost = Uint(k) * GasCosts.PRECOMPILE_BLS_G1MUL * discount // MULTIPLIER
100
    charge_gas(evm, gas_cost)
101
102
    # OPERATION
103
    for i in range(k):
104
        start_index = i * LENGTH_PER_PAIR
105
        end_index = start_index + LENGTH_PER_PAIR
106
107
        p, m = decode_g1_scalar_pair(data[start_index:end_index])
108
        product = bls12_multiply(p, m)
109
110
        if i == 0:
111
            result = product
112
        else:
113
            result = bls12_add(result, product)
114
115
    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:
119
    """
120
    Precompile to map field element to G1.
121
122
    Parameters
123
    ----------
124
    evm :
125
        The current EVM frame.
126
127
    Raises
128
    ------
129
    InvalidParameter
130
        If the input length is invalid.
131
132
    """
133
    data = evm.message.data
134
    if len(data) != 64:
135
        raise InvalidParameter("Invalid Input Length")
136
137
    # GAS
141
    charge_gas(evm, GasCosts.PRECOMPILE_BLS_G1MAP)
138
    charge_gas(evm, Uint(GasCosts.PRECOMPILE_BLS_G1MAP))
139
140
    # OPERATION
141
    fp = int.from_bytes(data, "big")
142
    if fp >= FQ.field_modulus:
143
        raise InvalidParameter("coordinate >= field modulus")
144
145
    g1_optimized_3d = clear_cofactor_G1(map_to_curve_G1(FQ(fp)))
146
    evm.output = g1_to_bytes(g1_optimized_3d)