forked from cronokirby/saferith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_test.go
200 lines (173 loc) · 4.22 KB
/
int_test.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package saferith
import (
"bytes"
"math/rand"
"reflect"
"testing"
"testing/quick"
)
func (*Int) Generate(r *rand.Rand, size int) reflect.Value {
bytes := make([]byte, r.Int()&127)
r.Read(bytes)
i := new(Int).SetBytes(bytes)
if r.Int()&1 == 1 {
i.Neg(1)
}
return reflect.ValueOf(i)
}
func testIntEqualReflexive(z *Int) bool {
return z.Eq(z) == 1
}
func TestIntEqualReflexive(t *testing.T) {
err := quick.Check(testIntEqualReflexive, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testIntMulCommutative(x, y *Int) bool {
way1 := new(Int).Mul(x, y, -1)
way2 := new(Int).Mul(y, x, -1)
return way1.Eq(way2) == 1
}
func TestIntMulCommutative(t *testing.T) {
err := quick.Check(testIntMulCommutative, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testIntMulZeroIsZero(x *Int) bool {
zero := new(Int)
timesZero := new(Int).Mul(zero, x, -1)
return timesZero.Eq(zero) == 1
}
func TestIntMulZeroIsZero(t *testing.T) {
err := quick.Check(testIntMulZeroIsZero, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testIntMulNegativeOneIsNeg(x *Int) bool {
minusOne := new(Int).SetUint64(1).Neg(1)
way1 := new(Int).SetInt(x).Neg(1)
way2 := new(Int).Mul(x, minusOne, -1)
return way1.Eq(way2) == 1
}
func TestIntMulNegativeOneIsNeg(t *testing.T) {
err := quick.Check(testIntMulNegativeOneIsNeg, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testIntModAddNegReturnsZero(x *Int, m Modulus) bool {
a := new(Int).SetInt(x).Neg(1).Mod(&m)
b := x.Mod(&m)
return b.ModAdd(a, b, &m).EqZero() == 1
}
func TestIntModAddNegReturnsZero(t *testing.T) {
err := quick.Check(testIntModAddNegReturnsZero, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testIntModRoundtrip(x Nat, m Modulus) bool {
xModM := new(Nat).Mod(&x, &m)
i := new(Int).SetModSymmetric(xModM, &m)
if i.CheckInRange(&m) != 1 {
return false
}
roundTrip := i.Mod(&m)
return xModM.Eq(roundTrip) == 1
}
func TestIntModRoundtrip(t *testing.T) {
err := quick.Check(testIntModRoundtrip, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testIntAddNegZero(i *Int) bool {
zero := new(Int)
neg := new(Int).SetInt(i).Neg(1)
shouldBeZero := new(Int).Add(i, neg, -1)
return shouldBeZero.Eq(zero) == 1
}
func TestIntAddNegZero(t *testing.T) {
err := quick.Check(testIntAddNegZero, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testIntAddCommutative(x *Int, y *Int) bool {
way1 := new(Int).Add(x, y, -1)
way2 := new(Int).Add(x, y, -1)
return way1.Eq(way2) == 1
}
func TestIntAddCommutative(t *testing.T) {
err := quick.Check(testIntAddCommutative, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testIntAddZeroIdentity(x *Int) bool {
zero := new(Int)
shouldBeX := new(Int).Add(x, zero, -1)
return shouldBeX.Eq(x) == 1
}
func TestIntAddZeroIdentity(t *testing.T) {
err := quick.Check(testIntAddZeroIdentity, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func TestCheckInRangeExamples(t *testing.T) {
x := new(Int).SetUint64(0)
m := ModulusFromUint64(13)
if x.CheckInRange(m) != 1 {
t.Errorf("expected zero to be in range of modulus")
}
}
func TestIntAddExamples(t *testing.T) {
x := new(Int).SetUint64(3).Resize(8)
y := new(Int).SetUint64(4).Neg(1).Resize(8)
expected := new(Int).SetUint64(1).Neg(1)
actual := new(Int).Add(x, y, -1)
if expected.Eq(actual) != 1 {
t.Errorf("%+v != %+v", expected, actual)
}
}
func testIntMarshalBinaryRoundTrip(x *Int) bool {
out, err := x.MarshalBinary()
if err != nil {
return false
}
y := new(Int)
err = y.UnmarshalBinary(out)
if err != nil {
return false
}
return x.Eq(y) == 1
}
func TestIntMarshalBinaryRoundTrip(t *testing.T) {
err := quick.Check(testIntMarshalBinaryRoundTrip, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func testInvalidInt(expected []byte) bool {
x := new(Int)
err := x.UnmarshalBinary(expected)
// empty slice is invalid, so we expect an error
if len(expected) == 0 {
return err != nil
}
expectedBytes := expected[1:]
expectedSign := Choice(expected[0]) & 1
actualBytes := x.Abs().Bytes()
actualSign := x.sign
return (expectedSign == actualSign) && bytes.Equal(expectedBytes, actualBytes)
}
func TestInvalidInt(t *testing.T) {
err := quick.Check(testInvalidInt, &quick.Config{})
if err != nil {
t.Error(err)
}
}