Repository navigation
Expand file tree
/
Copy pathAltBn128.lean
More file actions
173 lines (134 loc) · 6.67 KB
/
Copy pathAltBn128.lean
File metadata and controls
173 lines (134 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
module
public import EthLib.Base.BigEndian
public import EthLib.Base.Fork
public import EthLib.Cryptography.AltBn128.Pairing
public import EthLib.Execution.Evm.Machine.Memory
/-!
# `Evm.Precompiles.AltBn128`
The alt_bn128 precompiles at `0x06` to `0x08`, from EIP-196 and EIP-197.
They add two points of `G_1`, multiply a point by a scalar, and check a product of pairings.
EIP-1108 lowers their prices from Istanbul, so each call takes the fork.
-/
@[expose] public section
namespace EthLib.Execution.Evm.Precompiles.AltBn128
open EthLib.Base (Fork bytesToNat natToBytes)
open EthLib.Execution.Evm (bufferRead)
open EthLib.Cryptography.AltBn128
/-- Why an alt_bn128 call fails.
Every failure consumes all the gas, so the error only says why. -/
inductive AltBn128Error where
/-- The gas given was below the price. -/
| outOfGas (cost gas : Nat)
/-- A coordinate is not below the field modulus `p`. -/
| invalidFieldElement
/-- A point is not on its curve. -/
| notOnCurve
/-- The call data of a pairing check is not a whole number of 192-byte pairs. -/
| invalidInputLength (size : Nat)
/-- A point of the twist is not in the group of order `q`. -/
| notInSubgroup
deriving Repr, DecidableEq
/-- The stable code of an error, one per constructor.
The code ignores the arguments of the constructor.
A new constructor takes a new code, so that each code keeps its meaning. -/
def AltBn128Error.code : AltBn128Error → UInt32
| .outOfGas .. => 1
| .invalidFieldElement => 2
| .notOnCurve => 3
| .invalidInputLength .. => 4
| .notInSubgroup => 5
/-- The price of an addition: 500 from EIP-196, and 150 from Istanbul by EIP-1108. -/
def ecaddGas (fork : Fork) : Nat := if fork ≥ .istanbul then 150 else 500
/-- The price of a multiplication: 40000 from EIP-196, and 6000 from Istanbul by EIP-1108. -/
def ecmulGas (fork : Fork) : Nat := if fork ≥ .istanbul then 6000 else 40000
/-- The price of a pairing check on `size` bytes, with one pair per whole 192 bytes.
EIP-197 charges 100000, plus 80000 per pair.
From Istanbul, EIP-1108 charges 45000, plus 34000 per pair. -/
def ecpairingGas (fork : Fork) (size : Nat) : Nat :=
if fork ≥ .istanbul then 34000 * (size / 192) + 45000 else 80000 * (size / 192) + 100000
/-- A point of `G_1` from 64 bytes, with `(0, 0)` for the point at infinity. -/
def bytesToG1 (raw : List UInt8) : Except AltBn128Error Point := do
let x := bytesToNat (raw.take 32)
let y := bytesToNat (raw.drop 32)
if hx : x < Fp.modulus then
if hy : y < Fp.modulus then
if x = 0 ∧ y = 0 then
return .infinity
let point := Point.affine ⟨x, hx⟩ ⟨y, hy⟩
if point.isOnCurve then return point else throw .notOnCurve
else throw .invalidFieldElement
else throw .invalidFieldElement
/-- The 64 bytes of a point: its two coordinates, and all zeros for the point at infinity. -/
def g1ToBytes : Point → List UInt8
| .infinity => List.replicate 64 0
| .affine x y => natToBytes 32 x.val ++ natToBytes 32 y.val
/-- ECADD: the sum of the two points in the call data, once the gas is charged. -/
def ecadd (fork : Fork) (data : ByteArray) (gas : Nat) : Except AltBn128Error ByteArray := do
let cost := ecaddGas fork
if gas < cost then
throw (.outOfGas cost gas)
let p0 ← bytesToG1 (bufferRead data 0 64)
let p1 ← bytesToG1 (bufferRead data 64 64)
return ⟨(g1ToBytes (p0 + p1)).toArray⟩
/-- ECMUL: the point in the call data times the scalar after it, once the gas is charged.
The scalar is the full 32-byte integer, not reduced. -/
def ecmul (fork : Fork) (data : ByteArray) (gas : Nat) : Except AltBn128Error ByteArray := do
let cost := ecmulGas fork
if gas < cost then
throw (.outOfGas cost gas)
let p0 ← bytesToG1 (bufferRead data 0 64)
let n := bytesToNat (bufferRead data 64 32)
-- A 32-byte scalar is below `2^256`, so 256 bits cover it.
return ⟨(g1ToBytes (Point.mulBits 256 n p0)).toArray⟩
/-- An element of `Fp` from 32 bytes. -/
def bytesToFp (raw : List UInt8) : Except AltBn128Error Fp :=
let value := bytesToNat raw
if h : value < Fp.modulus then .ok ⟨value, h⟩ else .error .invalidFieldElement
/-- A point of the twist from 128 bytes, with zeros for the point at infinity.
Each coordinate gives its `i` part first. -/
def bytesToG2 (raw : List UInt8) : Except AltBn128Error Point2 := do
let x1 ← bytesToFp (raw.take 32)
let x0 ← bytesToFp ((raw.drop 32).take 32)
let y1 ← bytesToFp ((raw.drop 64).take 32)
let y0 ← bytesToFp (raw.drop 96)
let x : Fp2 := ⟨x0, x1⟩
let y : Fp2 := ⟨y0, y1⟩
if x = ⟨0, 0⟩ ∧ y = ⟨0, 0⟩ then
return .infinity
let point := Point2.affine x y
if point.isOnCurve then return point else throw .notOnCurve
/-- The 128 bytes of a point of the twist, with the `i` part of each coordinate first.
The point at infinity is 128 zero bytes.
No precompile returns a point of the twist; this encoding is the inverse of the decoder above. -/
def g2ToBytes : Point2 → List UInt8
| .infinity => List.replicate 128 0
| .affine x y =>
natToBytes 32 x.c1.val ++ natToBytes 32 x.c0.val ++
natToBytes 32 y.c1.val ++ natToBytes 32 y.c0.val
/-- One pair of the call data, decoded and checked to be in the groups of order `q`.
For `G_1`, the encoding and the curve equation, or the encoding of infinity, are enough.
So only the point of the twist has its order checked.
Source: [EIP-197][eip197], section Specification.
[eip197]: https://github.com/ethereum/EIPs/blob/46976b2a9bf99f727fb87dc8c809e3a3cdc6b445/EIPS/eip-197.md#specification
-/
def decodePair (data : ByteArray) (index : Nat) : Except AltBn128Error (Point × Point2) := do
let p ← bytesToG1 (bufferRead data (192 * index) 64)
let q ← bytesToG2 (bufferRead data (192 * index + 64) 128)
-- The order has 254 bits, so 256 bits of the scalar cover it.
if Point2.mulBits 256 Fr.modulus q ≠ .infinity then
throw .notInSubgroup
return (p, q)
/-- The 32-byte word of a boolean: 31 zero bytes, then `1` for true and `0` for false. -/
def ecpairingWord (result : Bool) : List UInt8 :=
List.replicate 31 0 ++ [if result then 1 else 0]
/-- ECPAIRING: the word 1 when the product of the pairings of the pairs is 1, and 0 otherwise.
The gas is charged before the length is checked.
EIP-197 does not order the two, so this module orders them. -/
def ecpairing (fork : Fork) (data : ByteArray) (gas : Nat) : Except AltBn128Error ByteArray := do
if gas < ecpairingGas fork data.size then
throw (.outOfGas (ecpairingGas fork data.size) gas)
if data.size % 192 ≠ 0 then
throw (.invalidInputLength data.size)
let pairs ← (List.range (data.size / 192)).mapM (decodePair data)
return ⟨(ecpairingWord (pairingCheck pairs)).toArray⟩
end EthLib.Execution.Evm.Precompiles.AltBn128