ethereum.paris.vm.precompiled_contracts.alt_bn128ethereum.shanghai.vm.precompiled_contracts.alt_bn128
Ethereum Virtual Machine (EVM) ALT_BN128 CONTRACTS ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementation of the ALT_BN128 precompiled contracts.
alt_bn128_add
The ALT_BN128 addition precompiled contract.
Parameters
evm : The current EVM frame.
def alt_bn128_add(evm: Evm) -> None:
34 | """ |
---|---|
35 | The ALT_BN128 addition precompiled contract. |
36 |
|
37 | Parameters |
38 | ---------- |
39 | evm : |
40 | The current EVM frame. |
41 | """ |
42 | data = evm.message.data |
43 | |
44 | # GAS |
45 | charge_gas(evm, Uint(150)) |
46 | |
47 | # OPERATION |
48 | x0_bytes = buffer_read(data, U256(0), U256(32)) |
49 | x0_value = int(U256.from_be_bytes(x0_bytes)) |
50 | y0_bytes = buffer_read(data, U256(32), U256(32)) |
51 | y0_value = int(U256.from_be_bytes(y0_bytes)) |
52 | x1_bytes = buffer_read(data, U256(64), U256(32)) |
53 | x1_value = int(U256.from_be_bytes(x1_bytes)) |
54 | y1_bytes = buffer_read(data, U256(96), U256(32)) |
55 | y1_value = int(U256.from_be_bytes(y1_bytes)) |
56 | |
57 | for i in (x0_value, y0_value, x1_value, y1_value): |
58 | if i >= ALT_BN128_PRIME: |
59 | raise OutOfGasError |
60 | |
61 | try: |
62 | p0 = BNP(BNF(x0_value), BNF(y0_value)) |
63 | p1 = BNP(BNF(x1_value), BNF(y1_value)) |
64 | except ValueError: |
65 | raise OutOfGasError |
66 | |
67 | p = p0 + p1 |
68 | |
69 | evm.output = p.x.to_be_bytes32() + p.y.to_be_bytes32() |
alt_bn128_mul
The ALT_BN128 multiplication precompiled contract.
Parameters
evm : The current EVM frame.
def alt_bn128_mul(evm: Evm) -> None:
73 | """ |
---|---|
74 | The ALT_BN128 multiplication precompiled contract. |
75 |
|
76 | Parameters |
77 | ---------- |
78 | evm : |
79 | The current EVM frame. |
80 | """ |
81 | data = evm.message.data |
82 | |
83 | # GAS |
84 | charge_gas(evm, Uint(6000)) |
85 | |
86 | # OPERATION |
87 | x0_bytes = buffer_read(data, U256(0), U256(32)) |
88 | x0_value = int(U256.from_be_bytes(x0_bytes)) |
89 | y0_bytes = buffer_read(data, U256(32), U256(32)) |
90 | y0_value = int(U256.from_be_bytes(y0_bytes)) |
91 | n = int(U256.from_be_bytes(buffer_read(data, U256(64), U256(32)))) |
92 | |
93 | for i in (x0_value, y0_value): |
94 | if i >= ALT_BN128_PRIME: |
95 | raise OutOfGasError |
96 | |
97 | try: |
98 | p0 = BNP(BNF(x0_value), BNF(y0_value)) |
99 | except ValueError: |
100 | raise OutOfGasError |
101 | |
102 | p = p0.mul_by(n) |
103 | |
104 | evm.output = p.x.to_be_bytes32() + p.y.to_be_bytes32() |
alt_bn128_pairing_check
The ALT_BN128 pairing check precompiled contract.
Parameters
evm : The current EVM frame.
def alt_bn128_pairing_check(evm: Evm) -> None:
108 | """ |
---|---|
109 | The ALT_BN128 pairing check precompiled contract. |
110 |
|
111 | Parameters |
112 | ---------- |
113 | evm : |
114 | The current EVM frame. |
115 | """ |
116 | data = evm.message.data |
117 | |
118 | # GAS |
119 | charge_gas(evm, Uint(34000 * (len(data) // 192) + 45000)) |
120 | |
121 | # OPERATION |
122 | if len(data) % 192 != 0: |
123 | raise OutOfGasError |
124 | result = BNF12.from_int(1) |
125 | for i in range(len(data) // 192): |
126 | values = [] |
127 | for j in range(6): |
128 | value = int( |
129 | U256.from_be_bytes( |
130 | data[i * 192 + 32 * j : i * 192 + 32 * (j + 1)] |
131 | ) |
132 | ) |
133 | if value >= ALT_BN128_PRIME: |
134 | raise OutOfGasError |
135 | values.append(value) |
136 |
|
137 | try: |
138 | p = BNP(BNF(values[0]), BNF(values[1])) |
139 | q = BNP2( |
140 | BNF2((values[3], values[2])), BNF2((values[5], values[4])) |
141 | ) |
142 | except ValueError: |
143 | raise OutOfGasError() |
144 | if p.mul_by(ALT_BN128_CURVE_ORDER) != BNP.point_at_infinity(): |
145 | raise OutOfGasError |
146 | if q.mul_by(ALT_BN128_CURVE_ORDER) != BNP2.point_at_infinity(): |
147 | raise OutOfGasError |
148 | if p != BNP.point_at_infinity() and q != BNP2.point_at_infinity(): |
149 | result = result * pairing(q, p) |
150 | |
151 | if result == BNF12.from_int(1): |
152 | evm.output = U256(1).to_be_bytes32() |
153 | else: |
154 | evm.output = U256(0).to_be_bytes32() |