Browse Source

Move the benchmarks to a different file and refactor a bit.

Yawning Angel 2 years ago
parent
commit
fc9c254c66
2 changed files with 98 additions and 96 deletions
  1. 98 0
      benchmark_test.go
  2. 0 96
      newhope_test.go

+ 98 - 0
benchmark_test.go

@@ -0,0 +1,98 @@
+// benchmark_test.go - NewHope benchmarks.
+//
+// To the extent possible under law, Yawning Angel has waived all copyright
+// and related or neighboring rights to newhope, using the Creative
+// Commons "CC0" public domain dedication. See LICENSE or
+// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
+
+package newhope
+
+import (
+	"bytes"
+	"crypto/rand"
+	"testing"
+)
+
+func benchGenerateKeyPairAlice(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		GenerateKeyPairAlice(rand.Reader)
+	}
+}
+
+func benchKeyExchangeAlice(b *testing.B) {
+	b.StopTimer()
+	for i := 0; i < b.N; i++ {
+		// Generate Alice's key's.
+		alicePriv, alicePub, err := GenerateKeyPairAlice(rand.Reader)
+		if err != nil {
+			b.Fatalf("GenerateKeyPairAlice failed: %v", err)
+		}
+
+		// Finish Bob's handshake.
+		bobPub, bobShared, err := KeyExchangeBob(rand.Reader, alicePub)
+		if err != nil {
+			b.Fatalf("KeyExchangeBob failed: %v", err)
+		}
+
+		// Finish Alice's handshake.
+		b.StartTimer()
+		aliceShared, err := KeyExchangeAlice(bobPub, alicePriv)
+		if err != nil {
+			b.Fatalf("KeyExchangeAlice failed: %v", err)
+		}
+		b.StopTimer()
+
+		// Sanity check.
+		if !bytes.Equal(aliceShared, bobShared) {
+			b.Fatalf("shared secrets mismatched")
+		}
+	}
+}
+
+func benchKeyExchangeBob(b *testing.B) {
+	b.StopTimer()
+	for i := 0; i < b.N; i++ {
+		// Generate Alice's key's.
+		alicePriv, alicePub, err := GenerateKeyPairAlice(rand.Reader)
+		if err != nil {
+			b.Fatalf("GenerateKeyPairAlice failed: %v", err)
+		}
+
+		// Finish Bob's handshake.
+		b.StartTimer()
+		bobPub, bobShared, err := KeyExchangeBob(rand.Reader, alicePub)
+		if err != nil {
+			b.Fatalf("KeyExchangeBob failed: %v", err)
+		}
+		b.StopTimer()
+
+		// Finish Alice's handshake.
+		aliceShared, err := KeyExchangeAlice(bobPub, alicePriv)
+		if err != nil {
+			b.Fatalf("KeyExchangeAlice failed: %v", err)
+		}
+
+		// Sanity check.
+		if !bytes.Equal(aliceShared, bobShared) {
+			b.Fatalf("shared secrets mismatched")
+		}
+	}
+
+}
+
+func BenchmarkNewHope(b *testing.B) {
+	TorSampling = false
+	b.Run("GenerateKeyPairAlice", benchGenerateKeyPairAlice)
+	b.Run("KeyExchangeAlice", benchKeyExchangeAlice)
+	b.Run("KeyExchangeBob", benchKeyExchangeBob)
+}
+
+func BenchmarkNewHopeTor(b *testing.B) {
+	if testing.Short() {
+		b.SkipNow()
+	}
+	TorSampling = true
+	b.Run("GenerateKeyPairAlice", benchGenerateKeyPairAlice)
+	b.Run("KeyExchangeAlice", benchKeyExchangeAlice)
+	b.Run("KeyExchangeBob", benchKeyExchangeBob)
+}

+ 0 - 96
newhope_test.go

@@ -13,102 +13,6 @@ import (
 	"testing"
 )
 
-func benchmarkKeyGen(b *testing.B) {
-	for i := 0; i < b.N; i++ {
-		GenerateKeyPairAlice(rand.Reader)
-	}
-}
-
-func BenchmarkKeyGen(b *testing.B) {
-	TorSampling = false
-	benchmarkKeyGen(b)
-}
-
-func BenchmarkKeyGenTor(b *testing.B) {
-	TorSampling = true
-	benchmarkKeyGen(b)
-}
-
-func benchmarkAlice(b *testing.B) {
-	for i := 0; i < b.N; i++ {
-		// Generate Alice's key's.
-		alicePriv, alicePub, err := GenerateKeyPairAlice(rand.Reader)
-		if err != nil {
-			b.Fatalf("GenerateKeyPairAlice failed: %v", err)
-		}
-
-		// Finish Bob's handshake.
-		b.StopTimer()
-		bobPub, bobShared, err := KeyExchangeBob(rand.Reader, alicePub)
-		if err != nil {
-			b.Fatalf("KeyExchangeBob failed: %v", err)
-		}
-		b.StartTimer()
-
-		// Finish Alice's handshake.
-		aliceShared, err := KeyExchangeAlice(bobPub, alicePriv)
-		if err != nil {
-			b.Fatalf("KeyExchangeAlice failed: %v", err)
-		}
-
-		b.StopTimer()
-		if !bytes.Equal(aliceShared, bobShared) {
-			b.Fatalf("shared secrets mismatched")
-		}
-		b.StartTimer()
-	}
-}
-
-func BenchmarkAlice(b *testing.B) {
-	TorSampling = false
-	benchmarkAlice(b)
-}
-
-func BenchmarkAliceTor(b *testing.B) {
-	TorSampling = true
-	benchmarkAlice(b)
-}
-
-func benchmarkBob(b *testing.B) {
-	for i := 0; i < b.N; i++ {
-		b.StopTimer()
-
-		// Generate Alice's key's.
-		alicePriv, alicePub, err := GenerateKeyPairAlice(rand.Reader)
-		if err != nil {
-			b.Fatalf("GenerateKeyPairAlice failed: %v", err)
-		}
-
-		// Finish Bob's handshake.
-		b.StartTimer()
-		bobPub, bobShared, err := KeyExchangeBob(rand.Reader, alicePub)
-		if err != nil {
-			b.Fatalf("KeyExchangeBob failed: %v", err)
-		}
-		b.StopTimer()
-
-		// Finish Alice's handshake.
-		aliceShared, err := KeyExchangeAlice(bobPub, alicePriv)
-		if err != nil {
-			b.Fatalf("KeyExchangeAlice failed: %v", err)
-		}
-
-		if !bytes.Equal(aliceShared, bobShared) {
-			b.Fatalf("shared secrets mismatched")
-		}
-	}
-}
-
-func BenchmarkBob(b *testing.B) {
-	TorSampling = false
-	benchmarkBob(b)
-}
-
-func BenchmarkBobTor(b *testing.B) {
-	TorSampling = true
-	benchmarkBob(b)
-}
-
 func testIntegration(t *testing.T) {
 	for i := 0; i < 1024; i++ {
 		// Generate Alice's key's.