Skip to content

Commit

Permalink
Feature/update state transition pub inputs (#59)
Browse files Browse the repository at this point in the history
* Rename nonce to profileNonce

* Rename nonce to profileNonce

* Generate test data. Rename file with test data for mtp

* Add IsOldStateGenesis to stateTransition pub signals

* Update test TestStateTransitionInputs
  • Loading branch information
demonsh authored Jan 4, 2023
1 parent 2e0df33 commit 94f22e3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 69 deletions.
21 changes: 16 additions & 5 deletions stateTransition.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ func (c StateTransitionInputs) InputsMarshal() ([]byte, error) {

// StateTransitionPubSignals stateTransition.circom public inputs
type StateTransitionPubSignals struct {
UserID *core.ID `json:"userID"`
OldUserState *merkletree.Hash `json:"oldUserState"`
NewUserState *merkletree.Hash `json:"newUserState"`
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
Expand All @@ -102,8 +103,8 @@ func (s *StateTransitionPubSignals) PubSignalsUnmarshal(data []byte) error {
return err
}

if len(sVals) != 3 {
return fmt.Errorf("invalid number of Output values expected {%d} got {%d} ", 3, len(sVals))
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 {
Expand All @@ -115,6 +116,16 @@ func (s *StateTransitionPubSignals) PubSignalsUnmarshal(data []byte) error {
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
}

Expand Down
100 changes: 36 additions & 64 deletions stateTransition_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package circuits

import (
"context"
"encoding/json"
"math/big"
"testing"

it "github.com/iden3/go-circuits/testing"
core "github.com/iden3/go-iden3-core"
"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/iden3/go-merkletree-sql/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestStateTransitionOutput_GetJSONObj(t *testing.T) {
Expand All @@ -33,69 +32,42 @@ func TestStateTransitionOutput_GetJSONObj(t *testing.T) {
}

func TestStateTransitionInputs_InputsMarshal(t *testing.T) {
userPK := "21a5e7321d0e2f3ca1cc6504396e6594a2211544b08c206847cdee96f832421a"
ctx := context.Background()

// Issuer
id, claimsTree, revTree, _, err, authClaim, userPrivKey := it.Generate(ctx,
userPK)
assert.Nil(t, err)

genesisState, err := merkletree.HashElems(
claimsTree.Root().BigInt(),
revTree.Root().BigInt(),
merkletree.HashZero.BigInt())
assert.Nil(t, err)

genesisTreeState := TreeState{
State: genesisState,
ClaimsRoot: claimsTree.Root(),
RevocationRoot: revTree.Root(),
RootOfRoots: &merkletree.HashZero,
out := new(StateTransitionPubSignals)
err := out.PubSignalsUnmarshal([]byte(`
[
"23148936466334350744548790012294489365207440754509988986684797708370051073",
"7115004997868594253010848596868364067574661249707337517331323113105592633327",
"4546963942567895423749885008322935416520496550192665955639269179690288593086",
"0"
]`))
require.NoError(t, err)

userIDStr, b := new(big.Int).SetString(
"23148936466334350744548790012294489365207440754509988986684797708370051073", 10)
assert.True(t, b)
userID, err := core.IDFromInt(userIDStr)
require.NoError(t, err)

oldUserState, err := merkletree.NewHashFromString(
"7115004997868594253010848596868364067574661249707337517331323113105592633327")
require.NoError(t, err)

newUserState, err := merkletree.NewHashFromString(
"4546963942567895423749885008322935416520496550192665955639269179690288593086")
require.NoError(t, err)

exp := StateTransitionPubSignals{
UserID: &userID,
OldUserState: oldUserState,
NewUserState: newUserState,
IsOldStateGenesis: false,
}

index, err := authClaim.HIndex()
assert.Nil(t, err)
authMTPProof, _, err := claimsTree.GenerateProof(ctx, index,
claimsTree.Root())
assert.Nil(t, err)

nonce := new(big.Int).SetUint64(authClaim.GetRevocationNonce())
authNonRevMTPProof, _, err := revTree.GenerateProof(ctx, nonce,
revTree.Root())
assert.Nil(t, err)

// update rev tree
err = revTree.Add(ctx, big.NewInt(1), big.NewInt(0))
assert.Nil(t, err)

newState, err := merkletree.HashElems(
claimsTree.Root().BigInt(),
revTree.Root().BigInt(),
merkletree.HashZero.BigInt())
assert.Nil(t, err)

// signature
hashOldAndNewStates, err := poseidon.Hash(
[]*big.Int{genesisState.BigInt(), newState.BigInt()})
assert.Nil(t, err)
signature := userPrivKey.SignPoseidon(hashOldAndNewStates)

sti := StateTransitionInputs{
ID: id,
OldTreeState: genesisTreeState,
NewState: newState,
IsOldStateGenesis: true,
AuthClaim: authClaim,
AuthClaimIncMtp: authMTPProof,
AuthClaimNonRevMtp: authNonRevMTPProof,
Signature: signature,
}

inputBytes, err := sti.InputsMarshal()
assert.Nil(t, err)

expectedJSONInputs := `{"authClaim":["304427537360709784173770334266246861770","0","9582165609074695838007712438814613121302719752874385708394134542816240804696","18271435592817415588213874506882839610978320325722319742324814767882756910515","11203087622270641253","0","0","0"],"authClaimMtp":["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],"authClaimNonRevMtp":["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],"authClaimNonRevMtpAuxHi":"0","authClaimNonRevMtpAuxHv":"0","authClaimNonRevMtpNoAux":"1","userID":"24839761684028550613296892625503994006188774664975540620786183594699522048","newUserState":"7569111473237253646417788189126468973900432716598921661470118514516731079797","oldUserState":"6317996369756476782464660619835940615734517981889733696047139451453239145426","isOldStateGenesis":"1","claimsTreeRoot":"18337129644116656308842422695567930755039142442806278977230099338026575870840","revTreeRoot":"0","rootsTreeRoot":"0","signatureR8x":"9484102035827996121666608170002743002783492772260590322761477321381254509037","signatureR8y":"19295134567339498210855406074518612682643335122341225376941332925036431891102","signatureS":"282291664505682519059669624505331509305429004374837545959385601323093440910"}`
jsonOut, err := json.Marshal(out)
require.NoError(t, err)
jsonExp, err := json.Marshal(exp)
require.NoError(t, err)

assert.JSONEq(t, expectedJSONInputs, string(inputBytes))
require.JSONEq(t, string(jsonExp), string(jsonOut))
}

0 comments on commit 94f22e3

Please sign in to comment.