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