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
add sequence of coin positions to the cert
  • Loading branch information
algoidan committed Apr 17, 2022
commit 5446caeb9b455ad23ca0c60eeb00a53ed523eb87
6 changes: 5 additions & 1 deletion crypto/compactcert/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,15 @@ func (b *Builder) Build() (*Cert, error) {

var proofPositions []uint64

revealsSequence := make([]uint64, nr)
choice := coinChoiceSeed{
SignedWeight: c.SignedWeight,
ProvenWeight: b.ProvenWeight,
Sigcom: c.SigCommit,
Partcom: b.parttree.Root(),
MsgHash: b.Msg,
}
coinHash := MakeCoinHash(choice)
coinHash := MakeCoinGenerator(choice)

for j := uint64(0); j < nr; j++ {
coin := coinHash.getNextCoin()
Expand All @@ -221,6 +222,8 @@ func (b *Builder) Build() (*Cert, error) {
return nil, fmt.Errorf("pos %d >= len(participants) %d", pos, len(b.participants))
}

revealsSequence[j] = pos

// If we already revealed pos, no need to do it again
_, alreadyRevealed := c.Reveals[pos]
if alreadyRevealed {
Expand Down Expand Up @@ -248,6 +251,7 @@ func (b *Builder) Build() (*Cert, error) {

c.SigProofs = *sigProofs
c.PartProofs = *partProofs
c.PositionsToReveal = revealsSequence
id-ms marked this conversation as resolved.
Show resolved Hide resolved

return c, nil
}
93 changes: 77 additions & 16 deletions crypto/compactcert/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ func generateTestSigner(firstValid uint64, lastValid uint64, interval uint64, a
return signer
}

func TestBuildVerify(t *testing.T) {
partitiontest.PartitionTest(t)

a := require.New(t)
func generateCertForTesting(a *require.Assertions) (*Cert, Params, crypto.GenericDigest, uint64) {
currentRound := basics.Round(compactCertRoundsForTests)
// Doing a full test of 1M accounts takes too much CPU time in CI.
doLargeTest := false
Expand Down Expand Up @@ -118,14 +115,10 @@ func TestBuildVerify(t *testing.T) {
}

partcom, err := merklearray.BuildVectorCommitmentTree(basics.ParticipantsArray(parts), crypto.HashFactory{HashType: HashType})
if err != nil {
t.Error(err)
}
a.NoError(err)

b, err := MkBuilder(param, parts, partcom)
if err != nil {
t.Error(err)
}
a.NoError(err)

for i := 0; i < npart; i++ {
a.False(b.Present(uint64(i)))
id-ms marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -134,9 +127,17 @@ func TestBuildVerify(t *testing.T) {
}

cert, err := b.Build()
if err != nil {
t.Error(err)
}
a.NoError(err)

return cert, param, partcom.Root(), uint64(npart)
}

func TestBuildVerify(t *testing.T) {
partitiontest.PartitionTest(t)

a := require.New(t)

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

var someReveal Reveal
for _, rev := range cert.Reveals {
Expand All @@ -156,9 +157,9 @@ 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.Root())
err = verif.Verify(cert)
require.NoError(t, err, "failed to verify the compact cert")
verif := MkVerifier(param, partCom)
err := verif.Verify(cert)
a.NoError(err, "failed to verify the compact cert")
}

func generateRandomParticipant(a *require.Assertions, testname string) basics.Participant {
Expand Down Expand Up @@ -425,6 +426,66 @@ func findLInCert(a *require.Assertions, signature merklesignature.Signature, cer
return 0
}

func TestCoinIndex(t *testing.T) {
partitiontest.PartitionTest(t)

n := 1000
b := &Builder{
sigs: make([]sigslot, n),
sigsHasValidL: true,
}

for i := 0; i < n; i++ {
b.sigs[i].L = uint64(i)
b.sigs[i].Weight = 1
}

for i := 0; i < n; i++ {
pos, err := b.coinIndex(uint64(i))
require.NoError(t, err)
require.Equal(t, pos, uint64(i))
}
}

func TestBuilder_AddRejectsInvalidSigVersion(t *testing.T) {
id-ms marked this conversation as resolved.
Show resolved Hide resolved
partitiontest.PartitionTest(t)

// setting up a builder
a := require.New(t)
currentRound := basics.Round(compactCertRoundsForTests)

totalWeight := 10000000
npartHi := 1
npartLo := 9

param := Params{
Msg: testMessage("hello world"),
ProvenWeight: uint64(totalWeight / 2),
SigRound: currentRound,
SecKQ: compactCertSecKQForTests,
}

key := generateTestSigner(0, uint64(compactCertRoundsForTests)*20+1, compactCertRoundsForTests, a)
var parts []basics.Participant
parts = append(parts, createParticipantSliceWithWeight(totalWeight, npartHi, key.GetVerifier())...)
parts = append(parts, createParticipantSliceWithWeight(totalWeight, npartLo, key.GetVerifier())...)

partcom, err := merklearray.BuildVectorCommitmentTree(basics.ParticipantsArray(parts), crypto.HashFactory{HashType: HashType})
a.NoError(err)

builder, err := MkBuilder(param, parts, partcom)
a.NoError(err)

// actual test:
signerInRound := key.GetSigner(uint64(currentRound))
sig, err := signerInRound.SignBytes(param.Msg)
require.NoError(t, err, "failed to create keys")
// Corrupting the version of the signature:
sig.Signature[1]++

a.ErrorIs(builder.IsValid(0, sig, true), merklesignature.ErrInvalidSignatureVersion)
}

func BenchmarkBuildVerify(b *testing.B) {
totalWeight := 1000000
npart := 10000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ func (cc coinChoiceSeed) ToBeHashed() (protocol.HashID, []byte) {
return protocol.CompactCertCoin, protocol.Encode(&cc)
}

// coinHashContext is used for extracting "randomized" 64 bits for coin flips
type coinHashContext struct {
// coinGenerator is used for extracting "randomized" 64 bits for coin flips
type coinGenerator struct {
shkContext sha3.ShakeHash
signedWeight uint64
}

// MakeCoinHash creates a new CoinHash context.
// MakeCoinGenerator creates a new CoinHash context.
// it is used for squeezing 64 bits for coin flips.
// the function inits the XOF function in the following manner
// Shake(sumhash(coinChoiceSeed))// we extract 64 bits from shake for each coin flip and divide it by SignedWeight
func MakeCoinHash(choice coinChoiceSeed) coinHashContext {
func MakeCoinGenerator(choice coinChoiceSeed) coinGenerator {
hash := crypto.HashFactory{HashType: CoinHashType}.NewHash()
hashedCoin := crypto.GenericHashObj(hash, choice)

shk := sha3.NewShake256()
shk.Write(hashedCoin)

return coinHashContext{shkContext: shk, signedWeight: choice.SignedWeight}
return coinGenerator{shkContext: shk, signedWeight: choice.SignedWeight}
}

// getNextCoin returns the next 64bits integer which represents a number between [0,SignedWeight)
func (ch *coinHashContext) getNextCoin() uint64 {
func (ch *coinGenerator) getNextCoin() uint64 {
var shakeDigest [64]byte

ch.shkContext.Read(shakeDigest[:])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestHashCoin(t *testing.T) {
Partcom: partcom,
MsgHash: msgHash,
}
coinHash := MakeCoinHash(choice)
coinHash := MakeCoinGenerator(choice)

for j := uint64(0); j < 1000; j++ {
coin := coinHash.getNextCoin()
Expand Down Expand Up @@ -79,7 +79,7 @@ func BenchmarkHashCoin(b *testing.B) {
Partcom: partcom,
MsgHash: msgHash,
}
coinHash := MakeCoinHash(choice)
coinHash := MakeCoinGenerator(choice)

for i := 0; i < b.N; i++ {
coinHash.getNextCoin()
Expand Down
Loading