-
Notifications
You must be signed in to change notification settings - Fork 0
/
hints.go
43 lines (39 loc) · 1.27 KB
/
hints.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
package arbo
import (
"fmt"
"math/big"
"github.com/consensys/gnark/constraint/solver"
)
func init() {
solver.RegisterHint(replaceSiblingHint)
}
// replaceSiblingHint gnark hint function receives the new sibling to set as
// first input, the index of the sibling to be replaced as second input, and the
// rest of the siblings as the rest of the inputs. The function should return
// the new siblings with the replacement done. The caller should ensure that the
// the result of the hint is correct checking that every sibling is the same
// that was passed as input except for the sibling with the index provided that
// should be replaced with the new sibling.
func replaceSiblingHint(_ *big.Int, inputs, outputs []*big.Int) error {
if len(inputs) != len(outputs)+2 {
return fmt.Errorf("invalid number of inputs/outputs")
}
// get the new sibling and the index to replace
newSibling := inputs[0]
index := int(inputs[1].Int64())
if index >= len(outputs) {
return fmt.Errorf("invalid index")
}
siblings := inputs[2:]
if len(siblings) != len(outputs) {
return fmt.Errorf("invalid number of siblings")
}
for i := 0; i < len(outputs); i++ {
if i == index {
outputs[i] = outputs[i].Set(newSibling)
} else {
outputs[i] = outputs[i].Set(siblings[i])
}
}
return nil
}