Compare commits

...
1 Commits
Author SHA1 Message Date
Sylvain Pelissier 93f329fc4b Encryption optimization 2023-11-10 11:25:40 +01:00
2 changed files with 32 additions and 2 deletions

No files matched your search

+3 -2
View File
@@ -39,10 +39,11 @@ func (pk *PublicKey) EncryptWithR(m *big.Int, r *big.Int) (*Cypher, error) {
nSquare := pk.GetNSquare()
// g is _always_ equal n+1
// g^m = (n+1)^m = 1 + mn
// Threshold encryption is safe only for g=n+1 choice.
// See [DJN 10], section 5.1
g := new(big.Int).Add(pk.N, big.NewInt(1))
gm := new(big.Int).Exp(g, m, nSquare)
gm := new(big.Int).Mul(pk.N, m)
gm = gm.Mod(new(big.Int).Add(gm, big.NewInt(1)), nSquare)
rn := new(big.Int).Exp(r, pk.N, nSquare)
return &Cypher{new(big.Int).Mod(new(big.Int).Mul(rn, gm), nSquare)}, nil
}
+29
View File
@@ -65,6 +65,35 @@ func TestEncryptDecryptSmall(t *testing.T) {
}
}
func TestEncrypt512(t *testing.T) {
p, _ := rand.Prime(rand.Reader, 512)
q, _ := rand.Prime(rand.Reader, 512)
privateKey := CreatePrivateKey(p, q)
for i := 1; i < 10; i++ {
initialValue, _ := rand.Int(rand.Reader, privateKey.PublicKey.N)
cypher, err := privateKey.Encrypt(initialValue, rand.Reader)
if err != nil {
t.Error(err)
}
returnedValue := privateKey.Decrypt(cypher)
if initialValue.Cmp(returnedValue) != 0 {
t.Error("wrong decryption ", returnedValue, " is not ", initialValue)
}
}
}
func BenchmarkEncrypt(b *testing.B) {
p, _ := rand.Prime(rand.Reader, 2048)
q, _ := rand.Prime(rand.Reader, 2048)
privateKey := CreatePrivateKey(p, q)
// run the Encrypt function b.N times
for n := 0; n < b.N; n++ {
initialValue, _ := rand.Int(rand.Reader, privateKey.PublicKey.N)
privateKey.Encrypt(initialValue, rand.Reader)
}
}
func TestCheckPlaintextSpace(t *testing.T) {
p := big.NewInt(13)
q := big.NewInt(11)