|
| 1 | +// Copyright 2022 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package modexp |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math/big" |
| 22 | + |
| 23 | + "github.com/ethereum/go-ethereum/common" |
| 24 | + "github.com/ethereum/go-ethereum/common/math" |
| 25 | + "github.com/ethereum/go-ethereum/core/vm" |
| 26 | +) |
| 27 | + |
| 28 | +// The function must return |
| 29 | +// 1 if the fuzzer should increase priority of the |
| 30 | +// given input during subsequent fuzzing (for example, the input is lexically |
| 31 | +// correct and was parsed successfully); |
| 32 | +// -1 if the input must not be added to corpus even if gives new coverage; and |
| 33 | +// 0 otherwise |
| 34 | +// other values are reserved for future use. |
| 35 | +func Fuzz(input []byte) int { |
| 36 | + if len(input) <= 96 { |
| 37 | + return -1 |
| 38 | + } |
| 39 | + // Abort on too expensive inputs |
| 40 | + precomp := vm.PrecompiledContractsBerlin[common.BytesToAddress([]byte{5})] |
| 41 | + if gas := precomp.RequiredGas(input); gas > 40_000_000 { |
| 42 | + return 0 |
| 43 | + } |
| 44 | + var ( |
| 45 | + baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64() |
| 46 | + expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64() |
| 47 | + modLen = new(big.Int).SetBytes(getData(input, 64, 32)).Uint64() |
| 48 | + ) |
| 49 | + // Handle a special case when both the base and mod length is zero |
| 50 | + if baseLen == 0 && modLen == 0 { |
| 51 | + return -1 |
| 52 | + } |
| 53 | + input = input[96:] |
| 54 | + // Retrieve the operands and execute the exponentiation |
| 55 | + var ( |
| 56 | + base = new(big.Int).SetBytes(getData(input, 0, baseLen)) |
| 57 | + exp = new(big.Int).SetBytes(getData(input, baseLen, expLen)) |
| 58 | + mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen)) |
| 59 | + ) |
| 60 | + if mod.BitLen() == 0 { |
| 61 | + // Modulo 0 is undefined, return zero |
| 62 | + return -1 |
| 63 | + } |
| 64 | + var a = math.FastExp(new(big.Int).Set(base), new(big.Int).Set(exp), new(big.Int).Set(mod)) |
| 65 | + var b = base.Exp(base, exp, mod) |
| 66 | + if a.Cmp(b) != 0 { |
| 67 | + panic(fmt.Sprintf("Inequality %x != %x", a, b)) |
| 68 | + } |
| 69 | + return 1 |
| 70 | +} |
| 71 | + |
| 72 | +// getData returns a slice from the data based on the start and size and pads |
| 73 | +// up to size with zero's. This function is overflow safe. |
| 74 | +func getData(data []byte, start uint64, size uint64) []byte { |
| 75 | + length := uint64(len(data)) |
| 76 | + if start > length { |
| 77 | + start = length |
| 78 | + } |
| 79 | + end := start + size |
| 80 | + if end > length { |
| 81 | + end = length |
| 82 | + } |
| 83 | + return common.RightPadBytes(data[start:end], int(size)) |
| 84 | +} |
0 commit comments