move gmp_cwrapper method one level up

This commit is contained in:
Kevaundray Wedderburn 2025-07-08 17:25:27 +01:00
parent e33fcb3811
commit c496edfa2e
3 changed files with 8 additions and 5 deletions

View file

@ -4,7 +4,7 @@ import (
"testing"
"github.com/ethereum/go-ethereum/crypto/modexp/bigint"
gmpcwrapper "github.com/ethereum/go-ethereum/crypto/modexp/gmp/cwrapper"
"github.com/ethereum/go-ethereum/crypto/modexp/gmp"
)
// generateWorstCase generates a byte array with all bits set to 1
@ -43,7 +43,7 @@ func BenchmarkComprehensive(b *testing.B) {
fn func([]byte, []byte, []byte) ([]byte, error)
}{
{"BigInt", bigint.ModExp},
{"GMPCWrapper", gmpcwrapper.ModExp},
{"GMP", gmp.ModExp},
}
for _, tc := range testCases {
@ -94,7 +94,7 @@ func BenchmarkWorstCaseOnly(b *testing.B) {
fn func([]byte, []byte, []byte) ([]byte, error)
}{
{"BigInt", bigint.ModExp},
{"GMPCWrapper", gmpcwrapper.ModExp},
{"GMP", gmp.ModExp},
}
b.Run("AllFF", func(b *testing.B) {
@ -139,7 +139,7 @@ func BenchmarkMemoryProfile(b *testing.B) {
fn func([]byte, []byte, []byte) ([]byte, error)
}{
{"BigInt", bigint.ModExp},
{"GMPCWrapper", gmpcwrapper.ModExp},
{"GMP", gmp.ModExp},
}
for _, size := range sizes {
@ -213,7 +213,7 @@ func BenchmarkComparison(b *testing.B) {
b.Run("GMPCWrapper", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gmpcwrapper.ModExp(data.base, data.exp, data.mod)
_, _ = gmp.ModExp(data.base, data.exp, data.mod)
}
})
})

View file

@ -97,6 +97,8 @@ import (
// ModExp performs modular exponentiation using the C implementation directly
// This is a lower-level function that bypasses the Go wrapper types
//
// This is thread safe.
func ModExp(base, exp, mod []byte) ([]byte, error) {
// Handle empty modulus - return empty result (EVM behavior)
if len(mod) == 0 {
@ -108,6 +110,7 @@ func ModExp(base, exp, mod []byte) ([]byte, error) {
resultLen := C.size_t(len(result))
// Handle empty slices - pass a dummy non-nil pointer with length 0
// This avoids UB when the length is zero
dummy := C.uint8_t(0)
var basePtr, expPtr, modPtr *C.uint8_t = &dummy, &dummy, &dummy