Skip to content

Commit

Permalink
Fix modexp when partial base/exp/mod is set
Browse files Browse the repository at this point in the history
  • Loading branch information
xavier-romero committed Nov 18, 2024
1 parent b1758e8 commit a8a7941
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions core/vm/contracts_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,24 @@ func (c *bigModExp_zkevm) Run(input []byte) ([]byte, error) {
mod = big.NewInt(0)
)

if len(input) >= 96 + int(baseLen) {
base = new(big.Int).SetBytes(getData(input, 96, uint64(baseLen)))
// Extract `base`, `exp`, and `mod` with padding as needed
baseData := getData(input, 96, uint64(baseLen))
if uint64(len(baseData)) < baseLen {
baseData = common.RightPadBytes(baseData, int(baseLen))
}
if len(input) >= 96 + int(baseLen) + int(expLen) {
exp = new(big.Int).SetBytes(getData(input, 96 + uint64(baseLen), uint64(expLen)))
}
if len(input) >= 96 + int(baseLen) + int(expLen) + int(modLen) {
mod = new(big.Int).SetBytes(getData(input, 96 + uint64(baseLen) + uint64(expLen), uint64(modLen)))
base.SetBytes(baseData)

expData := getData(input, 96+uint64(baseLen), uint64(expLen))
if uint64(len(expData)) < expLen {
expData = common.RightPadBytes(expData, int(expLen))
}
if len(input) < 96 + int(baseLen) + int(expLen) + int(modLen) {
input = common.LeftPadBytes(input, 96 + int(baseLen) + int(expLen) + int(modLen))
exp.SetBytes(expData)

modData := getData(input, 96+uint64(baseLen)+uint64(expLen), uint64(modLen))
if uint64(len(modData)) < modLen {
modData = common.RightPadBytes(modData, int(modLen))
}
mod.SetBytes(modData)

// Retrieve the operands and execute the exponentiation
var (
Expand All @@ -422,7 +428,7 @@ func (c *bigModExp_zkevm) Run(input []byte) ([]byte, error) {
)

if modBitLen == 0 {
return []byte{}, nil
return common.LeftPadBytes([]byte{}, int(modLen)), nil
}

if baseBitLen == 0 {
Expand Down

0 comments on commit a8a7941

Please sign in to comment.