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 lnProvenWe to coinhash
  • Loading branch information
algoidan committed Apr 17, 2022
commit 40a9de2ed549a0099e086b75d7136ae8385a189b
36 changes: 21 additions & 15 deletions crypto/compactcert/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +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
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
lnProvenWeightThreshold uint64 // ln(provenWeightThreshold) as integer with 16 bits of precision
participants []basics.Participant
parttree *merklearray.Tree

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

b := &Builder{
Params: param,

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

return b, nil
id-ms marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -193,20 +196,23 @@ func (b *Builder) Build() (*Cert, error) {
MerkleSignatureVersion: merklesignature.SchemeVersion,
}

nr, err := b.numReveals(b.signedWeight)
nr, err := b.numReveals()
if err != nil {
return nil, err
}

var proofPositions []uint64

revealsSequence := make([]uint64, nr)

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

coinHash := makeCoinGenerator(choice)

for j := uint64(0); j < nr; j++ {
Expand Down
9 changes: 5 additions & 4 deletions crypto/compactcert/coinGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import (
type coinChoiceSeed struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`

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

// ToBeHashed implements the crypto.Hashable interface.
Expand Down
9 changes: 5 additions & 4 deletions crypto/compactcert/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ func (v *Verifier) Verify(c *Cert) error {
}

choice := coinChoiceSeed{
SignedWeight: c.SignedWeight,
Sigcom: c.SigCommit,
Partcom: v.partcom,
MsgHash: v.StateProofMessageHash,
MsgHash: v.StateProofMessageHash,
LnProvenWeightThreshold: v.lnProvenWeightThreshold,
SignedWeight: c.SignedWeight,
Sigcom: c.SigCommit,
Partcom: v.partcom,
}

coinHash := makeCoinGenerator(choice)
Expand Down
30 changes: 12 additions & 18 deletions crypto/compactcert/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ func (v *Verifier) verifyWeights(signedWeight uint64, numOfReveals uint64) error
// in order to make the code more readable and reusable we will define the following expressions:
// y = signedWeight^2 + 2^(d + 2) * signedWeight + 2^2d
// a = 3 * 2^b * (signedWeight^2 - 2^2d)
// b = d * (T - 1)
// w = d * (T - 1)
//
algonautshant marked this conversation as resolved.
Show resolved Hide resolved
// numReveals * (3 * 2^b * (signedWeight^2 - 2^2d) + d * (T-1) * Y) >= ((k+q) * T + numReveals * P) * Y
// ||
// \/
// numReveals * (a + b * Y) >= ((k+q) * T + numReveals * P) * Y
y, a, b := getSubExpressions(signedWeight)
y, a, w := getSubExpressions(signedWeight)
lhs := &big.Int{}
lhs.Set(b).
lhs.Set(w).
Mul(lhs, y).
Add(a, lhs).
Mul(bigInt(numOfReveals), lhs)
Expand All @@ -129,39 +129,33 @@ func (v *Verifier) verifyWeights(signedWeight uint64, numOfReveals uint64) error
return nil
}

func (p *Builder) numReveals(signedWeight uint64) (uint64, error) {
return numReveals(signedWeight, p.ProvenWeightThreshold, p.SecKQ, MaxReveals)
}

// numReveals computes the number of reveals necessary to achieve the desired
// security parameters. we use value which satisfies the following equation:
//
// numReveals > = ((k+q) * T * Y / (3 * 2^b * (signedWeight^2 - 2^2d) + (d * (T - 1) - P) * Y))
func numReveals(signedWeight uint64, provenWeight uint64, secKQ uint64, bound uint64) (uint64, error) {
if provenWeight == 0 {
func (b *Builder) numReveals() (uint64, error) {
if b.ProvenWeightThreshold == 0 {
return 0, ErrZeroProvenWeightThreshold
}

lnProvenWe := lnIntApproximation(provenWeight, precisionBits)

// in order to make the code more readable and reusable we will define the following expressions:
// y = signedWeight^2 + 2^(d + 2) * signedWeight + 2^2d
// a = 3 * 2^b * (signedWeight^2 - 2^2d)
// b = d * (T - 1)
// w = d * (T - 1)
//
// numReveals > = ((k+q) * T * Y / (3 * 2^b * (signedWeight^2 - 2^2d) + (d * (T - 1) - P) * Y))
// ||
// \/
// numReveals >= ((k+q) * T * Y / (a + (b - P) * Y)
y, a, b := getSubExpressions(signedWeight)
y, a, w := getSubExpressions(b.signedWeight)

id-ms marked this conversation as resolved.
Show resolved Hide resolved
numerator := bigInt(secKQ)
numerator := bigInt(b.SecKQ)
numerator.Mul(numerator, bigInt(ln2IntApproximation)).
Mul(numerator, y)

id-ms marked this conversation as resolved.
Show resolved Hide resolved
denom := &big.Int{}
denom.Set(b).
Sub(denom, bigInt(lnProvenWe)).
denom.Set(w).
Sub(denom, bigInt(b.lnProvenWeightThreshold)).
Mul(denom, y).
Add(a, denom)

Expand All @@ -179,7 +173,7 @@ func numReveals(signedWeight uint64, provenWeight uint64, secKQ uint64, bound ui
// getSubExpressions calculate the following expression to make the code more readable and reusable
// y = signedWeight^2 + 2^(d + 2) * signedWeight + 2^2d
// a = 3 * 2^b * (signedWeight^2 - 2^2d)
// b = d * (T - 1)
// w = d * (T - 1)
func getSubExpressions(signedWeight uint64) (y *big.Int, a *big.Int, b *big.Int) {
// find d s.t 2^(d+1) >= signedWeight >= 2^(d)
d := uint64(bits.Len64(signedWeight)) - 1
Expand Down Expand Up @@ -207,7 +201,7 @@ func getSubExpressions(signedWeight uint64) (y *big.Int, a *big.Int, b *big.Int)
Mul(a, bigInt(3)).
Mul(a, bigInt(precisionBits))

// b = d*(T-1)
// w = d*(T-1)
b = bigInt(d)
b.Mul(b, bigInt(ln2IntApproximation-1))

Expand Down
34 changes: 22 additions & 12 deletions crypto/compactcert/weights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@
package compactcert

import (
"math"
"testing"

"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
"math"
"testing"
)

func numRevealsForTests(signedWeight uint64, provenWeight uint64, secKQ uint64) (uint64, error) {
p := Params{SecKQ: secKQ, ProvenWeightThreshold: provenWeight}
lnProvenWt := lnIntApproximation(provenWeight, precisionBits)
b := &Builder{
Params: p,
signedWeight: signedWeight,
lnProvenWeightThreshold: lnProvenWt,
}
return b.numReveals()
}

func TestVerifyMaxNumberOfRevealsInVerify(t *testing.T) {
partitiontest.PartitionTest(t)
a := require.New(t)
Expand All @@ -44,7 +56,7 @@ func TestVerifyMaxNumberOfReveals(t *testing.T) {
signedWeight := uint64(1<<10 + 1)
provenWeight := uint64(1 << 10)

_, err := numReveals(signedWeight, provenWeight, compactCertSecKQForTests, MaxReveals)
_, err := numRevealsForTests(signedWeight, provenWeight, compactCertSecKQForTests)
a.ErrorIs(err, ErrTooManyReveals)
}

Expand All @@ -68,7 +80,7 @@ func TestVerifyImpliedProvenGreaterThanThreshold(t *testing.T) {
signedWeight := uint64(1<<10 + 93)
provenWeight := uint64(1 << 10)

numOfReveals, err := numReveals(signedWeight, provenWeight, compactCertSecKQForTests, MaxReveals)
numOfReveals, err := numRevealsForTests(signedWeight, provenWeight, compactCertSecKQForTests)

param := Params{SecKQ: compactCertSecKQForTests, ProvenWeightThreshold: provenWeight}
verifier := MkVerifier(param, crypto.GenericDigest{})
Expand All @@ -78,7 +90,7 @@ func TestVerifyImpliedProvenGreaterThanThreshold(t *testing.T) {
signedWeight = uint64(1<<10 + 92)
provenWeight = uint64(1 << 10)

numOfReveals, err = numReveals(signedWeight, provenWeight, compactCertSecKQForTests, MaxReveals)
numOfReveals, err = numRevealsForTests(signedWeight, provenWeight, compactCertSecKQForTests)

param = Params{SecKQ: compactCertSecKQForTests, ProvenWeightThreshold: provenWeight}
verifier = MkVerifier(param, crypto.GenericDigest{})
Expand All @@ -93,7 +105,7 @@ func TestVerifyZeroNumberOfRevealsEquation(t *testing.T) {
signedWeight := uint64(1<<15 + 1)
provenWeight := uint64(1 << 15)

_, err := numReveals(signedWeight, provenWeight, compactCertSecKQForTests, MaxReveals)
_, err := numRevealsForTests(signedWeight, provenWeight, compactCertSecKQForTests)
a.ErrorIs(err, ErrNegativeNumOfRevealsEquation)
}

Expand Down Expand Up @@ -143,7 +155,7 @@ func TestNumReveals(t *testing.T) {

for i := uint64(3); i < 10; i++ {
signedWeight := i * billion * microalgo
n, err := numReveals(signedWeight, provenWeight, secKQ, bound)
n, err := numRevealsForTests(signedWeight, provenWeight, secKQ)
require.NoError(t, err)
n2, err := old(signedWeight, provenWeight, secKQ, bound)
t.Logf("%f", float64(n2)/float64(n))
Expand All @@ -167,9 +179,8 @@ func BenchmarkWeight(b *testing.B) {
provenWeight := 100 * billion * microalgo
signedWeight := 110 * billion * microalgo
secKQ := uint64(compactCertSecKQForTests)
bound := uint64(1000)

nr, err := numReveals(signedWeight, provenWeight, secKQ, bound)
nr, err := numRevealsForTests(signedWeight, provenWeight, secKQ)
if nr < 900 {
b.Errorf("numReveals(%d, %d, %d) = %d < 900", signedWeight, provenWeight, secKQ, nr)
}
Expand All @@ -190,15 +201,14 @@ func BenchmarkNumReveals(b *testing.B) {
provenWeight := 100 * billion * microalgo
signedWeight := 110 * billion * microalgo
secKQ := uint64(compactCertSecKQForTests)
bound := uint64(1000)

nr, err := numReveals(signedWeight, provenWeight, secKQ, bound)
nr, err := numRevealsForTests(signedWeight, provenWeight, secKQ)
if nr < 900 {
b.Errorf("numReveals(%d, %d, %d) = %d < 900", signedWeight, provenWeight, secKQ, nr)
}

for i := 0; i < b.N; i++ {
_, err = numReveals(signedWeight, provenWeight, secKQ, bound)
_, err = numRevealsForTests(signedWeight, provenWeight, secKQ)
if err != nil {
b.Error(err)
}
Expand Down