forked from cloudflare/circl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modular_test.go
91 lines (77 loc) · 2.15 KB
/
modular_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
package ed25519
import (
"crypto/rand"
"testing"
"github.com/cloudflare/circl/internal/conv"
"github.com/cloudflare/circl/internal/test"
)
func TestCalculateS(t *testing.T) {
const testTimes = 1 << 10
s := make([]byte, paramB)
k := make([]byte, paramB)
r := make([]byte, paramB)
a := make([]byte, paramB)
orderBig := conv.BytesLe2BigInt(order[:])
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(k[:])
_, _ = rand.Read(r[:])
_, _ = rand.Read(a[:])
bigK := conv.BytesLe2BigInt(k[:])
bigR := conv.BytesLe2BigInt(r[:])
bigA := conv.BytesLe2BigInt(a[:])
calculateS(s, r, k, a)
got := conv.BytesLe2BigInt(s[:])
bigK.Mul(bigK, bigA).Add(bigK, bigR)
want := bigK.Mod(bigK, orderBig)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, k, r, a)
}
}
}
func TestReduction(t *testing.T) {
const testTimes = 1 << 10
var x, y [paramB * 2]byte
orderBig := conv.BytesLe2BigInt(order[:])
for i := 0; i < testTimes; i++ {
for _, j := range []int{paramB, 2 * paramB} {
_, _ = rand.Read(x[:j])
bigX := conv.BytesLe2BigInt(x[:j])
copy(y[:j], x[:j])
reduceModOrder(y[:j], true)
got := conv.BytesLe2BigInt(y[:])
want := bigX.Mod(bigX, orderBig)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x)
}
}
}
}
func TestRangeOrder(t *testing.T) {
aboveOrder := [...][paramB]byte{
{ // order
0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
},
{ // order+1
0xed + 1, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
},
{ // all-ones
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
},
}
for i := range aboveOrder {
got := isLessThanOrder(aboveOrder[i][:])
want := false
if got != want {
test.ReportError(t, got, want, i, aboveOrder[i])
}
}
}