Skip to content

Instantly share code, notes, and snippets.

@HirbodBehnam
Created July 10, 2022 11:47
Show Gist options
  • Save HirbodBehnam/32fdd31dbacbeb3b2165e4bfdb4bc480 to your computer and use it in GitHub Desktop.
Save HirbodBehnam/32fdd31dbacbeb3b2165e4bfdb4bc480 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/WolframAlph/dh"
"golang.org/x/crypto/curve25519"
"math/rand"
"testing"
)
func X25519KeyGen() (key []byte, public []byte) {
key = make([]byte, 32)
rand.Read(key)
key[0] &= 248
key[31] &= 127
key[31] |= 64
public, _ = curve25519.X25519(key, curve25519.Basepoint)
return
}
func BenchmarkX25519KeyGen(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = X25519KeyGen()
}
}
func BenchmarkDHKeyGen(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = dh.New()
}
}
func BenchmarkX25519KeyAgreement(b *testing.B) {
alicePrivate, alicePublic := X25519KeyGen()
bobPrivate, bobPublic := X25519KeyGen()
for i := 0; i < b.N; i++ {
_, _ = curve25519.X25519(bobPrivate, alicePublic)
_, _ = curve25519.X25519(alicePrivate, bobPublic)
}
}
func BenchmarkDHKeyAgreement(b *testing.B) {
alice := dh.New()
bob := dh.New()
for i := 0; i < b.N; i++ {
_ = alice.ComputeSecret(bob.PublicKey)
_ = bob.ComputeSecret(alice.PublicKey)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment