core/vm: fix modexp fast path for modulus 1

This commit is contained in:
Sahil Sojitra 2026-04-01 21:53:31 +05:30
parent 59663aff1b
commit 4ce7b4cbcf

View file

@ -647,11 +647,10 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
// Allocate the result buffer once, zero-filled. // Allocate the result buffer once, zero-filled.
result := make([]byte, modLen) result := make([]byte, modLen)
switch { switch {
case mod.BitLen() == 0: case mod.BitLen() <= 1:
// Modulo 0 is undefined, return zero // Leave the result as zero for mod 0 (undefined) and 1.
return common.LeftPadBytes([]byte{}, int(modLen)), nil case base.BitLen() == 1:
case base.BitLen() == 1: // a bit length of 1 means it's 1 (or -1). // If base == 1 (and mod > 1), then the result is 1.
//If base == 1, then we can just return base % mod (if mod >= 1, which it is)
result[modLen-1] = 1 result[modLen-1] = 1
default: default:
base.Exp(base, exp, mod).FillBytes(result) base.Exp(base, exp, mod).FillBytes(result)