diff --git a/paillier.go b/paillier.go index 6012e09..2bd1203 100644 --- a/paillier.go +++ b/paillier.go @@ -48,6 +48,9 @@ func (pub *PublicKey) Encrypt(m *big.Int, random io.Reader) (*Cypher, error) { // Takes two cypher texts and returns a 3rd one that encode // the sum of the two plain texts. +// +// It's possible because Paillier is a homomorphic encryption scheme, where +// E(m1) * E(m2) = E(m1 + m2) func (this *PublicKey) Add(cypher1, cypher2 *Cypher) *Cypher { m := new(big.Int).Mul(cypher1.C, cypher2.C) return &Cypher{new(big.Int).Mod(m, this.GetNSquare())} diff --git a/thresholdkey_test.go b/thresholdkey_test.go index fdab2ac..ef85189 100644 --- a/thresholdkey_test.go +++ b/thresholdkey_test.go @@ -216,6 +216,30 @@ func TestEncryptingDecrypting(t *testing.T) { } } +func TestHomomorphicThresholdEncryption(t *testing.T) { + tkh := GetThresholdKeyGenerator(10, 2, 2, rand.Reader) + tpks, _ := tkh.Generate() + + plainText1 := b(13) + plainText2 := b(19) + + cypher1, _ := tpks[0].Encrypt(plainText1, rand.Reader) + cypher2, _ := tpks[1].Encrypt(plainText2, rand.Reader) + + cypher3 := tpks[0].Add(cypher1, cypher2) + + share1 := tpks[0].Decrypt(cypher3.C) + share2 := tpks[1].Decrypt(cypher3.C) + + combined, _ := tpks[0].CombinePartialDecryptions([]*PartialDecryption{share1, share2}) + + expected := big.NewInt(32) // 13 + 19 + + if !reflect.DeepEqual(combined, expected) { // 13 + 19 + t.Errorf("Unexpected decryption result. Expected %v but got %v", expected, combined) + } +} + func TestDecryption(t *testing.T) { // test the correct decryption of '100'. share1 := &PartialDecryption{1, b(384111638639)} @@ -347,5 +371,4 @@ func TestVerifyDecryption(t *testing.T) { if err = pk.VerifyDecryption(new(big.Int).Add(b(1), cypher.C), b(101), pds); err == nil { t.Error(err) } - }