-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBytecode.lean
More file actions
112 lines (92 loc) · 3.39 KB
/
Copy pathBytecode.lean
File metadata and controls
112 lines (92 loc) · 3.39 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
module
public import Ix.Aiur.Goldilocks
/-!
Stage 5 (Bytecode) IR — flat, post-lowering.
Later passes (`deduplicate`, `needsCircuit`) produce Stage 6 bytecode with the
same datatype.
-/
public section
namespace Aiur
namespace Bytecode
abbrev FunIdx := Nat
abbrev ValIdx := Nat
abbrev SelIdx := Nat
inductive Op
| const : G → Op
| add : ValIdx → ValIdx → Op
| sub : ValIdx → ValIdx → Op
| mul : ValIdx → ValIdx → Op
| eqZero : ValIdx → Op
| call : FunIdx → Array ValIdx → (outputSize : Nat) → (unconstrained : Bool) → Op
| store : Array ValIdx → Op
| load : (size : Nat) → ValIdx → Op
| assertEq : Array ValIdx → Array ValIdx → Op
| ioGetInfo : ValIdx → Array ValIdx → Op
| ioSetInfo : ValIdx → Array ValIdx → ValIdx → ValIdx → Op
| ioRead : ValIdx → ValIdx → Nat → Op
| ioWrite : ValIdx → Array ValIdx → Op
| u8BitDecomposition : ValIdx → Op
| u8ShiftLeft : ValIdx → Op
| u8ShiftRight : ValIdx → Op
| u8Xor : ValIdx → ValIdx → Op
| u8Add : ValIdx → ValIdx → Op
| u8Mul : ValIdx → ValIdx → Op
| u8Sub : ValIdx → ValIdx → Op
| u8And : ValIdx → ValIdx → Op
| u8Or : ValIdx → ValIdx → Op
| u8LessThan : ValIdx → ValIdx → Op
| u32LessThan : ValIdx → ValIdx → Op
| u8ChainRotr7 : ValIdx → ValIdx → Op
| u8ChainRotr4 : ValIdx → ValIdx → Op
| debug : String → Option (Array ValIdx) → Op
/-- Range-check the two values into `[0, 256)` via the byte chip. Produces no
new values: it is a pure side-effect (lookup), and its `u8` results alias the
two inputs. Kept last so its FFI tag (27) doesn't shift the others. -/
| u8RangeCheck : ValIdx → ValIdx → Op
/-- Unconstrained LE byte-list division-modulo hint. Inputs are pointers to
two `List<U64>` (klimbs) values. Produces 2 fresh pointer values
`(q_ptr, r_ptr)` to newly-built `List<U64>` values such that `q*b + r = a`
and `0 ≤ r < b` (when `b > 0`). No constraint relation emitted; caller
must verify in constrained code. -/
| unconstrainedBigUintDivMod : ValIdx → ValIdx → Op
deriving Repr, BEq, Hashable
mutual
inductive Ctrl where
| match : ValIdx → Array (G × Block) → Option Block → Ctrl
| return : SelIdx → Array ValIdx → Ctrl
| yield : SelIdx → Array ValIdx → Ctrl
| matchContinue : ValIdx → Array (G × Block) → Option Block
→ (outputSize : Nat) → (sharedAuxiliaries : Nat) → (sharedLookups : Nat)
→ Block → Ctrl
deriving Inhabited, Repr
structure Block where
ops : Array Op
ctrl : Ctrl
deriving Inhabited, Repr
end
deriving instance BEq, Hashable for Ctrl, Block
/-- The circuit layout of a function (non-semantic; the bytecode evaluator ignores it). -/
structure FunctionLayout where
inputSize : Nat
selectors : Nat
auxiliaries : Nat
lookups : Nat
deriving Inhabited, Repr, BEq, Hashable, DecidableEq
def FunctionLayout.width (l : FunctionLayout) : Nat :=
l.inputSize + l.selectors + l.auxiliaries
abbrev goldilocksExtensionDegree : Nat := 2
def FunctionLayout.totalWidth (l : FunctionLayout) : Nat :=
l.width + goldilocksExtensionDegree * (1 + l.lookups)
structure Function where
body : Block
layout: FunctionLayout
entry : Bool
constrained : Bool
deriving Inhabited, Repr
structure Toplevel where
functions : Array Function
memorySizes : Array Nat
deriving Repr
end Bytecode
end Aiur
end