• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16
17package mathfuzzer
18
19import "math/big"
20
21func FuzzBigIntCmp1(data []byte) int {
22    if !isDivisibleBy(len(data), 2) {
23        return -1
24    }
25    i1 := new(big.Int)
26    i2 := new(big.Int)
27
28    half := len(data) / 2
29
30    halfOne := data[:half]
31    halfTwo := data[half:]
32
33    i1.SetBytes(halfOne)
34    i2.SetBytes(halfTwo)
35
36    i1.Cmp(i2)
37    return 1
38}
39
40func FuzzBigIntCmp2(data []byte) int {
41    if !isDivisibleBy(len(data), 2) {
42        return -1
43    }
44    x, y := new(big.Int), new(big.Int)
45    half := len(data)/2
46    if err := x.UnmarshalText(data[:half]); err != nil {
47      return 0
48    }
49    if err := y.UnmarshalText(data[half:]); err != nil {
50      return 0
51    }
52    x.Cmp(y)
53    return 1
54}
55
56func FuzzRatSetString(data []byte) int {
57    _, _ = new(big.Rat).SetString(string(data))
58    return 1
59}
60
61func isDivisibleBy(n int, divisibleby int) bool {
62    return (n % divisibleby) == 0
63}
64