1
0
mirror of https://github.com/henrydcase/nobs.git synced 2024-11-22 23:28:57 +00:00
nobs/kem/mkem/csidh_test.go

110 lines
2.2 KiB
Go
Raw Normal View History

2020-08-25 11:19:07 +01:00
package mkem
import (
"bytes"
"crypto/rand"
2020-08-25 11:19:07 +01:00
"testing"
"github.com/henrydcase/nobs/dh/csidh"
"github.com/henrydcase/nobs/drbg"
2020-08-25 11:19:07 +01:00
)
var sPKE PKE
var mPKE MultiPKE
var testSKS []csidh.PrivateKey
var testPKS []csidh.PublicKey
2020-08-25 11:19:07 +01:00
// helper
func Ok(t testing.TB, f bool, msg string) {
t.Helper()
if !f {
t.Error(msg)
}
}
func init() {
var tmp [32]byte
var rng *drbg.CtrDrbg
rand.Read(tmp[:])
rng = drbg.NewCtrDrbg()
if !rng.Init(tmp[:], nil) {
panic("Can't initialize DRBG")
2020-08-25 11:19:07 +01:00
}
sPKE.Allocate(rng)
mPKE.Allocate(10, rng)
2020-08-25 11:19:07 +01:00
testSKS = make([]csidh.PrivateKey, len(mPKE.Cts))
testPKS = make([]csidh.PublicKey, len(mPKE.Cts))
2020-08-25 11:19:07 +01:00
for i, _ := range mPKE.Cts {
csidh.GeneratePrivateKey(&testSKS[i], mPKE.Rng)
csidh.GeneratePublicKey(&testPKS[i], &testSKS[i], mPKE.Rng)
2020-08-25 11:19:07 +01:00
}
2020-08-25 11:19:07 +01:00
}
func getCiphertext(ct *ciphertext, mPKE *MultiPKE, i int) {
copy(ct.U[:], mPKE.Ct0[:])
copy(ct.V[:], mPKE.Cts[i][:])
2020-08-25 11:19:07 +01:00
}
func TestSinglePKE(t *testing.T) {
var pk csidh.PublicKey
var sk csidh.PrivateKey
csidh.GeneratePrivateKey(&sk, sPKE.Rng)
csidh.GeneratePublicKey(&pk, &sk, sPKE.Rng)
var msg [16]byte
ct := sPKE.Enc(&pk, &msg)
pt := sPKE.Dec(&sk, &ct)
Ok(t, bytes.Equal(pt[:], msg[:]), "Decryption failed")
2020-08-25 11:19:07 +01:00
// Do it twice to ensure it works with same key pair
ct = sPKE.Enc(&pk, &msg)
pt = sPKE.Dec(&sk, &ct)
2020-08-25 11:19:07 +01:00
Ok(t, bytes.Equal(pt[:], msg[:]),
"Decryption failed")
}
func TestMultiPKE(t *testing.T) {
var msg [16]byte
2020-08-25 11:19:07 +01:00
var ct ciphertext
pks := make([]csidh.PublicKey, len(mPKE.Cts))
sks := make([]csidh.PrivateKey, len(mPKE.Cts))
2020-08-25 11:19:07 +01:00
// mct.Cts = make([][SharedSecretSz]byte)
2020-08-25 11:19:07 +01:00
for i, _ := range mPKE.Cts {
csidh.GeneratePrivateKey(&sks[i], mPKE.Rng)
csidh.GeneratePublicKey(&pks[i], &sks[i], mPKE.Rng)
2020-08-25 11:19:07 +01:00
}
mPKE.Encrypt(pks[:], &msg)
for i := 0; i < len(mPKE.Cts); i++ {
getCiphertext(&ct, &mPKE, i)
pt := sPKE.Dec(&sks[i], &ct)
2020-08-25 11:19:07 +01:00
Ok(t, bytes.Equal(pt[:], msg[:]),
"Multi decryption failed")
}
}
var MessgaeTest [16]byte
2020-08-25 11:19:07 +01:00
func BenchmarkEncrypt_CSIDH_p512(b *testing.B) {
2020-08-25 11:19:07 +01:00
for i := 0; i < b.N; i++ {
_ = sPKE.Enc(&testPKS[0], &MessgaeTest)
2020-08-25 11:19:07 +01:00
}
}
func BenchmarkMultiEncrypt_CSIDH_100keys(b *testing.B) {
2020-08-25 11:19:07 +01:00
for i := 0; i < b.N; i++ {
mPKE.Encrypt(testPKS[:], &MessgaeTest)
2020-08-25 11:19:07 +01:00
}
}