1
2
3
4
5
6
7 package nistec
8
9 import (
10 "bytes"
11 "crypto/internal/fips140/nistec/fiat"
12 "fmt"
13 "testing"
14 )
15
16 func TestP256PrecomputedTable(t *testing.T) {
17 base := NewP256Point().SetGenerator()
18
19 for i := 0; i < 43; i++ {
20 t.Run(fmt.Sprintf("table[%d]", i), func(t *testing.T) {
21 testP256AffineTable(t, base, &p256GeneratorTables[i])
22 })
23
24 for k := 0; k < 6; k++ {
25 base.Double(base)
26 }
27 }
28 }
29
30 func testP256AffineTable(t *testing.T, base *P256Point, table *p256AffineTable) {
31 p := NewP256Point()
32 zInv := new(fiat.P256Element)
33
34 for j := 0; j < 32; j++ {
35 p.Add(p, base)
36
37
38 zInv.Invert(&p.z)
39 p.x.Mul(&p.x, zInv)
40 p.y.Mul(&p.y, zInv)
41 p.z.One()
42
43 if !bytes.Equal(table[j].x.Bytes(), p.x.Bytes()) ||
44 !bytes.Equal(table[j].y.Bytes(), p.y.Bytes()) {
45 t.Fatalf("incorrect table entry at index %d", j)
46 }
47 }
48 }
49
View as plain text