-
Notifications
You must be signed in to change notification settings - Fork 0
/
fhe.go
124 lines (102 loc) · 3.16 KB
/
fhe.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
package cpu
/*
#cgo LDFLAGS: -ltfhe-spqlios-fma
#cgo CFLAGS: -I/usr/local/include
#include <stdlib.h>
#include <tfhe/tfhe.h>
*/
import "C"
var Params *C.TFheGateBootstrappingParameterSet
var CloudKey *C.TFheGateBootstrappingCloudKeySet
func LoadCloudKeyFromFile(path string) {
cloudKeyFile := C.fopen(C.CString(path), C.CString("rb"))
CloudKey = C.new_tfheGateBootstrappingCloudKeySet_fromFile(cloudKeyFile)
Params = CloudKey.params
}
// func EncryptMap(plaintext map[string]int) map[string]FheType {
// output := make(map[string]FheType)
// for key, value := range plaintext {
// output[key] = encrypt(value)
// }
// return output
// }
// func encrypt(bit int) FheType {
// ciphertext := C.new_gate_bootstrapping_ciphertext(params)
// C.bootsSymEncrypt(ciphertext, C.int32_t(bit), key)
// return FheType{isNil: false, value: ciphertext}
// }
// func decryptMap(ciphertext map[string]FheType) map[string]int {
// output := make(map[string]int)
// for key, value := range ciphertext {
// output[key] = decrypt(value)
// }
// return output
// }
// func decrypt(ciphertext FheType) int {
// return int(C.bootsSymDecrypt(ciphertext.value, key))
// }
func LoadInstructionsFromFile(instructionCount int, path string) [3][]FheType {
var instructions [3][]FheType
cloudDataFile := C.fopen(C.CString(path), C.CString("rb"))
for i := 0; i < instructionCount; i++ {
for tapeIdx := 0; tapeIdx < 3; tapeIdx++ {
value := NewCiphertext()
C.import_gate_bootstrapping_ciphertext_fromFile(cloudDataFile, value, Params)
instructions[tapeIdx] = append(instructions[tapeIdx], FheType{false, value})
}
}
C.fclose(cloudDataFile)
return instructions
}
func SaveCiphertext(path string, data FheType) {
ciphertextFile := C.fopen(C.CString(path), C.CString("ab"))
C.export_gate_bootstrapping_ciphertext_toFile(ciphertextFile, data.Value, Params)
C.fclose(ciphertextFile)
}
func NewCiphertext() *C.LweSample {
return C.new_gate_bootstrapping_ciphertext(Params)
}
func NewConstant(value int) FheType {
result := NewCiphertext()
C.bootsCONSTANT(result, C.int32_t(value), CloudKey)
return FheType{false, result}
}
var fheNand = decorator(func(result, a, b *C.LweSample) {
C.bootsNAND(result, a, b, CloudKey)
})
var fheAnd = decorator(func(result, a, b *C.LweSample) {
C.bootsAND(result, a, b, CloudKey)
})
var fheOr = decorator(func(result, a, b *C.LweSample) {
C.bootsOR(result, a, b, CloudKey)
})
var fheXor = decorator(func(result, a, b *C.LweSample) {
C.bootsXOR(result, a, b, CloudKey)
})
// sel?b:a
func fheMux(sel, b, a FheType) FheType {
result := NewCiphertext()
C.bootsMUX(result, sel.Value, b.Value, a.Value, CloudKey)
return FheType{false, result}
}
func fheNot(a FheType) FheType {
result := NewCiphertext()
C.bootsNOT(result, a.Value, CloudKey)
return FheType{false, result}
}
func fheCopy(a FheType) FheType {
result := NewCiphertext()
C.bootsCOPY(result, a.Value, CloudKey)
return FheType{false, result}
}
func decorator(f func(result, a, b *C.LweSample)) func(a, b FheType) FheType {
return func(a, b FheType) FheType {
result := NewCiphertext()
f(result, a.Value, b.Value)
return FheType{false, result}
}
}
type FheType struct {
IsNil bool
Value *C.LweSample
}