From 970db06efc3b9f15904a406b22905d1dd91b71d7 Mon Sep 17 00:00:00 2001 From: sachaservan Date: Sat, 18 Aug 2018 14:54:38 -0400 Subject: [PATCH] standardize naming --- paillier.go | 18 ++++++++++-------- paillier_test.go | 10 +++++----- thresholdkey_generator.go | 1 + 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/paillier.go b/paillier.go index 19b204f..faa1f54 100644 --- a/paillier.go +++ b/paillier.go @@ -8,6 +8,7 @@ import ( type PublicKey struct { N *big.Int + G *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 // 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).Exp(pk.G, m, nSquare) rn := new(big.Int).Exp(r, pk.N, nSquare) 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) } -// 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. // // It's possible because Paillier is a homomorphic encryption scheme, where @@ -109,17 +109,17 @@ type SecretKey struct { // Decodes ciphertext into a plaintext message. // -// c - cyphertext to decrypt +// c - ciphertext to decrypt // 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. -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) 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) - return + m := new(big.Int).Mod(new(big.Int).Mul(L(tmp, sk.N), mu), sk.N) + return m } type Ciphertext struct { @@ -162,10 +162,12 @@ func computePhi(p, q *big.Int) *big.Int { func CreateSecretKey(p, q *big.Int) *SecretKey { n := new(big.Int).Mul(p, q) lambda := computePhi(p, q) + g := new(big.Int).Add(n, big.NewInt(1)) return &SecretKey{ PublicKey: PublicKey{ N: n, + G: g, }, Lambda: lambda, } diff --git a/paillier_test.go b/paillier_test.go index 3704c93..5bc4a08 100644 --- a/paillier_test.go +++ b/paillier_test.go @@ -124,7 +124,7 @@ func TestCheckPlaintextSpace(t *testing.T) { } } -func TestAddCyphers(t *testing.T) { +func TestAddCiphertexts(t *testing.T) { p, q := GenKeyPrimes(10) 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)) 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) 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)) ct, err := sk.Encrypt(big.NewInt(30), rand.Reader) @@ -197,7 +197,7 @@ func GenKeyPrimes(bits int) (p, q *big.Int) { continue } q, err = rand.Prime(rand.Reader, bits) - if err != nil { + if err != nil || p.Cmp(q) == 0 { continue } diff --git a/thresholdkey_generator.go b/thresholdkey_generator.go index dec8b98..31be763 100644 --- a/thresholdkey_generator.go +++ b/thresholdkey_generator.go @@ -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 { ret := new(ThresholdSecretKey) ret.N = tkg.n + ret.G = new(big.Int).Add(tkg.n, big.NewInt(1)) ret.V = tkg.v ret.TotalNumberOfDecryptionServers = tkg.TotalNumberOfDecryptionServers