|
| 1 | +// Copyright (C) 2019-2025 Algorand, Inc. |
| 2 | +// This file is part of go-algorand |
| 3 | +// |
| 4 | +// go-algorand is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// go-algorand is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package crypto |
| 18 | + |
| 19 | +import ( |
| 20 | + cryptorand "crypto/rand" |
| 21 | + "io" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/algorand/go-algorand/test/partitiontest" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func randSignedMsg(t testing.TB, r io.Reader) (SignatureVerifier, Hashable, Signature) { |
| 29 | + mlen := 100 |
| 30 | + msg := TestingHashable{data: make([]byte, mlen)} |
| 31 | + n, err := r.Read(msg.data) |
| 32 | + require.NoError(t, err) |
| 33 | + require.Equal(t, n, mlen) |
| 34 | + var s Seed |
| 35 | + n, err = r.Read(s[:]) |
| 36 | + require.NoError(t, err) |
| 37 | + require.Equal(t, 32, n) |
| 38 | + secrets := GenerateSignatureSecrets(s) |
| 39 | + return secrets.SignatureVerifier, msg, secrets.Sign(msg) |
| 40 | +} |
| 41 | + |
| 42 | +// BenchmarkBatchVerifierImpls benchmarks different batch verification implementations |
| 43 | +// with realistic batch sizes (100 batches of 64 signatures each) |
| 44 | +func BenchmarkBatchVerifierImpls(b *testing.B) { |
| 45 | + partitiontest.PartitionTest(b) |
| 46 | + |
| 47 | + numBatches := 100 |
| 48 | + batchSize := 64 |
| 49 | + msgs := make([][]Hashable, numBatches) |
| 50 | + pks := make([][]SignatureVerifier, numBatches) |
| 51 | + sigs := make([][]Signature, numBatches) |
| 52 | + r := cryptorand.Reader |
| 53 | + for i := 0; i < numBatches; i++ { |
| 54 | + for j := 0; j < batchSize; j++ { |
| 55 | + pk, msg, sig := randSignedMsg(b, r) |
| 56 | + msgs[i] = append(msgs[i], msg) |
| 57 | + pks[i] = append(pks[i], pk) |
| 58 | + sigs[i] = append(sigs[i], sig) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + b.Log("running with", b.N, "iterations using", len(msgs), "batches of", batchSize, "signatures") |
| 63 | + runImpl := func(b *testing.B, bv BatchVerifier, |
| 64 | + msgs [][]Hashable, pks [][]SignatureVerifier, sigs [][]Signature) { |
| 65 | + b.ResetTimer() |
| 66 | + for i := 0; i < b.N; i++ { |
| 67 | + batchIdx := i % numBatches |
| 68 | + for j := range msgs[batchIdx] { |
| 69 | + bv.EnqueueSignature(pks[batchIdx][j], msgs[batchIdx][j], sigs[batchIdx][j]) |
| 70 | + } |
| 71 | + require.NoError(b, bv.Verify()) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + b.Run("libsodium_single", func(b *testing.B) { |
| 76 | + bv := makeLibsodiumBatchVerifier(batchSize) |
| 77 | + bv.(*cgoBatchVerifier).useSingle = true |
| 78 | + runImpl(b, bv, msgs, pks, sigs) |
| 79 | + }) |
| 80 | + b.Run("libsodium_batch", func(b *testing.B) { |
| 81 | + bv := makeLibsodiumBatchVerifier(batchSize) |
| 82 | + bv.(*cgoBatchVerifier).useSingle = false |
| 83 | + runImpl(b, bv, msgs, pks, sigs) |
| 84 | + }) |
| 85 | + b.Run("ed25519consensus", func(b *testing.B) { |
| 86 | + bv := makeEd25519ConsensusBatchVerifier(batchSize) |
| 87 | + runImpl(b, bv, msgs, pks, sigs) |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func BenchmarkCanonicalityCheck(b *testing.B) { |
| 92 | + partitiontest.PartitionTest(b) |
| 93 | + |
| 94 | + const maxN = 10000 |
| 95 | + pubkeys := make([]SignatureVerifier, maxN) |
| 96 | + sigs := make([]Signature, maxN) |
| 97 | + for i := 0; i < maxN; i++ { |
| 98 | + var s Seed |
| 99 | + RandBytes(s[:]) |
| 100 | + sigSecrets := GenerateSignatureSecrets(s) |
| 101 | + pubkeys[i] = sigSecrets.SignatureVerifier |
| 102 | + msg := randString() |
| 103 | + sigs[i] = sigSecrets.Sign(msg) |
| 104 | + } |
| 105 | + |
| 106 | + b.Run("pubkey_check", func(b *testing.B) { |
| 107 | + for i := 0; i < b.N; i++ { |
| 108 | + _ = isCanonicalPoint(pubkeys[i%maxN]) |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + b.Run("signature_R_check", func(b *testing.B) { |
| 113 | + for i := 0; i < b.N; i++ { |
| 114 | + _ = isCanonicalPoint([32]byte(sigs[i%maxN][:32])) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + b.Run("both_checks", func(b *testing.B) { |
| 119 | + for i := 0; i < b.N; i++ { |
| 120 | + _ = !isCanonicalPoint(pubkeys[i%maxN]) || !isCanonicalPoint([32]byte(sigs[i%maxN][:32])) |
| 121 | + } |
| 122 | + }) |
| 123 | +} |
0 commit comments