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
fix CRs
  • Loading branch information
algoidan committed Apr 17, 2022
commit e7941801674596a2c9f88c4eeb073b48d906ccf3
16 changes: 8 additions & 8 deletions compactcert/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ func TestWorkerAllSigs(t *testing.T) {
require.False(t, overflowed)

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

voters, err := s.CompactCertVoters(tx.Txn.CertIntervalLatestRound - basics.Round(proto.CompactCertRounds) - basics.Round(proto.CompactCertVotersLookback))
Expand Down Expand Up @@ -361,10 +361,10 @@ func TestWorkerPartialSigs(t *testing.T) {
require.False(t, overflowed)

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

voters, err := s.CompactCertVoters(tx.Txn.CertIntervalLatestRound - basics.Round(proto.CompactCertRounds) - basics.Round(proto.CompactCertVotersLookback))
Expand Down
50 changes: 25 additions & 25 deletions crypto/compactcert/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ type sigslot struct {
// a compact certificate for that message.
type Builder struct {
Params
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
participants []basics.Participant
parttree *merklearray.Tree
lnProvenWeightThreshold uint64
sigs []sigslot // Indexed by pos in participants
sigsHasValidL bool // The L values in sigs are consistent with weights
id-ms marked this conversation as resolved.
Show resolved Hide resolved
signedWeight uint64 // Total weight of signatures so far
participants []basics.Participant
parttree *merklearray.Tree
lnProvenWeight uint64

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

b := &Builder{
Params: param,

sigs: make([]sigslot, npart),
sigsHasValidL: false,
signedWeight: 0,
participants: part,
parttree: parttree,
lnProvenWeightThreshold: lnProvenWt,
sigs: make([]sigslot, npart),
sigsHasValidL: false,
signedWeight: 0,
participants: part,
parttree: parttree,
lnProvenWeight: lnProvenWt,
}

return b, nil
id-ms marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -104,9 +104,9 @@ func (b *Builder) IsValid(pos uint64, sig merklesignature.Signature, verifySig b
return err
}

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 {
cpy := make([]byte, len(b.Params.Data))
copy(cpy, b.Params.Data[:]) // TODO: onmce cfalcon is fixed can remove this copy.
if err := p.PK.VerifyBytes(uint64(b.Round), cpy, sig); err != nil {
return err
}
}
Expand All @@ -127,7 +127,7 @@ func (b *Builder) Add(pos uint64, sig merklesignature.Signature) {

id-ms marked this conversation as resolved.
Show resolved Hide resolved
// Ready returns whether the certificate is ready to be built.
func (b *Builder) Ready() bool {
id-ms marked this conversation as resolved.
Show resolved Hide resolved
return b.signedWeight > b.Params.ProvenWeightThreshold
return b.signedWeight > b.Params.ProvenWeight
}

// SignedWeight returns the total weight of signatures added so far.
Expand Down Expand Up @@ -175,8 +175,8 @@ func (b *Builder) Build() (*Cert, error) {
return b.cert, nil
}

if b.signedWeight <= b.Params.ProvenWeightThreshold {
return nil, fmt.Errorf("%w: %d <= %d", ErrSignedWeightLessThanProvenWeight, b.signedWeight, b.Params.ProvenWeightThreshold)
if b.signedWeight <= b.Params.ProvenWeight {
return nil, fmt.Errorf("%w: %d <= %d", ErrSignedWeightLessThanProvenWeight, b.signedWeight, b.Params.ProvenWeight)
}

// Commit to the sigs array
Expand All @@ -199,7 +199,7 @@ func (b *Builder) Build() (*Cert, error) {
MerkleSignatureVersion: merklesignature.SchemeVersion,
}

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

choice := coinChoiceSeed{
msgHash: b.Params.MessageHash,
lnProvenWeightThreshold: b.lnProvenWeightThreshold,
signedWeight: c.SignedWeight,
sigCommitment: c.SigCommit,
partCommitment: b.parttree.Root(),
data: b.Params.Data,
lnProvenWeight: b.lnProvenWeight,
signedWeight: c.SignedWeight,
sigCommitment: c.SigCommit,
partCommitment: b.parttree.Root(),
}

coinHash := makeCoinGenerator(&choice)
Expand Down
45 changes: 22 additions & 23 deletions crypto/compactcert/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func generateCertForTesting(a *require.Assertions) (*Cert, Params, crypto.Generi
// Doing a full test of 1M accounts takes too much CPU time in CI.
doLargeTest := false

totalWeight := 10
000
totalWeight := 10000000
npartHi := 10
npartLo := 9990

Expand All @@ -93,10 +92,10 @@ func generateCertForTesting(a *require.Assertions) (*Cert, Params, crypto.Generi
npart := npartHi + npartLo

param := Params{
MessageHash: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeightThreshold: uint64(totalWeight / 2),
SigRound: currentRound,
SecKQ: compactCertSecKQForTests,
Data: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeight: uint64(totalWeight / 2),
Round: currentRound,
SecKQ: compactCertSecKQForTests,
}

// Share the key; we allow the same vote key to appear in multiple accounts..
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.MessageHash[:])
sig, err := signerInRound.SignBytes(param.Data[:])
a.NoError(err, "failed to create keys")

for i := 0; i < npart; i++ {
Expand Down Expand Up @@ -240,10 +239,10 @@ func TestSignatureCommitmentBinaryFormat(t *testing.T) {
numPart := 4

param := Params{
MessageHash: testMessage("test!").IntoStateProofMessageHash(),
ProvenWeightThreshold: uint64(totalWeight / (2 * numPart)),
SigRound: currentRound,
SecKQ: compactCertSecKQForTests,
Data: testMessage("test!").IntoStateProofMessageHash(),
ProvenWeight: uint64(totalWeight / (2 * numPart)),
Round: currentRound,
SecKQ: compactCertSecKQForTests,
}

var parts []basics.Participant
Expand All @@ -258,7 +257,7 @@ func TestSignatureCommitmentBinaryFormat(t *testing.T) {
}
parts = append(parts, part)

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

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

param := Params{
MessageHash: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeightThreshold: uint64(totalWeight / 2),
SigRound: currentRound,
SecKQ: compactCertSecKQForTests,
Data: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeight: uint64(totalWeight / 2),
Round: currentRound,
SecKQ: compactCertSecKQForTests,
}

key := generateTestSigner(0, uint64(compactCertRoundsForTests)*20+1, compactCertRoundsForTests, a)
Expand All @@ -459,7 +458,7 @@ func TestBuilder_AddRejectsInvalidSigVersion(t *testing.T) {

// actual test:
signerInRound := key.GetSigner(uint64(currentRound))
sig, err := signerInRound.SignBytes(param.MessageHash[:])
sig, err := signerInRound.SignBytes(param.Data[:])
require.NoError(t, err, "failed to create keys")
// Corrupting the version of the signature:
sig.Signature[1]++
Expand Down Expand Up @@ -492,7 +491,7 @@ func TestBuilderWithZeroProvenWeight(t *testing.T) {
partitiontest.PartitionTest(t)
a := require.New(t)

p := Params{ProvenWeightThreshold: 0}
p := Params{ProvenWeight: 0}

_, err := MkBuilder(p, nil, nil)
a.ErrorIs(err, ErrIllegalInputForLnApprox)
Expand All @@ -507,10 +506,10 @@ func BenchmarkBuildVerify(b *testing.B) {
a := require.New(b)

param := Params{
MessageHash: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeightThreshold: uint64(totalWeight / 2),
SigRound: compactCertRoundsForTests,
SecKQ: compactCertSecKQForTests,
Data: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeight: uint64(totalWeight / 2),
Round: compactCertRoundsForTests,
SecKQ: compactCertSecKQForTests,
}

var parts []basics.Participant
Expand All @@ -524,7 +523,7 @@ func BenchmarkBuildVerify(b *testing.B) {
}

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

partkeys = append(partkeys, signer)
Expand Down
24 changes: 11 additions & 13 deletions crypto/compactcert/coinGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
// The coinChoiceSeed defines the randomness seed that will be given to an XOF function. This will be used for choosing
id-ms marked this conversation as resolved.
Show resolved Hide resolved
// the index of the coin to reveal as part of the compact certificate.
type coinChoiceSeed struct {
msgHash StateProofMessageHash
signedWeight uint64
lnProvenWeightThreshold uint64
sigCommitment crypto.GenericDigest
partCommitment crypto.GenericDigest
data StateProofMessageHash
signedWeight uint64
lnProvenWeight uint64
sigCommitment crypto.GenericDigest
partCommitment crypto.GenericDigest
}

// ToBeHashed returns a binary representation of the coinChoiceSeed structure.
Expand All @@ -44,10 +44,10 @@ func (cc *coinChoiceSeed) ToBeHashed() (protocol.HashID, []byte) {
binary.LittleEndian.PutUint64(signedWtAsBytes, cc.signedWeight)

lnProvenWtAsBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(lnProvenWtAsBytes, cc.lnProvenWeightThreshold)
binary.LittleEndian.PutUint64(lnProvenWtAsBytes, cc.lnProvenWeight)

coinChoiceBytes := make([]byte, 0, len(cc.msgHash)+len(signedWtAsBytes)+len(lnProvenWtAsBytes)+len(cc.sigCommitment)+len(cc.partCommitment))
coinChoiceBytes = append(coinChoiceBytes, cc.msgHash[:]...)
coinChoiceBytes := make([]byte, 0, len(cc.data)+len(signedWtAsBytes)+len(lnProvenWtAsBytes)+len(cc.sigCommitment)+len(cc.partCommitment))
coinChoiceBytes = append(coinChoiceBytes, cc.data[:]...)
coinChoiceBytes = append(coinChoiceBytes, signedWtAsBytes...)
coinChoiceBytes = append(coinChoiceBytes, lnProvenWtAsBytes...)
coinChoiceBytes = append(coinChoiceBytes, cc.sigCommitment...)
Expand All @@ -66,14 +66,12 @@ type coinGenerator struct {
// 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))
// Shake(coinChoiceSeed)
// we extract 64 bits from shake for each coin flip and divide it by signedWeight
func makeCoinGenerator(choice *coinChoiceSeed) coinGenerator {
hash := crypto.HashFactory{HashType: CoinHashType}.NewHash()
hashedCoin := crypto.GenericHashObj(hash, choice)

rep := crypto.HashRep(choice)
shk := sha3.NewShake256()
shk.Write(hashedCoin)
shk.Write(rep)

threshold, singedWt := prepareRejectionSamplingValues(choice.signedWeight)
return coinGenerator{shkContext: shk, signedWeight: singedWt, threshold: threshold}
Expand Down
6 changes: 3 additions & 3 deletions crypto/compactcert/coinGenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestHashCoin(t *testing.T) {
signedWeight: uint64(len(slots)),
sigCommitment: sigcom,
partCommitment: partcom,
msgHash: msgHash,
data: msgHash,
}
coinHash := makeCoinGenerator(&choice)

Expand Down Expand Up @@ -75,7 +75,7 @@ func BenchmarkHashCoin(b *testing.B) {
signedWeight: 1025,
sigCommitment: sigcom,
partCommitment: partcom,
msgHash: msgHash,
data: msgHash,
}
coinHash := makeCoinGenerator(&choice)

Expand All @@ -97,7 +97,7 @@ func BenchmarkHashCoinGenerate(b *testing.B) {
signedWeight: 1025,
sigCommitment: sigcom,
partCommitment: partcom,
msgHash: msgHash,
data: msgHash,
}

for i := 0; i < b.N; i++ {
Expand Down
6 changes: 3 additions & 3 deletions crypto/compactcert/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
const (
HashType = crypto.Sumhash
HashSize = crypto.SumhashDigestSize
CoinHashType = crypto.Sumhash
precisionBits = uint64(1 << 16) // number of bits used for log approximation
ln2IntApproximation = uint64(45427) // the value of the ln(2) with 16 bits of precision
precisionBits = uint8(16) // number of bits used for log approximation
ln2IntApproximation = uint64(45427) // the value of the ln(2) with 16 bits of precision
id-ms marked this conversation as resolved.
Show resolved Hide resolved
)

const (
id-ms marked this conversation as resolved.
Show resolved Hide resolved
// MaxReveals is a bound on allocation and on numReveals to limit log computation
// CR double-check
MaxReveals = 2048
)
9 changes: 5 additions & 4 deletions crypto/compactcert/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ const StateProofMessageHashType = crypto.Sha256

// Params defines common parameters for the verifier and builder.
type Params struct {
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
Data StateProofMessageHash
ProvenWeight uint64 // Weight proven by the certificate
Round basics.Round // The round for which the ephemeral key is committed to
SecKQ uint64 // Security parameter (k+q) from analysis document
}

// A sigslotCommit is a single slot in the sigs array that forms the certificate.
Expand Down Expand Up @@ -69,6 +69,7 @@ type Cert struct {
SigProofs merklearray.Proof `codec:"S"`
PartProofs merklearray.Proof `codec:"P"`

// CR salt version - chagne the falcon to uint8 or change this to int
MerkleSignatureVersion int32 `codec:"v"`

// Reveals is a sparse map from the position being revealed
Expand Down
Loading