-
Notifications
You must be signed in to change notification settings - Fork 5
/
authV2.go
176 lines (140 loc) · 5.12 KB
/
authV2.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
package circuits
import (
"encoding/json"
"fmt"
"math/big"
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"
)
// AuthV2Inputs type represent authV2.circom inputs
type AuthV2Inputs struct {
BaseConfig
GenesisID *core.ID `json:"genesisID"`
ProfileNonce *big.Int `json:"profileNonce"`
AuthClaim *core.Claim `json:"authClaim"`
AuthClaimIncMtp *merkletree.Proof `json:"authClaimIncMtp"`
AuthClaimNonRevMtp *merkletree.Proof `json:"authClaimNonRevMtp"`
TreeState TreeState `json:"treeState"`
GISTProof GISTProof `json:"gistProof"`
Signature *babyjub.Signature `json:"signature"`
Challenge *big.Int `json:"challenge"`
}
// authCircuitInputs type reflect auth.circom private inputs required by prover
type authV2CircuitInputs struct {
// ID
GenesisID string `json:"genesisID"`
ProfileNonce string `json:"profileNonce"`
// AuthClaim proof of inclusion
AuthClaim *core.Claim `json:"authClaim"`
AuthClaimMtp []*merkletree.Hash `json:"authClaimIncMtp"`
// AuthClaim non revocation proof
AuthClaimNonRevMtp []*merkletree.Hash `json:"authClaimNonRevMtp"`
AuthClaimNonRevMtpAuxHi *merkletree.Hash `json:"authClaimNonRevMtpAuxHi"`
AuthClaimNonRevMtpAuxHv *merkletree.Hash `json:"authClaimNonRevMtpAuxHv"`
AuthClaimNonRevMtpNoAux string `json:"authClaimNonRevMtpNoAux"`
Challenge string `json:"challenge"`
ChallengeSignatureR8X string `json:"challengeSignatureR8x"`
ChallengeSignatureR8Y string `json:"challengeSignatureR8y"`
ChallengeSignatureS string `json:"challengeSignatureS"`
// User State
ClaimsTreeRoot *merkletree.Hash `json:"claimsTreeRoot"`
RevTreeRoot *merkletree.Hash `json:"revTreeRoot"`
RootsTreeRoot *merkletree.Hash `json:"rootsTreeRoot"`
State *merkletree.Hash `json:"state"`
// Global on-cain state
GISTRoot *merkletree.Hash `json:"gistRoot"`
GISTMtp []*merkletree.Hash `json:"gistMtp"`
GISTMtpAuxHi *merkletree.Hash `json:"gistMtpAuxHi"`
GISTMtpAuxHv *merkletree.Hash `json:"gistMtpAuxHv"`
GISTMtpNoAux string `json:"gistMtpNoAux"`
}
func (a AuthV2Inputs) Validate() error {
if a.GenesisID == nil {
return errors.New(ErrorEmptyID)
}
if a.AuthClaimIncMtp == nil {
return errors.New(ErrorEmptyAuthClaimProof)
}
if a.AuthClaimNonRevMtp == nil {
return errors.New(ErrorEmptyAuthClaimNonRevProof)
}
if a.GISTProof.Proof == nil {
return errors.New(ErrorEmptyGISTProof)
}
if a.Signature == nil {
return errors.New(ErrorEmptyChallengeSignature)
}
if a.Challenge == nil {
return errors.New(ErrorEmptyChallenge)
}
return nil
}
// InputsMarshal returns Circom private inputs for auth.circom
func (a AuthV2Inputs) InputsMarshal() ([]byte, error) {
if err := a.Validate(); err != nil {
return nil, err
}
s := authV2CircuitInputs{
GenesisID: a.GenesisID.BigInt().String(),
ProfileNonce: a.ProfileNonce.String(),
AuthClaim: a.AuthClaim,
AuthClaimMtp: merkletree.CircomSiblingsFromSiblings(a.AuthClaimIncMtp.AllSiblings(),
a.GetMTLevel()-1),
AuthClaimNonRevMtp: merkletree.CircomSiblingsFromSiblings(a.AuthClaimNonRevMtp.AllSiblings(),
a.GetMTLevel()-1),
Challenge: a.Challenge.String(),
ChallengeSignatureR8X: a.Signature.R8.X.String(),
ChallengeSignatureR8Y: a.Signature.R8.Y.String(),
ChallengeSignatureS: a.Signature.S.String(),
ClaimsTreeRoot: a.TreeState.ClaimsRoot,
RevTreeRoot: a.TreeState.RevocationRoot,
RootsTreeRoot: a.TreeState.RootOfRoots,
State: a.TreeState.State,
GISTRoot: a.GISTProof.Root,
GISTMtp: merkletree.CircomSiblingsFromSiblings(a.GISTProof.Proof.AllSiblings(),
a.GetMTLevelOnChain()-1),
}
nodeAuxAuth := GetNodeAuxValue(a.AuthClaimNonRevMtp)
s.AuthClaimNonRevMtpAuxHi = nodeAuxAuth.key
s.AuthClaimNonRevMtpAuxHv = nodeAuxAuth.value
s.AuthClaimNonRevMtpNoAux = nodeAuxAuth.noAux
gistNodeAux := GetNodeAuxValue(a.GISTProof.Proof)
s.GISTMtpAuxHi = gistNodeAux.key
s.GISTMtpAuxHv = gistNodeAux.value
s.GISTMtpNoAux = gistNodeAux.noAux
return json.Marshal(s)
}
// AuthV2PubSignals auth.circom public signals
type AuthV2PubSignals struct {
UserID *core.ID `json:"userID"`
Challenge *big.Int `json:"challenge"`
GISTRoot *merkletree.Hash `json:"GISTRoot"`
}
// PubSignalsUnmarshal unmarshal auth.circom public inputs to AuthPubSignals
func (a *AuthV2PubSignals) PubSignalsUnmarshal(data []byte) error {
var sVals []string
err := json.Unmarshal(data, &sVals)
if err != nil {
return err
}
if len(sVals) != 3 {
return fmt.Errorf("invalid number of Output values expected {%d} got {%d} ", 3, len(sVals))
}
if a.UserID, err = idFromIntStr(sVals[0]); err != nil {
return err
}
var ok bool
if a.Challenge, ok = big.NewInt(0).SetString(sVals[1], 10); !ok {
return fmt.Errorf("invalid challenge value: '%s'", sVals[0])
}
if a.GISTRoot, err = merkletree.NewHashFromString(sVals[2]); err != nil {
return err
}
return nil
}
// GetObjMap returns AuthPubSignals as a map
func (a AuthV2PubSignals) GetObjMap() map[string]interface{} {
return toMap(a)
}