Source file src/crypto/internal/fips140/edwards25519/tables_test.go

     1  // Copyright (c) 2019 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  package edwards25519
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestProjLookupTable(t *testing.T) {
    12  	var table projLookupTable
    13  	table.FromP3(B)
    14  
    15  	var tmp1, tmp2, tmp3 projCached
    16  	table.SelectInto(&tmp1, 6)
    17  	table.SelectInto(&tmp2, -2)
    18  	table.SelectInto(&tmp3, -4)
    19  	// Expect T1 + T2 + T3 = identity
    20  
    21  	var accP1xP1 projP1xP1
    22  	accP3 := NewIdentityPoint()
    23  
    24  	accP1xP1.Add(accP3, &tmp1)
    25  	accP3.fromP1xP1(&accP1xP1)
    26  	accP1xP1.Add(accP3, &tmp2)
    27  	accP3.fromP1xP1(&accP1xP1)
    28  	accP1xP1.Add(accP3, &tmp3)
    29  	accP3.fromP1xP1(&accP1xP1)
    30  
    31  	if accP3.Equal(I) != 1 {
    32  		t.Errorf("Consistency check on ProjLookupTable.SelectInto failed!  %x %x %x", tmp1, tmp2, tmp3)
    33  	}
    34  }
    35  
    36  func TestAffineLookupTable(t *testing.T) {
    37  	var table affineLookupTable
    38  	table.FromP3(B)
    39  
    40  	var tmp1, tmp2, tmp3 affineCached
    41  	table.SelectInto(&tmp1, 3)
    42  	table.SelectInto(&tmp2, -7)
    43  	table.SelectInto(&tmp3, 4)
    44  	// Expect T1 + T2 + T3 = identity
    45  
    46  	var accP1xP1 projP1xP1
    47  	accP3 := NewIdentityPoint()
    48  
    49  	accP1xP1.AddAffine(accP3, &tmp1)
    50  	accP3.fromP1xP1(&accP1xP1)
    51  	accP1xP1.AddAffine(accP3, &tmp2)
    52  	accP3.fromP1xP1(&accP1xP1)
    53  	accP1xP1.AddAffine(accP3, &tmp3)
    54  	accP3.fromP1xP1(&accP1xP1)
    55  
    56  	if accP3.Equal(I) != 1 {
    57  		t.Errorf("Consistency check on ProjLookupTable.SelectInto failed!  %x %x %x", tmp1, tmp2, tmp3)
    58  	}
    59  }
    60  
    61  func TestNafLookupTable5(t *testing.T) {
    62  	var table nafLookupTable5
    63  	table.FromP3(B)
    64  
    65  	var tmp1, tmp2, tmp3, tmp4 projCached
    66  	table.SelectInto(&tmp1, 9)
    67  	table.SelectInto(&tmp2, 11)
    68  	table.SelectInto(&tmp3, 7)
    69  	table.SelectInto(&tmp4, 13)
    70  	// Expect T1 + T2 = T3 + T4
    71  
    72  	var accP1xP1 projP1xP1
    73  	lhs := NewIdentityPoint()
    74  	rhs := NewIdentityPoint()
    75  
    76  	accP1xP1.Add(lhs, &tmp1)
    77  	lhs.fromP1xP1(&accP1xP1)
    78  	accP1xP1.Add(lhs, &tmp2)
    79  	lhs.fromP1xP1(&accP1xP1)
    80  
    81  	accP1xP1.Add(rhs, &tmp3)
    82  	rhs.fromP1xP1(&accP1xP1)
    83  	accP1xP1.Add(rhs, &tmp4)
    84  	rhs.fromP1xP1(&accP1xP1)
    85  
    86  	if lhs.Equal(rhs) != 1 {
    87  		t.Errorf("Consistency check on nafLookupTable5 failed")
    88  	}
    89  }
    90  
    91  func TestNafLookupTable8(t *testing.T) {
    92  	var table nafLookupTable8
    93  	table.FromP3(B)
    94  
    95  	var tmp1, tmp2, tmp3, tmp4 affineCached
    96  	table.SelectInto(&tmp1, 49)
    97  	table.SelectInto(&tmp2, 11)
    98  	table.SelectInto(&tmp3, 35)
    99  	table.SelectInto(&tmp4, 25)
   100  	// Expect T1 + T2 = T3 + T4
   101  
   102  	var accP1xP1 projP1xP1
   103  	lhs := NewIdentityPoint()
   104  	rhs := NewIdentityPoint()
   105  
   106  	accP1xP1.AddAffine(lhs, &tmp1)
   107  	lhs.fromP1xP1(&accP1xP1)
   108  	accP1xP1.AddAffine(lhs, &tmp2)
   109  	lhs.fromP1xP1(&accP1xP1)
   110  
   111  	accP1xP1.AddAffine(rhs, &tmp3)
   112  	rhs.fromP1xP1(&accP1xP1)
   113  	accP1xP1.AddAffine(rhs, &tmp4)
   114  	rhs.fromP1xP1(&accP1xP1)
   115  
   116  	if lhs.Equal(rhs) != 1 {
   117  		t.Errorf("Consistency check on nafLookupTable8 failed")
   118  	}
   119  }
   120  

View as plain text