benchmark_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // benchmark_test.go - NewHope benchmarks.
  2. //
  3. // To the extent possible under law, Yawning Angel has waived all copyright
  4. // and related or neighboring rights to newhope, using the Creative
  5. // Commons "CC0" public domain dedication. See LICENSE or
  6. // <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
  7. package newhope
  8. import (
  9. "bytes"
  10. "crypto/rand"
  11. "testing"
  12. )
  13. func benchGenerateKeyPairAlice(b *testing.B) {
  14. for i := 0; i < b.N; i++ {
  15. _, _, _ = GenerateKeyPairAlice(rand.Reader)
  16. }
  17. }
  18. func benchKeyExchangeAlice(b *testing.B) {
  19. b.StopTimer()
  20. for i := 0; i < b.N; i++ {
  21. // Generate Alice's key's.
  22. alicePriv, alicePub, err := GenerateKeyPairAlice(rand.Reader)
  23. if err != nil {
  24. b.Fatalf("GenerateKeyPairAlice failed: %v", err)
  25. }
  26. // Finish Bob's handshake.
  27. bobPub, bobShared, err := KeyExchangeBob(rand.Reader, alicePub)
  28. if err != nil {
  29. b.Fatalf("KeyExchangeBob failed: %v", err)
  30. }
  31. // Finish Alice's handshake.
  32. b.StartTimer()
  33. aliceShared, err := KeyExchangeAlice(bobPub, alicePriv)
  34. if err != nil {
  35. b.Fatalf("KeyExchangeAlice failed: %v", err)
  36. }
  37. b.StopTimer()
  38. // Sanity check.
  39. if !bytes.Equal(aliceShared, bobShared) {
  40. b.Fatalf("shared secrets mismatched")
  41. }
  42. }
  43. }
  44. func benchKeyExchangeBob(b *testing.B) {
  45. b.StopTimer()
  46. for i := 0; i < b.N; i++ {
  47. // Generate Alice's key's.
  48. alicePriv, alicePub, err := GenerateKeyPairAlice(rand.Reader)
  49. if err != nil {
  50. b.Fatalf("GenerateKeyPairAlice failed: %v", err)
  51. }
  52. // Finish Bob's handshake.
  53. b.StartTimer()
  54. bobPub, bobShared, err := KeyExchangeBob(rand.Reader, alicePub)
  55. if err != nil {
  56. b.Fatalf("KeyExchangeBob failed: %v", err)
  57. }
  58. b.StopTimer()
  59. // Finish Alice's handshake.
  60. aliceShared, err := KeyExchangeAlice(bobPub, alicePriv)
  61. if err != nil {
  62. b.Fatalf("KeyExchangeAlice failed: %v", err)
  63. }
  64. // Sanity check.
  65. if !bytes.Equal(aliceShared, bobShared) {
  66. b.Fatalf("shared secrets mismatched")
  67. }
  68. }
  69. }
  70. func BenchmarkNewHope(b *testing.B) {
  71. TorSampling = false
  72. b.Run("GenerateKeyPairAlice", benchGenerateKeyPairAlice)
  73. b.Run("KeyExchangeAlice", benchKeyExchangeAlice)
  74. b.Run("KeyExchangeBob", benchKeyExchangeBob)
  75. }
  76. func BenchmarkNewHopeTor(b *testing.B) {
  77. if testing.Short() {
  78. b.SkipNow()
  79. }
  80. TorSampling = true
  81. b.Run("GenerateKeyPairAlice", benchGenerateKeyPairAlice)
  82. b.Run("KeyExchangeAlice", benchKeyExchangeAlice)
  83. b.Run("KeyExchangeBob", benchKeyExchangeBob)
  84. }
  85. func benchGenerateKeyPairSimpleAlice(b *testing.B) {
  86. for i := 0; i < b.N; i++ {
  87. _, _, _ = GenerateKeyPairSimpleAlice(rand.Reader)
  88. }
  89. }
  90. func benchKeyExchangeSimpleAlice(b *testing.B) {
  91. b.StopTimer()
  92. for i := 0; i < b.N; i++ {
  93. // Generate Alice's key's.
  94. alicePriv, alicePub, err := GenerateKeyPairSimpleAlice(rand.Reader)
  95. if err != nil {
  96. b.Fatalf("GenerateKeyPairSimpleAlice failed: %v", err)
  97. }
  98. // Finish Bob's handshake.
  99. bobPub, bobShared, err := KeyExchangeSimpleBob(rand.Reader, alicePub)
  100. if err != nil {
  101. b.Fatalf("KeyExchangeSimpleBob failed: %v", err)
  102. }
  103. // Finish Alice's handshake.
  104. b.StartTimer()
  105. aliceShared, err := KeyExchangeSimpleAlice(bobPub, alicePriv)
  106. if err != nil {
  107. b.Fatalf("KeyExchangeSimpleAlice failed: %v", err)
  108. }
  109. b.StopTimer()
  110. // Sanity check.
  111. if !bytes.Equal(aliceShared, bobShared) {
  112. b.Fatalf("shared secrets mismatched")
  113. }
  114. }
  115. }
  116. func benchKeyExchangeSimpleBob(b *testing.B) {
  117. b.StopTimer()
  118. for i := 0; i < b.N; i++ {
  119. // Generate Alice's key's.
  120. alicePriv, alicePub, err := GenerateKeyPairSimpleAlice(rand.Reader)
  121. if err != nil {
  122. b.Fatalf("GenerateKeyPairSimpleAlice failed: %v", err)
  123. }
  124. // Finish Bob's handshake.
  125. b.StartTimer()
  126. bobPub, bobShared, err := KeyExchangeSimpleBob(rand.Reader, alicePub)
  127. if err != nil {
  128. b.Fatalf("KeyExchangeSimpleBob failed: %v", err)
  129. }
  130. b.StopTimer()
  131. // Finish Alice's handshake.
  132. aliceShared, err := KeyExchangeSimpleAlice(bobPub, alicePriv)
  133. if err != nil {
  134. b.Fatalf("KeyExchangeSimpleAlice failed: %v", err)
  135. }
  136. // Sanity check.
  137. if !bytes.Equal(aliceShared, bobShared) {
  138. b.Fatalf("shared secrets mismatched")
  139. }
  140. }
  141. }
  142. func BenchmarkNewHopeSimple(b *testing.B) {
  143. TorSampling = false
  144. b.Run("GenerateKeyPairSimpleAlice", benchGenerateKeyPairSimpleAlice)
  145. b.Run("KeyExchangeSimpleAlice", benchKeyExchangeSimpleAlice)
  146. b.Run("KeyExchangeSimpleBob", benchKeyExchangeSimpleBob)
  147. }