-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
54 lines (46 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"log"
"math/big"
"github.com/lucasmenendez/gopaillier/pkg/paillier"
)
func main() {
var a, b = big.NewInt(2), big.NewInt(5)
// Creating a new private key with 64 bits of length. It contains its public
// key with the same size.
key, err := paillier.NewKeys(128)
if err != nil {
log.Fatalln(err)
}
// Encrypting inputs A' = E(A) & B' = E(B)
var encryptedA, _ = key.PubKey.Encrypt(a)
var encryptedB, _ = key.PubKey.Encrypt(b)
// Calculating some operations:
// - A' + B
// - A' + B'
// - A' - B
// - A' * B
var sum = key.PubKey.Add(encryptedA, b)
var sumEnc = key.PubKey.AddEncrypted(encryptedA, encryptedB)
var sub = key.PubKey.Add(encryptedA, new(big.Int).Neg(b))
var mul = key.PubKey.Mul(encryptedA, b)
// Decrypting results
var decryptedSum, _ = key.Decrypt(sum)
var decryptedSumEnc, _ = key.Decrypt(sumEnc)
var decryptedSub, _ = key.Decrypt(sub)
var decryptedMul, _ = key.Decrypt(mul)
// Printing results
fmt.Printf("Original A: %d\n", a)
fmt.Printf("Constant B: %d\n", b)
fmt.Printf("Encrypted A (A'): %d\n", encryptedA)
fmt.Printf("Encrypted B (B'): %d\n\n", encryptedB)
fmt.Printf("Encrypted Sum (A' + B): %d\n", sum)
fmt.Printf("Decrypted Sum: %d + %d = %d\n\n", a, b, decryptedSum)
fmt.Printf("Encrypted SumEnc (A' + B'): %d\n", sumEnc)
fmt.Printf("Decrypted SumEnc: %d + %d = %d\n\n", a, b, decryptedSumEnc)
fmt.Printf("Encrypted Sub (A' - B): %d\n", sub)
fmt.Printf("Decrypted Sub: %d - %d = %d\n\n", a, b, decryptedSub)
fmt.Printf("Encrypted Mul (A' * B): %d\n", mul)
fmt.Printf("Decrypted Mul: %d * %d = %d\n\n", a, b, decryptedMul)
}