Last active
July 25, 2018 00:05
-
-
Save cjpatton/6b72bf72c6134e4b7f882142fbed1b0a to your computer and use it in GitHub Desktop.
In Go, P256 is twice as fast as Ed25519
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// chris@cloudtron:~/go/src/fun$ go test -bench . | |
// goos: linux | |
// goarch: amd64 | |
// pkg: fun | |
// BenchmarkSignP256-4 50000 32375 ns/op | |
// BenchmarkVerifyP256-4 20000 90285 ns/op | |
// BenchmarkSignEd25519-4 20000 62050 ns/op | |
// BenchmarkVerifyEd25519-4 10000 167410 ns/op | |
package main | |
import ( | |
"crypto" | |
"crypto/ecdsa" | |
"crypto/elliptic" | |
"crypto/rand" | |
"crypto/sha256" | |
"encoding/asn1" | |
"math/big" | |
"testing" | |
"golang.org/x/crypto/ed25519" | |
) | |
var msg = []byte("Let's just see how this all works out then why don't we?") | |
func BenchmarkSignP256(b *testing.B) { | |
b.ResetTimer() | |
p256 := elliptic.P256() | |
sk, _ := ecdsa.GenerateKey(p256, rand.Reader) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
h := sha256.New() | |
h.Write(msg) | |
_, _ = sk.Sign(rand.Reader, h.Sum(nil), crypto.SHA256) | |
} | |
} | |
type ecdsaSignature struct { | |
R, S *big.Int | |
} | |
func BenchmarkVerifyP256(b *testing.B) { | |
b.ResetTimer() | |
p256 := elliptic.P256() | |
sk, _ := ecdsa.GenerateKey(p256, rand.Reader) | |
pk := sk.Public().(*ecdsa.PublicKey) | |
h := sha256.New() | |
h.Write(msg) | |
der, _ := sk.Sign(rand.Reader, h.Sum(nil), crypto.SHA256) | |
sig := new(ecdsaSignature) | |
_, err := asn1.Unmarshal(der, sig) | |
if err != nil { | |
b.Fatal(err) | |
} | |
h = sha256.New() | |
h.Write(msg) | |
if !ecdsa.Verify(pk, h.Sum(nil), sig.R, sig.S) { | |
b.Fatal("uh oh") | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
sig = new(ecdsaSignature) | |
_, _ = asn1.Unmarshal(der, sig) | |
h = sha256.New() | |
h.Write(msg) | |
_ = ecdsa.Verify(pk, h.Sum(nil), sig.R, sig.S) | |
} | |
} | |
func BenchmarkSignEd25519(b *testing.B) { | |
b.ResetTimer() | |
_, sk, _ := ed25519.GenerateKey(rand.Reader) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_, _ = sk.Sign(rand.Reader, msg, crypto.Hash(0)) | |
} | |
} | |
func BenchmarkVerifyEd25519(b *testing.B) { | |
b.ResetTimer() | |
pk, sk, _ := ed25519.GenerateKey(rand.Reader) | |
sig := ed25519.Sign(sk, msg) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
_ = ed25519.Verify(pk, msg, sig) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment