sidh-torture/csidh/cmd/torturer/main.go

120 lines
2.5 KiB
Go

package main
import (
"bytes"
"fmt"
"time"
"crypto/rand"
"github.com/henrydcase/sidh_torture/csidh/ref/go-wrapper"
"github.com/henrydcase/nobs/dh/csidh"
)
const (
PrvSz = 37 // PrvSz is a size of private key in bytes.
PubSz = 64 // PubSz is a size of public key in bytes.
SsSz = 64 // SsSz is a size of shared secret in bytes.
)
func TestSS() {
var r wrapper.Ref
var ssRef [SsSz]byte
var ss [SsSz]byte
// Go types
prA := r.KeygenPrv()
pkA := r.KeygenPub(&prA)
prB := csidh.NewPrivateKey()
pkB := csidh.NewPublicKey()
prB.Generate(rand.Reader)
pkB.Generate(&prB)
s1 := time.Now()
// Generate with Go
pkB.DeriveSecret(ss[:], &pkA, &prB)
s2 := time.Since(s1)
// Generate with C
s1 = time.Now()
r.Derive(ssRef[:], &pkB, &prA)
s3 := time.Since(s1)
fmt.Printf("|TestSharedSecret | %.8s | %.8s |\n", s2, s3)
if !bytes.Equal(ss[:], ssRef[:]) {
panic("TestSS")
}
}
func TestKeyImport() {
var r wrapper.Ref
var ssRef [SsSz]byte
var ss [SsSz]byte
// Go types
prA := r.KeygenPrv()
pkA := r.KeygenPub(&prA)
prB := r.KeygenPrv()
pkB := r.KeygenPub(&prB)
s1 := time.Now()
// Generate with Go
pkB.DeriveSecret(ss[:], &pkA, &prB)
s2 := time.Since(s1)
// Generate with C
s1 = time.Now()
r.Derive(ssRef[:], &pkB, &prA)
s3 := time.Since(s1)
fmt.Printf("|TestKeyImport | %.8s | %.8s |\n", s2, s3)
if !bytes.Equal(ss[:], ssRef[:]) {
panic("TestKeyImport")
}
}
func TestKeyGeneration() {
var r wrapper.Ref
var ssRef [SsSz]byte
var ss [SsSz]byte
// Go types
prA := csidh.NewPrivateKey()
pkA := csidh.NewPublicKey()
prB := csidh.NewPrivateKey()
pkB := csidh.NewPublicKey()
prA.Generate(rand.Reader)
pkA.Generate(&prA)
prB.Generate(rand.Reader)
pkB.Generate(&prB)
s1 := time.Now()
// Generate with Go
r.Derive(ss[:], &pkA, &prB)
s2 := time.Since(s1)
// Generate with C
s1 = time.Now()
r.Derive(ssRef[:], &pkB, &prA)
s3 := time.Since(s1)
fmt.Printf("|TestKeyGeneration | %.8s | %.8s |\n", s2, s3)
if !bytes.Equal(ss[:], ssRef[:]) {
panic("TestKeyGeneration")
}
}
func main() {
fmt.Printf("| TestName |Go | C |\n")
fmt.Printf("|------------------|----------|----------|\n")
// for i:=0; i<100; i++ {
for {
TestSS()
TestKeyImport()
TestKeyGeneration()
}
}