diff --git a/crypto/modexp/benchmark_comprehensive_test.go b/crypto/modexp/benchmark_comprehensive_test.go index a303df679e..bd785074e1 100644 --- a/crypto/modexp/benchmark_comprehensive_test.go +++ b/crypto/modexp/benchmark_comprehensive_test.go @@ -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) } }) }) diff --git a/crypto/modexp/gmp/cwrapper/gmp.go b/crypto/modexp/gmp/gmp.go similarity index 98% rename from crypto/modexp/gmp/cwrapper/gmp.go rename to crypto/modexp/gmp/gmp.go index a6415feb0f..76bb83f533 100644 --- a/crypto/modexp/gmp/cwrapper/gmp.go +++ b/crypto/modexp/gmp/gmp.go @@ -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 diff --git a/crypto/modexp/gmp/cwrapper/gmp_test.go b/crypto/modexp/gmp/gmp_test.go similarity index 100% rename from crypto/modexp/gmp/cwrapper/gmp_test.go rename to crypto/modexp/gmp/gmp_test.go