2020-08-25 11:19:07 +01:00
|
|
|
package mkem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-17 03:33:57 +01:00
|
|
|
"crypto/rand"
|
2020-08-25 11:19:07 +01:00
|
|
|
"testing"
|
2020-05-17 03:33:57 +01:00
|
|
|
|
|
|
|
"github.com/henrydcase/nobs/dh/csidh"
|
|
|
|
"github.com/henrydcase/nobs/drbg"
|
2020-08-25 11:19:07 +01:00
|
|
|
)
|
|
|
|
|
2020-05-17 03:33:57 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 03:33:57 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-17 03:33:57 +01:00
|
|
|
sPKE.Allocate(rng)
|
|
|
|
mPKE.Allocate(10, rng)
|
2020-08-25 11:19:07 +01:00
|
|
|
|
2020-05-17 03:33:57 +01:00
|
|
|
testSKS = make([]csidh.PrivateKey, len(mPKE.Cts))
|
|
|
|
testPKS = make([]csidh.PublicKey, len(mPKE.Cts))
|
2020-08-25 11:19:07 +01:00
|
|
|
|
2020-05-17 03:33:57 +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-05-17 03:33:57 +01:00
|
|
|
|
2020-08-25 11:19:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 03:33:57 +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) {
|
2020-05-17 03:33:57 +01:00
|
|
|
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
|
2020-05-17 03:33:57 +01:00
|
|
|
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) {
|
2020-05-17 03:33:57 +01:00
|
|
|
var msg [16]byte
|
2020-08-25 11:19:07 +01:00
|
|
|
var ct ciphertext
|
|
|
|
|
2020-05-17 03:33:57 +01:00
|
|
|
pks := make([]csidh.PublicKey, len(mPKE.Cts))
|
|
|
|
sks := make([]csidh.PrivateKey, len(mPKE.Cts))
|
2020-08-25 11:19:07 +01:00
|
|
|
|
2020-05-17 03:33:57 +01:00
|
|
|
// mct.Cts = make([][SharedSecretSz]byte)
|
2020-08-25 11:19:07 +01:00
|
|
|
|
2020-05-17 03:33:57 +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
|
|
|
}
|
|
|
|
|
2020-05-17 03:33:57 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 03:33:57 +01:00
|
|
|
var MessgaeTest [16]byte
|
2020-08-25 11:19:07 +01:00
|
|
|
|
2020-05-17 03:33:57 +01:00
|
|
|
func BenchmarkEncrypt_CSIDH_p512(b *testing.B) {
|
2020-08-25 11:19:07 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-05-17 03:33:57 +01:00
|
|
|
_ = sPKE.Enc(&testPKS[0], &MessgaeTest)
|
2020-08-25 11:19:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 03:33:57 +01:00
|
|
|
func BenchmarkMultiEncrypt_CSIDH_100keys(b *testing.B) {
|
2020-08-25 11:19:07 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-05-17 03:33:57 +01:00
|
|
|
mPKE.Encrypt(testPKS[:], &MessgaeTest)
|
2020-08-25 11:19:07 +01:00
|
|
|
}
|
|
|
|
}
|