cryptofun/paillier/paillier_test.go
paillier/paillier_test.goRaw
1package paillier
2
3import (
4 "bytes"
5 "fmt"
6 "math/big"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10)
11
12func TestEncryptDecrypt(t *testing.T) {
13 key, err := GenerateKeyPair()
14 assert.Nil(t, err)
15
16 mBytes := []byte("Hi")
17 m := new(big.Int).SetBytes(mBytes)
18 c := Encrypt(m, key.PubK)
19 d := Decrypt(c, key.PubK, key.PrivK)
20 if m == d {
21 fmt.Println(key)
22 t.Errorf("m not equal to decrypted")
23 }
24}
25
26func TestHomomorphicAddition(t *testing.T) {
27 // key, err := GenerateKeyPair()
28 // assert.Nil(t, err)
29
30 // key harcoded for tests
31 pubK := PublicKey{
32 N: big.NewInt(204223),
33 G: big.NewInt(24929195694),
34 }
35 privK := PrivateKey{
36 Lambda: big.NewInt(101660),
37 Mu: big.NewInt(117648),
38 }
39 key := Key{
40 PubK: pubK,
41 PrivK: privK,
42 }
43
44 n1 := big.NewInt(int64(110))
45 n2 := big.NewInt(int64(150))
46 c1 := Encrypt(n1, key.PubK)
47 c2 := Encrypt(n2, key.PubK)
48 c3c4 := HomomorphicAddition(c1, c2, key.PubK)
49 d := Decrypt(c3c4, key.PubK, key.PrivK)
50 if !bytes.Equal(new(big.Int).Add(n1, n2).Bytes(), d.Bytes()) {
51 fmt.Println(key)
52 t.Errorf("decrypted result not equal to original result")
53 }
54}