The code for generating private key is based on approach described in [KL 08], construction 11.32 which is compatible with the one described in [DJN 10], section 3.2 except that instead of generating Lamda private key compontent from LCM of `p` and `q` we use Euler's totient function as suggested in [KL 08]. I cleared up naming as well as removed G and Mu parameters from Paillier key. For the modification of Paillier scheme we use, where g = n + 1, N is the only one parameter needed for a public key. Also, Mu is needed only for Decrypting and can be computed there from Lambda and N so there is no need to keep it in the private key.
I added documentation to <Key, Enc, Dec> functions with an important note that `p` and `q` supplied to `CreatePrivateKey` must be either of equal length or `gcd(pq, (p-1)(q-1)) = 1` which is assured if both primes are of equal length. This requirement is mentioned both in [KL 08] and [DJN 10].
[KL 08]: Jonathan Katz, Yehuda Lindell, (2008)
Introduction to Modern Cryptography: Principles and Protocols,
Chapman & Hall/CRC
[DJN 10]: Ivan Damgard, Mads Jurik, Jesper Buus Nielsen, (2010)
A Generalization of Paillier’s Public-Key System
with Applications to Electronic Voting
Aarhus University, Dept. of Computer Science, BRICS
77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package paillier
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"math/big"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestComputeL(t *testing.T) {
|
|
u := big.NewInt(21)
|
|
n := big.NewInt(3)
|
|
|
|
expected := big.NewInt(6)
|
|
actual := L(u, n)
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
t.Errorf("Unexpected L function result [%v]", actual)
|
|
}
|
|
}
|
|
|
|
func TestComputePhi(t *testing.T) {
|
|
a := big.NewInt(5)
|
|
b := big.NewInt(7)
|
|
|
|
expected := big.NewInt(24)
|
|
actual := computePhi(a, b)
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
t.Errorf("Unexpected phi value [%v]", actual)
|
|
}
|
|
}
|
|
|
|
func TestCreatePrivateKey(t *testing.T) {
|
|
p := big.NewInt(463)
|
|
q := big.NewInt(631)
|
|
|
|
privateKey := CreatePrivateKey(p, q)
|
|
|
|
if !reflect.DeepEqual(privateKey.N, big.NewInt(292153)) {
|
|
t.Errorf("Unexpected N PublicKey value [%v]", privateKey.N)
|
|
}
|
|
|
|
if !reflect.DeepEqual(privateKey.Lambda, big.NewInt(291060)) {
|
|
t.Errorf("Unexpected Lambda Public key value [%v]", privateKey.Lambda)
|
|
}
|
|
}
|
|
|
|
func TestEncryptDecryptSmall(t *testing.T) {
|
|
p := big.NewInt(13)
|
|
q := big.NewInt(11)
|
|
for i := 1; i < 10; i++ {
|
|
privateKey := CreatePrivateKey(p, q)
|
|
|
|
inicialValue := big.NewInt(100)
|
|
cypher, err := privateKey.Encrypt(inicialValue, rand.Reader)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
returnedValue := privateKey.Decrypt(cypher)
|
|
if !reflect.DeepEqual(inicialValue, returnedValue) {
|
|
t.Error("wrong decryption ", returnedValue, " is not ", inicialValue)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAddCypher(t *testing.T) {
|
|
privateKey := CreatePrivateKey(big.NewInt(13), big.NewInt(11))
|
|
cypher1, _ := privateKey.Encrypt(big.NewInt(12), rand.Reader)
|
|
cypher2, _ := privateKey.Encrypt(big.NewInt(13), rand.Reader)
|
|
cypher3 := privateKey.Add(cypher1, cypher2)
|
|
m := privateKey.Decrypt(cypher3)
|
|
if !reflect.DeepEqual(m, big.NewInt(25)) {
|
|
t.Error(m)
|
|
}
|
|
}
|