-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstateTransition.go
148 lines (125 loc) · 5.08 KB
/
stateTransition.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
package circuits
import (
"encoding/json"
"fmt"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-merkletree-sql/v2"
"github.com/pkg/errors"
)
// StateTransitionInputs ZK private inputs for stateTransition.circom
type StateTransitionInputs struct {
BaseConfig
ID *core.ID
OldTreeState TreeState
NewTreeState TreeState
IsOldStateGenesis bool
AuthClaim *core.Claim `json:"claim"`
AuthClaimIncMtp *merkletree.Proof `json:"authClaimIncMtp"`
AuthClaimNonRevMtp *merkletree.Proof `json:"authClaimNonRevMtp"`
AuthClaimNewStateIncMtp *merkletree.Proof `json:"authClaimNewStateIncMtp"`
Signature *babyjub.Signature
}
// stateTransitionInputsInternal type represents stateTransition.circom private inputs required by prover
type stateTransitionInputsInternal struct {
AuthClaim core.Claim `json:"authClaim"`
AuthClaimMtp []string `json:"authClaimMtp"`
AuthClaimNonRevMtp []string `json:"authClaimNonRevMtp"`
AuthClaimNonRevMtpAuxHi *merkletree.Hash `json:"authClaimNonRevMtpAuxHi"`
AuthClaimNonRevMtpAuxHv *merkletree.Hash `json:"authClaimNonRevMtpAuxHv"`
AuthClaimNonRevMtpNoAux string `json:"authClaimNonRevMtpNoAux"`
UserID string `json:"userID"`
NewIdState *merkletree.Hash `json:"newUserState"`
OldIdState *merkletree.Hash `json:"oldUserState"`
IsOldStateGenesis string `json:"isOldStateGenesis"`
ClaimsTreeRoot *merkletree.Hash `json:"claimsTreeRoot"`
RevTreeRoot *merkletree.Hash `json:"revTreeRoot"`
RootsTreeRoot *merkletree.Hash `json:"rootsTreeRoot"`
SignatureR8X string `json:"signatureR8x"`
SignatureR8Y string `json:"signatureR8y"`
SignatureS string `json:"signatureS"`
NewAuthClaimMtp []string `json:"newAuthClaimMtp"`
NewClaimsTreeRoot *merkletree.Hash `json:"newClaimsTreeRoot"`
NewRevTreeRoot *merkletree.Hash `json:"newRevTreeRoot"`
NewRootsTreeRoot *merkletree.Hash `json:"newRootsTreeRoot"`
}
// InputsMarshal returns Circom private inputs for stateTransition.circom
func (c StateTransitionInputs) InputsMarshal() ([]byte, error) {
if c.AuthClaimIncMtp == nil {
return nil, errors.New(ErrorEmptyAuthClaimProof)
}
if c.AuthClaimNewStateIncMtp == nil {
return nil, errors.New(ErrorEmptyAuthClaimInNewStateProof)
}
if c.AuthClaimNonRevMtp == nil {
return nil, errors.New(ErrorEmptyAuthClaimNonRevProof)
}
s := stateTransitionInputsInternal{
AuthClaim: *c.AuthClaim,
AuthClaimMtp: PrepareSiblingsStr(c.AuthClaimIncMtp.AllSiblings(), c.GetMTLevel()),
AuthClaimNonRevMtp: PrepareSiblingsStr(c.AuthClaimNonRevMtp.AllSiblings(), c.GetMTLevel()),
UserID: c.ID.BigInt().String(),
NewIdState: c.NewTreeState.State,
ClaimsTreeRoot: c.OldTreeState.ClaimsRoot,
OldIdState: c.OldTreeState.State,
RevTreeRoot: c.OldTreeState.RevocationRoot,
RootsTreeRoot: c.OldTreeState.RootOfRoots,
SignatureR8X: c.Signature.R8.X.String(),
SignatureR8Y: c.Signature.R8.Y.String(),
SignatureS: c.Signature.S.String(),
NewAuthClaimMtp: PrepareSiblingsStr(c.AuthClaimNewStateIncMtp.AllSiblings(), c.GetMTLevel()),
NewClaimsTreeRoot: c.NewTreeState.ClaimsRoot,
NewRevTreeRoot: c.NewTreeState.RevocationRoot,
NewRootsTreeRoot: c.NewTreeState.RootOfRoots,
}
if c.IsOldStateGenesis {
s.IsOldStateGenesis = "1"
} else {
s.IsOldStateGenesis = "0"
}
nodeAuxAuth := GetNodeAuxValue(c.AuthClaimNonRevMtp)
s.AuthClaimNonRevMtpAuxHi = nodeAuxAuth.key
s.AuthClaimNonRevMtpAuxHv = nodeAuxAuth.value
s.AuthClaimNonRevMtpNoAux = nodeAuxAuth.noAux
return json.Marshal(s)
}
// StateTransitionPubSignals stateTransition.circom public inputs
type StateTransitionPubSignals struct {
UserID *core.ID `json:"userID"`
OldUserState *merkletree.Hash `json:"oldUserState"`
NewUserState *merkletree.Hash `json:"newUserState"`
IsOldStateGenesis bool `json:"isOldStateGenesis"`
}
// PubSignalsUnmarshal unmarshal stateTransition.circom public signals
func (s *StateTransitionPubSignals) PubSignalsUnmarshal(data []byte) error {
var sVals []string
err := json.Unmarshal(data, &sVals)
if err != nil {
return err
}
if len(sVals) != 4 {
return fmt.Errorf("invalid number of Output values expected {%d} got {%d} ", 4, len(sVals))
}
if s.UserID, err = idFromIntStr(sVals[0]); err != nil {
return err
}
if s.OldUserState, err = merkletree.NewHashFromString(sVals[1]); err != nil {
return err
}
if s.NewUserState, err = merkletree.NewHashFromString(sVals[2]); err != nil {
return err
}
switch sVals[3] {
case "1":
s.IsOldStateGenesis = true
case "0":
s.IsOldStateGenesis = false
default:
return fmt.Errorf("invalid value for IsOldStateGenesis {%s}", sVals[3])
}
return nil
}
// GetObjMap returns struct field as a map
func (s StateTransitionPubSignals) GetObjMap() map[string]interface{} {
return toMap(s)
}