mirror of
https://github.com/arnaucube/arbo.git
synced 2026-01-09 23:41:29 +01:00
Add Tree Circom Verifier Proofs
Circom Verifier Proofs allow to verify through a zkSNARK proof the inclusion/exclusion of a leaf in a tree. This commit adds the needed code in go to generate the circuit inputs for a CircomVerifierProof.
This commit is contained in:
86
circomproofs.go
Normal file
86
circomproofs.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package arbo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// CircomVerifierProof contains the needed data to check a Circom Verifier Proof
|
||||
// inside a circom circuit. CircomVerifierProof allow to verify through a
|
||||
// zkSNARK proof the inclusion/exclusion of a leaf in a tree.
|
||||
type CircomVerifierProof struct {
|
||||
Root []byte `json:"root"`
|
||||
Siblings [][]byte `json:"siblings"`
|
||||
OldKey []byte `json:"oldKey"`
|
||||
OldValue []byte `json:"oldValue"`
|
||||
IsOld0 bool `json:"isOld0"`
|
||||
Key []byte `json:"key"`
|
||||
Value []byte `json:"value"`
|
||||
Fnc int `json:"fnc"` // 0: inclusion, 1: non inclusion
|
||||
}
|
||||
|
||||
// MarshalJSON implements the JSON marshaler
|
||||
func (cvp CircomVerifierProof) MarshalJSON() ([]byte, error) {
|
||||
m := make(map[string]interface{})
|
||||
|
||||
m["root"] = BytesToBigInt(cvp.Root).String()
|
||||
m["siblings"] = siblingsToStringArray(cvp.Siblings)
|
||||
m["oldKey"] = BytesToBigInt(cvp.OldKey).String()
|
||||
m["oldValue"] = BytesToBigInt(cvp.OldValue).String()
|
||||
if cvp.IsOld0 {
|
||||
m["isOld0"] = "1"
|
||||
} else {
|
||||
m["isOld0"] = "0"
|
||||
}
|
||||
m["key"] = BytesToBigInt(cvp.Key).String()
|
||||
m["value"] = BytesToBigInt(cvp.Value).String()
|
||||
m["fnc"] = cvp.Fnc
|
||||
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
func siblingsToStringArray(s [][]byte) []string {
|
||||
var r []string
|
||||
for i := 0; i < len(s); i++ {
|
||||
r = append(r, BytesToBigInt(s[i]).String())
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (t *Tree) fillMissingEmptySiblings(s [][]byte) [][]byte {
|
||||
for i := len(s); i < t.maxLevels; i++ {
|
||||
s = append(s, emptyValue)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// GenerateCircomVerifierProof generates a CircomVerifierProof for a given key
|
||||
// in the Tree
|
||||
func (t *Tree) GenerateCircomVerifierProof(k []byte) (*CircomVerifierProof, error) {
|
||||
kAux, v, siblings, existence, err := t.GenProof(k)
|
||||
if err != nil && err != ErrKeyNotFound {
|
||||
return nil, err
|
||||
}
|
||||
var cp CircomVerifierProof
|
||||
cp.Root = t.Root()
|
||||
s, err := UnpackSiblings(t.hashFunction, siblings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cp.Siblings = t.fillMissingEmptySiblings(s)
|
||||
if !existence {
|
||||
cp.OldKey = kAux
|
||||
cp.OldValue = v
|
||||
} else {
|
||||
cp.OldKey = emptyValue
|
||||
cp.OldValue = emptyValue
|
||||
}
|
||||
cp.Key = k
|
||||
cp.Value = v
|
||||
if existence {
|
||||
cp.Fnc = 0 // inclusion
|
||||
} else {
|
||||
cp.Fnc = 1 // non inclusion
|
||||
}
|
||||
|
||||
return &cp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user