Fix spelling/naming issues, slightly more robust testing of encryption and decryption. #12

Closed
sachaservan wants to merge 7 commits from master into master
pull from: master
3 changed files with 16 additions and 13 deletions
Showing only changes of commit 970db06efc - Show all commits

No files matched your search

+10 -8
View File
@@ -8,6 +8,7 @@ import (
type PublicKey struct { type PublicKey struct {
N *big.Int N *big.Int
G *big.Int
} }
func (pk *PublicKey) GetNSquare() *big.Int { func (pk *PublicKey) GetNSquare() *big.Int {
@@ -41,8 +42,7 @@ func (pk *PublicKey) EncryptWithR(m *big.Int, r *big.Int) (*Ciphertext, error) {
// g is _always_ equal n+1 // g is _always_ equal n+1
// Threshold encryption is safe only for g=n+1 choice. // Threshold encryption is safe only for g=n+1 choice.
// See [DJN 10], section 5.1 // See [DJN 10], section 5.1
g := new(big.Int).Add(pk.N, big.NewInt(1)) gm := new(big.Int).Exp(pk.G, m, nSquare)
gm := new(big.Int).Exp(g, m, nSquare)
rn := new(big.Int).Exp(r, pk.N, nSquare) rn := new(big.Int).Exp(r, pk.N, nSquare)
return &Ciphertext{new(big.Int).Mod(new(big.Int).Mul(rn, gm), nSquare)}, nil return &Ciphertext{new(big.Int).Mod(new(big.Int).Mul(rn, gm), nSquare)}, nil
} }
@@ -66,7 +66,7 @@ func (pk *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Ciphertext, error)
return pk.EncryptWithR(m, r) return pk.EncryptWithR(m, r)
} }
// Add takes an arbitrary number of cyphertexts and returns one that encodes // Add takes an arbitrary number of ciphertexts and returns one that encodes
// their sum. // their sum.
// //
// It's possible because Paillier is a homomorphic encryption scheme, where // It's possible because Paillier is a homomorphic encryption scheme, where
@@ -109,17 +109,17 @@ type SecretKey struct {
// Decodes ciphertext into a plaintext message. // Decodes ciphertext into a plaintext message.
// //
// c - cyphertext to decrypt // c - ciphertext to decrypt
// N, lambda - key attributes // N, lambda - key attributes
// //
// D(c) = [ ((c^lambda) mod N^2) - 1) / N ] lambda^-1 mod N // D(c) = [ ((c^lambda)*mu mod N^2) - 1) / N ] lambda^-1 mod N
// //
// See [KL 08] construction 11.32, page 414. // See [KL 08] construction 11.32, page 414.
func (sk *SecretKey) Decrypt(ct *Ciphertext) (msg *big.Int) { func (sk *SecretKey) Decrypt(ct *Ciphertext) *big.Int {
mu := new(big.Int).ModInverse(sk.Lambda, sk.N) mu := new(big.Int).ModInverse(sk.Lambda, sk.N)
tmp := new(big.Int).Exp(ct.C, sk.Lambda, sk.GetNSquare()) tmp := new(big.Int).Exp(ct.C, sk.Lambda, sk.GetNSquare())
msg = new(big.Int).Mod(new(big.Int).Mul(L(tmp, sk.N), mu), sk.N) m := new(big.Int).Mod(new(big.Int).Mul(L(tmp, sk.N), mu), sk.N)
return return m
} }
type Ciphertext struct { type Ciphertext struct {
@@ -162,10 +162,12 @@ func computePhi(p, q *big.Int) *big.Int {
func CreateSecretKey(p, q *big.Int) *SecretKey { func CreateSecretKey(p, q *big.Int) *SecretKey {
n := new(big.Int).Mul(p, q) n := new(big.Int).Mul(p, q)
lambda := computePhi(p, q) lambda := computePhi(p, q)
g := new(big.Int).Add(n, big.NewInt(1))
nkuba commented 2018-08-22 09:32:27 -04:00 (Migrated from github.com)
Review

Paillier is an asymmetric cryptographic algorithm so the PrivateKey is 100% fine for this, there is no need to change it.

Paillier is an asymmetric cryptographic algorithm so the `PrivateKey` is 100% fine for this, there is no need to change it.
return &SecretKey{ return &SecretKey{
PublicKey: PublicKey{ PublicKey: PublicKey{
N: n, N: n,
G: g,
}, },
Lambda: lambda, Lambda: lambda,
} }
+5 -5
View File
@@ -124,7 +124,7 @@ func TestCheckPlaintextSpace(t *testing.T) {
} }
} }
func TestAddCyphers(t *testing.T) { func TestAddCiphertexts(t *testing.T) {
p, q := GenKeyPrimes(10) p, q := GenKeyPrimes(10)
sk := CreateSecretKey(p, q) sk := CreateSecretKey(p, q)
@@ -140,7 +140,7 @@ func TestAddCyphers(t *testing.T) {
} }
} }
func TestAddCypherWithSmallKeyModulus(t *testing.T) { func TestAddCiphertextWithSmallKeyModulus(t *testing.T) {
sk := CreateSecretKey(big.NewInt(7), big.NewInt(5)) sk := CreateSecretKey(big.NewInt(7), big.NewInt(5))
ct1, _ := sk.Encrypt(big.NewInt(30), rand.Reader) ct1, _ := sk.Encrypt(big.NewInt(30), rand.Reader)
@@ -154,7 +154,7 @@ func TestAddCypherWithSmallKeyModulus(t *testing.T) {
} }
} }
func TestMulCypher(t *testing.T) { func TestMulCiphertext(t *testing.T) {
p, q := GenKeyPrimes(10) p, q := GenKeyPrimes(10)
sk := CreateSecretKey(p, q) sk := CreateSecretKey(p, q)
@@ -172,7 +172,7 @@ func TestMulCypher(t *testing.T) {
} }
} }
func TestMulCypherWithSmallKeyModulus(t *testing.T) { func TestMulCiphertextWithSmallKeyModulus(t *testing.T) {
sk := CreateSecretKey(big.NewInt(7), big.NewInt(5)) sk := CreateSecretKey(big.NewInt(7), big.NewInt(5))
ct, err := sk.Encrypt(big.NewInt(30), rand.Reader) ct, err := sk.Encrypt(big.NewInt(30), rand.Reader)
@@ -197,7 +197,7 @@ func GenKeyPrimes(bits int) (p, q *big.Int) {
continue continue
} }
q, err = rand.Prime(rand.Reader, bits) q, err = rand.Prime(rand.Reader, bits)
if err != nil { if err != nil || p.Cmp(q) == 0 {
continue continue
} }
+1
View File
@@ -243,6 +243,7 @@ func (tkg *ThresholdKeyGenerator) createViArray(shares []*big.Int) (viArray []*b
func (tkg *ThresholdKeyGenerator) createSecretKey(i int, share *big.Int, viArray []*big.Int) *ThresholdSecretKey { func (tkg *ThresholdKeyGenerator) createSecretKey(i int, share *big.Int, viArray []*big.Int) *ThresholdSecretKey {
ret := new(ThresholdSecretKey) ret := new(ThresholdSecretKey)
ret.N = tkg.n ret.N = tkg.n
ret.G = new(big.Int).Add(tkg.n, big.NewInt(1))
ret.V = tkg.v ret.V = tkg.v
ret.TotalNumberOfDecryptionServers = tkg.TotalNumberOfDecryptionServers ret.TotalNumberOfDecryptionServers = tkg.TotalNumberOfDecryptionServers