-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (146 loc) · 4.76 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"log"
"math/big"
"github.com/lucasmenendez/gopsi/pkg/client"
)
var err error
var alice *client.Client
var bob *client.Client
var aliceData = []string{
}
var bobData = []string{
}
var encryptedAliceData [][]*big.Int
var encryptedBobData [][]*big.Int
// startIntances initializes the client (alice) and server (bob) instances and
// perform a secure common prime number exchange using RSA (read more here:
// https://github.com/lucasmenendez/gopsi/blob/dev/internal/rsa/rsa.go).
func startIntances() {
fmt.Println("\nSTARTING SILOS INSTANCES")
fmt.Println("------------------------")
// start client instance (alice) to generate public key and share it with
// the server (bob)
alice, err = client.Init()
if err != nil {
log.Fatalln(err)
}
// get client (alice) public key byte slice to share it with the server
// (bob)
var alicePubKey []byte
alicePubKey, err = alice.PubKey()
// start server instance (bob) with the client public key to generate common
// prime, encrypt it with the key provided and share the result with the
// client
bob, err = client.Init()
if err != nil {
log.Fatalln(err)
}
var encPrime []byte
if encPrime, err = bob.GenEncryptedPrime(alicePubKey); err != nil {
log.Fatalln(err)
}
fmt.Printf("[bob] common prime: %s\n", bob.CommonPrime.String())
// decrypt the common prime on the client side from the encrypted text
// provided
err = alice.SetEncryptedPrime(encPrime)
if err != nil {
log.Println(err)
}
fmt.Printf("[alice] common prime: %s\n", alice.CommonPrime.String())
}
// loadSilosData inject mocked data into client and server instances to get
// encrypted using SRA (read more here:
// https://github.com/lucasmenendez/gopsi/blob/dev/pkg/sra/sra.go)
func loadSilosData(aliceData, bobData []string) {
fmt.Println("\nLOADING SILOS DATA")
fmt.Println("------------------")
// create client (alice) data and load into the client intance to get it
// encrypted
encryptedAliceData, err = alice.Encrypt(aliceData)
if err != nil {
log.Fatalln("[alice] error encrypting data:", err)
}
fmt.Printf("[alice] %d items encrypted. Raw items:\n", len(encryptedAliceData))
for i, d := range aliceData {
fmt.Printf("\t%d. %v\n", i, d)
}
// create server (bob) data, encrypt and store it into the server intance
// and share it with the client
if encryptedBobData, err = bob.Encrypt(bobData); err != nil {
log.Fatalln("[bob] error encrypting data:", err)
}
fmt.Printf("[bob] %d items encrypted. Raw items:\n", len(encryptedBobData))
for i, d := range bobData {
fmt.Printf("\t%d. %v\n", i, d)
}
}
// executeIntersection function performs two actions. First shares the server
// encrypted data to the client to re-encrypt it into the client. Then share
// the re-encrypted server data and encrypted client data with the server.
// Second, creates a BloomFilter (read more here:
// https://github.com/lucasmenendez/gopsi/blob/dev/pkg/bloomfilter/bloomfilter.go)
// with the re-encrypted server data and iterates over client encrypted data,
// re-encrypting it and testing over the created filter.
func executeIntersection() {
fmt.Println("\nEXECUTING INTERSECTION")
fmt.Println("----------------------")
// re-encrypt the server encrypted data into the client and share the
// result and the encrypted client data with the server
var encryptedBobDataByAlice [][]*big.Int
encryptedBobDataByAlice, err = alice.EncryptExt(encryptedBobData)
if err != nil {
log.Fatalln(err)
}
// initialize the intersection creating a filter with the re-encrypted
// server data received from the client
bob.PrepareIntersection(encryptedBobDataByAlice)
// perform intersection re-encrypyting client data and comparing with
// the re-encrypted data of the server
intersection, err := bob.GetIntersection(encryptedAliceData)
if err != nil {
log.Fatalln(err)
}
// Parse received results from the server on the client.
var results []string
results, err = alice.ParseIntersection(intersection)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("[alice] %d common items received:\n", len(intersection))
for _, d := range results {
var index int
for i, c := range aliceData {
index = i
if c == d {
break
}
}
fmt.Printf("\t%d. %v\n", index, d)
}
}
func main() {
// request intersection
startIntances()
// perform intersection
loadSilosData(aliceData, bobData)
executeIntersection()
}