summaryrefslogtreecommitdiff
path: root/crypto/stateproof/weights_test.go
blob: 718382563dbddc4eb9c7b0ca2116126c11e8a775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package stateproof

import (
	"fmt"
	"math"
	"testing"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto/merklearray"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

func TestMaxNumberOfRevealsInVerify(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	signedWeight := uint64(10)
	provenWeight := uint64(10)
	lnProvenWt, err := LnIntApproximation(provenWeight)
	a.NoError(err)

	err = verifyWeights(signedWeight, lnProvenWt, MaxReveals+1, stateProofStrengthTargetForTests)
	a.ErrorIs(err, ErrTooManyReveals)
}

func TestMaxNumberOfReveals(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	signedWeight := uint64(1<<10 + 1)
	provenWeight := uint64(1 << 10)
	lnProvenWt, err := LnIntApproximation(provenWeight)
	a.NoError(err)

	_, err = numReveals(signedWeight, lnProvenWt, stateProofStrengthTargetForTests)
	a.ErrorIs(err, ErrTooManyReveals)
}

func TestVerifyProvenWeight(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	signedWeight := uint64(1 << 11)
	provenWeight := uint64(1 << 10)
	lnProvenWt, err := LnIntApproximation(provenWeight)
	a.NoError(err)

	numOfReveals, err := numReveals(signedWeight, lnProvenWt, stateProofStrengthTargetForTests)
	a.NoError(err)

	err = verifyWeights(signedWeight, lnProvenWt, numOfReveals, stateProofStrengthTargetForTests)
	a.NoError(err)

	err = verifyWeights(signedWeight, lnProvenWt, numOfReveals-1, stateProofStrengthTargetForTests)
	a.ErrorIs(err, ErrInsufficientSignedWeight)
}

func TestVerifyZeroNumberOfRevealsEquation(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	signedWeight := uint64(1<<15 + 1)
	provenWeight := uint64(1 << 15)
	lnProvenWt, err := LnIntApproximation(provenWeight)
	a.NoError(err)

	_, err = numReveals(signedWeight, lnProvenWt, stateProofStrengthTargetForTests)
	a.ErrorIs(err, ErrNegativeNumOfRevealsEquation)
}

func TestLnWithPrecision(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	val, err := LnIntApproximation(2)
	a.NoError(err)

	// check that precisionBits will not overflow
	exp := 1 << precisionBits
	a.Less(precisionBits, uint8(64))

	a.GreaterOrEqual(float64(val)/float64(exp), math.Log(2))
	a.Greater(math.Log(2), float64(val-1)/float64(exp))

	ln2, err := LnIntApproximation(2)
	a.NoError(err)
	a.Equal(ln2IntApproximation, ln2)
}

func TestVerifyLimits(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	signedWeight := uint64(0)
	provenWeight := uint64(1<<10 - 1)
	lnProvenWt, err := LnIntApproximation(provenWeight)
	a.NoError(err)

	err = verifyWeights(signedWeight, lnProvenWt, MaxReveals-1, stateProofStrengthTargetForTests)
	a.ErrorIs(err, ErrZeroSignedWeight)
}

func TestNumRevealsApproxBound(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	// In order to create a valid state proof we need to be bound to a MaxNumberOfReveals.
	// according to SNARK-friendly weight-verification formula there would be a ratio signedWt/provenWt > 1
	// that we would not be able to generate proof since the MaxReveals would be too high.
	// This test points out on the minimal ratio signedWt/provenWt we would ever prdouce.

	for j := 0; j < 10; j++ {
		sigWt := uint64(1<<(40-j) - 1)
		// we check the ratios = signedWt/provenWt {3, 2.99, 2.98...1}
		// ratio = 1.33 (i==167) would give 625 would be the lower bound we can expect
		for i := 0; i < 168; i++ {
			a.NoError(checkRatio(i, sigWt, stateProofStrengthTargetForTests))
		}
		a.ErrorIs(checkRatio(168, sigWt, stateProofStrengthTargetForTests), ErrTooManyReveals)
	}
}

func checkRatio(i int, sigWt uint64, secParam uint64) error {
	provenWtRatio := 3 - (float64(i) / 100)
	provenWt := uint64(float64(sigWt) / (provenWtRatio))
	lnProvenWt, err := LnIntApproximation(provenWt)
	if err != nil {
		return err
	}

	numOfReveals, err := numReveals(sigWt, lnProvenWt, secParam)
	if err != nil {
		return fmt.Errorf("failed on sigWt %v provenWt %d ratio is %v i %v err: %w", sigWt, provenWt, provenWtRatio, i, err)
	}

	log2Sig := math.Log(float64(sigWt)) / math.Log(2)
	log2Prov := math.Log(float64(provenWt)) / math.Log(2)
	nr := float64(secParam) / (log2Sig - log2Prov)
	if 1.01 < float64(numOfReveals)/nr {
		return fmt.Errorf("approximated number of reveals exceeds limit "+
			"limit %v, signedWeight: %v provenWeight %v, "+
			"appox numberOfReveals: %v, real numberOfReveals %v ratio is %v", 1.01, sigWt, provenWt, numOfReveals, nr, provenWtRatio)
	}
	return nil
}

func TestNumReveals(t *testing.T) {
	partitiontest.PartitionTest(t)
	a := require.New(t)

	billion := uint64(1000 * 1000 * 1000)
	microalgo := uint64(1000 * 1000)
	provenWeight := 2 * billion * microalgo
	strengthTarget := uint64(stateProofStrengthTargetForTests)
	lnProvenWt, err := LnIntApproximation(provenWeight)
	a.NoError(err)

	for i := uint64(3); i < 10; i++ {
		signedWeight := i * billion * microalgo
		n, err := numReveals(signedWeight, lnProvenWt, strengthTarget)
		a.NoError(err)
		if n < 50 || n > 500 {
			t.Errorf("numReveals(%d, %d, %d) = %d looks suspect",
				signedWeight, provenWeight, strengthTarget, n)
		}

		err = verifyWeights(signedWeight, lnProvenWt, n, stateProofStrengthTargetForTests)
		a.NoError(err)
	}
}

func TestSigPartProofMaxSize(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()
	// Ensures that the SigPartProofMaxSize constant used for maxtotalbytes for StateProof.(Sig|Part)Proof(s) is
	// correct. It should be logically bound by the maximum number of StateProofTopVoters. It is scaled by 1/2
	// see merkelarray.Proof comment for explanation of the size calculation.
	require.Equal(t, SigPartProofMaxSize, merklearray.ProofMaxSizeByElements(config.StateProofTopVoters/2))
}

func BenchmarkVerifyWeights(b *testing.B) {
	billion := uint64(1000 * 1000 * 1000)
	microalgo := uint64(1000 * 1000)
	provenWeight := 100 * billion * microalgo
	signedWeight := 110 * billion * microalgo
	strengthTarget := uint64(stateProofStrengthTargetForTests)

	lnProvenWt, err := LnIntApproximation(provenWeight)
	require.NoError(b, err)

	nr, err := numReveals(signedWeight, lnProvenWt, strengthTarget)
	if nr < 900 {
		b.Errorf("numReveals(%d, %d, %d) = %d < 900", signedWeight, provenWeight, strengthTarget, nr)
	}
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		verifyWeights(signedWeight, lnProvenWt, nr, strengthTarget)
	}
}

func BenchmarkNumReveals(b *testing.B) {
	billion := uint64(1000 * 1000 * 1000)
	microalgo := uint64(1000 * 1000)
	provenWeight := 100 * billion * microalgo
	signedWeight := 110 * billion * microalgo
	strengthTarget := uint64(stateProofStrengthTargetForTests)
	lnProvenWt, err := LnIntApproximation(provenWeight)
	require.NoError(b, err)

	nr, err := numReveals(signedWeight, lnProvenWt, strengthTarget)
	if nr < 900 {
		b.Errorf("numReveals(%d, %d, %d) = %d < 900", signedWeight, provenWeight, strengthTarget, nr)
	}
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		numReveals(signedWeight, lnProvenWt, strengthTarget)
	}
}