-
Notifications
You must be signed in to change notification settings - Fork 0
/
more_math.go
137 lines (112 loc) · 3.17 KB
/
more_math.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
_ "embed"
"fmt"
"math/big"
"math/rand"
"os"
"strings"
"github.com/spf13/cobra"
)
//go:embed more_math.move.template
var moreMathTemplateText string
// MoreMath are math utilities functions that don't introduce new types.
type MoreMath struct {
UintWidth uint
Address string
Args string
Version string
DoTest bool
}
type SqrtTestCase struct {
Squared string
Root string
}
type MoreMathGenerated struct {
MoreMath
ModuleName string
UintTypeName string
AllOnes string
HalfWidth uint
MaxShiftSize uint
UnrolledForLeadingZeros []unrolledUpper
SqrtTestCases []SqrtTestCase
}
func NewSqrtTestCase(b *big.Int) SqrtTestCase {
c := big.NewInt(0).Sqrt(b)
return SqrtTestCase{
Squared: b.String(),
Root: c.String(),
}
}
func NewMoreMathGenerated(m MoreMath) *MoreMathGenerated {
result := &MoreMathGenerated{
MoreMath: m,
}
result.ModuleName = fmt.Sprintf("more_math_u%d", m.UintWidth)
result.UintTypeName = fmt.Sprintf("u%d", m.UintWidth)
result.AllOnes = big.NewInt(0).Sub(big.NewInt(0).Lsh(big.NewInt(1), m.UintWidth), big.NewInt(1)).String()
result.HalfWidth = m.UintWidth / 2
result.MaxShiftSize = m.UintWidth - 1
for n := result.UintWidth >> 1; n > 0; n = n >> 1 {
result.UnrolledForLeadingZeros = append(result.UnrolledForLeadingZeros, newUnrolledUpper(n, result.UintWidth))
}
r := rand.New(rand.NewSource(0))
upperBound := big.NewInt(0).Lsh(big.NewInt(1), m.UintWidth)
zero := big.NewInt(0)
one := big.NewInt(1)
t := big.NewInt(0)
nTests := int(2 * m.UintWidth)
if nTests > 256 {
nTests = 256
}
for i := 0; i < nTests; i++ {
t = t.Rand(r, upperBound)
result.SqrtTestCases = append(result.SqrtTestCases, NewSqrtTestCase(t))
t = t.Sqrt(t)
t = t.Mul(t, t)
if t.Cmp(zero) > 0 {
t = t.Sub(t, one)
}
result.SqrtTestCases = append(result.SqrtTestCases, NewSqrtTestCase(t))
}
return result
}
func (m MoreMathGenerated) GenText() (string, error) {
return GenText(m.ModuleName, moreMathTemplateText, m)
}
func newMoreMathCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "more-math",
Short: "more math utils for move-lang",
Args: cobra.NoArgs,
}
output := "./sources/more.move"
address := "more_math"
width := newIntWidth([]uint{8, 16, 32, 64, 128, 256})
doTest := false
cmd.Flags().StringVarP(&output, "out", "o", output, "output file. this should be in teh sources folder of your move package")
cmd.MarkFlagFilename("out")
cmd.Flags().StringVarP(&address, "address", "p", address, "(named) address")
cmd.Flags().VarP(width, "width", "w", fmt.Sprintf("int widths, must be one of %s. can be specified multiple times.", sliceToString(width.permitted)))
cmd.Flags().BoolVarP(&doTest, "do-test", "t", false, "generate test code")
cmd.Run = func(*cobra.Command, []string) {
allTexts := []string{}
for _, w := range width.values {
s := MoreMath{
UintWidth: w,
Address: address,
Args: strings.Join(os.Args[1:], " "),
Version: version,
DoTest: doTest,
}
code, err := NewMoreMathGenerated(s).GenText()
if err != nil {
panic(err)
}
allTexts = append(allTexts, code)
}
os.WriteFile(output, []byte(strings.Join(allTexts, "\n")), 0o666)
}
return cmd
}