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
more CR fix
  • Loading branch information
algoidan committed Apr 17, 2022
commit 106e6f4dffe562f00210b0151d7e103d08b9908a
4 changes: 2 additions & 2 deletions compactcert/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func TestWorkerAllSigs(t *testing.T) {
Data: tx.Txn.CertMsg.IntoStateProofMessageHash(),
ProvenWeight: provenWeight,
Round: tx.Txn.CertIntervalLatestRound,
SecurityTarget: proto.CompactCertSecurityTarget,
StrengthTarget: proto.CompactCertStrengthTarget,
}

voters, err := s.CompactCertVoters(tx.Txn.CertIntervalLatestRound - basics.Round(proto.CompactCertRounds) - basics.Round(proto.CompactCertVotersLookback))
Expand Down Expand Up @@ -364,7 +364,7 @@ func TestWorkerPartialSigs(t *testing.T) {
Data: msg.IntoStateProofMessageHash(),
ProvenWeight: provenWeight,
Round: tx.Txn.CertIntervalLatestRound,
SecurityTarget: proto.CompactCertSecurityTarget,
StrengthTarget: proto.CompactCertStrengthTarget,
}

voters, err := s.CompactCertVoters(tx.Txn.CertIntervalLatestRound - basics.Round(proto.CompactCertRounds) - basics.Round(proto.CompactCertVotersLookback))
Expand Down
6 changes: 3 additions & 3 deletions config/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ type ConsensusParams struct {
// The threshold is computed as CompactCertWeightThreshold/(1<<32).
CompactCertWeightThreshold uint32

// CompactCertSecurityTarget is the security parameter for the compact
// CompactCertStrengthTarget is the security parameter for the compact
// certificate scheme.
CompactCertSecurityTarget uint64
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 +1130,7 @@ func initConsensusProtocols() {
vFuture.CompactCertTopVoters = 1024 * 1024
vFuture.CompactCertVotersLookback = 16
vFuture.CompactCertWeightThreshold = (1 << 32) * 30 / 100
vFuture.CompactCertSecurityTarget = 256
vFuture.CompactCertStrengthTarget = 256

vFuture.LogicSigVersion = 7

Expand Down
20 changes: 10 additions & 10 deletions crypto/compactcert/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Builder struct {
// parttree.
func MkBuilder(param Params, part []basics.Participant, parttree *merklearray.Tree) (*Builder, error) {
npart := len(part)
lnProvenWt, err := lnIntApproximation(param.ProvenWeight, precisionBits)
lnProvenWt, err := lnIntApproximation(param.ProvenWeight)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (b *Builder) IsValid(pos uint64, sig merklesignature.Signature, verifySig b

id-ms marked this conversation as resolved.
Show resolved Hide resolved
// Check signature
if verifySig {
if err := sig.ValidateSigVersion(merklesignature.SchemeVersion); err != nil {
if err := sig.IsSaltVersionEqual(merklesignature.SchemeSaltVersion); err != nil {
return err
}

Expand Down Expand Up @@ -193,13 +193,13 @@ func (b *Builder) Build() (*Cert, error) {

// Reveal sufficient number of signatures
c := &Cert{
SigCommit: sigtree.Root(),
SignedWeight: b.signedWeight,
Reveals: make(map[uint64]Reveal),
MerkleSignatureVersion: merklesignature.SchemeVersion,
SigCommit: sigtree.Root(),
SignedWeight: b.signedWeight,
Reveals: make(map[uint64]Reveal),
MerkleSignatureSaltVersion: merklesignature.SchemeSaltVersion,
}

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

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

coinHash := makeCoinGenerator(&choice)
Expand Down
12 changes: 6 additions & 6 deletions crypto/compactcert/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (m testMessage) IntoStateProofMessageHash() StateProofMessageHash {
}

const compactCertRoundsForTests = 256
const compactCertSecurityTarget = 256
const compactCertStrengthTarget = 256

func hashBytes(hash hash.Hash, m []byte) []byte {
hash.Reset()
Expand Down Expand Up @@ -95,7 +95,7 @@ func generateCertForTesting(a *require.Assertions) (*Cert, Params, crypto.Generi
Data: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeight: uint64(totalWeight / 2),
Round: currentRound,
SecurityTarget: compactCertSecurityTarget,
StrengthTarget: compactCertStrengthTarget,
}

// Share the key; we allow the same vote key to appear in multiple accounts..
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestSignatureCommitmentBinaryFormat(t *testing.T) {
Data: testMessage("test!").IntoStateProofMessageHash(),
ProvenWeight: uint64(totalWeight / (2 * numPart)),
Round: currentRound,
SecurityTarget: compactCertSecurityTarget,
StrengthTarget: compactCertStrengthTarget,
}

var parts []basics.Participant
Expand Down Expand Up @@ -442,7 +442,7 @@ func TestBuilder_AddRejectsInvalidSigVersion(t *testing.T) {
Data: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeight: uint64(totalWeight / 2),
Round: currentRound,
SecurityTarget: compactCertSecurityTarget,
StrengthTarget: compactCertStrengthTarget,
}

key := generateTestSigner(0, uint64(compactCertRoundsForTests)*20+1, compactCertRoundsForTests, a)
Expand All @@ -463,7 +463,7 @@ func TestBuilder_AddRejectsInvalidSigVersion(t *testing.T) {
// Corrupting the version of the signature:
sig.Signature[1]++

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

func TestCoinIndex(t *testing.T) {
Expand Down Expand Up @@ -509,7 +509,7 @@ func BenchmarkBuildVerify(b *testing.B) {
Data: testMessage("hello world").IntoStateProofMessageHash(),
ProvenWeight: uint64(totalWeight / 2),
Round: compactCertRoundsForTests,
SecurityTarget: compactCertSecurityTarget,
StrengthTarget: compactCertStrengthTarget,
}

var parts []basics.Participant
Expand Down
35 changes: 19 additions & 16 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 {
data StateProofMessageHash
signedWeight uint64
partCommitment crypto.GenericDigest
lnProvenWeight uint64
sigCommitment crypto.GenericDigest
partCommitment crypto.GenericDigest
signedWeight uint64
data StateProofMessageHash
}

// ToBeHashed returns a binary representation of the coinChoiceSeed structure.
Expand All @@ -46,12 +46,12 @@ func (cc *coinChoiceSeed) ToBeHashed() (protocol.HashID, []byte) {
lnProvenWtAsBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(lnProvenWtAsBytes, cc.lnProvenWeight)

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 := make([]byte, 0, len(cc.partCommitment)+len(lnProvenWtAsBytes)+len(cc.sigCommitment)+len(signedWtAsBytes)+len(cc.data))
coinChoiceBytes = append(coinChoiceBytes, cc.partCommitment...)
coinChoiceBytes = append(coinChoiceBytes, lnProvenWtAsBytes...)
coinChoiceBytes = append(coinChoiceBytes, cc.sigCommitment...)
coinChoiceBytes = append(coinChoiceBytes, cc.partCommitment...)
coinChoiceBytes = append(coinChoiceBytes, signedWtAsBytes...)
coinChoiceBytes = append(coinChoiceBytes, cc.data[:]...)

return protocol.CompactCertCoin, coinChoiceBytes
}
Expand All @@ -72,9 +72,9 @@ func makeCoinGenerator(choice *coinChoiceSeed) coinGenerator {
rep := crypto.HashRep(choice)
shk := sha3.NewShake256()
shk.Write(rep)

threshold, singedWt := prepareRejectionSamplingValues(choice.signedWeight)
return coinGenerator{shkContext: shk, signedWeight: singedWt, threshold: threshold}
threshold, signedWt := prepareRejectionSamplingValues(choice.signedWeight)
return coinGenerator{shkContext: shk, signedWeight: signedWt, threshold: threshold}

}

Expand All @@ -85,14 +85,17 @@ func prepareRejectionSamplingValues(signedWeight uint64) (*big.Int, *big.Int) {
// and threshold = k*signedWeight
threshold := &big.Int{}
threshold.SetUint64(1)
threshold.Lsh(threshold, 64)

singedWt := &big.Int{}
singedWt.SetUint64(signedWeight)
threshold.Div(threshold, singedWt)
const numberOfBitsPerAttempt = 64
threshold.Lsh(threshold, numberOfBitsPerAttempt)

signedWt := &big.Int{}
signedWt.SetUint64(signedWeight)

id-ms marked this conversation as resolved.
Show resolved Hide resolved
threshold.Div(threshold, signedWt)

threshold.Mul(threshold, singedWt)
return threshold, singedWt
threshold.Mul(threshold, signedWt)
return threshold, signedWt
}

// getNextCoin returns the next 64bits integer which represents a number between [0,signedWeight)
Expand Down
10 changes: 5 additions & 5 deletions crypto/compactcert/committableSignatureSlot.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func (cs *committableSignatureSlot) ToBeHashed() (protocol.HashID, []byte) {
}
binaryLValue := make([]byte, 8)
binary.LittleEndian.PutUint64(binaryLValue, cs.sigCommit.L)

sigSlotByteRepresentation := make([]byte, 0, len(binaryLValue)+len(cs.serializedSignature))
sigSlotByteRepresentation = append(sigSlotByteRepresentation, binaryLValue...)
sigSlotByteRepresentation = append(sigSlotByteRepresentation, cs.serializedSignature...)

sigSlotCommitment := make([]byte, 0, len(binaryLValue)+len(cs.serializedSignature))
sigSlotCommitment = append(sigSlotCommitment, binaryLValue...)
sigSlotCommitment = append(sigSlotCommitment, cs.serializedSignature...)

return protocol.CompactCertSig, sigSlotCommitment
return protocol.CompactCertSig, sigSlotByteRepresentation
}
3 changes: 1 addition & 2 deletions crypto/compactcert/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ const (

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
MaxReveals = 1024
)
14 changes: 7 additions & 7 deletions crypto/compactcert/msgp_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions crypto/compactcert/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Params struct {
Data StateProofMessageHash
ProvenWeight uint64 // Weight proven by the certificate
Round basics.Round // The round for which the ephemeral key is committed to
SecurityTarget uint64
StrengthTarget uint64
}

// A sigslotCommit is a single slot in the sigs array that forms the certificate.
Expand Down Expand Up @@ -68,10 +68,8 @@ type Cert struct {
SignedWeight uint64 `codec:"w"`
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"`

// CR salt version - chagne the falcon to byte or change this to int
MerkleSignatureSaltVersion int32 `codec:"v"`
// Reveals is a sparse map from the position being revealed
// to the corresponding elements from the sigs and participants
// arrays.
Expand Down
22 changes: 11 additions & 11 deletions crypto/compactcert/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
type Verifier struct {
data StateProofMessageHash
round basics.Round // The round for which the ephemeral key is committed to
SecurityTarget uint64
strengthTarget uint64
lnProvenWeight uint64 // ln(provenWeight) as integer with 16 bits of precision
participantsCommitment crypto.GenericDigest
}
Expand All @@ -44,15 +44,15 @@ type Verifier struct {
// 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, error) {
lnProvenWt, err := lnIntApproximation(p.ProvenWeight, precisionBits)
lnProvenWt, err := lnIntApproximation(p.ProvenWeight)
if err != nil {
return nil, err
}

return &Verifier{
data: p.Data,
round: p.Round,
SecurityTarget: p.SecurityTarget,
strengthTarget: p.StrengthTarget,
lnProvenWeight: lnProvenWt,
participantsCommitment: partcom,
}, nil
Expand All @@ -62,21 +62,21 @@ func MkVerifier(p Params, partcom crypto.GenericDigest) (*Verifier, error) {
// and participants that were used to construct the Verifier.
func (v *Verifier) Verify(c *Cert) error {
nr := uint64(len(c.PositionsToReveal))
if err := verifyWeights(c.SignedWeight, v.lnProvenWeight, nr, v.SecurityTarget); err != nil {
if err := verifyWeights(c.SignedWeight, v.lnProvenWeight, nr, v.strengthTarget); err != nil {
return err
}

version := int(c.MerkleSignatureVersion)
version := int(c.MerkleSignatureSaltVersion)
for _, reveal := range c.Reveals {
if err := reveal.SigSlot.Sig.ValidateSigVersion(version); err != nil {
if err := reveal.SigSlot.Sig.IsSaltVersionEqual(version); err != nil {
return err
}
}

sigs := make(map[uint64]crypto.Hashable)
parts := make(map[uint64]crypto.Hashable)

msghash := v.data
data := v.data
for pos, r := range c.Reveals {
sig, err := buildCommittableSignature(r.SigSlot)
if err != nil {
Expand All @@ -89,7 +89,7 @@ func (v *Verifier) Verify(c *Cert) error {
// verify that the msg and the signature is valid under the given participant's Pk
err = r.Part.PK.VerifyBytes(
uint64(v.round),
msghash[:],
data[:],
r.SigSlot.Sig,
)

Expand All @@ -109,11 +109,11 @@ func (v *Verifier) Verify(c *Cert) error {
}

choice := coinChoiceSeed{
data: v.data,
partCommitment: v.participantsCommitment,
lnProvenWeight: v.lnProvenWeight,
signedWeight: c.SignedWeight,
sigCommitment: c.SigCommit,
partCommitment: v.participantsCommitment,
signedWeight: c.SignedWeight,
data: v.data,
}

coinHash := makeCoinGenerator(&choice)
Expand Down
Loading