-
Notifications
You must be signed in to change notification settings - Fork 170
/
class.go
208 lines (178 loc) · 5.72 KB
/
class.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package core
import (
"encoding/json"
"fmt"
"math/big"
"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
)
var (
_ Class = (*Cairo0Class)(nil)
_ Class = (*Cairo1Class)(nil)
)
// Class unambiguously defines a [Contract]'s semantics.
type Class interface {
Version() uint64
Hash() (*felt.Felt, error)
}
// Cairo0Class unambiguously defines a [Contract]'s semantics.
type Cairo0Class struct {
Abi json.RawMessage
// External functions defined in the class.
Externals []EntryPoint
// Functions that receive L1 messages. See
// https://www.cairo-lang.org/docs/hello_starknet/l1l2.html#receiving-a-message-from-l1
L1Handlers []EntryPoint
// Constructors for the class. Currently, only one is allowed.
Constructors []EntryPoint
// Base64 encoding of compressed Program
Program string
}
// EntryPoint uniquely identifies a Cairo function to execute.
type EntryPoint struct {
// starknet_keccak hash of the function signature.
Selector *felt.Felt
// The offset of the instruction in the class's bytecode.
Offset *felt.Felt
}
func (c *Cairo0Class) Version() uint64 {
return 0
}
func (c *Cairo0Class) Hash() (*felt.Felt, error) {
return cairo0ClassHash(c)
}
// Cairo1Class unambiguously defines a [Contract]'s semantics.
type Cairo1Class struct {
Abi string
AbiHash *felt.Felt
EntryPoints struct {
Constructor []SierraEntryPoint
External []SierraEntryPoint
L1Handler []SierraEntryPoint
}
Program []*felt.Felt
ProgramHash *felt.Felt
SemanticVersion string
Compiled *CompiledClass
}
type SegmentLengths struct {
Children []SegmentLengths
Length uint64
}
type CompiledClass struct {
Bytecode []*felt.Felt
PythonicHints json.RawMessage
CompilerVersion string
Hints json.RawMessage
Prime *big.Int
External []CompiledEntryPoint
L1Handler []CompiledEntryPoint
Constructor []CompiledEntryPoint
BytecodeSegmentLengths SegmentLengths
}
type CompiledEntryPoint struct {
Offset uint64
Builtins []string
Selector *felt.Felt
}
type SierraEntryPoint struct {
Index uint64
Selector *felt.Felt
}
func (c *Cairo1Class) Version() uint64 {
return 1
}
func (c *Cairo1Class) Hash() (*felt.Felt, error) {
return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("CONTRACT_CLASS_V"+c.SemanticVersion)),
crypto.PoseidonArray(flattenSierraEntryPoints(c.EntryPoints.External)...),
crypto.PoseidonArray(flattenSierraEntryPoints(c.EntryPoints.L1Handler)...),
crypto.PoseidonArray(flattenSierraEntryPoints(c.EntryPoints.Constructor)...),
c.AbiHash,
c.ProgramHash,
), nil
}
var compiledClassV1Prefix = new(felt.Felt).SetBytes([]byte("COMPILED_CLASS_V1"))
func (c *CompiledClass) Hash() *felt.Felt {
var bytecodeHash *felt.Felt
if len(c.BytecodeSegmentLengths.Children) == 0 {
bytecodeHash = crypto.PoseidonArray(c.Bytecode...)
} else {
bytecodeHash = SegmentedBytecodeHash(c.Bytecode, c.BytecodeSegmentLengths.Children)
}
return crypto.PoseidonArray(
compiledClassV1Prefix,
crypto.PoseidonArray(flattenCompiledEntryPoints(c.External)...),
crypto.PoseidonArray(flattenCompiledEntryPoints(c.L1Handler)...),
crypto.PoseidonArray(flattenCompiledEntryPoints(c.Constructor)...),
bytecodeHash,
)
}
func SegmentedBytecodeHash(bytecode []*felt.Felt, segmentLengths []SegmentLengths) *felt.Felt {
var startingOffset uint64
var digestSegment func(segments []SegmentLengths) (uint64, *felt.Felt)
digestSegment = func(segments []SegmentLengths) (uint64, *felt.Felt) {
var totalLength uint64
var digest crypto.PoseidonDigest
for _, segment := range segments {
var curSegmentLength uint64
var curSegmentHash *felt.Felt
if len(segment.Children) == 0 {
curSegmentLength = segment.Length
segmentBytecode := bytecode[startingOffset : startingOffset+segment.Length]
curSegmentHash = crypto.PoseidonArray(segmentBytecode...)
} else {
curSegmentLength, curSegmentHash = digestSegment(segment.Children)
}
digest.Update(new(felt.Felt).SetUint64(curSegmentLength))
digest.Update(curSegmentHash)
startingOffset += curSegmentLength
totalLength += curSegmentLength
}
digestRes := digest.Finish()
return totalLength, digestRes.Add(digestRes, new(felt.Felt).SetUint64(1))
}
_, hash := digestSegment(segmentLengths)
return hash
}
func flattenSierraEntryPoints(entryPoints []SierraEntryPoint) []*felt.Felt {
result := make([]*felt.Felt, len(entryPoints)*2)
for i, entryPoint := range entryPoints {
// It is important that Selector is first because the order
// influences the class hash.
result[2*i] = entryPoint.Selector
result[2*i+1] = new(felt.Felt).SetUint64(entryPoint.Index)
}
return result
}
func flattenCompiledEntryPoints(entryPoints []CompiledEntryPoint) []*felt.Felt {
result := make([]*felt.Felt, len(entryPoints)*3)
for i, entryPoint := range entryPoints {
// It is important that Selector is first, then Offset is second because the order
// influences the class hash.
result[3*i] = entryPoint.Selector
result[3*i+1] = new(felt.Felt).SetUint64(entryPoint.Offset)
builtins := make([]*felt.Felt, len(entryPoint.Builtins))
for idx, buil := range entryPoint.Builtins {
builtins[idx] = new(felt.Felt).SetBytes([]byte(buil))
}
result[3*i+2] = crypto.PoseidonArray(builtins...)
}
return result
}
func VerifyClassHashes(classes map[felt.Felt]Class) error {
for hash, class := range classes {
if _, ok := class.(*Cairo0Class); ok {
// skip validation of cairo0 class hash
continue
}
cHash, err := class.Hash()
if err != nil {
return err
}
if !cHash.Equal(&hash) {
return fmt.Errorf("cannot verify class hash: calculated hash %v, received hash %v", cHash.String(), hash.String())
}
}
return nil
}