x25519ell2_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (c) 2021 Yawning Angel <yawning at schwanenlied dot me>
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. package x25519ell2
  16. import (
  17. "bytes"
  18. "crypto/rand"
  19. "testing"
  20. "filippo.io/edwards25519/field"
  21. "golang.org/x/crypto/curve25519"
  22. )
  23. func TestX25519Ell2(t *testing.T) {
  24. t.Run("Constants", testConstants)
  25. t.Run("KeyExchage", testKeyExchange)
  26. }
  27. func testConstants(t *testing.T) {
  28. // While the constants were calculated and serialized with a known
  29. // correct implementation of the field arithmetic, re-derive them
  30. // to be sure.
  31. t.Run("NegTwo", func(t *testing.T) {
  32. expected := new(field.Element).Add(feOne, feOne)
  33. expected.Negate(expected)
  34. if expected.Equal(feNegTwo) != 1 {
  35. t.Fatalf("invalid value for -2: %x", feNegTwo.Bytes())
  36. }
  37. })
  38. t.Run("LopX", func(t *testing.T) {
  39. // d = -121665/121666
  40. d := mustFeFromUint64(121666)
  41. d.Invert(d)
  42. d.Multiply(d, mustFeFromUint64(121665))
  43. d.Negate(d)
  44. // lop_x = sqrt((sqrt(d + 1) + 1) / d)
  45. expected := new(field.Element).Add(d, feOne)
  46. expected.Invert(expected)
  47. expected.SqrtRatio(feOne, expected)
  48. expected.Add(expected, feOne)
  49. expected.SqrtRatio(expected, d)
  50. if expected.Equal(feLopX) != 1 {
  51. t.Fatalf("invalid value for low order point X: %x", feLopX.Bytes())
  52. }
  53. })
  54. t.Run("LopY", func(t *testing.T) {
  55. // lop_y = -lop_x * sqrtm1
  56. expected := new(field.Element).Negate(feLopX)
  57. expected.Multiply(expected, feSqrtM1)
  58. if expected.Equal(feLopY) != 1 {
  59. t.Fatalf("invalid value for low order point Y: %x", feLopY.Bytes())
  60. }
  61. })
  62. }
  63. func testKeyExchange(t *testing.T) {
  64. var randSk [32]byte
  65. _, _ = rand.Read(randSk[:])
  66. var good, bad int
  67. for i := 0; i < 1000; i++ {
  68. var (
  69. publicKey, privateKey, representative [32]byte
  70. publicKeyClean [32]byte
  71. tweak [1]byte
  72. )
  73. _, _ = rand.Read(privateKey[:])
  74. _, _ = rand.Read(tweak[:])
  75. // This won't match the public key from the Elligator2-ed scalar
  76. // basepoint multiply, but we want to ensure that the public keys
  77. // we do happen to generate are interoperable (otherwise something
  78. // is badly broken).
  79. curve25519.ScalarBaseMult(&publicKeyClean, &privateKey)
  80. if !ScalarBaseMult(&publicKey, &representative, &privateKey, tweak[0]) {
  81. t.Logf("bad: %x", privateKey)
  82. bad++
  83. continue
  84. }
  85. t.Logf("good: %x", privateKey)
  86. t.Logf("publicKey: %x, repr: %x", publicKey, representative)
  87. var shared, sharedRep, sharedClean, pkFromRep [32]byte
  88. RepresentativeToPublicKey(&pkFromRep, &representative)
  89. if !bytes.Equal(pkFromRep[:], publicKey[:]) {
  90. t.Fatalf("public key mismatch(repr): expected %x, actual: %x", publicKey, pkFromRep)
  91. }
  92. curve25519.ScalarMult(&sharedClean, &randSk, &publicKeyClean) //nolint: staticcheck
  93. curve25519.ScalarMult(&shared, &randSk, &publicKey) //nolint: staticcheck
  94. curve25519.ScalarMult(&sharedRep, &randSk, &pkFromRep) //nolint: staticcheck
  95. if !bytes.Equal(shared[:], sharedRep[:]) {
  96. t.Fatalf("shared secret mismatch: expected %x, actual: %x", shared, sharedRep)
  97. }
  98. if !bytes.Equal(shared[:], sharedClean[:]) {
  99. t.Fatalf("shared secret mismatch(clean): expected %x, actual: %x", shared, sharedClean)
  100. }
  101. good++
  102. }
  103. t.Logf("good: %d, bad: %d", good, bad)
  104. }
  105. func BenchmarkKeyGeneration(b *testing.B) {
  106. var publicKey, representative, privateKey [32]byte
  107. // Find the private key that results in a point that's in the image of the map.
  108. for {
  109. _, _ = rand.Reader.Read(privateKey[:])
  110. if ScalarBaseMult(&publicKey, &representative, &privateKey, 0) {
  111. break
  112. }
  113. }
  114. b.ResetTimer()
  115. for i := 0; i < b.N; i++ {
  116. ScalarBaseMult(&publicKey, &representative, &privateKey, 0)
  117. }
  118. }
  119. func BenchmarkMap(b *testing.B) {
  120. var publicKey, representative [32]byte
  121. _, _ = rand.Reader.Read(representative[:])
  122. b.ResetTimer()
  123. for i := 0; i < b.N; i++ {
  124. RepresentativeToPublicKey(&publicKey, &representative)
  125. }
  126. }