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 36 commits
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
72 changes: 42 additions & 30 deletions compactcert/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,20 @@ func (ccw *Worker) builderForRound(rnd basics.Round) (builder, error) {
}
ccw.Message = msg

p, err := ledger.CompactCertParams(msg, votersHdr, hdr)
provenWeight, err := ledger.GetProvenWeight(votersHdr, hdr)
if err != nil {
return builder{}, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning the error directly like this is problematic. When eventually the error is logged or reported to the user, it will be very difficult, if not impossible, to know where the error came from.
The convention followed is to wrap the received error before reporting it upwards.
For example, see below (which should be %w and not %v):

		ccw.log.Warnf("ccw.handleSigMessage(): decode: %v", err)

}

var res builder
res.votersHdr = votersHdr
res.voters = voters
res.Builder, err = compactcert.MkBuilder(p, voters.Participants, voters.Tree)
res.Builder, err = compactcert.MkBuilder(msg.IntoStateProofMessageHash(),
uint64(hdr.Round),
provenWeight,
voters.Participants,
voters.Tree,
config.Consensus[votersHdr.CurrentProtocol].CompactCertStrengthTarget)
if err != nil {
return builder{}, err
}
Expand All @@ -96,35 +101,40 @@ func (ccw *Worker) initBuilders() {
}

for rnd, sigs := range roundSigs {
_, ok := ccw.builders[rnd]
if ok {
if _, ok := ccw.builders[rnd]; ok {
ccw.log.Warnf("initBuilders: round %d already present", rnd)
continue
}
ccw.addSigsToBuilder(sigs, rnd)
}
}

builder, err := ccw.builderForRound(rnd)
if err != nil {
ccw.log.Warnf("initBuilders: builderForRound(%d): %v", rnd, err)
func (ccw *Worker) addSigsToBuilder(sigs []pendingSig, rnd basics.Round) {
builderForRound, err := ccw.builderForRound(rnd)
if err != nil {
ccw.log.Warnf("initBuilders: builderForRound(%d): %v", rnd, err)
id-ms marked this conversation as resolved.
Show resolved Hide resolved
return
}

for _, sig := range sigs {
pos, ok := builderForRound.voters.AddrToPos[sig.signer]
if !ok {
ccw.log.Warnf("initBuilders: cannot find %v in round %d", sig.signer, rnd)
id-ms marked this conversation as resolved.
Show resolved Hide resolved
continue
}

for _, sig := range sigs {
pos, ok := builder.voters.AddrToPos[sig.signer]
if !ok {
ccw.log.Warnf("initBuilders: cannot find %v in round %d", sig.signer, rnd)
continue
}

if builder.Present(pos) {
ccw.log.Warnf("initBuilders: cannot add %v in round %d: position %d already added", sig.signer, rnd, pos)
continue
}
if isPresent, err := builderForRound.Present(pos); err != nil || isPresent {
ccw.log.Warnf("initBuilders: cannot add %v in round %d: position %d already added", sig.signer, rnd, pos)
id-ms marked this conversation as resolved.
Show resolved Hide resolved
continue
}

if err := builder.IsValid(pos, sig.sig, false); err != nil {
ccw.log.Warnf("initBuilders: cannot add %v in round %d: %v", sig.signer, rnd, err)
continue
}
builder.Add(pos, sig.sig)
if err := builderForRound.IsValid(pos, sig.sig, false); err != nil {
ccw.log.Warnf("initBuilders: cannot add %v in round %d: %v", sig.signer, rnd, err)
id-ms marked this conversation as resolved.
Show resolved Hide resolved
continue
}
if err := builderForRound.Add(pos, sig.sig); err != nil {
ccw.log.Warnf("initBuilders: error while adding sig. inner error: %w", err)
id-ms marked this conversation as resolved.
Show resolved Hide resolved
continue
}
}
}
Expand All @@ -149,7 +159,7 @@ func (ccw *Worker) handleSig(sfa sigFromAddr, sender network.Peer) (network.Forw
ccw.mu.Lock()
defer ccw.mu.Unlock()

builder, ok := ccw.builders[sfa.Round]
builderForRound, ok := ccw.builders[sfa.Round]
if !ok {
latest := ccw.ledger.Latest()
latestHdr, err := ccw.ledger.BlockHdr(latest)
Expand All @@ -163,23 +173,23 @@ func (ccw *Worker) handleSig(sfa sigFromAddr, sender network.Peer) (network.Forw
return network.Ignore, nil
}

builder, err = ccw.builderForRound(sfa.Round)
builderForRound, err = ccw.builderForRound(sfa.Round)
if err != nil {
return network.Disconnect, err
}
}

pos, ok := builder.voters.AddrToPos[sfa.Signer]
pos, ok := builderForRound.voters.AddrToPos[sfa.Signer]
if !ok {
return network.Disconnect, fmt.Errorf("handleSig: %v not in participants for %d", sfa.Signer, sfa.Round)
}

if builder.Present(pos) {
// Signature already part of the builder, ignore.
if isPresent, err := builderForRound.Present(pos); err != nil || isPresent {
// Signature already part of the builderForRound, ignore.
return network.Ignore, nil
}

if err := builder.IsValid(pos, sfa.Sig, true); err != nil {
if err := builderForRound.IsValid(pos, sfa.Sig, true); err != nil {
return network.Disconnect, err
}

Expand All @@ -194,7 +204,9 @@ func (ccw *Worker) handleSig(sfa sigFromAddr, sender network.Peer) (network.Forw
return network.Ignore, err
}
// validated that we can add the sig previously.
builder.Add(pos, sfa.Sig)
if err := builderForRound.Add(pos, sfa.Sig); err != nil {
return network.Ignore, err
}
return network.Broadcast, nil
}

Expand Down
25 changes: 7 additions & 18 deletions compactcert/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,13 @@ func TestWorkerAllSigs(t *testing.T) {
provenWeight, overflowed := basics.Muldiv(uint64(s.totalWeight), uint64(proto.CompactCertWeightThreshold), 1<<32)
require.False(t, overflowed)

ccparams := compactcert.Params{
StateProofMessageHash: tx.Txn.CertMsg.IntoStateProofMessageHash(),
ProvenWeight: provenWeight,
SigRound: tx.Txn.CertIntervalLatestRound,
SecKQ: proto.CompactCertSecKQ,
}

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())
err = verif.Verify(&tx.Txn.Cert)
verif, err := compactcert.MkVerifier(voters.Tree.Root(), provenWeight, proto.CompactCertStrengthTarget)
require.NoError(t, err)

err = verif.Verify(uint64(tx.Txn.CertIntervalLatestRound), tx.Txn.CertMsg.IntoStateProofMessageHash(), &tx.Txn.Cert)
require.NoError(t, err)
break
}
Expand Down Expand Up @@ -358,18 +353,12 @@ func TestWorkerPartialSigs(t *testing.T) {
provenWeight, overflowed := basics.Muldiv(uint64(s.totalWeight), uint64(proto.CompactCertWeightThreshold), 1<<32)
require.False(t, overflowed)

ccparams := compactcert.Params{
StateProofMessageHash: msg.IntoStateProofMessageHash(),
ProvenWeight: provenWeight,
SigRound: basics.Round(tx.Txn.CertIntervalLatestRound),
SecKQ: proto.CompactCertSecKQ,
}

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())
err = verif.Verify(&tx.Txn.Cert)
verif, err := compactcert.MkVerifier(voters.Tree.Root(), provenWeight, proto.CompactCertStrengthTarget)
require.NoError(t, err)
err = verif.Verify(uint64(tx.Txn.CertIntervalLatestRound), msg.IntoStateProofMessageHash(), &tx.Txn.Cert)
require.NoError(t, err)
}

Expand Down
7 changes: 3 additions & 4 deletions config/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,8 @@ type ConsensusParams struct {
// The threshold is computed as CompactCertWeightThreshold/(1<<32).
CompactCertWeightThreshold uint32

// CompactCertSecKQ is the security parameter (k+q) for the compact
// certificate scheme.
CompactCertSecKQ uint64
// CompactCertStrengthTarget represents either k+q (for pre-quantum security) or k+2q (for post-quantum security)
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
CompactCertStrengthTarget uint64

// EnableAssetCloseAmount adds an extra field to the ApplyData. The field contains the amount of the remaining
// asset that were sent to the close-to address.
Expand Down Expand Up @@ -1130,7 +1129,7 @@ func initConsensusProtocols() {
vFuture.CompactCertTopVoters = 1024 * 1024
vFuture.CompactCertVotersLookback = 16
vFuture.CompactCertWeightThreshold = (1 << 32) * 30 / 100
vFuture.CompactCertSecKQ = 128
vFuture.CompactCertStrengthTarget = 256

vFuture.LogicSigVersion = 7

Expand Down
186 changes: 0 additions & 186 deletions crypto/compactcert/bigfloat.go

This file was deleted.

Loading