-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_test.go
94 lines (77 loc) · 2.65 KB
/
math_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
// Copyright 2018 ProximaX Limited. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package crypto
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMathUtils_AddGroupElements_Neutral(t *testing.T) {
neutral := NewEd25519GroupElementP3(
Ed25519FieldZero(),
Ed25519FieldOne(),
Ed25519FieldOne(),
Ed25519FieldZero())
for i := 0; i < 0; i++ {
g := MathUtils.GetRandomGroupElement()
h1 := MathUtils.AddGroupElements(g, neutral)
h2 := MathUtils.AddGroupElements(neutral, g)
assert.True(t, g.Equals(h1), `g and h1 must by equal !`)
assert.True(t, g.Equals(h2), `g and h2 must by equal !`)
}
}
// Simple test for verifying that the MathUtils code works as expected.
func TestMathUtilsWorkAsExpected(t *testing.T) {
for i := 0; i < numIter; i++ {
g := MathUtils.GetRandomGroupElement()
// P3 -> P2.
h, err := MathUtils.ToRepresentation(g, P2)
assert.Nil(t, err)
assert.True(t, h.Equals(g), `h and g must by equal !`)
// P3 -> P1xP1.
h, err = MathUtils.ToRepresentation(g, P1xP1)
assert.Nil(t, err)
assert.True(t, g.Equals(h), `g and h must by equal !`)
// P3 -> CACHED.
h, err = MathUtils.ToRepresentation(g, CACHED)
assert.Nil(t, err)
assert.True(t, h.Equals(g), `h and g must by equal !`)
// P3 -> P2 -> P3.
g, err = MathUtils.ToRepresentation(g, P2)
assert.Nil(t, err)
h, err = MathUtils.ToRepresentation(g, P3)
assert.Nil(t, err)
assert.True(t, g.Equals(h), `g and h must by equal !`)
// P3 -> P2 -> P1xP1.
g, err = MathUtils.ToRepresentation(g, P2)
assert.Nil(t, err)
h, err = MathUtils.ToRepresentation(g, P1xP1)
assert.Nil(t, err)
assert.True(t, g.Equals(h), `g and h must by equal !`)
}
}
func TestMathUtils_ScalarMultiplyGroupElement(t *testing.T) {
for i := 0; i < 10; i++ {
g := MathUtils.GetRandomGroupElement()
h := MathUtils.ScalarMultiplyGroupElement(g, Ed25519Field.ZERO)
assert.True(t, Ed25519Group.ZERO_P3().Equals(h), `Ed25519Group.ZERO_P3 and h must by equal !`)
}
}
func TestMath_PrecomputedTableContainsExpectedGroupElements(t *testing.T) {
defer testRecover(t)
grEl := Ed25519Group.BASE_POINT()
for i := 0; i < numIter; i++ {
g, err := MathUtils.ToRepresentation(grEl, PRECOMPUTED)
assert.Nil(t, err)
assert.True(t, grEl.precomputedForSingle[0][0].Equals(g), "iter = %d", i)
}
}
func TestMathUtils_ReduceModGroupOrder(t *testing.T) {
defer testRecover(t)
for i := 0; i < numIter; i++ {
encoded := MathUtils.GetRandomEncodedFieldElement(64)
reduced1 := encoded.modQ()
reduced2 := MathUtils.ReduceModGroupOrder(encoded)
assert.True(t, reduced2.Equals(reduced1), "iter = %d", i)
}
}