Skip to content

Commit a72572d

Browse files
committed
feat: add BenchmarkBytesModExp to benchmark bmodexp
1 parent 7b6b6c2 commit a72572d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

data/transactions/logic/eval_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package logic
1818

1919
import (
20+
"crypto/rand"
2021
"encoding/base64"
2122
"encoding/binary"
2223
"encoding/hex"
@@ -5065,6 +5066,57 @@ func TestBitLen(t *testing.T) {
50655066

50665067
}
50675068

5069+
func BenchmarkBytesModExp(b *testing.B) {
5070+
type ModexpTestVector struct {
5071+
Base string
5072+
Exponent string
5073+
Modulus string
5074+
Name string
5075+
}
5076+
5077+
// Function to generate a random hex string of a specified length in bytes
5078+
generateRandomHexString := func(length int) string {
5079+
bytes := make([]byte, length)
5080+
rand.Read(bytes)
5081+
return fmt.Sprintf("0x%x", bytes)
5082+
}
5083+
5084+
// Define the accepted test vectors using nested loops
5085+
modexpTestVectors := []ModexpTestVector{}
5086+
incr := 128
5087+
maxDim := 1024
5088+
for baseLen := incr; baseLen <= maxDim; baseLen += incr {
5089+
for expLen := incr; expLen <= maxDim; expLen += incr {
5090+
for modLen := incr; modLen <= maxDim; modLen += incr {
5091+
modexpTestVectors = append(modexpTestVectors, ModexpTestVector{
5092+
Name: fmt.Sprintf(`TestVector_Dim(%d,%d,%d)`, baseLen, expLen, modLen),
5093+
Base: generateRandomHexString(baseLen),
5094+
Exponent: generateRandomHexString(expLen),
5095+
Modulus: generateRandomHexString(modLen),
5096+
})
5097+
}
5098+
}
5099+
}
5100+
b.Run("bmod_cost", func(b *testing.B) {
5101+
b.ReportAllocs()
5102+
progText := fmt.Sprintf(`byte %s; byte %s;`, generateRandomHexString(64), generateRandomHexString(64)) + " b%; pop"
5103+
benchmarkOperation(b, "", progText, "int 1")
5104+
})
5105+
b.Run("max_bmodexp_cost", func(b *testing.B) {
5106+
b.ReportAllocs()
5107+
progText := fmt.Sprintf(`byte %s; byte %s; byte %s; bmodexp; pop`, generateRandomHexString(4096), generateRandomHexString(4096), generateRandomHexString(4096))
5108+
benchmarkOperation(b, "", progText, "int 1")
5109+
})
5110+
// Iterate through the test vectors and benchmark the bmodexp computation
5111+
for _, tv := range modexpTestVectors {
5112+
b.Run(tv.Name, func(b *testing.B) {
5113+
b.ReportAllocs()
5114+
progText := fmt.Sprintf(`byte %s; byte %s; byte %s; bmodexp; pop`, tv.Base, tv.Exponent, tv.Modulus)
5115+
benchmarkOperation(b, "", progText, "int 1")
5116+
})
5117+
}
5118+
}
5119+
50685120
func TestBytesModExp(t *testing.T) {
50695121
partitiontest.PartitionTest(t)
50705122
t.Parallel()

0 commit comments

Comments
 (0)