From 23e6da3a55e77a53023433fb5f87be6d8a4ba36a Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Thu, 26 Sep 2024 13:52:56 +0800 Subject: [PATCH] core/vm: use optimized bigint (#26021) --- core/vm/contracts.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index a1ea48fa00ef6..42cff82bbd058 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -29,6 +29,7 @@ import ( "github.com/XinFinOrg/XDPoSChain/crypto/blake2b" "github.com/XinFinOrg/XDPoSChain/crypto/bn256" "github.com/XinFinOrg/XDPoSChain/params" + big2 "github.com/holiman/big" //lint:ignore SA1019 Needed for precompile "golang.org/x/crypto/ripemd160" @@ -401,17 +402,17 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) { } // Retrieve the operands and execute the exponentiation var ( - base = new(big.Int).SetBytes(getData(input, 0, baseLen)) - exp = new(big.Int).SetBytes(getData(input, baseLen, expLen)) - mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen)) + base = new(big2.Int).SetBytes(getData(input, 0, baseLen)) + exp = new(big2.Int).SetBytes(getData(input, baseLen, expLen)) + mod = new(big2.Int).SetBytes(getData(input, baseLen+expLen, modLen)) v []byte ) switch { case mod.BitLen() == 0: // Modulo 0 is undefined, return zero return common.LeftPadBytes([]byte{}, int(modLen)), nil - case base.Cmp(common.Big1) == 0: - //If base == 1, then we can just return base % mod (if mod >= 1, which it is) + case base.BitLen() == 1: // a bit length of 1 means it's 1 (or -1). + // 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()