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
fixed hash representation for coinhash
  • Loading branch information
algoidan committed Apr 17, 2022
commit 2f275f3b3bdee077ca47b3dc9193e285fb32b191
12 changes: 6 additions & 6 deletions crypto/compactcert/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ func (b *Builder) Build() (*Cert, error) {
revealsSequence := make([]uint64, nr)

choice := coinChoiceSeed{
MsgHash: b.StateProofMessageHash,
LnProvenWeightThreshold: b.lnProvenWeightThreshold,
SignedWeight: c.SignedWeight,
Sigcom: c.SigCommit,
Partcom: b.parttree.Root(),
msgHash: b.StateProofMessageHash,
lnProvenWeightThreshold: b.lnProvenWeightThreshold,
signedWeight: c.SignedWeight,
sigCommitment: c.SigCommit,
partCommitment: b.parttree.Root(),
}

coinHash := makeCoinGenerator(choice)
coinHash := makeCoinGenerator(&choice)

for j := uint64(0); j < nr; j++ {
coin := coinHash.getNextCoin()
Expand Down
51 changes: 32 additions & 19 deletions crypto/compactcert/coinGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,32 @@ 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 {
_struct struct{} `codec:",omitempty,omitemptyarray"`

MsgHash StateProofMessageHash `codec:"msghash"`
SignedWeight uint64 `codec:"sigweight"`
LnProvenWeightThreshold uint64 `codec:"lnweight"`
Sigcom crypto.GenericDigest `codec:"sigcom"`
Partcom crypto.GenericDigest `codec:"partcom"`
msgHash StateProofMessageHash
signedWeight uint64
lnProvenWeightThreshold uint64
sigCommitment crypto.GenericDigest
partCommitment crypto.GenericDigest
}

// ToBeHashed implements the crypto.Hashable interface.
func (cc coinChoiceSeed) ToBeHashed() (protocol.HashID, []byte) {
// TODO should create a fixed length representation
return protocol.CompactCertCoin, protocol.Encode(&cc)
// ToBeHashed returns a binary representation of the coinChoiceSeed structure.
// Since this code is also implemented as a circuit in the stateproof SNARK prover we can't use
// msgpack encoding since it may result in a variable length byte slice.
// Alternatively, we serialize the fields in the structure in a specific format.
func (cc *coinChoiceSeed) ToBeHashed() (protocol.HashID, []byte) {
signedWtAsBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(signedWtAsBytes, cc.signedWeight)

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

coinChoiceBytes := make([]byte, 0, len(cc.msgHash)+len(signedWtAsBytes)+len(lnProvenWtAsBytes)+len(cc.sigCommitment)+len(cc.partCommitment))
coinChoiceBytes = append(coinChoiceBytes, cc.msgHash[:]...)
coinChoiceBytes = append(coinChoiceBytes, signedWtAsBytes...)
coinChoiceBytes = append(coinChoiceBytes, lnProvenWtAsBytes...)
coinChoiceBytes = append(coinChoiceBytes, cc.sigCommitment...)
coinChoiceBytes = append(coinChoiceBytes, cc.partCommitment...)

return protocol.CompactCertCoin, coinChoiceBytes
}

// coinGenerator is used for extracting "randomized" 64 bits for coin flips
Expand All @@ -54,15 +67,15 @@ type coinGenerator struct {
// 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 makeCoinGenerator(choice coinChoiceSeed) coinGenerator {
// 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)

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

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

}
Expand All @@ -84,23 +97,23 @@ func prepareRejectionSamplingValues(signedWeight uint64) (*big.Int, *big.Int) {
return threshold, singedWt
}

// getNextCoin returns the next 64bits integer which represents a number between [0,SignedWeight)
func (ch *coinGenerator) getNextCoin() uint64 {
// getNextCoin returns the next 64bits integer which represents a number between [0,signedWeight)
func (cg *coinGenerator) getNextCoin() uint64 {
// take b bits from the XOF and generate an integer z.
// we accept the sample if z < threshold
// else, we reject the sample and repeat the process.
z := &big.Int{}
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
for true {
var shakeDigest [8]byte
ch.shkContext.Read(shakeDigest[:])
cg.shkContext.Read(shakeDigest[:])
randNumFromXof := binary.LittleEndian.Uint64(shakeDigest[:])

z.SetUint64(randNumFromXof)
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
if comp := z.Cmp(ch.threshold); comp == -1 {
if comp := z.Cmp(cg.threshold); comp == -1 {
break
}
}

z.Mod(z, ch.signedWeight)
z.Mod(z, cg.signedWeight)
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
return z.Uint64()
}
30 changes: 15 additions & 15 deletions crypto/compactcert/coinGenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestHashCoin(t *testing.T) {
crypto.RandBytes(msgHash[:])

choice := coinChoiceSeed{
SignedWeight: uint64(len(slots)),
Sigcom: sigcom,
Partcom: partcom,
MsgHash: msgHash,
signedWeight: uint64(len(slots)),
sigCommitment: sigcom,
partCommitment: partcom,
msgHash: msgHash,
}
coinHash := makeCoinGenerator(choice)
coinHash := makeCoinGenerator(&choice)

for j := uint64(0); j < 1000; j++ {
coin := coinHash.getNextCoin()
Expand Down Expand Up @@ -72,12 +72,12 @@ func BenchmarkHashCoin(b *testing.B) {
crypto.RandBytes(msgHash[:])

choice := coinChoiceSeed{
SignedWeight: 1025,
Sigcom: sigcom,
Partcom: partcom,
MsgHash: msgHash,
signedWeight: 1025,
sigCommitment: sigcom,
partCommitment: partcom,
msgHash: msgHash,
}
coinHash := makeCoinGenerator(choice)
coinHash := makeCoinGenerator(&choice)

for i := 0; i < b.N; i++ {
coinHash.getNextCoin()
Expand All @@ -94,13 +94,13 @@ func BenchmarkHashCoinGenerate(b *testing.B) {
crypto.RandBytes(msgHash[:])

choice := coinChoiceSeed{
SignedWeight: 1025,
Sigcom: sigcom,
Partcom: partcom,
MsgHash: msgHash,
signedWeight: 1025,
sigCommitment: sigcom,
partCommitment: partcom,
msgHash: msgHash,
}

for i := 0; i < b.N; i++ {
makeCoinGenerator(choice)
makeCoinGenerator(&choice)
}
}
183 changes: 0 additions & 183 deletions crypto/compactcert/msgp_gen.go

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

Loading