ethereum.forks.bpo5.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

39
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:
43
    """
44
    The bls12_381 G2 point addition precompile.
45
46
    Parameters
47
    ----------
48
    evm :
49
        The current EVM frame.
50
51
    Raises
52
    ------
53
    InvalidParameter
54
        If the input length is invalid.
55
56
    """
57
    data = evm.message.data
58
    if len(data) != 512:
59
        raise InvalidParameter("Invalid Input Length")
60
61
    # GAS
62
    charge_gas(evm, Uint(GasCosts.PRECOMPILE_BLS_G2ADD))
63
64
    # OPERATION
65
    p1 = bytes_to_g2(buffer_read(data, U256(0), U256(256)))
66
    p2 = bytes_to_g2(buffer_read(data, U256(256), U256(256)))
67
68
    result = bls12_add(p1, p2)
69
70
    evm.output = g2_to_bytes(result)

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
    """
75
    The bls12_381 G2 multi-scalar multiplication precompile.
76
    Note: This uses the naive approach to multi-scalar multiplication
77
    which is not suitably optimized for production clients. Clients are
78
    required to implement a more efficient algorithm such as the Pippenger
79
    algorithm.
80
81
    Parameters
82
    ----------
83
    evm :
84
        The current EVM frame.
85
86
    Raises
87
    ------
88
    InvalidParameter
89
        If the input length is invalid.
90
91
    """
92
    data = evm.message.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 = Uint(k) * GasCosts.PRECOMPILE_BLS_G2MUL * discount // MULTIPLIER
104
    charge_gas(evm, gas_cost)
105
106
    # OPERATION
107
    for i in range(k):
108
        start_index = i * LENGTH_PER_PAIR
109
        end_index = start_index + LENGTH_PER_PAIR
110
111
        p, m = decode_g2_scalar_pair(data[start_index:end_index])
112
        product = bls12_multiply(p, m)
113
114
        if i == 0:
115
            result = product
116
        else:
117
            result = bls12_add(result, product)
118
119
    evm.output = g2_to_bytes(result)

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:
123
    """
124
    Precompile to map field element to G2.
125
126
    Parameters
127
    ----------
128
    evm :
129
        The current EVM frame.
130
131
    Raises
132
    ------
133
    InvalidParameter
134
        If the input length is invalid.
135
136
    """
137
    data = evm.message.data
138
    if len(data) != 128:
139
        raise InvalidParameter("Invalid Input Length")
140
141
    # GAS
142
    charge_gas(evm, Uint(GasCosts.PRECOMPILE_BLS_G2MAP))
143
144
    # OPERATION
145
    field_element = bytes_to_fq2(data)
146
    assert isinstance(field_element, FQ2)
147
148
    fp2 = bytes_to_fq2(data)
149
    g2_3d = clear_cofactor_G2(map_to_curve_G2(fp2))
150
151
    evm.output = g2_to_bytes(g2_3d)