From 90176ce5e5474e859ea9d190f889e6a12fa377c7 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Wed, 29 Nov 2023 14:09:11 +0100 Subject: [PATCH] prover: use PubSignals from circuit.Global() instead of hardcoded indexes --- apiclient/vote.go | 2 +- crypto/zk/prover/prover.go | 60 ++++++++++++---------------------- vochain/transaction/vote_tx.go | 2 +- 3 files changed, 22 insertions(+), 42 deletions(-) diff --git a/apiclient/vote.go b/apiclient/vote.go index 469b2f3be..cd8ce64b6 100644 --- a/apiclient/vote.go +++ b/apiclient/vote.go @@ -113,7 +113,7 @@ func (cl *HTTPclient) Vote(v *VoteData) (types.HexBytes, error) { return nil, err } // include vote nullifier and the encoded proof in a VoteEnvelope - nullifier, err := proof.Nullifier() + nullifier, err := proof.ExtractPubSignal("nullifier") if err != nil { return nil, err } diff --git a/crypto/zk/prover/prover.go b/crypto/zk/prover/prover.go index 4c06b2c31..89f351bc8 100644 --- a/crypto/zk/prover/prover.go +++ b/crypto/zk/prover/prover.go @@ -14,6 +14,7 @@ import ( "github.com/iden3/go-rapidsnark/types" "github.com/iden3/go-rapidsnark/verifier" "github.com/iden3/go-rapidsnark/witness" + "go.vocdoni.io/dvote/crypto/zk/circuit" "go.vocdoni.io/dvote/tree/arbo" ) @@ -21,9 +22,8 @@ import ( // into the error returned. var ( ErrPublicSignalFormat = fmt.Errorf("invalid proof public signals format") - ErrParsingWeight = fmt.Errorf("error parsing proof weight string to big.Int") - ErrParsingNullifier = fmt.Errorf("error parsing proof nullifier string to big.Int") - ErrParsingSIKRoot = fmt.Errorf("error parsing proof sIKRoot string to []byte") + ErrPubSignalNotFound = fmt.Errorf("public signal not found in circuit definition") + ErrParsingProofSignal = fmt.Errorf("error parsing proof signal string to big.Int") ErrParsingWitness = fmt.Errorf("error parsing provided circuit inputs, it must be a not empty marshalled bytes of a json") ErrInitWitnessCalc = fmt.Errorf("error parsing circuit wasm during calculator instance") ErrWitnessCalc = fmt.Errorf("error during witness calculation") @@ -35,10 +35,6 @@ var ( ErrVerifyProof = fmt.Errorf("error during zksnark verification") ) -// DefaultPubSignals constant contains the default number of public signal that -// a proof has. -const DefaultPubSignals = 8 - // ProofData struct contains the calculated parameters of a Proof. It allows to // encode and decode go-rapidsnark inputs and outputs easily. type ProofData struct { @@ -86,51 +82,35 @@ func (p *Proof) Bytes() ([]byte, []byte, error) { return proofData, pubSignals, nil } -// VoteWeight decodes the vote weight value from the current proof public -// signals and return it as a big.Int. -func (p *Proof) VoteWeight() (*big.Int, error) { +// ExtractPubSignal decodes the requested public signal (identified by a string: "nullifier", "sikRoot", etc) +// from the current proof and returns it as a big.Int. +func (p *Proof) ExtractPubSignal(id string) (*big.Int, error) { // Check if the current proof contains public signals and it contains the // correct number of positions. - if p.PubSignals == nil || len(p.PubSignals) != DefaultPubSignals { + if p.PubSignals == nil || len(p.PubSignals) != len(circuit.Global().Config.PublicSignals) { return nil, ErrPublicSignalFormat } - // Get the weight from the fifth public signal of the proof - strWeight := p.PubSignals[7] - // Parse it into a big.Int - weight, ok := new(big.Int).SetString(strWeight, 10) - if !ok { - return nil, ErrParsingWeight - } - return weight, nil -} - -// Nullifier decodes the vote nullifier value from the current proof public -// signals and return it as a big.Int -func (p *Proof) Nullifier() (*big.Int, error) { - if p.PubSignals == nil || len(p.PubSignals) != DefaultPubSignals { - return nil, ErrPublicSignalFormat + idx, found := circuit.Global().Config.PublicSignals[id] + if !found { + return nil, ErrPubSignalNotFound } - // Get the nullifier from the third public signal of the proof - strNullifier := p.PubSignals[2] + s := p.PubSignals[idx] // Parse it into a big.Int - nullifier, ok := new(big.Int).SetString(strNullifier, 10) + i, ok := new(big.Int).SetString(s, 10) if !ok { - return nil, ErrParsingNullifier + return nil, ErrParsingProofSignal } - return nullifier, nil + return i, nil + } -// SIKRoot function returns the sIKRoot included into the current proof. +// SIKRoot function returns the SIKRoot included into the current proof. func (p *Proof) SIKRoot() ([]byte, error) { - if p.PubSignals == nil || len(p.PubSignals) != DefaultPubSignals { - return nil, ErrPublicSignalFormat - } - arboSIK, ok := new(big.Int).SetString(p.PubSignals[5], 10) - if !ok { - return nil, ErrParsingSIKRoot + sikRoot, err := p.ExtractPubSignal("sikRoot") + if err != nil { + return nil, err } - - return arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), arboSIK), nil + return arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), sikRoot), nil } // calcWitness perform the witness calculation using go-rapidsnark library based diff --git a/vochain/transaction/vote_tx.go b/vochain/transaction/vote_tx.go index 17d097f2c..191155e16 100644 --- a/vochain/transaction/vote_tx.go +++ b/vochain/transaction/vote_tx.go @@ -155,7 +155,7 @@ func (t *TransactionHandler) VoteTxCheck(vtx *vochaintx.Tx, forCommit bool) (*vs return nil, fmt.Errorf("expired sik root provided, generate the proof again") } // get vote weight from proof publicSignals - vote.Weight, err = proof.VoteWeight() + vote.Weight, err = proof.ExtractPubSignal("voteWeight") if err != nil { return nil, fmt.Errorf("failed on parsing vote weight from public inputs provided: %w", err) }