• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5//go:build !purego && (amd64 || arm64 || ppc64le || s390x)
6
7package nistec
8
9import (
10	"fmt"
11	"testing"
12)
13
14func TestP256PrecomputedTable(t *testing.T) {
15	base := NewP256Point().SetGenerator()
16
17	for i := 0; i < 43; i++ {
18		t.Run(fmt.Sprintf("table[%d]", i), func(t *testing.T) {
19			testP256AffineTable(t, base, &p256Precomputed[i])
20		})
21
22		for k := 0; k < 6; k++ {
23			base.Double(base)
24		}
25	}
26}
27
28func testP256AffineTable(t *testing.T, base *P256Point, table *p256AffineTable) {
29	p := NewP256Point()
30	zInv := new(p256Element)
31	zInvSq := new(p256Element)
32
33	for j := 0; j < 32; j++ {
34		p.Add(p, base)
35
36		// Convert p to affine coordinates.
37		p256Inverse(zInv, &p.z)
38		p256Sqr(zInvSq, zInv, 1)
39		p256Mul(zInv, zInv, zInvSq)
40
41		p256Mul(&p.x, &p.x, zInvSq)
42		p256Mul(&p.y, &p.y, zInv)
43		p.z = p256One
44
45		if p256Equal(&table[j].x, &p.x) != 1 || p256Equal(&table[j].y, &p.y) != 1 {
46			t.Fatalf("incorrect table entry at index %d", j)
47		}
48	}
49}
50