use abstraction from modexp package

This commit is contained in:
Kevaundray Wedderburn 2025-07-11 00:08:33 +01:00
parent e131482fe2
commit 9f789b6272

View file

@ -35,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/crypto/blake2b" "github.com/ethereum/go-ethereum/crypto/blake2b"
"github.com/ethereum/go-ethereum/crypto/bn256" "github.com/ethereum/go-ethereum/crypto/bn256"
"github.com/ethereum/go-ethereum/crypto/kzg4844" "github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/crypto/modexp"
"github.com/ethereum/go-ethereum/crypto/secp256r1" "github.com/ethereum/go-ethereum/crypto/secp256r1"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256" "github.com/holiman/uint256"
@ -623,21 +624,17 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
} }
// Retrieve the operands and execute the exponentiation // Retrieve the operands and execute the exponentiation
var ( var (
base = new(big.Int).SetBytes(getData(input, 0, baseLen)) base = getData(input, 0, baseLen)
exp = new(big.Int).SetBytes(getData(input, baseLen, expLen)) exp = getData(input, baseLen, expLen)
mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen)) mod = getData(input, baseLen+expLen, modLen)
v []byte
) )
switch {
case mod.BitLen() == 0: // Use the modexp implementation
// Modulo 0 is undefined, return zero v, err := modexp.ModExp(base, exp, mod)
return common.LeftPadBytes([]byte{}, int(modLen)), nil if err != nil {
case base.BitLen() == 1: // a bit length of 1 means it's 1 (or -1). return nil, err
//If base == 1, then we can just return base % mod (if mod >= 1, which it is)
v = base.Mod(base, mod).Bytes()
default:
v = base.Exp(base, exp, mod).Bytes()
} }
return common.LeftPadBytes(v, int(modLen)), nil return common.LeftPadBytes(v, int(modLen)), nil
} }