tables_test.go 2.7 KB

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