Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stateproof verifier snark friendly #3895

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ebd1016
change coin filps to be shake(sumhash(seed))
Feb 16, 2022
5446cae
add sequence of coin positions to the cert
Mar 10, 2022
4363c91
compute CC security using implied provenWeight
Mar 13, 2022
3ee4616
create a log2 appr func
Mar 15, 2022
6f4de56
fix stateproof message issues.
Mar 15, 2022
92ce531
remove proven weight form the coin hash
Mar 16, 2022
a21f7b7
fix cc test
Mar 17, 2022
f8168fe
use reject sampling in coin hash.
Mar 17, 2022
0afd674
use logarithmic approximation
Mar 27, 2022
b548344
refactoring
Mar 28, 2022
27c4e11
builder uses same appox function
Mar 31, 2022
b4c9be4
comments and doc
Mar 31, 2022
4ce22b6
handle negative value in number of reveals equation
Apr 4, 2022
40a9de2
add lnProvenWe to coinhash
Apr 10, 2022
2f275f3
fixed hash representation for coinhash
Apr 10, 2022
c76184b
fix CC benchmark
Apr 10, 2022
ac4df71
refactor
Apr 10, 2022
8911552
remove old numberofreveals code
Apr 10, 2022
593b62f
change secKQ to 256
Apr 11, 2022
e794180
fix CRs
Apr 12, 2022
c13ce05
CR fixes + rename
Apr 13, 2022
106e6f4
more CR fix
Apr 14, 2022
a3507ad
refactor the trusted params on the verifer.
Apr 14, 2022
3789b44
more refactoring
Apr 17, 2022
ec0543c
fix flaky test
Apr 17, 2022
6e35aad
remove Param structure
Apr 17, 2022
867d2cc
more fixes
Apr 17, 2022
cffa28a
update falcon lib + use byte as salt version
Apr 19, 2022
dd70755
add coinhash kat generator
Apr 20, 2022
485b44d
fix some CR comments
Apr 26, 2022
2cea300
clear out some documentation
Apr 27, 2022
2f03f65
Apply suggestions from code review
id-ms Apr 27, 2022
b566381
fix comments
Apr 28, 2022
ab2a2ff
refactor rejection sampling
Apr 28, 2022
112e5e7
CR fix
May 1, 2022
b7f38d9
refactoring
May 1, 2022
25a33d3
fix comments
May 3, 2022
30ee541
reduce the bytes allocated for stateproof message
May 3, 2022
ed9f141
Apply suggestions from code review
id-ms May 4, 2022
5ae415b
fix test since stateproof message hash was reduce
May 4, 2022
32a8164
fix CR comments
May 8, 2022
9c6a06a
more refactoring
May 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
algoidan committed Apr 17, 2022
commit ac4df7124b036949c37dd69bfd3b6cffeca164b0
11 changes: 7 additions & 4 deletions compactcert/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func TestWorkerAllSigs(t *testing.T) {
require.False(t, overflowed)

ccparams := compactcert.Params{
StateProofMessageHash: tx.Txn.CertMsg.IntoStateProofMessageHash(),
MessageHash: tx.Txn.CertMsg.IntoStateProofMessageHash(),
ProvenWeightThreshold: provenWeight,
SigRound: tx.Txn.CertIntervalLatestRound,
SecKQ: proto.CompactCertSecKQ,
Expand All @@ -304,7 +304,9 @@ func TestWorkerAllSigs(t *testing.T) {
voters, err := s.CompactCertVoters(tx.Txn.CertIntervalLatestRound - basics.Round(proto.CompactCertRounds) - basics.Round(proto.CompactCertVotersLookback))
require.NoError(t, err)

verif := compactcert.MkVerifier(ccparams, voters.Tree.Root())
verif, err := compactcert.MkVerifier(ccparams, voters.Tree.Root())
require.NoError(t, err)

err = verif.Verify(&tx.Txn.Cert)
require.NoError(t, err)
break
Expand Down Expand Up @@ -359,7 +361,7 @@ func TestWorkerPartialSigs(t *testing.T) {
require.False(t, overflowed)

ccparams := compactcert.Params{
StateProofMessageHash: msg.IntoStateProofMessageHash(),
MessageHash: msg.IntoStateProofMessageHash(),
ProvenWeightThreshold: provenWeight,
SigRound: basics.Round(tx.Txn.CertIntervalLatestRound),
SecKQ: proto.CompactCertSecKQ,
Expand All @@ -368,7 +370,8 @@ func TestWorkerPartialSigs(t *testing.T) {
voters, err := s.CompactCertVoters(tx.Txn.CertIntervalLatestRound - basics.Round(proto.CompactCertRounds) - basics.Round(proto.CompactCertVotersLookback))
require.NoError(t, err)

verif := compactcert.MkVerifier(ccparams, voters.Tree.Root())
verif, err := compactcert.MkVerifier(ccparams, voters.Tree.Root())
require.NoError(t, err)
err = verif.Verify(&tx.Txn.Cert)
require.NoError(t, err)
}
Expand Down
15 changes: 9 additions & 6 deletions crypto/compactcert/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ type Builder struct {
sigs []sigslot // Indexed by pos in participants
sigsHasValidL bool // The L values in sigs are consistent with weights
signedWeight uint64 // Total weight of signatures so far
lnProvenWeightThreshold uint64 // ln(provenWeightThreshold) as integer with 16 bits of precision
participants []basics.Participant
parttree *merklearray.Tree
lnProvenWeightThreshold uint64

// Cached cert, if Build() was called and no subsequent
// Add() calls were made.
Expand All @@ -59,7 +59,10 @@ type Builder struct {
// parttree.
func MkBuilder(param Params, part []basics.Participant, parttree *merklearray.Tree) (*Builder, error) {
npart := len(part)
lnProvenWt := lnIntApproximation(param.ProvenWeightThreshold, precisionBits)
lnProvenWt, err := lnIntApproximation(param.ProvenWeightThreshold, precisionBits)
if err != nil {
return nil, err
}

b := &Builder{
Params: param,
Expand Down Expand Up @@ -101,8 +104,8 @@ func (b *Builder) IsValid(pos uint64, sig merklesignature.Signature, verifySig b
return err
}

cpy := make([]byte, len(b.Params.StateProofMessageHash))
copy(cpy, b.Params.StateProofMessageHash[:]) // TODO: onmce cfalcon is fixed can remove this copy.
cpy := make([]byte, len(b.Params.MessageHash))
copy(cpy, b.Params.MessageHash[:]) // TODO: onmce cfalcon is fixed can remove this copy.
if err := p.PK.VerifyBytes(uint64(b.SigRound), cpy, sig); err != nil {
return err
}
Expand Down Expand Up @@ -196,7 +199,7 @@ func (b *Builder) Build() (*Cert, error) {
MerkleSignatureVersion: merklesignature.SchemeVersion,
}

nr, err := b.numReveals()
nr, err := numReveals(b.signedWeight, b.lnProvenWeightThreshold, b.SecKQ)
if err != nil {
return nil, err
}
Expand All @@ -206,7 +209,7 @@ func (b *Builder) Build() (*Cert, error) {
revealsSequence := make([]uint64, nr)

choice := coinChoiceSeed{
msgHash: b.StateProofMessageHash,
msgHash: b.Params.MessageHash,
lnProvenWeightThreshold: b.lnProvenWeightThreshold,
signedWeight: c.SignedWeight,
sigCommitment: c.SigCommit,
Expand Down
36 changes: 24 additions & 12 deletions crypto/compactcert/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func generateCertForTesting(a *require.Assertions) (*Cert, Params, crypto.Generi
npart := npartHi + npartLo

param := Params{
StateProofMessageHash: testMessage("hello world").IntoStateProofMessageHash(),

MessageHash: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeightThreshold: uint64(totalWeight / 2),
SigRound: currentRound,
SecKQ: compactCertSecKQForTests,
Expand All @@ -107,7 +106,7 @@ func generateCertForTesting(a *require.Assertions) (*Cert, Params, crypto.Generi
parts = append(parts, createParticipantSliceWithWeight(totalWeight, npartLo, key.GetVerifier())...)

signerInRound := key.GetSigner(uint64(currentRound))
sig, err := signerInRound.SignBytes(param.StateProofMessageHash[:])
sig, err := signerInRound.SignBytes(param.MessageHash[:])
a.NoError(err, "failed to create keys")

for i := 0; i < npart; i++ {
Expand Down Expand Up @@ -157,8 +156,10 @@ func TestBuildVerify(t *testing.T) {
fmt.Printf(" %6d bytes reveals[*] total\n", len(protocol.Encode(&someReveal)))
fmt.Printf(" %6d bytes total\n", len(certenc))

verif := MkVerifier(param, partCom)
err := verif.Verify(cert)
verif, err := MkVerifier(param, partCom)
a.NoError(err)

err = verif.Verify(cert)
a.NoError(err, "failed to verify the compact cert")
}

Expand Down Expand Up @@ -238,7 +239,7 @@ func TestSignatureCommitmentBinaryFormat(t *testing.T) {
numPart := 4

param := Params{
StateProofMessageHash: testMessage("test!").IntoStateProofMessageHash(),
MessageHash: testMessage("test!").IntoStateProofMessageHash(),
ProvenWeightThreshold: uint64(totalWeight / (2 * numPart)),
SigRound: currentRound,
SecKQ: compactCertSecKQForTests,
Expand All @@ -256,7 +257,7 @@ func TestSignatureCommitmentBinaryFormat(t *testing.T) {
}
parts = append(parts, part)

sig, err := key.GetSigner(uint64(currentRound)).SignBytes(param.StateProofMessageHash[:])
sig, err := key.GetSigner(uint64(currentRound)).SignBytes(param.MessageHash[:])
require.NoError(t, err, "failed to create keys")
sigs = append(sigs, sig)

Expand Down Expand Up @@ -438,7 +439,7 @@ func TestBuilder_AddRejectsInvalidSigVersion(t *testing.T) {
npartLo := 9

param := Params{
StateProofMessageHash: testMessage("hello world").IntoStateProofMessageHash(),
MessageHash: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeightThreshold: uint64(totalWeight / 2),
SigRound: currentRound,
SecKQ: compactCertSecKQForTests,
Expand All @@ -457,7 +458,7 @@ func TestBuilder_AddRejectsInvalidSigVersion(t *testing.T) {

// actual test:
signerInRound := key.GetSigner(uint64(currentRound))
sig, err := signerInRound.SignBytes(param.StateProofMessageHash[:])
sig, err := signerInRound.SignBytes(param.MessageHash[:])
require.NoError(t, err, "failed to create keys")
// Corrupting the version of the signature:
sig.Signature[1]++
Expand Down Expand Up @@ -486,6 +487,17 @@ func TestCoinIndex(t *testing.T) {
}
}

func TestBuilderWithZeroProvenWeight(t *testing.T) {
partitiontest.PartitionTest(t)
a := require.New(t)

p := Params{ProvenWeightThreshold: 0}

_, err := MkBuilder(p, nil, nil)
a.ErrorIs(err, ErrIllegalInputForLnApprox)

}

func BenchmarkBuildVerify(b *testing.B) {
totalWeight := 1000000
npart := 1000
Expand All @@ -494,7 +506,7 @@ func BenchmarkBuildVerify(b *testing.B) {
a := require.New(b)

param := Params{
StateProofMessageHash: testMessage("hello world").IntoStateProofMessageHash(),
MessageHash: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeightThreshold: uint64(totalWeight / 2),
SigRound: compactCertRoundsForTests,
SecKQ: compactCertSecKQForTests,
Expand All @@ -511,7 +523,7 @@ func BenchmarkBuildVerify(b *testing.B) {
}

signerInRound := signer.GetSigner(uint64(currentRound))
sig, err := signerInRound.SignBytes(param.StateProofMessageHash[:])
sig, err := signerInRound.SignBytes(param.MessageHash[:])
require.NoError(b, err, "failed to create keys")

partkeys = append(partkeys, signer)
Expand Down Expand Up @@ -547,7 +559,7 @@ func BenchmarkBuildVerify(b *testing.B) {

b.Run("Verify", func(b *testing.B) {
for i := 0; i < b.N; i++ {
verif := MkVerifier(param, partcom.Root())
verif, _ := MkVerifier(param, partcom.Root())
if err = verif.Verify(cert); err != nil {
b.Error(err)
}
Expand Down
3 changes: 1 addition & 2 deletions crypto/compactcert/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ const StateProofMessageHashType = crypto.Sha256

// Params defines common parameters for the verifier and builder.
type Params struct {
StateProofMessageHash

MessageHash StateProofMessageHash
ProvenWeightThreshold uint64 // Weight threshold proven by the certificate
SigRound basics.Round // The round for which the ephemeral key is committed to
SecKQ uint64 // Security parameter (k+q) from analysis document
Expand Down
38 changes: 23 additions & 15 deletions crypto/compactcert/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package compactcert
import (
"errors"
"fmt"

"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/crypto/merklearray"
"github.com/algorand/go-algorand/data/basics"
)

// Errors for the CompactCert verifier
Expand All @@ -31,30 +33,36 @@ var (

// Verifier is used to verify a compact certificate.
type Verifier struct {
Params

lnProvenWeightThreshold uint64 // ln(provenWeightThreshold) as integer with 16 bits of precision
partcom crypto.GenericDigest
MessageHash StateProofMessageHash
SigRound basics.Round // The round for which the ephemeral key is committed to
SecKQ uint64 // Security parameter (k+q) from analysis document
lnProvenWeightThreshold uint64 // ln(provenWeightThreshold) as integer with 16 bits of precision
participantsCommitment crypto.GenericDigest
}

// MkVerifier constructs a verifier to check the compact certificate
// on the message specified in p, with partcom specifying the Merkle
// on the message specified in p, with participantsCommitment specifying the Merkle
// root of the participants that must sign the message.
func MkVerifier(p Params, partcom crypto.GenericDigest) *Verifier {
lnProvenWt := lnIntApproximation(p.ProvenWeightThreshold, precisionBits)
func MkVerifier(p Params, partcom crypto.GenericDigest) (*Verifier, error) {
lnProvenWt, err := lnIntApproximation(p.ProvenWeightThreshold, precisionBits)
if err != nil {
return nil, err
}

return &Verifier{
Params: p,
MessageHash: p.MessageHash,
SigRound: p.SigRound,
SecKQ: p.SecKQ,
lnProvenWeightThreshold: lnProvenWt,
partcom: partcom,
}
participantsCommitment: partcom,
}, nil
}

// Verify checks if c is a valid compact certificate for the message
// and participants that were used to construct the Verifier.
func (v *Verifier) Verify(c *Cert) error {
nr := uint64(len(c.PositionsToReveal))
if err := v.verifyWeights(c.SignedWeight, nr); err != nil {
if err := verifyWeights(c.SignedWeight, v.lnProvenWeightThreshold, nr, v.SecKQ); err != nil {
return err
}

Expand All @@ -68,7 +76,7 @@ func (v *Verifier) Verify(c *Cert) error {
sigs := make(map[uint64]crypto.Hashable)
parts := make(map[uint64]crypto.Hashable)

msghash := v.Params.StateProofMessageHash
msghash := v.MessageHash
for pos, r := range c.Reveals {
sig, err := buildCommittableSignature(r.SigSlot)
if err != nil {
Expand Down Expand Up @@ -96,16 +104,16 @@ func (v *Verifier) Verify(c *Cert) error {
}

// verify all the reveals proofs on the participant tree.
if err := merklearray.VerifyVectorCommitment(v.partcom[:], parts, &c.PartProofs); err != nil {
if err := merklearray.VerifyVectorCommitment(v.participantsCommitment[:], parts, &c.PartProofs); err != nil {
return err
}

choice := coinChoiceSeed{
msgHash: v.StateProofMessageHash,
msgHash: v.MessageHash,
lnProvenWeightThreshold: v.lnProvenWeightThreshold,
signedWeight: c.SignedWeight,
sigCommitment: c.SigCommit,
partCommitment: v.partcom,
partCommitment: v.participantsCommitment,
}

coinHash := makeCoinGenerator(&choice)
Expand Down
40 changes: 31 additions & 9 deletions crypto/compactcert/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/crypto/merklesignature"
"github.com/algorand/go-algorand/test/partitiontest"
)
Expand All @@ -31,8 +32,10 @@ func TestVerifyRevelForEachPosition(t *testing.T) {

cert, param, partCom, numPart := generateCertForTesting(a)

verifier := MkVerifier(param, partCom)
err := verifier.Verify(cert)
verifier, err := MkVerifier(param, partCom)
a.NoError(err)

err = verifier.Verify(cert)
a.NoError(err)

for i := uint64(0); i < numPart; i++ {
Expand All @@ -43,7 +46,9 @@ func TestVerifyRevelForEachPosition(t *testing.T) {
}
}

verifier = MkVerifier(param, partCom)
verifier, err = MkVerifier(param, partCom)
a.NoError(err)

err = verifier.Verify(cert)
a.ErrorIs(err, ErrNoRevealInPos)

Expand All @@ -55,15 +60,19 @@ func TestVerifyWrongCoinSlot(t *testing.T) {

cert, param, partCom, _ := generateCertForTesting(a)

verifier := MkVerifier(param, partCom)
err := verifier.Verify(cert)
verifier, err := MkVerifier(param, partCom)
a.NoError(err)

err = verifier.Verify(cert)
a.NoError(err)

swap := cert.PositionsToReveal[1]
cert.PositionsToReveal[1] = cert.PositionsToReveal[0]
cert.PositionsToReveal[0] = swap

verifier = MkVerifier(param, partCom)
verifier, err = MkVerifier(param, partCom)
a.NoError(err)

err = verifier.Verify(cert)
a.ErrorIs(err, ErrCoinNotInRange)
}
Expand All @@ -74,14 +83,27 @@ func TestVerifyBadSignature(t *testing.T) {

cert, param, partCom, _ := generateCertForTesting(a)

verifier := MkVerifier(param, partCom)
err := verifier.Verify(cert)
verifier, err := MkVerifier(param, partCom)
a.NoError(err)
err = verifier.Verify(cert)
a.NoError(err)

rev := cert.Reveals[cert.PositionsToReveal[0]]
rev.SigSlot.Sig.Signature[10]++

verifier = MkVerifier(param, partCom)
verifier, err = MkVerifier(param, partCom)
a.NoError(err)
err = verifier.Verify(cert)
a.ErrorIs(err, merklesignature.ErrSignatureSchemeVerificationFailed)
}

func TestVerifyZeroProvenWeight(t *testing.T) {
partitiontest.PartitionTest(t)
a := require.New(t)

p := Params{ProvenWeightThreshold: 0}
partcommit := crypto.GenericDigest{}

_, err := MkVerifier(p, partcommit)
a.ErrorIs(err, ErrIllegalInputForLnApprox)
}
Loading