Skip to content

Commit

Permalink
prover: use PubSignals from circuit.Global() instead of hardcoded ind…
Browse files Browse the repository at this point in the history
…exes
  • Loading branch information
altergui committed Dec 4, 2023
1 parent 6b09e81 commit 45fe77f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
2 changes: 1 addition & 1 deletion apiclient/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
60 changes: 20 additions & 40 deletions crypto/zk/prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ 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"
)

// TODO: Refactor the error handling to include the trace of the original error
// 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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vochain/transaction/vote_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 45fe77f

Please sign in to comment.