-
Notifications
You must be signed in to change notification settings - Fork 353
/
blueprint_logderivlookup.go
111 lines (95 loc) · 2.99 KB
/
blueprint_logderivlookup.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
package constraint
import (
"fmt"
)
// TODO @gbotrel this shouldn't be there, but we need to figure out a clean way to serialize
// blueprints
// BlueprintLookupHint is a blueprint that facilitates the lookup of values in a table.
// It is essentially a hint to the solver, but enables storing the table entries only once.
type BlueprintLookupHint struct {
EntriesCalldata []uint32
}
// ensures BlueprintLookupHint implements the BlueprintSolvable interface
var _ BlueprintSolvable = (*BlueprintLookupHint)(nil)
func (b *BlueprintLookupHint) Solve(s Solver, inst Instruction) error {
nbEntries := int(inst.Calldata[1])
entries := make([]Element, nbEntries)
// read the static entries from the blueprint
// TODO @gbotrel cache that.
offset, delta := 0, 0
for i := 0; i < nbEntries; i++ {
entries[i], delta = s.Read(b.EntriesCalldata[offset:])
offset += delta
}
nbInputs := int(inst.Calldata[2])
// read the inputs from the instruction
inputs := make([]Element, nbInputs)
offset = 3
for i := 0; i < nbInputs; i++ {
inputs[i], delta = s.Read(inst.Calldata[offset:])
offset += delta
}
// set the outputs
nbOutputs := nbInputs
for i := 0; i < nbOutputs; i++ {
idx, isUint64 := s.Uint64(inputs[i])
if !isUint64 || idx >= uint64(len(entries)) {
return fmt.Errorf("lookup query too large")
}
// we set the output wire to the value of the entry
s.SetValue(uint32(i+int(inst.WireOffset)), entries[idx])
}
return nil
}
func (b *BlueprintLookupHint) CalldataSize() int {
// variable size
return -1
}
func (b *BlueprintLookupHint) NbConstraints() int {
return 0
}
// NbOutputs return the number of output wires this blueprint creates.
func (b *BlueprintLookupHint) NbOutputs(inst Instruction) int {
return int(inst.Calldata[2])
}
// Wires returns a function that walks the wires appearing in the blueprint.
// This is used by the level builder to build a dependency graph between instructions.
func (b *BlueprintLookupHint) WireWalker(inst Instruction) func(cb func(wire uint32)) {
return func(cb func(wire uint32)) {
// depend on the table UP to the number of entries at time of instruction creation.
nbEntries := int(inst.Calldata[1])
// invoke the callback on each wire appearing in the table
j := 0
for i := 0; i < nbEntries; i++ {
// first we have the length of the linear expression
n := int(b.EntriesCalldata[j])
j++
for k := 0; k < n; k++ {
t := Term{CID: b.EntriesCalldata[j], VID: b.EntriesCalldata[j+1]}
if !t.IsConstant() {
cb(t.VID)
}
j += 2
}
}
// invoke the callback on each wire appearing in the inputs
nbInputs := int(inst.Calldata[2])
j = 3
for i := 0; i < nbInputs; i++ {
// first we have the length of the linear expression
n := int(inst.Calldata[j])
j++
for k := 0; k < n; k++ {
t := Term{CID: inst.Calldata[j], VID: inst.Calldata[j+1]}
if !t.IsConstant() {
cb(t.VID)
}
j += 2
}
}
// finally we have the outputs
for i := 0; i < nbInputs; i++ {
cb(uint32(i + int(inst.WireOffset)))
}
}
}