Source file src/crypto/internal/fips140/nistec/p256_table_test.go

     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 (!amd64 && !arm64 && !ppc64le && !s390x) || purego
     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  		// Convert p to affine coordinates.
    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