Skip to content

Commit

Permalink
update credentialAtomicQueryMTPWithRelay pubInputs marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
demonsh committed May 9, 2022
1 parent aeb5834 commit d1422ff
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 57 deletions.
34 changes: 22 additions & 12 deletions credentialAtomicQueryMTPWithRelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,32 @@ func (a AtomicQueryMTPWithRelayInputs) InputsMarshal() ([]byte, error) {

// AtomicQueryMTPWithRelayPubSignals public signals
type AtomicQueryMTPWithRelayPubSignals struct {
BaseConfig
UserID *core.ID `json:"userID"`
RelayState *merkletree.Hash `json:"relayState"`
Challenge *big.Int `json:"challenge"`
ClaimSchema core.SchemaHash `json:"claimSchema"`
SlotIndex int `json:"slotIndex"`
Operator int `json:"operator"`
Value *big.Int `json:"value"`
Values []*big.Int `json:"value"`
Timestamp int64 `json:"timestamp"`
IssuerID *core.ID `json:"issuerID"`
}

// PubSignalsUnmarshal unmarshal credentialAtomicQueryMTPWithRelay.circom public signals
func (ao *AtomicQueryMTPWithRelayPubSignals) PubSignalsUnmarshal(data []byte) error {
var sVals []string
// 8 is a number of fields in AtomicQueryMTPWithRelayPubSignals before values, values is last element in the proof and
// it is length could be different base on the circuit configuration. The length could be modified by set value
// in ValueArraySize
const fieldLength = 8

err := json.Unmarshal(data, &sVals)
if err != nil {
return err
}

if len(sVals) != 9 {
if len(sVals) != fieldLength+ao.GetValueArrSize() {
return fmt.Errorf("invalid number of Output values expected {%d} go {%d} ", 9, len(sVals))
}

Expand All @@ -181,30 +187,34 @@ func (ao *AtomicQueryMTPWithRelayPubSignals) PubSignalsUnmarshal(data []byte) er
return fmt.Errorf("invalid challenge value: '%s'", sVals[0])
}

var schemaInt *big.Int
if schemaInt, ok = big.NewInt(0).SetString(sVals[3], 10); !ok {
if ao.IssuerID, err = idFromIntStr(sVals[3]); err != nil {
return err
}
ao.ClaimSchema = core.NewSchemaHashFromInt(schemaInt)

if ao.SlotIndex, err = strconv.Atoi(sVals[4]); err != nil {
if ao.Timestamp, err = strconv.ParseInt(sVals[4], 10, 64); err != nil {
return err
}

if ao.Operator, err = strconv.Atoi(sVals[5]); err != nil {
var schemaInt *big.Int
if schemaInt, ok = big.NewInt(0).SetString(sVals[5], 10); !ok {
return err
}
ao.ClaimSchema = core.NewSchemaHashFromInt(schemaInt)

if ao.Value, ok = big.NewInt(0).SetString(sVals[6], 10); !ok {
return fmt.Errorf("invalid challenge value: '%s'", sVals[0])
if ao.SlotIndex, err = strconv.Atoi(sVals[6]); err != nil {
return err
}

if ao.Timestamp, err = strconv.ParseInt(sVals[7], 10, 64); err != nil {
if ao.Operator, err = strconv.Atoi(sVals[7]); err != nil {
return err
}

if ao.IssuerID, err = idFromIntStr(sVals[8]); err != nil {
return err
for i, v := range sVals[fieldLength : fieldLength+ao.GetValueArrSize()] {
bi, ok := big.NewInt(0).SetString(v, 10)
if !ok {
return fmt.Errorf("invalid value in index: %d", i)
}
ao.Values = append(ao.Values, bi)
}

return nil
Expand Down
77 changes: 33 additions & 44 deletions credentialAtomicQueryMTPWithRelay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"math/big"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -252,55 +251,45 @@ func addClaimToTree(tree *merkletree.MerkleTree,
}

func TestAtomicQueryMTPWithRelayOutputs_CircuitUnmarshal(t *testing.T) {
userPrivKHex := "28156abe7fe2fd433dc9df969286b96666489bac508612d0e16593e944c4f69f"
issuerPrivKHex := "21a5e7321d0e2f3ca1cc6504396e6594a2211544b08c206847cdee96f832421a"
challenge := new(big.Int).SetInt64(1)
ctx := context.Background()

userID, uClaimsTree, _, _, err, _, _ := it.Generate(ctx,
userPrivKHex)
assert.Nil(t, err)

relayState, err := merkletree.HashElems(
uClaimsTree.Root().BigInt(),
merkletree.HashZero.BigInt(),
merkletree.HashZero.BigInt())
assert.Nil(t, err)
userID, err := idFromIntStr("379949150130214723420589610911161895495647789006649785264738141299135414272")
assert.NoError(t, err)

// Issuer
issuerID, _, _, _, err, _, _ := it.Generate(ctx,
issuerPrivKHex)
assert.Nil(t, err)
issuerID, err := idFromIntStr("26599707002460144379092755370384635496563807452878989192352627271768342528")
assert.NoError(t, err)

claimSchema, err := core.NewSchemaHashFromHex("ce6bb12c96bfd1544c02c289c6b4b987")
assert.Nil(t, err)
relayStateInt, ok := new(big.Int).SetString(
"4239448240735161374561925497474400621823161116770305241717998726622296721696", 10)
assert.True(t, ok)
relayState, err := merkletree.NewHashFromBigInt(relayStateInt)
assert.NoError(t, err)

slotIndex := "1"
value := "1"
operator := "1"
timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
schemaInt, ok := new(big.Int).SetString("180410020913331409885634153623124536270", 10)
assert.True(t, ok)
schema := core.NewSchemaHashFromInt(schemaInt)

outputsData := []string{
userID.BigInt().String(), relayState.BigInt().String(), challenge.String(), claimSchema.BigInt().String(), slotIndex,
operator, value, timeStamp, issuerID.BigInt().String(),
values := make([]*big.Int, 64)
for i := 0; i < 64; i++ {
values[i] = big.NewInt(0)
}
values[0].SetInt64(10)
values[63].SetInt64(9999)

expectedOut := AtomicQueryMTPWithRelayPubSignals{
UserID: userID,
RelayState: relayState,
Challenge: big.NewInt(1),
ClaimSchema: schema,
SlotIndex: 2,
Operator: 0,
Values: values,
Timestamp: int64(1642074362),
IssuerID: issuerID,
}

data, err := json.Marshal(outputsData)
assert.NoError(t, err)

out := new(AtomicQueryMTPWithRelayPubSignals)
err = out.PubSignalsUnmarshal(data)
assert.NoError(t, err)
err = out.PubSignalsUnmarshal([]byte(
`["379949150130214723420589610911161895495647789006649785264738141299135414272", "4239448240735161374561925497474400621823161116770305241717998726622296721696", "1", "26599707002460144379092755370384635496563807452878989192352627271768342528", "1642074362", "180410020913331409885634153623124536270", "2", "0", "10", "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", "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", "9999"]`))

assert.Equal(t, userID, out.UserID)
assert.Equal(t, relayState, out.RelayState)
assert.Equal(t, challenge, out.Challenge)

assert.Equal(t, claimSchema, out.ClaimSchema)

assert.Equal(t, slotIndex, strconv.Itoa(out.SlotIndex))
assert.Equal(t, operator, strconv.Itoa(out.Operator))
assert.Equal(t, value, out.Value.String())
assert.Equal(t, timeStamp, strconv.FormatInt(out.Timestamp, 10))
assert.Equal(t, issuerID, out.IssuerID)
assert.NoError(t, err)
assert.Equal(t, expectedOut, *out)
}
1 change: 0 additions & 1 deletion credentialAtomicQuerySig.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ func (ao *AtomicQuerySigPubSignals) PubSignalsUnmarshal(data []byte) error {
return err
}

// 22 doesn't include in final slice.
for i, v := range sVals[fieldLength : fieldLength+ao.GetValueArrSize()] {
bi, ok := big.NewInt(0).SetString(v, 10)
if !ok {
Expand Down

0 comments on commit d1422ff

Please sign in to comment.