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 n1 := big.NewInt(int64(110))
31 n2 := big.NewInt(int64(150))
32 c1 := Encrypt(n1, key.PubK)
33 c2 := Encrypt(n2, key.PubK)
34 c3c4 := HomomorphicAddition(c1, c2, key.PubK)
35 d := Decrypt(c3c4, key.PubK, key.PrivK)
36 if !bytes.Equal(new(big.Int).Add(n1, n2).Bytes(), d.Bytes()) {
37 fmt.Println(key)
38 t.Errorf("decrypted result not equal to original result")
39 }
40}