summaryrefslogtreecommitdiffstats
path: root/src/crypto/internal/edwards25519/tables_test.go
blob: b5d161ad495146ef51512a0b5bb271bbf995498d (plain)
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
// Copyright (c) 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package edwards25519

import (
	"testing"
)

func TestProjLookupTable(t *testing.T) {
	var table projLookupTable
	table.FromP3(B)

	var tmp1, tmp2, tmp3 projCached
	table.SelectInto(&tmp1, 6)
	table.SelectInto(&tmp2, -2)
	table.SelectInto(&tmp3, -4)
	// Expect T1 + T2 + T3 = identity

	var accP1xP1 projP1xP1
	accP3 := NewIdentityPoint()

	accP1xP1.Add(accP3, &tmp1)
	accP3.fromP1xP1(&accP1xP1)
	accP1xP1.Add(accP3, &tmp2)
	accP3.fromP1xP1(&accP1xP1)
	accP1xP1.Add(accP3, &tmp3)
	accP3.fromP1xP1(&accP1xP1)

	if accP3.Equal(I) != 1 {
		t.Errorf("Consistency check on ProjLookupTable.SelectInto failed!  %x %x %x", tmp1, tmp2, tmp3)
	}
}

func TestAffineLookupTable(t *testing.T) {
	var table affineLookupTable
	table.FromP3(B)

	var tmp1, tmp2, tmp3 affineCached
	table.SelectInto(&tmp1, 3)
	table.SelectInto(&tmp2, -7)
	table.SelectInto(&tmp3, 4)
	// Expect T1 + T2 + T3 = identity

	var accP1xP1 projP1xP1
	accP3 := NewIdentityPoint()

	accP1xP1.AddAffine(accP3, &tmp1)
	accP3.fromP1xP1(&accP1xP1)
	accP1xP1.AddAffine(accP3, &tmp2)
	accP3.fromP1xP1(&accP1xP1)
	accP1xP1.AddAffine(accP3, &tmp3)
	accP3.fromP1xP1(&accP1xP1)

	if accP3.Equal(I) != 1 {
		t.Errorf("Consistency check on ProjLookupTable.SelectInto failed!  %x %x %x", tmp1, tmp2, tmp3)
	}
}

func TestNafLookupTable5(t *testing.T) {
	var table nafLookupTable5
	table.FromP3(B)

	var tmp1, tmp2, tmp3, tmp4 projCached
	table.SelectInto(&tmp1, 9)
	table.SelectInto(&tmp2, 11)
	table.SelectInto(&tmp3, 7)
	table.SelectInto(&tmp4, 13)
	// Expect T1 + T2 = T3 + T4

	var accP1xP1 projP1xP1
	lhs := NewIdentityPoint()
	rhs := NewIdentityPoint()

	accP1xP1.Add(lhs, &tmp1)
	lhs.fromP1xP1(&accP1xP1)
	accP1xP1.Add(lhs, &tmp2)
	lhs.fromP1xP1(&accP1xP1)

	accP1xP1.Add(rhs, &tmp3)
	rhs.fromP1xP1(&accP1xP1)
	accP1xP1.Add(rhs, &tmp4)
	rhs.fromP1xP1(&accP1xP1)

	if lhs.Equal(rhs) != 1 {
		t.Errorf("Consistency check on nafLookupTable5 failed")
	}
}

func TestNafLookupTable8(t *testing.T) {
	var table nafLookupTable8
	table.FromP3(B)

	var tmp1, tmp2, tmp3, tmp4 affineCached
	table.SelectInto(&tmp1, 49)
	table.SelectInto(&tmp2, 11)
	table.SelectInto(&tmp3, 35)
	table.SelectInto(&tmp4, 25)
	// Expect T1 + T2 = T3 + T4

	var accP1xP1 projP1xP1
	lhs := NewIdentityPoint()
	rhs := NewIdentityPoint()

	accP1xP1.AddAffine(lhs, &tmp1)
	lhs.fromP1xP1(&accP1xP1)
	accP1xP1.AddAffine(lhs, &tmp2)
	lhs.fromP1xP1(&accP1xP1)

	accP1xP1.AddAffine(rhs, &tmp3)
	rhs.fromP1xP1(&accP1xP1)
	accP1xP1.AddAffine(rhs, &tmp4)
	rhs.fromP1xP1(&accP1xP1)

	if lhs.Equal(rhs) != 1 {
		t.Errorf("Consistency check on nafLookupTable8 failed")
	}
}