forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
limit modexp call to 8192 bit inputs (#1391)
* limit modexp call to 8192 bit inputs * logic change to mod exp revert rules * mod len 0 logic in modExp * remove comment from modexp * more mod exp zk tweaks
- Loading branch information
Showing
2 changed files
with
123 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package vm | ||
|
||
import ( | ||
"testing" | ||
"math/big" | ||
) | ||
|
||
var ( | ||
big0 = big.NewInt(0) | ||
big10 = big.NewInt(10) | ||
big8194 = big.NewInt(0).Lsh(big.NewInt(1), 8194) | ||
) | ||
|
||
func Test_ModExpZkevm_Gas(t *testing.T) { | ||
modExp := bigModExp_zkevm{enabled: true, eip2565: true} | ||
|
||
cases := map[string]struct { | ||
base *big.Int | ||
exp *big.Int | ||
mod *big.Int | ||
expected uint64 | ||
}{ | ||
"simple test": {big10, big10, big10, 200}, | ||
"0 mod - normal gas": {big10, big10, big0, 200}, | ||
"base 0 - mod < 8192 - normal gas": {big0, big10, big10, 200}, | ||
"base 0 - mod > 8192 - 0 gas": {big0, big10, big8194, 0}, | ||
"base over 8192 - 0 gas": {big8194, big10, big10, 0}, | ||
"exp over 8192 - 0 gas": {big10, big8194, big10, 0}, | ||
"mod over 8192 - 0 gas": {big10, big10, big8194, 0}, | ||
} | ||
|
||
for name, test := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
input := make([]byte, 0) | ||
|
||
base := len(test.base.Bytes()) | ||
exp := len(test.exp.Bytes()) | ||
mod := len(test.mod.Bytes()) | ||
|
||
input = append(input, uint64To32Bytes(base)...) | ||
input = append(input, uint64To32Bytes(exp)...) | ||
input = append(input, uint64To32Bytes(mod)...) | ||
input = append(input, uint64ToDeterminedBytes(test.base, base)...) | ||
input = append(input, uint64ToDeterminedBytes(test.exp, exp)...) | ||
input = append(input, uint64ToDeterminedBytes(test.mod, mod)...) | ||
|
||
gas := modExp.RequiredGas(input) | ||
|
||
if gas != test.expected { | ||
t.Errorf("Expected %d, got %d", test.expected, gas) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func uint64To32Bytes(input int) []byte { | ||
bigInt := new(big.Int).SetUint64(uint64(input)) | ||
bytes := bigInt.Bytes() | ||
result := make([]byte, 32) | ||
copy(result[32-len(bytes):], bytes) | ||
return result | ||
} | ||
|
||
func uint64ToDeterminedBytes(input *big.Int, length int) []byte { | ||
bytes := input.Bytes() | ||
result := make([]byte, length) | ||
copy(result[length-len(bytes):], bytes) | ||
return result | ||
} |