|
17 | 17 | package logic
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "crypto/rand" |
20 | 21 | "encoding/base64"
|
21 | 22 | "encoding/binary"
|
22 | 23 | "encoding/hex"
|
@@ -5065,6 +5066,57 @@ func TestBitLen(t *testing.T) {
|
5065 | 5066 |
|
5066 | 5067 | }
|
5067 | 5068 |
|
| 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 | + |
5068 | 5120 | func TestBytesModExp(t *testing.T) {
|
5069 | 5121 | partitiontest.PartitionTest(t)
|
5070 | 5122 | t.Parallel()
|
|
0 commit comments