Added test ensuring threshold Paillier is a partially homomorphic system

This commit is contained in:
Piotr Dyraga committed 2018-05-30 14:49:05 +02:00
1 parent 915950cb35
commit 76f8f63a47
2 files changed
+27 -1

No files matched your search

+3
View File
@@ -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 // Takes two cypher texts and returns a 3rd one that encode
// the sum of the two plain texts. // 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 { func (this *PublicKey) Add(cypher1, cypher2 *Cypher) *Cypher {
m := new(big.Int).Mul(cypher1.C, cypher2.C) m := new(big.Int).Mul(cypher1.C, cypher2.C)
return &Cypher{new(big.Int).Mod(m, this.GetNSquare())} return &Cypher{new(big.Int).Mod(m, this.GetNSquare())}
+24 -1
View File
@@ -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) { func TestDecryption(t *testing.T) {
// test the correct decryption of '100'. // test the correct decryption of '100'.
share1 := &PartialDecryption{1, b(384111638639)} 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 { if err = pk.VerifyDecryption(new(big.Int).Add(b(1), cypher.C), b(101), pds); err == nil {
t.Error(err) t.Error(err)
} }
} }