Commit c6c9d2f5 authored by Hoang Gia NGUYEN's avatar Hoang Gia NGUYEN
Browse files

first commit

parent fe2d6195
Pipeline #60 failed with stages
in 0 seconds

Too many changes to show.

To preserve performance only 275 of 275+ files are displayed.
#decrypt
resultPath=$1
# ex: result/l.ct
sample=$2
# ex: 40
keyDir=$3
# ex: keys/
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./ANN_decrypt_v1 "$resultPath" "$sample" "$keyDir"
time ${CURR_DIR}/ANN_decrypt_v1 "$resultPath" "$sample" "$keyDir"
\ No newline at end of file
#decrypt result
resultPath=$1
# ex: result/l.ct
sample=$2
# ex: 40
keyDir=$3
# ex: keys/
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./ANN_decrypt_result_v1 "$resultPath" "$sample" "$keyDir"
time ${CURR_DIR}/ANN_decrypt_result_v1 "$resultPath" "$sample" "$keyDir"
\ No newline at end of file
#encrypt
licenseNo=$1
# ex: "23 65 78 127 255" (en ASCII)
filename=$2
# ex: "l" (extension will be .ct)
outputDir=$3
# ex: lcheck/
sample=$4
# ex: 40
keyDir=$5
# ex: keys/
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./ANN_encrypt_v1 "$licenseNo" "$filename" "$outputDir" "$sample" "$keyDir"
time ${CURR_DIR}/ANN_encrypt_v1 "$licenseNo" "$filename" "$outputDir" "$sample" "$keyDir"
\ No newline at end of file
# evaluate
licensePath=$1
# ex: lcheck/l.ct
licensePathList=${@: 2:$#-5}
# licensePathList="${@:2:102}"
# for var in "${@: 2:$#-7}"
# do
# echo "$var"
# done
# ex: parent/l0/l.ct parent/l1/l.ct parent/l2/l.ct parent/l3/l.ct parent/l4/l.ct parent/l5/l.ct parent/l6/l.ct parent/l7/l.ct parent/l8/l.ct parent/l9/l.ct
filename=${@: -4:1}
# ex: "l" (extension will be .ct)
outputDir=${@: -3:1}
# ex: result/
sample=${@: -2:1}
# ex: 40
KeyDir=${@: -1}
# ex: keys/
# linkingKeyPath=${@: -3:1}
# # ex: ckks.lk
# galoisKeyPath=${@: -2:1}
# # ex: ckks.gk
# publicKeyPath=${@: -1}
# # ex: ckks.pk
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./ANN_evaluate_v1 "$licensePath" $licensePathList "$filename" "$outputDir" "$sample" "$linkingKeyPath" "$galoisKeyPath" "$publicKeyPath"
# time ./ANN_evaluate_v1 "$licensePath" $licensePathList "$filename" "$outputDir" "$sample" "$KeyDir"
time ${CURR_DIR}/ANN_evaluate_v1 "$licensePath" $licensePathList "$filename" "$outputDir" "$sample" "$KeyDir"
\ No newline at end of file
#gen key
keyDir=$1
# ex: keys/
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# time ./ANN_genkey_v1 "$keyDir"
time ${CURR_DIR}/ANN_genkey_v1 "$keyDir"
\ No newline at end of file
243 65 191 66 134 2 36 209 39 215 143 95 22 211 213 168 142 178 25 77 88 120 159 33 247 144 244 253 14 39 205 86 123 249 215 166 113 82 205 150
175 50 195 231 96 99 136 186 176 104 92 99 13 0 110 219 168 146 101 169 70 189 249 6 205 90 118 46 166 136 3 243 158 95 230 38 149 234 81 68
175 223 168 200 187 205 246 220 192 127 220 10 38 6 241 89 77 25 53 169 12 91 49 133 75 89 118 147 211 16 18 90 159 50 175 114 249 103 145 153
49 38 145 31 236 196 166 137 78 10 134 2 9 48 43 202 166 45 141 90 71 90 3 252 26 204 222 82 120 151 253 232 223 158 248 92 76 218 17 20
253 197 52 226 110 61 176 101 59 117 111 242 135 173 213 205 211 108 89 132 49 113 195 86 63 122 64 184 137 144 183 174 195 112 166 50 32 10 55 61
174 82 23 246 3 4 64 68 96 49 0 64 111 225 149 57 79 42 33 154 9 38 130 10 151 121 104 222 138 228 248 83 145 192 106 30 126 198 248 76
180 247 84 38 133 245 173 82 166 53 211 109 137 8 48 191 137 233 238 3 6 252 162 232 253 125 29 210 241 208 9 1 160 232 209 215 203 250 19 95
224 122 90 44 22 59 96 241 96 5 186 23 249 4 201 102 23 99 38 242 38 11 99 253 184 38 146 248 198 74 214 21 114 63 242 42 100 142 50 9
27 95 228 0 180 66 2 225 77 120 56 85 176 254 198 157 229 112 97 253 181 26 235 172 93 123 154 100 112 157 17 231 249 250 168 61 185 10 23 223
93 79 52 151 155 73 110 9 111 93 241 249 109 94 46 181 223 180 146 145 180 220 10 88 208 243 229 116 55 68 219 252 230 119 188 235 75 213 186 49
113 68 160 136 24 127 12 242 95 245 251 193 159 128 167 209 169 141 59 75 148 209 46 147 127 11 96 49 101 36 27 168 159 70 133 223 190 41 62 111
204 224 117 188 183 127 35 194 202 71 227 252 187 199 93 187 45 6 13 26 160 189 74 4 42 41 220 73 14 149 232 126 38 240 250 32 154 10 86 117
50 177 30 212 186 247 71 41 17 66 116 129 97 126 206 97 211 146 215 132 166 164 159 196 179 219 118 9 223 202 9 36 165 18 103 127 127 86 141 7
100 132 232 248 220 67 101 79 90 171 184 246 27 161 186 73 60 207 87 41 90 216 92 128 37 168 132 223 226 77 122 237 189 84 167 2 177 29 190 34
125 249 230 63 58 47 50 166 229 202 116 158 178 187 13 206 170 66 68 247 225 96 191 118 244 173 231 73 104 151 81 94 129 186 112 2 177 16 25 176
3 110 136 235 79 87 181 238 1 10 63 76 249 75 138 219 113 139 120 142 22 14 15 10 106 41 20 88 214 10 222 249 64 154 201 65 155 15 64 126
122 208 157 182 115 121 122 45 23 70 77 10 118 119 196 193 133 76 171 235 237 84 161 69 250 250 87 180 132 227 173 58 224 173 76 127 61 216 180 42
160 8 76 36 240 227 62 127 191 177 184 122 236 65 168 71 127 41 29 237 240 13 243 13 82 252 189 84 207 17 237 36 148 39 100 79 188 117 226 34
220 170 19 17 240 120 251 108 73 236 13 222 6 151 158 15 114 111 224 137 96 67 227 226 163 183 75 168 169 142 68 168 43 116 110 2 40 48 246 56
129 18 215 139 153 101 0 78 41 84 166 43 124 117 38 143 234 111 196 215 224 227 139 168 121 168 243 190 113 206 242 244 106 238 36 228 232 242 116 23
96 196 168 14 3 243 231 228 157 42 92 68 180 245 158 92 24 220 95 20 242 40 220 203 97 204 117 236 167 197 168 145 194 116 54 83 240 204 224 196
242 120 49 36 230 129 177 52 253 37 64 122 162 25 33 241 144 30 57 243 96 218 181 34 83 111 47 109 12 222 236 60 106 211 151 240 106 90 222 176
188 94 62 191 29 58 226 217 156 180 232 75 128 182 96 135 50 43 237 169 239 132 195 85 110 183 221 218 19 132 178 6 56 170 239 25 173 18 2 177
172 121 70 34 39 58 92 54 221 187 16 209 74 118 106 236 157 67 137 56 63 186 26 77 163 164 49 180 219 71 60 51 6 153 226 123 72 154 108 218
37 50 1 84 90 216 199 53 10 51 50 112 92 140 146 32 34 216 92 88 2 27 35 110 103 123 14 36 85 250 238 108 175 212 31 253 203 237 252 38
186 93 22 42 241 233 240 19 6 1 135 63 216 27 244 242 234 225 172 60 202 29 162 209 45 48 50 2 25 114 16 193 49 18 223 6 92 226 247 152
7 52 3 64 158 201 131 133 244 252 137 177 48 12 212 32 80 155 115 46 69 104 164 229 73 85 47 20 43 109 127 157 74 198 173 230 80 81 77 89
52 157 7 85 253 86 36 188 136 153 153 217 240 216 10 220 82 128 206 131 3 132 187 146 154 129 173 249 241 223 194 181 34 119 223 47 12 3 120 175
79 248 87 79 141 253 81 183 218 242 193 142 225 128 43 194 89 249 91 113 156 9 6 225 67 81 62 247 53 240 54 233 203 167 43 45 89 87 38 191
139 236 151 143 164 112 20 19 46 27 245 112 95 111 228 78 41 202 219 223 46 51 0 87 22 93 206 164 184 244 227 156 65 85 196 12 39 10 38 211
204 6 40 128 152 29 83 46 202 117 5 182 176 253 39 35 178 54 196 62 92 208 132 41 120 55 154 34 104 253 99 105 140 31 192 187 27 121 250 170
112 151 190 147 230 92 201 191 166 248 147 40 120 158 197 26 157 180 66 205 96 164 166 202 85 140 163 189 45 57 100 208 8 45 220 97 122 92 187 79
19 235 223 178 38 129 247 230 75 205 34 201 225 86 107 188 195 222 57 209 209 29 117 39 137 150 179 210 135 220 201 87 59 66 14 230 125 107 69 215
128 238 197 217 149 64 76 132 85 73 113 194 38 240 118 246 236 157 86 79 68 64 183 85 231 97 77 65 242 215 205 237 149 121 71 10 186 84 138 81
73 148 48 26 197 191 106 117 210 190 160 17 57 243 43 154 33 229 19 97 121 124 148 187 205 157 31 228 139 110 77 43 227 234 153 106 37 84 195 95
105 171 209 97 188 215 172 152 250 234 104 67 10 36 190 3 10 28 163 200 182 80 230 38 136 196 67 194 36 234 231 236 208 214 138 112 219 50 161 115
35 110 94 87 17 149 240 187 223 184 156 48 252 243 43 13 122 214 98 93 13 69 47 71 19 12 20 83 31 32 69 77 95 251 5 50 179 115 22 104
144 58 242 80 215 234 31 8 220 168 129 223 119 88 142 201 43 128 151 32 164 73 209 241 222 162 16 196 207 5 200 198 155 249 138 206 115 56 176 7
192 241 134 145 232 209 129 59 165 109 163 35 191 115 10 193 94 52 28 2 156 95 47 234 252 128 169 37 145 175 244 123 31 122 48 90 208 18 199 48
30 155 143 133 138 228 122 76 0 216 17 161 128 159 70 128 98 233 124 98 111 43 12 110 112 126 185 230 2 254 115 1 184 188 252 154 244 87 24 251
246 230 251 113 82 63 122 176 177 13 143 199 184 66 180 221 86 138 5 204 70 44 78 205 175 57 231 112 250 16 27 57 0 96 72 15 231 113 220 142
96 219 38 213 82 125 36 56 146 138 21 184 89 206 248 215 129 133 61 173 68 178 44 242 135 198 95 140 30 73 4 135 133 243 168 75 126 161 230 144
77 148 87 166 0 213 254 109 133 197 23 176 116 139 183 177 14 247 103 168 2 179 58 189 65 224 241 240 51 117 227 95 151 93 246 82 50 31 36 146
54 44 18 64 95 15 195 97 161 107 78 170 56 203 219 88 87 205 251 144 95 235 164 90 158 165 154 197 100 124 41 4 190 250 215 247 113 6 118 168
82 38 87 61 160 228 230 70 1 82 221 201 88 10 251 185 131 73 130 223 85 253 51 53 38 225 197 115 15 24 86 118 142 33 78 1 134 27 7 63
187 49 168 238 221 95 101 148 142 48 32 216 107 44 178 5 151 239 9 210 27 63 80 213 78 154 185 115 116 154 205 218 151 72 51 0 19 80 47 35
129 83 198 111 206 165 128 66 93 136 144 1 98 43 191 32 140 108 4 61 96 220 223 208 190 103 181 221 120 177 28 171 118 109 104 16 181 205 89 119
144 237 152 98 240 33 69 171 190 105 241 216 47 79 100 77 232 102 3 207 45 218 12 252 50 169 172 79 199 8 74 185 102 204 239 68 60 110 180 103
134 147 162 53 146 7 22 61 179 85 73 136 152 129 215 91 155 20 98 184 167 140 71 220 176 102 0 232 24 230 98 64 238 198 78 121 214 47 49 30
33 152 125 6 139 198 251 17 102 221 96 44 14 97 132 122 47 148 123 78 242 79 90 68 41 195 234 94 92 88 144 149 151 222 149 222 23 169 128 96
5 147 175 167 148 160 245 249 149 190 251 144 170 43 6 31 105 179 238 220 78 96 162 100 33 102 222 230 41 20 220 71 197 21 2 230 105 89 186 23
189 25 11 66 195 114 227 94 239 107 114 60 13 135 173 221 29 106 188 224 2 99 101 16 65 8 59 118 108 101 53 130 218 58 85 241 161 202 95 28
242 7 36 125 186 134 74 190 201 239 248 17 67 198 17 130 101 233 9 144 50 24 141 186 153 168 116 176 244 46 85 75 185 226 242 187 114 201 46 185
189 212 128 105 116 216 27 158 103 36 104 182 63 48 134 164 141 221 192 138 64 161 12 226 96 199 15 220 232 232 249 160 199 174 150 213 83 237 67 98
172 128 175 243 30 178 226 235 6 21 96 79 29 55 68 61 186 133 3 238 238 209 235 225 142 205 245 110 81 201 214 118 121 230 157 75 87 156 171 221
150 88 231 85 232 232 25 199 105 115 195 242 253 157 187 99 254 189 133 153 204 26 44 247 99 116 201 198 134 144 221 113 37 100 179 225 44 204 112 58
195 216 121 2 140 125 171 120 57 131 195 38 29 171 168 44 75 216 34 21 205 136 228 148 69 76 224 157 29 177 246 42 193 208 29 15 227 142 249 113
6 169 58 207 217 235 221 2 55 238 220 201 52 72 150 86 195 201 162 243 28 39 14 36 73 171 61 153 126 79 50 23 157 244 201 32 94 176 3 148
184 136 252 175 214 160 230 141 6 28 32 31 25 201 120 165 51 201 28 174 47 41 88 137 243 141 3 147 81 144 45 227 1 88 167 242 135 229 96 226
221 63 43 181 252 208 89 114 28 160 232 107 239 139 37 235 122 161 137 215 4 225 151 69 107 136 192 240 119 72 193 67 248 98 164 222 134 27 217 71
159 177 248 8 22 8 225 53 34 85 30 59 239 208 75 34 55 10 48 36 186 78 177 130 153 47 204 153 244 14 195 56 42 194 9 16 158 37 179 54
196 80 154 144 140 106 144 178 227 226 95 98 19 205 74 248 25 212 145 71 103 17 206 163 253 31 219 0 207 78 163 214 162 230 119 242 161 43 242 9
26 214 71 182 197 167 77 154 90 51 97 152 93 239 253 62 210 6 78 166 134 153 7 28 194 211 96 83 238 97 207 60 125 99 152 91 176 215 43 131
49 171 224 67 45 8 152 176 154 138 185 111 138 218 209 10 161 147 61 202 2 206 158 133 47 184 69 109 209 120 33 232 150 52 19 220 12 222 145 121
14 223 234 125 182 85 47 214 2 201 239 31 136 171 239 137 222 109 126 64 185 240 242 15 60 174 204 141 78 201 251 143 147 18 174 242 50 104 212 96
80 99 123 154 99 103 182 179 200 107 120 89 184 38 133 206 80 117 99 95 134 166 168 87 133 46 113 218 165 148 116 204 176 163 84 97 153 208 247 104
75 14 63 135 208 245 119 40 181 217 233 28 201 238 140 156 80 1 202 29 233 18 152 65 180 195 39 169 179 18 48 191 226 96 118 68 119 160 34 82
230 207 122 86 55 40 145 243 126 215 183 187 111 55 55 234 127 170 154 229 227 55 63 146 253 122 192 181 163 140 229 197 33 178 145 40 131 170 244 131
77 215 49 68 142 109 44 231 248 204 237 147 226 134 103 161 32 229 126 164 4 164 17 54 225 215 166 43 134 152 151 244 1 112 143 54 42 0 145 164
100 251 82 139 171 82 80 102 203 100 203 167 167 185 30 80 117 175 229 222 221 128 18 172 218 62 57 134 93 174 46 23 155 10 82 182 138 209 5 121
154 163 46 67 239 144 180 0 114 232 112 119 238 71 119 235 6 38 161 70 37 29 170 205 117 236 128 200 73 186 104 31 160 1 236 14 170 232 80 42
17 211 4 136 175 119 46 100 228 130 217 224 34 254 36 73 50 35 236 177 243 151 152 121 156 155 159 177 160 173 39 90 43 2 241 223 76 228 87 103
169 206 87 76 200 243 28 63 172 126 243 19 22 243 9 180 62 155 244 218 211 133 169 59 36 6 200 112 23 237 4 59 143 210 169 242 19 88 164 83
19 203 87 241 39 235 176 141 68 184 116 111 143 134 252 160 155 51 235 214 69 89 14 81 124 191 99 187 154 142 44 2 123 183 239 22 35 159 237 180
100 103 196 211 197 44 201 83 240 240 222 237 168 8 79 242 252 119 225 133 27 75 12 91 213 192 108 124 41 183 73 158 228 122 151 61 24 203 4 40
240 146 118 63 84 208 253 218 31 219 161 245 139 155 215 133 67 97 31 63 70 213 26 136 208 18 148 175 143 106 89 110 109 11 20 140 232 54 54 116
215 63 197 97 52 142 119 104 12 43 251 248 94 37 20 189 121 237 26 55 180 214 121 227 200 56 181 212 141 231 254 77 145 6 13 52 69 67 87 21
94 140 175 252 248 213 69 144 114 122 24 93 97 189 221 148 37 34 153 216 50 84 143 42 128 92 244 34 163 170 8 96 53 34 79 141 215 178 251 248
31 4 33 217 50 106 111 32 68 196 73 66 223 10 61 162 187 4 230 107 238 130 186 94 154 124 188 109 57 171 174 100 141 110 44 233 21 149 46 1
152 108 19 201 39 178 165 29 169 210 27 14 208 86 23 161 146 39 253 59 211 124 49 231 175 20 212 194 210 182 142 49 46 232 97 113 250 23 89 46
150 94 60 73 120 117 241 166 229 253 163 220 4 156 89 79 139 185 209 141 35 178 90 243 148 111 132 233 120 19 132 5 227 65 10 61 156 92 30 22
127 65 65 78 70 235 178 212 111 49 75 249 129 26 181 128 2 159 144 214 62 27 23 23 90 129 187 119 52 88 131 103 137 248 212 143 108 63 217 83
108 148 116 144 155 94 50 23 216 40 142 27 137 26 171 179 122 53 5 11 104 129 218 212 73 175 160 80 170 228 161 243 179 185 191 7 186 8 42 243
108 192 115 49 141 130 194 33 150 24 72 230 212 61 228 92 250 253 243 86 163 21 223 67 85 58 227 243 176 44 230 49 174 154 31 230 209 233 142 202
222 102 86 205 182 235 109 79 237 138 210 168 197 185 206 124 45 25 209 76 19 132 51 13 195 170 125 214 235 205 49 243 51 89 90 179 64 202 195 105
36 122 78 19 11 191 90 179 49 157 56 195 227 248 207 108 120 85 93 84 227 201 61 223 78 53 221 35 207 127 82 100 227 155 111 89 199 192 98 142
86 154 170 18 104 19 94 133 7 234 85 143 38 236 120 171 31 181 4 104 98 172 219 246 152 121 162 144 201 251 55 71 55 13 248 197 122 44 75 232
134 84 38 28 8 252 208 217 203 36 157 114 143 226 223 176 182 165 20 18 156 48 94 136 183 76 165 148 33 72 157 171 172 181 0 195 20 75 2 175
86 84 249 178 214 96 104 190 18 15 235 239 147 165 182 68 244 89 42 152 252 156 13 96 254 3 204 249 116 87 128 54 136 100 64 24 39 20 159 247
208 126 15 89 202 90 63 197 48 181 85 57 169 154 18 79 101 94 63 39 94 11 59 137 64 87 55 152 237 161 183 82 253 47 5 245 39 10 176 125
206 62 169 198 37 59 60 83 153 8 31 221 76 29 237 198 22 0 239 101 20 128 189 218 0 108 250 9 92 221 22 228 197 155 56 245 238 173 44 157
58 233 22 58 38 241 229 253 55 231 94 137 50 139 112 139 95 193 64 100 114 43 165 235 236 13 47 14 109 145 221 244 45 151 105 187 2 0 19 234
217 111 78 154 50 197 31 175 85 41 147 217 128 203 183 159 0 55 4 41 36 15 108 184 139 183 55 13 85 113 62 11 234 44 87 204 209 210 120 29
152 12 157 80 80 148 225 215 145 90 229 157 196 4 174 194 17 127 211 169 84 25 133 97 91 137 203 33 120 216 44 108 11 158 58 214 148 165 90 173
228 210 184 66 66 145 254 216 116 7 190 96 113 173 236 1 81 225 39 123 188 8 24 15 80 254 77 59 1 142 172 154 139 52 105 40 219 99 81 216
62 181 17 211 233 249 43 93 6 240 107 20 107 37 123 233 243 131 225 137 17 193 70 74 73 177 180 247 134 139 145 36 97 101 101 125 194 147 136 134
153 225 152 193 104 116 18 63 151 230 241 230 135 204 10 44 126 48 68 30 251 160 17 140 98 52 151 201 154 17 36 73 17 179 138 215 98 75 235 42
214 103 74 89 186 178 138 124 197 2 237 205 136 135 246 179 83 136 194 148 152 142 133 56 61 150 56 64 46 164 25 71 178 242 131 196 171 174 93 184
228 20 26 114 115 154 89 87 229 168 36 186 81 202 99 223 196 218 70 252 30 151 232 18 163 116 251 165 43 189 95 250 44 52 103 244 95 43 217 220
6 29 54 106 164 42 19 49 142 133 94 82 174 204 18 83 132 184 239 212 26 83 58 223 237 88 237 225 88 4 53 50 79 8 90 224 103 157 174 101
201 2 143 195 58 94 175 28 0 254 237 152 126 249 240 56 247 138 11 219 155 178 229 199 153 146 136 86 22 59 200 18 124 136 54 241 181 170 183 196
32 114 115 105 20 163 240 105 135 124 190 106 63 159 1 200 136 118 212 148 179 200 155 113 103 47 37 85 32 55 228 105 244 202 66 24 199 102 20 90
/*
(C) Copyright 2022 CEA LIST. All Rights Reserved.
*/
#include "print_api.h"
using namespace std;
void print_vectors(vector<int64_t> &v)
{
for (int i = 0; i < (int)(v.size()); ++i)
cout << v[i] << " ";
cout << endl;
}
void print_vectors(vector<double> &v)
{
for (int i = 0; i < (int)(v.size()); ++i)
cout << v[i] << " ";
cout << endl;
}
void print_matrix(vector<vector<int64_t>> &m)
{
for (int i = 0; i < (int)(m.size()); ++i)
print_vectors(m[i]);
}
void print_matrix(vector<vector<double>> &m)
{
for (int i = 0; i < (int)(m.size()); ++i)
print_vectors(m[i]);
}
/*void rand_vector(int seed, uint64_t v_space, uint32_t v_size, vector<int64_t>& v)
{
srand(seed);
for (uint32_t i = 0; i < v_size; ++i)
v[i] = rand() % v_space;
}
*/
void rand_vector(int seed, uint64_t v_space, uint32_t v_size, vector<int64_t>& v)
{
std::uniform_real_distribution<double> unif(0, v_space);
std::default_random_engine re;
re.seed(seed);
for (uint32_t i = 0; i < v_size; ++i)
v[i] = (int64_t)unif(re) % v_space;
}
void rand_vector(int seed, uint64_t v_space, uint32_t v_size, vector<double>& v)
{
std::uniform_real_distribution<double> unif(0, v_space);
std::default_random_engine re;
re.seed(seed);
for (uint32_t i = 0; i < v_size; ++i)
v[i] = unif(re);
}
/*
(C) Copyright 2022 CEA LIST. All Rights Reserved.
*/
#ifndef _PRINT_API_H_
#define _PRINT_API_H_
/* includes */
#include <iostream>
#include <random>
#include <cstdio>
#include <vector>
#include <time.h>
/* prototypes */
void print_vectors(std::vector<int64_t>& v);
void print_vectors(std::vector<double> &v);
void print_matrix(std::vector<std::vector<int64_t>>& m);
void print_matrix(std::vector<std::vector<double>>& m);
void rand_vector(int seed, uint64_t v_space, uint32_t v_size, std::vector<int64_t>& v);
void rand_vector(int seed, uint64_t v_space, uint32_t v_size, std::vector<double>& v);
#endif
#include "seal_api.h"
/* namespaces */
using namespace std;
using namespace seal;
/* functions */
int open_binary_file(ifstream &in_file, const string &filename)
{
int ret = 1;
in_file = ifstream(filename, ios::binary);
if (!in_file)
{
// cerr << "[ERRROR] file opening failure" << endl;
ret = 0;
}
return ret;
}
int open_binary_file(ofstream &out_file, const string &filename)
{
int ret = 1;
out_file = ofstream(filename, ios::binary);
if (!out_file)
{
// cerr << "[ERRROR] file opening failure" << endl;
ret = 0;
}
return ret;
}
int save_params(EncryptionParameters &params, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
params.save(out_file);
else
ret = 0;
return ret;
}
int load_params(EncryptionParameters &params, const string &filename)
{
int ret = 1;
ifstream in_file;
if (open_binary_file(in_file, filename))
params.load(in_file);
else
ret = 0;
return ret;
}
void init_context(size_t poly_d, size_t p_modulus, shared_ptr<SEALContext> &context)
{
EncryptionParameters params(scheme_type::BFV);
params.set_poly_modulus_degree(poly_d);
params.set_coeff_modulus(CoeffModulus::BFVDefault(poly_d));
params.set_plain_modulus(p_modulus);
save_params(params, "bfv_params.conf");
context = SEALContext::Create(params);
print_context(context);
}
void init_context_batching(size_t poly_d, int bit_size, shared_ptr<SEALContext> &context, const std::string &key_dir)
{
EncryptionParameters params(scheme_type::BFV);
params.set_poly_modulus_degree(poly_d);
params.set_coeff_modulus(CoeffModulus::BFVDefault(poly_d));
params.set_plain_modulus(PlainModulus::Batching(poly_d, bit_size));
save_params(params, key_dir + "bfv_params.conf");
context = SEALContext::Create(params);
print_context(context);
}
// void ckks_init_context_batching(
// size_t poly_d, int bit_size, shared_ptr<SEALContext> &context, const std::string &key_dir)
// {
// EncryptionParameters params(scheme_type::CKKS);
// params.set_poly_modulus_degree(poly_d);
// params.set_coeff_modulus(CoeffModulus::BFVDefault(poly_d));
// params.set_plain_modulus(PlainModulus::Batching(poly_d, bit_size));
// save_params(params, key_dir + "bfv_params.conf");
// context = SEALContext::Create(params);
// print_context(context);
// }
void init_context_batching(
size_t poly_d, vector<int> bit_sizes, std::uint64_t plain_modulus, shared_ptr<SEALContext> &context,
const std::string &key_dir)
{
EncryptionParameters params(scheme_type::BFV);
params.set_poly_modulus_degree(poly_d);
params.set_coeff_modulus(CoeffModulus::Create(poly_d, { 36, 36, 37 }));
params.set_plain_modulus(plain_modulus);
save_params(params, key_dir + "bfv_params.conf");
context = SEALContext::Create(params);
print_context(context);
}
// void ckks_init_context_batching(
// size_t poly_d, vector<int> bit_sizes, std::uint64_t plain_modulus, shared_ptr<SEALContext> &context,
// const std::string &key_dir)
// {
// EncryptionParameters params(scheme_type::CKKS);
// params.set_poly_modulus_degree(poly_d);
// params.set_coeff_modulus(CoeffModulus::Create(poly_d, { 36, 36, 37 }));
// params.set_plain_modulus(plain_modulus);
// save_params(params, key_dir + "ckks_params.conf");
// context = SEALContext::Create(params);
// print_context(context);
// }
// void init_context_ckks(size_t poly_d, std::uint64_t plain_modulus, shared_ptr<SEALContext> &context,
// const std::string &key_dir)
// {
// int ret = 1;
// EncryptionParameters params(scheme_type::CKKS);
// params.set_poly_modulus_degree(poly_d);
// switch (poly_d)
// {
// case 4096:
// params.set_coeff_modulus(CoeffModulus::Create(poly_d, { 30, 20, 20, 30 }));
// break;
// case 8192:
// params.set_coeff_modulus(CoeffModulus::Create(poly_d, { 60, 40, 40, 60 }));
// break;
// default:
// cout << "[seal-error] enter valid poly degree: 4096 or 8192" << endl;
// break;
// }
// // params.set_plain_modulus(plain_modulus);
// save_params(params, key_dir + "ckks_params.conf");
// context = SEALContext::Create(params);
// print_context(context);
// // context = SEALContext(params);
// // if (context.parameter_error_message() == "valid")
// // {
// // clog << "[seal-ckks] parameter validation: valid" << endl;
// // save_params(params, "ckks_params.conf");
// // }
// // else
// // {
// // clog << "[seal-error] parameter validation: invalid" << endl;
// // ret = 0;
// // }
// // return ret;
// }
// int init_context_ckks(uint32_t poly_deg, SEALContext& context)
int init_context_ckks(uint32_t poly_deg, shared_ptr<SEALContext> &context, const std::string &key_dir)
{
int ret = 1;
EncryptionParameters params(scheme_type::CKKS);
params.set_poly_modulus_degree(poly_deg);
switch (poly_deg)
{
case 4096:
params.set_coeff_modulus(CoeffModulus::Create(poly_deg, { 30, 20, 20, 30 }));
break;
case 8192:
params.set_coeff_modulus(CoeffModulus::Create(poly_deg, { 60, 40, 40, 60 }));
break;
case 16384:
params.set_coeff_modulus(CoeffModulus::Create(poly_deg, { 60, 40, 40, 40, 40, 40, 40, 40, 60 }));
// params.set_coeff_modulus(CoeffModulus::Create(poly_deg, { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 }));
// params.set_coeff_modulus(CoeffModulus::Create(poly_deg, { 30, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 30 }));
break;
default:
cout << "[seal-error] enter valid poly degree: 4096 or 8192" << endl;
break;
}
context = SEALContext::Create(params);
// if (context.parameter_error_message() == "valid") {
// clog << "[seal-ckks] parameter validation: valid" << endl;
save_params(params, key_dir + "ckks_params.conf");
// }
// else {
// clog << "[seal-error] parameter validation: invalid" << endl;
// ret = 0;
// }
print_context(context);
return ret;
}
void load_context(shared_ptr<SEALContext> &context, const string &filename)
{
EncryptionParameters params;
load_params(params, filename);
context = SEALContext::Create(params);
print_context(context);
}
/* print paramaeters function from examples.h */
void print_context(shared_ptr<SEALContext> &context)
{
if (!context)
throw invalid_argument("[ERROR] context is not set");
auto &context_data = *context->key_context_data();
/* which scheme is used */
string scheme_name;
switch (context_data.parms().scheme())
{
case seal::scheme_type::BFV:
scheme_name = "BFV";
break;
case seal::scheme_type::CKKS:
scheme_name = "CKKS";
break;
default:
throw invalid_argument("[ERROR] unsupported scheme");
}
cout << "[CONTEXT] scheme: " << scheme_name << endl;
cout << "[CONTEXT] poly_modulus_degree: " << context_data.parms().poly_modulus_degree() << endl;
// /* Print the size of the true (product) coefficient modulus */
cout << "[CONTEXT] coeff_modulus size: ";
cout << context_data.total_coeff_modulus_bit_count() << " (";
auto coeff_modulus = context_data.parms().coeff_modulus();
size_t coeff_modulus_size = coeff_modulus.size();
for (size_t i = 0; i < coeff_modulus_size - 1; i++)
cout << coeff_modulus[i].bit_count() << " + ";
cout << coeff_modulus.back().bit_count();
cout << ") bits" << endl;
cout << "[CONTEXT] coeff_modulus values: (";
for (int i = 0; i < (coeff_modulus.size() - 1); ++i)
{
cout << coeff_modulus[i].value() << ",\t";
cout << coeff_modulus[coeff_modulus.size() - 1].value() << ")" << endl;
}
/* For the BFV scheme print the plain_modulus parameter */
if (context_data.parms().scheme() == seal::scheme_type::BFV)
{
cout << "[CONTEXT] plain_modulus: " << context_data.parms().plain_modulus().value() << endl;
}
}
int save_key(PublicKey &k, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
k.save(out_file);
else
ret = 0;
return ret;
}
int save_key(SecretKey &k, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
k.save(out_file);
else
ret = 0;
return ret;
}
int save_key(RelinKeys &k, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
k.save(out_file);
else
ret = 0;
return ret;
}
int save_key(Serializable<RelinKeys> &k, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
k.save(out_file);
else
ret = 0;
return ret;
}
int save_key(GaloisKeys &k, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
k.save(out_file);
else
ret = 0;
return ret;
}
int save_key(Serializable<GaloisKeys> &k, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
k.save(out_file);
else
ret = 0;
return ret;
}
int load_key(shared_ptr<SEALContext> &context, const string &filename, PublicKey &k)
{
int ret = 1;
ifstream in_file;
if (open_binary_file(in_file, filename))
k.load(context, in_file);
else
ret = 0;
return ret;
}
int load_key(shared_ptr<SEALContext> &context, const string &filename, SecretKey &k)
{
int ret = 1;
ifstream in_file;
if (open_binary_file(in_file, filename))
k.load(context, in_file);
else
ret = 0;
return ret;
}
int load_key(shared_ptr<SEALContext> &context, const string &filename, RelinKeys &k)
{
int ret = 1;
ifstream in_file;
if (open_binary_file(in_file, filename))
k.load(context, in_file);
else
ret = 0;
return ret;
}
int load_key(shared_ptr<SEALContext> &context, const string &filename, GaloisKeys &k)
{
int ret = 1;
ifstream in_file;
if (open_binary_file(in_file, filename))
k.load(context, in_file);
else
ret = 0;
return ret;
}
void generate_keys(size_t poly_d, size_t p_modulus, bool serializable)
{
shared_ptr<SEALContext> context;
init_context(poly_d, p_modulus, context);
KeyGenerator keygen(context);
PublicKey pk = keygen.public_key();
save_key(pk, "bfv.pk");
SecretKey sk = keygen.secret_key();
save_key(sk, "bfv.sk");
if (serializable)
{
Serializable<RelinKeys> lk = keygen.relin_keys();
save_key(lk, "bfv.lk");
}
else
{
RelinKeys lk = keygen.relin_keys_local();
save_key(lk, "bfv.lk");
}
}
bool suffix_exist(const std::string &str, const std::string &suffix)
{
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
/*
Helper function: Prints the parameters in a SEALContext.
*/
inline void print_parameters(std::shared_ptr<seal::SEALContext> context)
{
// Verify parameters
if (!context)
{
throw std::invalid_argument("context is not set");
}
auto &context_data = *context->key_context_data();
/*
Which scheme are we using?
*/
std::string scheme_name;
switch (context_data.parms().scheme())
{
case seal::scheme_type::BFV:
scheme_name = "BFV";
break;
case seal::scheme_type::CKKS:
scheme_name = "CKKS";
break;
default:
throw std::invalid_argument("unsupported scheme");
}
// cout << "/" << std::endl;
// cout << "| Encryption parameters :" << std::endl;
// cout << "| scheme: " << scheme_name << std::endl;
// cout << "| poly_modulus_degree: " << context_data.parms().poly_modulus_degree() << std::endl;
/*
Print the size of the true (product) coefficient modulus.
*/
// cout << "| coeff_modulus size: ";
// cout << context_data.total_coeff_modulus_bit_count() << " (";
auto coeff_modulus = context_data.parms().coeff_modulus();
std::size_t coeff_modulus_size = coeff_modulus.size();
// for (std::size_t i = 0; i < coeff_modulus_size - 1; i++)
// {
// cout << coeff_modulus[i].bit_count() << " + ";
// }
// cout << coeff_modulus.back().bit_count();
// cout << ") bits" << std::endl;
/*
For the BFV scheme print the plain_modulus parameter.
*/
// if (context_data.parms().scheme() == seal::scheme_type::BFV)
// {
// cout << "| plain_modulus: " << context_data.parms().plain_modulus().value() << std::endl;
// }
// cout << "\\" << std::endl;
}
void batching_generate_keys(size_t poly_d, int bit_size, string key_dir, bool serializable)
{
shared_ptr<SEALContext> context;
// init_context_batching(poly_d, bit_size, context, key_dir);
// auto qualifiers = context->first_context_data()->qualifiers();
// cout << "Batching enabled: " << boolalpha << qualifiers.using_batching << endl;
string keypath("");
string keypath1("/bfv.pk");
string keypath2("/bfv.sk");
string keypath3("/bfv.lk");
string keypath4("/bfv.gk");
string bfv_params_path(key_dir + "/");
if (suffix_exist(key_dir, "/"))
{
keypath1 = "bfv.pk";
keypath2 = "bfv.sk";
keypath3 = "bfv.lk";
keypath4 = "bfv.gk";
bfv_params_path = key_dir;
}
init_context_batching(poly_d, bit_size, context, bfv_params_path);
KeyGenerator keygen(context);
PublicKey pk = keygen.public_key();
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypathname pk: " << keypath << endl;
save_key(pk, keypath);
keypath = "";
keypath.append(key_dir);
keypath.append(keypath2);
SecretKey sk = keygen.secret_key();
// cout << "[INFO] Keypathname sk: " << keypath << endl;
save_key(sk, keypath);
keypath = "";
keypath.append(key_dir);
keypath.append(keypath3);
// cout << "[INFO] Keypathname lk: " << keypath << endl;
if (serializable)
{
Serializable<RelinKeys> lk = keygen.relin_keys();
save_key(lk, keypath);
}
else
{
RelinKeys lk = keygen.relin_keys_local();
save_key(lk, keypath);
}
keypath = "";
keypath.append(key_dir);
keypath.append(keypath4);
// cout << "[INFO] Keypathname gk: " << keypath << endl;
if (serializable)
{
Serializable<GaloisKeys> gk = keygen.galois_keys();
save_key(gk, keypath);
}
else
{
GaloisKeys gk = keygen.galois_keys_local();
save_key(gk, keypath);
}
// print_parameters(context);
}
void generate_keys_ckks(size_t poly_d, int bit_size, string key_dir, bool serializable)
{
shared_ptr<SEALContext> context;
// init_context_batching(poly_d, bit_size, context, key_dir);
// auto qualifiers = context->first_context_data()->qualifiers();
// cout << "Batching enabled: " << boolalpha << qualifiers.using_batching << endl;
string keypath("");
string keypath1("/ckks.pk");
string keypath2("/ckks.sk");
string keypath3("/ckks.lk");
string keypath4("/ckks.gk");
string ckks_params_path(key_dir + "/");
if (suffix_exist(key_dir, "/"))
{
keypath1 = "ckks.pk";
keypath2 = "ckks.sk";
keypath3 = "ckks.lk";
keypath4 = "ckks.gk";
ckks_params_path = key_dir;
}
// ckks_init_context_batching(poly_d, bit_size, context, ckks_params_path);
// init_context_ckks(poly_d, bit_size, context, ckks_params_path);
init_context_ckks(poly_d, context, ckks_params_path);
KeyGenerator keygen(context);
PublicKey pk = keygen.public_key();
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypathname pk: " << keypath << endl;
save_key(pk, keypath);
keypath = "";
keypath.append(key_dir);
keypath.append(keypath2);
SecretKey sk = keygen.secret_key();
// cout << "[INFO] Keypathname sk: " << keypath << endl;
save_key(sk, keypath);
keypath = "";
keypath.append(key_dir);
keypath.append(keypath3);
// cout << "[INFO] Keypathname lk: " << keypath << endl;
if (serializable)
{
Serializable<RelinKeys> lk = keygen.relin_keys();
save_key(lk, keypath);
}
else
{
RelinKeys lk = keygen.relin_keys_local();
save_key(lk, keypath);
}
keypath = "";
keypath.append(key_dir);
keypath.append(keypath4);
// cout << "[INFO] Keypathname gk: " << keypath << endl;
if (serializable)
{
Serializable<GaloisKeys> gk = keygen.galois_keys();
save_key(gk, keypath);
}
else
{
GaloisKeys gk = keygen.galois_keys_local();
save_key(gk, keypath);
}
print_parameters(context);
}
void batching_generate_keys(
size_t poly_d, vector<int> bit_sizes, std::uint64_t plain_modulus, string key_dir, bool serializable)
{
shared_ptr<SEALContext> context;
// init_context_batching(poly_d, bit_sizes, plain_modulus, context, key_dir);
// auto qualifiers = context->first_context_data()->qualifiers();
// cout << "Batching enabled: " << boolalpha << qualifiers.using_batching << endl;
string keypath("");
string keypath1("/bfv.pk");
string keypath2("/bfv.sk");
string keypath3("/bfv.lk");
string keypath4("/bfv.gk");
string bfv_params_path(key_dir + "/");
if (suffix_exist(key_dir, "/"))
{
keypath1 = "bfv.pk";
keypath2 = "bfv.sk";
keypath3 = "bfv.lk";
keypath4 = "bfv.gk";
bfv_params_path = key_dir;
}
init_context_batching(poly_d, bit_sizes, plain_modulus, context, bfv_params_path);
KeyGenerator keygen(context);
PublicKey pk = keygen.public_key();
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypathname pk: " << keypath << endl;
save_key(pk, keypath);
keypath = "";
keypath.append(key_dir);
keypath.append(keypath2);
SecretKey sk = keygen.secret_key();
// cout << "[INFO] Keypathname sk: " << keypath << endl;
save_key(sk, keypath);
keypath = "";
keypath.append(key_dir);
keypath.append(keypath3);
// cout << "[INFO] Keypathname lk: " << keypath << endl;
if (serializable)
{
Serializable<RelinKeys> lk = keygen.relin_keys();
save_key(lk, keypath);
}
else
{
RelinKeys lk = keygen.relin_keys_local();
save_key(lk, keypath);
}
keypath = "";
keypath.append(key_dir);
keypath.append(keypath4);
// cout << "[INFO] Keypathname gk: " << keypath << endl;
if (serializable)
{
Serializable<GaloisKeys> gk = keygen.galois_keys();
save_key(gk, keypath);
}
else
{
GaloisKeys gk = keygen.galois_keys_local();
save_key(gk, keypath);
}
// print_parameters(context);
}
void init_operator(struct encryptor_t &op_st)
{
load_context(op_st.context, "bfv_params.conf");
PublicKey pk;
load_key(op_st.context, "bfv.pk", pk);
op_st.encr = new Encryptor(op_st.context, pk);
op_st.icode = new IntegerEncoder(op_st.context);
}
void init_operator(struct encryptor_t &op_st, const string &key_dir)
{
string bfv_params_path("");
string bfv_params_path1("/bfv_params.conf");
bfv_params_path = "";
bfv_params_path.append(key_dir);
bfv_params_path.append(bfv_params_path1);
// cout << "[INFO] bfvparams: " << bfv_params_path << endl;
load_context(op_st.context, bfv_params_path);
PublicKey pk;
string keypath("");
string keypath1("bfv.pk");
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypath encrypt: " << keypath << endl;
load_key(op_st.context, keypath, pk);
op_st.encr = new Encryptor(op_st.context, pk);
op_st.icode = new IntegerEncoder(op_st.context);
}
// void init_operator_ckks(struct encryptor_t &op_st, const string &key_dir)
void init_operator_ckks(struct cencryptor_t &op_st, const string &key_dir)
{
string ckks_params_path("");
string ckks_params_path1("/ckks_params.conf");
ckks_params_path = "";
ckks_params_path.append(key_dir);
ckks_params_path.append(ckks_params_path1);
// cout << "[INFO] ckksparams: " << ckks_params_path << endl;
load_context(op_st.context, ckks_params_path);
PublicKey pk;
string keypath("");
string keypath1("ckks.pk");
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypath encrypt: " << keypath << endl;
load_key(op_st.context, keypath, pk);
op_st.encr = new Encryptor(op_st.context, pk);
// op_st.icode = new IntegerEncoder(op_st.context);
op_st.ccode = new CKKSEncoder(op_st.context);
// auto &context_data = *(op_st.context.key_context_data());
auto &context_data = *op_st.context->key_context_data();
int poly_deg = context_data.parms().poly_modulus_degree();
switch (poly_deg)
{
case 4096:
op_st.scale = pow(2.0, 20);
case 8192:
op_st.scale = pow(2.0, 40);
case 16384:
op_st.scale = pow(2.0, 40);
// op_st.scale = pow(2.0, 20);
}
// return 1;
}
void init_operator_ckks(struct cdecryptor_t &op_st, const string &key_dir)
{
string ckks_params_path("");
string ckks_params_path1("/ckks_params.conf");
ckks_params_path = "";
ckks_params_path.append(key_dir);
ckks_params_path.append(ckks_params_path1);
// cout << "[INFO] ckksparams: " << ckks_params_path << endl;
load_context(op_st.context, ckks_params_path);
// PublicKey pk;
SecretKey sk;
string keypath("");
string keypath1("ckks.sk");
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypath encrypt: " << keypath << endl;
load_key(op_st.context, keypath, sk);
op_st.decr = new Decryptor(op_st.context, sk);
// op_st.icode = new IntegerEncoder(op_st.context);
op_st.ccode = new CKKSEncoder(op_st.context);
// auto &context_data = *(op_st.context.key_context_data());
auto &context_data = *op_st.context->key_context_data();
int poly_deg = context_data.parms().poly_modulus_degree();
switch (poly_deg)
{
case 4096:
op_st.scale = pow(2.0, 20);
case 8192:
op_st.scale = pow(2.0, 40);
case 16384:
op_st.scale = pow(2.0, 40);
// op_st.scale = pow(2.0, 20);
}
// return 1;
}
void init_operator_ckks(struct cevaluator_t &op_st, const string &key_dir)
{
string ckks_params_path("");
string ckks_params_path1("/ckks_params.conf");
ckks_params_path = "";
ckks_params_path.append(key_dir);
ckks_params_path.append(ckks_params_path1);
// cout << "[INFO] bfvparams: " << ckks_params_path << endl;
load_context(op_st.context, ckks_params_path);
string keypath("");
string keypath1("ckks.lk");
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypath evaluator: " << keypath << endl;
load_key(op_st.context, keypath, op_st.lk);
string keypath3("");
string keypath4("ckks.gk");
keypath3.append(key_dir);
keypath3.append(keypath4);
// cout << "[INFO] Keypath evaluator: " << keypath3 << endl;
load_key(op_st.context, keypath3, op_st.gk);
// op_st.eval = new Evaluator(op_st.context, sk);
// op_st.icode = new IntegerEncoder(op_st.context);
// op_st.ccode = new CKKSEncoder(op_st.context);
op_st.eval = new Evaluator(op_st.context);
// auto &context_data = *(op_st.context.key_context_data());
auto &context_data = *op_st.context->key_context_data();
int poly_deg = context_data.parms().poly_modulus_degree();
switch (poly_deg)
{
case 4096:
op_st.scale = pow(2.0, 20);
case 8192:
op_st.scale = pow(2.0, 40);
case 16384:
op_st.scale = pow(2.0, 40);
// op_st.scale = pow(2.0, 20);
}
// return 1;
}
// int init_operator_ckks(struct cencryptor_t &op_st, const string &key_dir)
// {
// int ret = 1;
// // if (!load_context("/ckks_params.conf", op_st.context)) {
// // cerr << "[seal-error] failure for loading ckks context during encryptor setting!" << endl;
// // ret = 0;
// // }
// // else {
// // PublicKey pk;
// // if (!load_key(op_st.context, "ckks.pk", pk)) {
// // cerr << "[seal-error] failure for loading ckks public key during encryptor setting!" << endl;
// // ret = 0;
// // }
// // else {
// op_st.encr = new Encryptor(op_st.context, pk);
// op_st.ccode = new CKKSEncoder(op_st.context);
// auto &context_data = *(op_st.context.key_context_data());
// int poly_deg = context_data.parms().poly_modulus_degree();
// switch (poly_deg)
// {
// case 4096:
// op_st.scale = pow(2.0, 20);
// case 8192:
// op_st.scale = pow(2.0, 40);
// }
// // }
// // }
// return ret;
// }
void init_operator_batching(struct encryptor_t &op_st, const string &key_dir)
{
init_operator(op_st, key_dir);
op_st.bcode = new BatchEncoder(op_st.context);
}
void delete_operator(struct encryptor_t &op_st)
{
delete op_st.encr;
delete op_st.icode;
}
void delete_operator_batching(struct encryptor_t &op_st)
{
delete_operator(op_st);
delete op_st.bcode;
}
void delete_operator_ckks(struct cencryptor_t &op_st)
{
delete op_st.encr;
delete op_st.ccode;
}
void delete_operator_ckks(struct cdecryptor_t &op_st)
{
delete op_st.decr;
delete op_st.ccode;
}
void delete_operator_ckks(struct cevaluator_t &op_st)
{
delete op_st.eval;
}
void init_operator(struct decryptor_t &op_st)
{
load_context(op_st.context, "bfv_params.conf");
SecretKey sk;
load_key(op_st.context, "bfv.sk", sk);
op_st.decr = new Decryptor(op_st.context, sk);
op_st.icode = new IntegerEncoder(op_st.context);
}
void init_operator(struct decryptor_t &op_st, const std::string &key_dir)
{
string bfv_params_path("");
string bfv_params_path1("/bfv_params.conf");
bfv_params_path = "";
bfv_params_path.append(key_dir);
bfv_params_path.append(bfv_params_path1);
// cout << "[INFO] bfvparams: " << bfv_params_path << endl;
load_context(op_st.context, bfv_params_path);
SecretKey sk;
string keypath("");
string keypath1("bfv.sk");
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypath decrypt: " << keypath << endl;
load_key(op_st.context, keypath, sk);
op_st.decr = new Decryptor(op_st.context, sk);
op_st.icode = new IntegerEncoder(op_st.context);
}
void init_operator_batching(struct decryptor_t &op_st, const string &key_dir)
{
init_operator(op_st, key_dir);
op_st.bcode = new BatchEncoder(op_st.context);
}
void delete_operator(struct decryptor_t &op_st)
{
delete op_st.decr;
delete op_st.icode;
}
void delete_operator_batching(struct decryptor_t &op_st)
{
delete_operator(op_st);
delete op_st.bcode;
}
void init_operator(struct evaluator_t &op_st)
{
load_context(op_st.context, "bfv_params.conf");
load_key(op_st.context, "bfv.lk", op_st.lk);
op_st.eval = new Evaluator(op_st.context);
}
void init_operator(struct evaluator_t &op_st, const string &relink_key_path)
{
load_context(op_st.context, "bfv_params.conf");
load_key(op_st.context, relink_key_path, op_st.lk);
op_st.eval = new Evaluator(op_st.context);
}
void init_operator_batching(struct evaluator_t &op_st, const string &key_dir)
{
string bfv_params_path("");
string bfv_params_path1("/bfv_params.conf");
bfv_params_path = "";
bfv_params_path.append(key_dir);
bfv_params_path.append(bfv_params_path1);
// cout << "[INFO] bfvparams: " << bfv_params_path << endl;
load_context(op_st.context, bfv_params_path);
string keypath("");
string keypath1("bfv.lk");
keypath.append(key_dir);
keypath.append(keypath1);
// cout << "[INFO] Keypath evaluator: " << keypath << endl;
load_key(op_st.context, keypath, op_st.lk);
string keypath3("");
string keypath4("bfv.gk");
keypath3.append(key_dir);
keypath3.append(keypath4);
// cout << "[INFO] Keypath evaluator: " << keypath3 << endl;
load_key(op_st.context, keypath3, op_st.gk);
op_st.eval = new Evaluator(op_st.context);
}
void delete_operator(struct evaluator_t &op_st)
{
delete op_st.eval;
}
void delete_operator_batching(struct evaluator_t &op_st)
{
delete_operator(op_st);
}
void init_plaintext(struct encryptor_t &op_st, int64_t plain, Plaintext &pt)
{
pt = Plaintext(op_st.icode->encode(plain));
}
void init_ciphertext(struct encryptor_t &op_st, int64_t plain, Ciphertext &ct)
{
// cout << "[INFO] encrypting: " << endl;
op_st.encr->encrypt(Plaintext(op_st.icode->encode(plain)), ct);
// cout << plain << endl;
}
void init_ciphermatrix(struct encryptor_t &op_st, vector<int64_t> &plain_matrix, Ciphertext &encrypted_matrix)
{
// cout << "[INFO] encrypting: " << endl;
Plaintext plaintext_matrix;
// BatchEncoder batch_encoder(op_st.context);
// batch_encoder.encode(plain_matrix, plaintext_matrix);
op_st.bcode->encode(plain_matrix, plaintext_matrix);
// op_st.ccode->encode(plain_matrix, plaintext_matrix);
op_st.encr->encrypt(plaintext_matrix, encrypted_matrix);
// cout << plain << endl;
}
/* ckks plaintext management */
// void init_plaintext_ckks(CKKSEncoder& encoder, double scale, vector<double>& p_vector, Plaintext& pt, bool
// print_info) void init_plaintext_ckks(CKKSEncoder& encoder, double scale, vector<double>& p_vector, Plaintext& pt)
// {
// try {
// encoder.encode(p_vector, scale, pt);
// }
// catch (std::exception &e) {
// clog << "[seal-error] catched exception: " << e.what() << endl;
// clog << "[seal-error] ckks slots encoding error" << endl;
// }
// if (print_info) {
// clog << "[seal-ckks] plaintexts: " << endl;
// print_vectors(p_vector);
// }
// }
/* ckks ciphertext management */
// void init_ciphertext(CKKSEncoder& encoder, Encryptor& encryptor, double scale, vector<double>& c_vector, Ciphertext&
// ct, bool print_info)
void init_ciphertext_ckks(
CKKSEncoder &encoder, Encryptor &encryptor, double scale, vector<double> &c_vector, Ciphertext &ct)
{
Plaintext pt;
try
{
encoder.encode(c_vector, scale, pt);
}
catch (std::exception &e)
{
clog << "[seal-error] catched exception: " << e.what() << endl;
clog << "[seal-error] ckks slots encoding error" << endl;
}
encryptor.encrypt(pt, ct);
// if (print_info) {
// clog << "[seal-ckks] ciphertext inputs: " << endl;
// print_vectors(c_vector);
// }
}
void init_ciphertext_ckks(cencryptor_t &op_st, double scale, vector<double> &c_vector, Ciphertext &ct)
{
init_ciphertext_ckks(*(op_st.ccode), *(op_st.encr), scale, c_vector, ct);
}
void init_ciphertext_ckks(cencryptor_t &op_st, vector<double> &c_vector, Ciphertext &ct)
{
init_ciphertext_ckks(*(op_st.ccode), *(op_st.encr), op_st.scale, c_vector, ct);
}
/* ckks decryption */
void decrypt_ciphertext_ckks(cdecryptor_t &op_st, Ciphertext &ct, vector<double> &pt_val)
{
decrypt_ciphertext_ckks(*(op_st.ccode), *(op_st.decr), ct, pt_val);
}
void decrypt_ciphertext_ckks(CKKSEncoder &encoder, Decryptor &decryptor, Ciphertext &ct, vector<double> &pt_val)
{
Plaintext pt;
decryptor.decrypt(ct, pt);
encoder.decode(pt, pt_val);
// if (print_info) {
// clog << "[seal-ckks] decrypted plaintexts: " << endl;
// print_vectors(pt_val);
// }
}
// vector<double> decrypt_ciphermatrix_ckks(struct decryptor_t &op_st, Ciphertext &ct)
// {
// Plaintext pt;
// op_st.decr->decrypt(ct, pt);
// vector<double> pod_matrix;
// // BatchEncoder batch_encoder(op_st.context);
// // batch_encoder.decode(pt, pod_matrix);
// encoder.decode(pt, pod_matrix);
// // print_matrix(pod_matrix, 3);
// decrypt_ciphertext_ckks
// return pod_matrix;
// }
/* saving ciphertexts and plaintexts */
// plaintexts
int load_plaintext_ckks(struct cencryptor_t &op_st, Plaintext &pt, const string &filename)
{
return load_plaintext(op_st.context, pt, filename);
}
int load_plaintext_ckks(struct cdecryptor_t &op_st, Plaintext &pt, const string &filename)
{
return load_plaintext(op_st.context, pt, filename);
}
int load_plaintext_ckks(struct cevaluator_t &op_st, Plaintext &pt, const string &filename)
{
return load_plaintext(op_st.context, pt, filename);
}
// ciphertexts
int load_ciphertext_ckks(struct cevaluator_t &op_st, Ciphertext &ct, const string &filename)
{
return load_ciphertext(op_st.context, ct, filename);
}
int load_ciphertext_ckks(struct cencryptor_t &op_st, Ciphertext &ct, const string &filename)
{
return load_ciphertext(op_st.context, ct, filename);
}
int load_ciphertext_ckks(struct cdecryptor_t &op_st, Ciphertext &ct, const string &filename)
{
return load_ciphertext(op_st.context, ct, filename);
}
void decrypt_ciphertext(struct decryptor_t &op_st, Ciphertext &ct)
{
Plaintext pt;
op_st.decr->decrypt(ct, pt);
int64_t res = op_st.icode->decode_int64(pt);
// cout << "[INFO] decrypted result: " << res << endl;
}
int64_t decrypt_ciphertext_and_return_value(struct decryptor_t &op_st, Ciphertext &ct)
{
Plaintext pt;
op_st.decr->decrypt(ct, pt);
int64_t res = op_st.icode->decode_int64(pt);
// cout << "[INFO] Decrypted result: " << res << endl;
return res;
}
vector<int64_t> decrypt_ciphermatrix(struct decryptor_t &op_st, Ciphertext &ct)
{
Plaintext pt;
op_st.decr->decrypt(ct, pt);
vector<int64_t> pod_matrix;
BatchEncoder batch_encoder(op_st.context);
batch_encoder.decode(pt, pod_matrix);
// print_matrix(pod_matrix, 3);
return pod_matrix;
}
int save_plaintext(Plaintext &pt, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
pt.save(out_file);
else
ret = 0;
return ret;
}
int save_ciphertext(Ciphertext &ct, const string &filename)
{
int ret = 1;
ofstream out_file;
if (open_binary_file(out_file, filename))
ct.save(out_file);
else
ret = 0;
return ret;
}
int load_plaintext(shared_ptr<SEALContext> &context, Plaintext &pt, const string &filename)
{
int ret = 1;
ifstream in_file;
if (open_binary_file(in_file, filename))
pt.load(context, in_file);
else
ret = 0;
return ret;
}
int load_plaintext(struct evaluator_t &op_st, Plaintext &pt, const string &filename)
{
return load_plaintext(op_st.context, pt, filename);
}
int load_plaintext(struct decryptor_t &op_st, Plaintext &pt, const string &filename)
{
return load_plaintext(op_st.context, pt, filename);
}
int load_ciphertext(shared_ptr<SEALContext> &context, Ciphertext &ct, const string &filename)
{
int ret = 1;
ifstream in_file;
if (open_binary_file(in_file, filename))
ct.load(context, in_file);
else
ret = 0;
return ret;
}
int load_ciphertext(struct evaluator_t &op_st, Ciphertext &ct, const string &filename)
{
return load_ciphertext(op_st.context, ct, filename);
}
int load_ciphertext(struct decryptor_t &op_st, Ciphertext &ct, const string &filename)
{
return load_ciphertext(op_st.context, ct, filename);
}
/* ckks plaintext management */
void init_plaintext_ckks(CKKSEncoder &encoder, double scale, vector<double> &p_vector, Plaintext &pt, bool print_info)
{
try
{
encoder.encode(p_vector, scale, pt);
}
catch (std::exception &e)
{
clog << "[seal-error] catched exception: " << e.what() << endl;
clog << "[seal-error] ckks slots encoding error" << endl;
}
// if (print_info)
// {
// clog << "[seal-ckks] plaintexts: " << endl;
// print_vectors(p_vector);
// }
}
void init_plaintext_ckks(cencryptor_t &op_st, double scale, vector<double> &p_vector, Plaintext &pt, bool print_info)
{
init_plaintext_ckks(*(op_st.ccode), scale, p_vector, pt, print_info);
}
void init_plaintext_ckks(cencryptor_t &op_st, vector<double> &p_vector, Plaintext &pt, bool print_info)
{
init_plaintext_ckks(*(op_st.ccode), op_st.scale, p_vector, pt, print_info);
}
void init_plaintext_ckks(cdecryptor_t &op_st, double scale, vector<double> &p_vector, Plaintext &pt, bool print_info)
{
init_plaintext_ckks(*(op_st.ccode), scale, p_vector, pt, print_info);
}
void init_plaintext_ckks(cdecryptor_t &op_st, vector<double> &p_vector, Plaintext &pt, bool print_info)
{
init_plaintext_ckks(*(op_st.ccode), op_st.scale, p_vector, pt, print_info);
}
\ No newline at end of file
#ifndef _SEAL_API_H_
#define _SEAL_API_H_
/* includes */
#include <sys/time.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cassert>
#include <cstdint>
#include <boost/lexical_cast.hpp>
#include "seal/seal.h"
#include "csv_api.h"
/** BFV definitions **/
struct encryptor_t {
std::shared_ptr<seal::SEALContext> context;
seal::Encryptor *encr;
seal::BatchEncoder *bcode;
seal::IntegerEncoder *icode;
};
struct decryptor_t {
std::shared_ptr<seal::SEALContext> context;
seal::Decryptor *decr;
seal::BatchEncoder *bcode;
seal::IntegerEncoder *icode;
};
struct evaluator_t {
std::shared_ptr<seal::SEALContext> context;
seal::RelinKeys lk;
seal::GaloisKeys gk;
seal::Evaluator *eval;
};
/** CKKS definitions **/
struct cencryptor_t {
// seal::SEALContext context{0};
std::shared_ptr<seal::SEALContext> context;
seal::Encryptor* encr{nullptr};
seal::CKKSEncoder* ccode{nullptr};
double scale{0};
};
struct cdecryptor_t {
// seal::SEALContext context{0};
std::shared_ptr<seal::SEALContext> context;
seal::Decryptor* decr{nullptr};
seal::CKKSEncoder* ccode{nullptr};
double scale{0};
};
struct cevaluator_t {
// seal::SEALContext context{0};
std::shared_ptr<seal::SEALContext> context;
seal::RelinKeys lk{};
seal::GaloisKeys gk{};
seal::Evaluator* eval{nullptr};
// seal::CKKSEncoder* ccode{nullptr};
double scale{0};
};
/** prototypes **/
/** for binary files management **/
int open_binary_file(std::ifstream& in_file, const std::string& filename);
int open_binary_file(std::ofstream& out_file, const std::string& filename);
/** for context management **/
int save_params(seal::EncryptionParameters &params, const std::string &filename);
int load_params(seal::EncryptionParameters &params, const std::string &filename);
void init_context(size_t poly_d, size_t p_modulus, std::shared_ptr<seal::SEALContext>& context);
void load_context(std::shared_ptr<seal::SEALContext>& context, const std::string& filename);
void print_context(std::shared_ptr<seal::SEALContext>& context);
/** for key management **/
int save_key(seal::PublicKey& k, const std::string& filename);
int save_key(seal::SecretKey& k, const std::string& filename);
int save_key(seal::RelinKeys& k, const std::string& filename);
int save_key(seal::GaloisKeys& k, const std::string& filename);
int load_key(std::shared_ptr<seal::SEALContext>& context, const std::string& filename, seal::PublicKey& k);
int load_key(std::shared_ptr<seal::SEALContext>& context, const std::string& filename, seal::SecretKey& k);
int load_key(std::shared_ptr<seal::SEALContext>& context, const std::string& filename, seal::RelinKeys& k);
void generate_keys(size_t poly_d, size_t p_modulus, bool seriablizable = false);
void batching_generate_keys(size_t poly_d, int bit_size, const std::string key_path, bool serializable = false);
void batching_generate_keys(size_t poly_d, std::vector<int> bit_sizes, std::uint64_t plain_modulus, std::string key_dir, bool serializable = false);
/** for homomorphic operators management **/
void init_operator(struct encryptor_t& op_st);
void init_operator(struct encryptor_t &op_st, const std::string& public_key_path);
void init_operator_batching(struct encryptor_t &op_st, const std::string& key_dir);
void init_operator(struct decryptor_t& op_st);
void init_operator(struct decryptor_t &op_st, const std::string& secret_key_path);
void init_operator_batching(struct decryptor_t &op_st, const std::string& key_dir);
void init_operator(struct evaluator_t& op_st);
void init_operator(struct evaluator_t& op_st, const std::string& relink_key_path);
void init_operator_batching(struct evaluator_t &op_st, const std::string &key_dir);
void delete_operator(struct encryptor_t& op_st);
void delete_operator_batching(struct encryptor_t& op_st);
void delete_operator(struct decryptor_t& op_st);
void delete_operator_batching(struct decryptor_t& op_st);
void delete_operator(struct evaluator_t& op_st);
void delete_operator_batching(struct evaluator_t& op_st);
/** for plaintexts and ciphertexts management **/
void init_plaintext(struct encryptor_t& op_st, int64_t plain, seal::Plaintext& pt);
void init_ciphertext(struct encryptor_t& op_st, int64_t plain, seal::Ciphertext& ct);
void init_ciphermatrix(struct encryptor_t &op_st, std::vector<int64_t> &plain_matrix, seal::Ciphertext &encrypted_matrix);
void decrypt_ciphertext(struct decryptor_t& op_st, seal::Ciphertext& ct);
std::int64_t decrypt_ciphertext_and_return_value(struct decryptor_t& op_st, seal::Ciphertext& ct);
std::vector<int64_t> decrypt_ciphermatrix(struct decryptor_t &op_st, seal::Ciphertext &ct);
/*** ckks plaintexts ***/
void init_plaintext_ckks(seal::CKKSEncoder& encoder, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
void init_plaintext_ckks(struct cencryptor_t& op_st, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
void init_plaintext_ckks(struct cencryptor_t& op_st, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
void init_plaintext_ckks(struct cdecryptor_t& op_st, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
void init_plaintext_ckks(struct cdecryptor_t& op_st, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
int save_plaintext(seal::Plaintext& pt, const std::string& filename);
int save_ciphertext(seal::Ciphertext& ct, const std::string& filename);
int load_plaintext(std::shared_ptr<seal::SEALContext>& context, seal::Plaintext& pt, const std::string& filename);
int load_plaintext(struct evaluator_t& op_st, seal::Plaintext& pt, const std::string& filename);
int load_plaintext(struct decryptor_t& op_st, seal::Plaintext& pt, const std::string& filename);
int load_ciphertext(std::shared_ptr<seal::SEALContext>& context, seal::Ciphertext& ct, const std::string& filename);
int load_ciphertext(struct evaluator_t& op_st, seal::Ciphertext& ct, const std::string& filename);
int load_ciphertext(struct decryptor_t& op_st, seal::Ciphertext& ct, const std::string& filename);
int load_ciphertext_ckks(struct cevaluator_t& op_st, seal::Ciphertext& ct, const std::string& filename);
int load_ciphertext_ckks(struct cdecryptor_t& op_st, seal::Ciphertext& ct, const std::string& filename);
/** prototypes **/
/*** ckks opertors ***/
// int init_operator(struct cencryptor_t& op_st);
// int init_operator_ckks(struct cencryptor_t& op_st);
void init_operator_ckks(struct cencryptor_t &op_st, const std::string& key_dir);
void init_operator_ckks(struct cdecryptor_t &op_st, const std::string& key_dir);
void init_operator_ckks(struct cevaluator_t& op_st, const std::string& key_dir);
// int init_operator(struct cdecryptor_t& op_st);
// int init_operator(struct cevaluator_t& op_st);
void delete_operator_ckks(struct cencryptor_t& op_st);
void delete_operator_ckks(struct cdecryptor_t& op_st);
void delete_operator_ckks(struct cevaluator_t& op_st);
// void delete_operator(struct cevaluator_t& op_st);
/*** ckks plaintexts ***/
// void init_plaintext(seal::CKKSEncoder& encoder, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
// void init_plaintext(struct cencryptor_t& op_st, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
// void init_plaintext(struct cencryptor_t& op_st, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
// void init_plaintext(struct cdecryptor_t& op_st, double scale, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
// void init_plaintext(struct cdecryptor_t& op_st, std::vector<double>& p_vector, seal::Plaintext& pt, bool print_info = 1);
/*** ckks ciphertexts ***/
void init_ciphertext_ckks(seal::CKKSEncoder& encoder, seal::Encryptor& encryptor, double scale, std::vector<double>& c_vector, seal::Ciphertext& ct);
void init_ciphertext_ckks(struct cencryptor_t& op_st, double scale, std::vector<double>& c_vector, seal::Ciphertext& ct);
void init_ciphertext_ckks(struct cencryptor_t& op_st, std::vector<double>& c_vector, seal::Ciphertext& ct);
/*** ckks decryption ***/
void decrypt_ciphertext_ckks(seal::CKKSEncoder& encoder, seal::Decryptor& decryptor, seal::Ciphertext& ct, std::vector<double>& pt_val);
void decrypt_ciphertext_ckks(struct cdecryptor_t& op_st, seal::Ciphertext& ct, std::vector<double>& pt_val);
// std::vector<double> decrypt_ciphermatrix_ckks(struct cdecryptor_t &op_st, seal::Ciphertext &ct);
/*** saving plaintexts and ciphertexts ***/
int load_plaintext_ckks(struct cevaluator_t& op_st, seal::Plaintext& pt, const std::string& filename);
int load_plaintext_ckks(struct cencryptor_t& op_st, seal::Plaintext& pt, const std::string& filename);
int load_plaintext_ckks(struct cdecryptor_t& op_st, seal::Plaintext& pt, const std::string& filename);
int load_ciphertext_ckks(struct cevaluator_t& op_st, seal::Ciphertext& ct, const std::string& filename);
int load_ciphertext_ckks(struct cencryptor_t& op_st, seal::Ciphertext& ct, const std::string& filename);
int load_ciphertext_ckks(struct cdecryptor_t& op_st, seal::Ciphertext& ct, const std::string& filename);
void generate_keys_ckks(size_t poly_d, int bit_size, const std::string key_path, bool serializable = false);
/*** sum ***/
// void sum_batch_ciphertext(struct cevaluator_t& op_st, std::vector<seal::Ciphertext>& ct, seal::Ciphertext& ct_out);
/*** testing ***/
// void test_ckks_batch_functions(const uint32_t poly_deg);
#endif
mkdir -p keys/
mkdir -p lcheck/
mkdir -p parent/
mkdir -p result/
# rm ls.txt
# bash data_creating.sh 0 99 9 1000
# Number of driving licenses, eg: (0..99)
min=0
max=101
# max=1631
# max=101
# sim: (eval: 0.465s, 0.488s, 0.480s decrypt: 0.028s, 0.080s, 0.077s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=203
# (eval: 3.635s, 3.510s, 3.628s decrypt: 0.062s, 0.153s. 0.134s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=407
# (eval: 38.607s, 39.600s, 39.565s decrypt: 0.264s, 0.260s, 0.245s)
# seq: (eval: 0.xxx, 0.xxx, 0.xxx decrypt: 0.xxx, 0.xxx, 0.xxx)
# max=815
# (eval: x decrypt: x )
# Number of chars/words of a driving license, eg: 9
n_char=39
# Encrypted number range, eg: 10, 100, 1000 for 1 2 3 4 digits
range=255
# (8-bit)
# sampling size
sample=40
CURR_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# Note
# gen keys
# ./ANN_genkey_v1 keys/
${CURR_DIR}/ANN_genkey_v1 keys/
line=$((RANDOM %max))
lcheck=""
# gen data
for i in $(seq $min $max)
do
mkdir -p parent/l"$i"/
echo "Created folder $i"
value=""
for j in $(seq 0 $n_char)
do
value+="$((RANDOM %$range)) "
done
echo $value
echo $value >> ls.txt
# ./ANN_encrypt_v1 "$value" "l" parent/l"$i"/ $sample keys/
${CURR_DIR}/ANN_encrypt_v1 "$value" "l" parent/l"$i"/ $sample keys/
if [[ "$line" -eq "$i" ]]
then
lcheck="$value"
fi
done
# get random lc
# lcheck=$(sed -n "$((RANDOM %9){p;q}" ls.txt)
# line=$((RANDOM %max))
# lcheck="$(sed -n "${line}{p;q}" ls.txt)"
echo "Pick up license to check: $lcheck"
# ./ANN_encrypt_v1 "$lcheck" "l" lcheck/ $sample keys/
${CURR_DIR}/ANN_encrypt_v1 "$lcheck" "l" lcheck/ $sample keys/
# ./ANN_encrypt "47 25 76 23 30 21 1 47 88 3" "a" keys/bfv.pk lcheck/
# eval
value1="lcheck/l.ct "
value2=""
for i in $(seq 0 $max)
do
value2+="parent/l${i}/l.ct "
done
# value2="${value1} ${value2}"
value3="$value1$value2"
echo "$value3"
# time ./ANN_evaluate_v1 $value3 "l" result/ $sample keys/
# ./ANN_evaluate_v1 $value3 "l" result/ $sample keys/
time ${CURR_DIR}/ANN_evaluate_v1 $value3 "l" result/ $sample keys/
echo ""
# time ./ANN_decrypt_result_v1 result/l.ct $sample keys/
# ./ANN_decrypt_result_v1 result/l.ct $sample keys/
${CURR_DIR}/ANN_decrypt_result_v1 result/l.ct $sample keys/
# rm -r keys/*
# rm -r lcheck/*
# rm -r parent/*
# rm -r result/*
# rm ls.txt
#include "util.h"
// #include <filesystem>
#include "seal_api.h"
// #include <boost/filesystem.hpp>
// namespace fs = boost::filesystem;
bool sub_str_exist(const std::string &str, const std::string &sub_str)
{
return str.size() >= sub_str.size() && str.compare(str.size() - sub_str.size(), sub_str.size(), sub_str) == 0;
}
// int findNumberOfFilesInDirectory(const std::string &path)
// {
// auto dirIter = std::filesystem::directory_iterator(path);
// int fileCount = std::count_if(
// begin(dirIter),
// end(dirIter),
// [](auto &entry) { return entry.is_regular_file(); });
// return fileCount;
// }
// int findNumberOfFilesInDirectory(std::string &path, std::string &ext)
// int findNumberOfFilesInDirectory(const std::string &path)
// {
// // namespace fs = boost::filesystem;
// std::string ext = ".ct";
// fs::path Path(path);
// int Nb_ext = 0;
// fs::directory_iterator end_iter; // Default constructor for an iterator is the end iterator
// for (fs::directory_iterator iter(Path); iter != end_iter; ++iter)
// if (iter->path().extension() == ext)
// ++Nb_ext;
// return Nb_ext;
// }
// c++ 17
// std::vector<std::string> get_directories(const std::string &s)
// {
// std::vector<std::string> r;
// for(auto& p : std::filesystem::recursive_directory_iterator(s))
// if (p.is_directory())
// r.push_back(p.path().string());
// return r;
// }
// struct path_leaf_string
// {
// std::string operator()(const boost::filesystem::directory_entry &entry) const
// {
// return entry.path().leaf().string();
// }
// };
// std::vector<std::string> get_directories(const std::string &s)
// {
// std::vector<std::string> v;
// boost::filesystem::path p(s);
// boost::filesystem::directory_iterator start(p);
// boost::filesystem::directory_iterator end;
// std::transform(start, end, std::back_inserter(v), path_leaf_string());
// std::copy(v.begin(), v.end(),
// std::ostream_iterator<std::string>(std::cout, "\n"));
// return v;
// }
#ifndef _UTIL_H_
#define _UTIL_H_
#include <string>
#include <vector>
// int findNumberOfFilesInDirectory(const std::string &path);
bool sub_str_exist(const std::string &str, const std::string &sub_str);
// std::vector<std::string> get_directories(const std::string &s);
// std::vector<std::string> get_directories_deep(const std::string &s);
#endif
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
target_sources(sealexamples
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/examples.cpp
${CMAKE_CURRENT_LIST_DIR}/1_bfv_basics.cpp
${CMAKE_CURRENT_LIST_DIR}/2_encoders.cpp
${CMAKE_CURRENT_LIST_DIR}/3_levels.cpp
${CMAKE_CURRENT_LIST_DIR}/4_ckks_basics.cpp
${CMAKE_CURRENT_LIST_DIR}/5_rotation.cpp
${CMAKE_CURRENT_LIST_DIR}/6_serialization.cpp
${CMAKE_CURRENT_LIST_DIR}/7_performance.cpp
)
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{2B57D847-26DC-45FF-B9AF-EE33910B5093}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>SEALExamples</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(ProjectDir)..\..\bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
<TargetName>sealexamples</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(ProjectDir)..\..\bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
<TargetName>sealexamples</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(ProjectDir)..\..\bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
<TargetName>sealexamples</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(ProjectDir)..\..\bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(ProjectDir)obj\$(Platform)\$(Configuration)\</IntDir>
<TargetName>sealexamples</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_ENABLE_EXTENDED_ALIGNED_STORAGE</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir)native\src</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/Zc:__cplusplus %(AdditionalOptions)</AdditionalOptions>
<ControlFlowGuard>Guard</ControlFlowGuard>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\lib\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>seal.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_ENABLE_EXTENDED_ALIGNED_STORAGE</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir)native\src</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/Zc:__cplusplus %(AdditionalOptions)</AdditionalOptions>
<ControlFlowGuard>Guard</ControlFlowGuard>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\lib\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>seal.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_ENABLE_EXTENDED_ALIGNED_STORAGE</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir)native\src</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/Zc:__cplusplus %(AdditionalOptions)</AdditionalOptions>
<ControlFlowGuard>Guard</ControlFlowGuard>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\lib\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>seal.lib;%(AdditionalDependencies)</AdditionalDependencies>
<Profile>true</Profile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>_ENABLE_EXTENDED_ALIGNED_STORAGE</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir)native\src</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<AdditionalOptions>/Zc:__cplusplus %(AdditionalOptions)</AdditionalOptions>
<ControlFlowGuard>Guard</ControlFlowGuard>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(ProjectDir)..\..\lib\$(Platform)\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>seal.lib;%(AdditionalDependencies)</AdditionalDependencies>
<Profile>true</Profile>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="1_bfv_basics.cpp" />
<ClCompile Include="2_encoders.cpp" />
<ClCompile Include="4_ckks_basics.cpp" />
<ClCompile Include="5_rotation.cpp" />
<ClCompile Include="3_levels.cpp" />
<ClCompile Include="6_serialization.cpp" />
<ClCompile Include="7_performance.cpp" />
<ClCompile Include="examples.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="examples.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Other">
<UniqueIdentifier>{abd2e216-316f-4dad-a2a4-a72ffccfd92b}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="examples.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="3_levels.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="5_rotation.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="1_bfv_basics.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="2_encoders.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="4_ckks_basics.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="7_performance.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="6_serialization.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt">
<Filter>Other</Filter>
</Text>
</ItemGroup>
<ItemGroup>
<ClInclude Include="examples.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "examples.h"
using namespace std;
using namespace seal;
int main()
{
cout << "Microsoft SEAL version: " << SEAL_VERSION << endl;
while (true)
{
cout << "+---------------------------------------------------------+" << endl;
cout << "| The following examples should be executed while reading |" << endl;
cout << "| comments in associated files in native/examples/. |" << endl;
cout << "+---------------------------------------------------------+" << endl;
cout << "| Examples | Source Files |" << endl;
cout << "+----------------------------+----------------------------+" << endl;
cout << "| 1. BFV Basics | 1_bfv_basics.cpp |" << endl;
cout << "| 2. Encoders | 2_encoders.cpp |" << endl;
cout << "| 3. Levels | 3_levels.cpp |" << endl;
cout << "| 4. CKKS Basics | 4_ckks_basics.cpp |" << endl;
cout << "| 5. Rotation | 5_rotation.cpp |" << endl;
cout << "| 6. Serialization | 6_serialization.cpp |" << endl;
cout << "| 7. Performance Test | 7_performance.cpp |" << endl;
cout << "+----------------------------+----------------------------+" << endl;
/*
Print how much memory we have allocated from the current memory pool.
By default the memory pool will be a static global pool and the
MemoryManager class can be used to change it. Most users should have
little or no reason to touch the memory allocation system.
*/
size_t megabytes = MemoryManager::GetPool().alloc_byte_count() >> 20;
cout << "[" << setw(7) << right << megabytes << " MB] "
<< "Total allocation from the memory pool" << endl;
int selection = 0;
bool invalid = true;
do
{
cout << endl << "> Run example (1 ~ 7) or exit (0): ";
if (!(cin >> selection))
{
invalid = false;
}
else if (selection < 0 || selection > 7)
{
invalid = false;
}
else
{
invalid = true;
}
if (!invalid)
{
cout << " [Beep~~] Invalid option: type 0 ~ 7" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (!invalid);
switch (selection)
{
case 1:
example_bfv_basics();
break;
case 2:
example_encoders();
break;
case 3:
example_levels();
break;
case 4:
example_ckks_basics();
break;
case 5:
example_rotation();
break;
case 6:
example_serialization();
break;
case 7:
example_performance_test();
break;
case 0:
return 0;
}
}
return 0;
}
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/seal.h"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <memory>
#include <mutex>
#include <numeric>
#include <random>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
void example_bfv_basics();
void example_encoders();
void example_levels();
void example_ckks_basics();
void example_rotation();
void example_serialization();
void example_performance_test();
/*
Helper function: Prints the name of the example in a fancy banner.
*/
inline void print_example_banner(std::string title)
{
if (!title.empty())
{
std::size_t title_length = title.length();
std::size_t banner_length = title_length + 2 * 10;
std::string banner_top = "+" + std::string(banner_length - 2, '-') + "+";
std::string banner_middle = "|" + std::string(9, ' ') + title + std::string(9, ' ') + "|";
std::cout << std::endl << banner_top << std::endl << banner_middle << std::endl << banner_top << std::endl;
}
}
/*
Helper function: Prints the parameters in a SEALContext.
*/
inline void print_parameters(std::shared_ptr<seal::SEALContext> context)
{
// Verify parameters
if (!context)
{
throw std::invalid_argument("context is not set");
}
auto &context_data = *context->key_context_data();
/*
Which scheme are we using?
*/
std::string scheme_name;
switch (context_data.parms().scheme())
{
case seal::scheme_type::BFV:
scheme_name = "BFV";
break;
case seal::scheme_type::CKKS:
scheme_name = "CKKS";
break;
default:
throw std::invalid_argument("unsupported scheme");
}
std::cout << "/" << std::endl;
std::cout << "| Encryption parameters :" << std::endl;
std::cout << "| scheme: " << scheme_name << std::endl;
std::cout << "| poly_modulus_degree: " << context_data.parms().poly_modulus_degree() << std::endl;
/*
Print the size of the true (product) coefficient modulus.
*/
std::cout << "| coeff_modulus size: ";
std::cout << context_data.total_coeff_modulus_bit_count() << " (";
auto coeff_modulus = context_data.parms().coeff_modulus();
std::size_t coeff_modulus_size = coeff_modulus.size();
for (std::size_t i = 0; i < coeff_modulus_size - 1; i++)
{
std::cout << coeff_modulus[i].bit_count() << " + ";
}
std::cout << coeff_modulus.back().bit_count();
std::cout << ") bits" << std::endl;
/*
For the BFV scheme print the plain_modulus parameter.
*/
if (context_data.parms().scheme() == seal::scheme_type::BFV)
{
std::cout << "| plain_modulus: " << context_data.parms().plain_modulus().value() << std::endl;
}
std::cout << "\\" << std::endl;
}
/*
Helper function: Prints the `parms_id' to std::ostream.
*/
inline std::ostream &operator<<(std::ostream &stream, seal::parms_id_type parms_id)
{
/*
Save the formatting information for std::cout.
*/
std::ios old_fmt(nullptr);
old_fmt.copyfmt(std::cout);
stream << std::hex << std::setfill('0') << std::setw(16) << parms_id[0] << " " << std::setw(16) << parms_id[1]
<< " " << std::setw(16) << parms_id[2] << " " << std::setw(16) << parms_id[3] << " ";
/*
Restore the old std::cout formatting.
*/
std::cout.copyfmt(old_fmt);
return stream;
}
/*
Helper function: Prints a vector of floating-point values.
*/
template <typename T>
inline void print_vector(std::vector<T> vec, std::size_t print_size = 4, int prec = 3)
{
/*
Save the formatting information for std::cout.
*/
std::ios old_fmt(nullptr);
old_fmt.copyfmt(std::cout);
std::size_t slot_count = vec.size();
std::cout << std::fixed << std::setprecision(prec);
std::cout << std::endl;
if (slot_count <= 2 * print_size)
{
std::cout << " [";
for (std::size_t i = 0; i < slot_count; i++)
{
std::cout << " " << vec[i] << ((i != slot_count - 1) ? "," : " ]\n");
}
}
else
{
vec.resize(std::max(vec.size(), 2 * print_size));
std::cout << " [";
for (std::size_t i = 0; i < print_size; i++)
{
std::cout << " " << vec[i] << ",";
}
if (vec.size() > 2 * print_size)
{
std::cout << " ...,";
}
for (std::size_t i = slot_count - print_size; i < slot_count; i++)
{
std::cout << " " << vec[i] << ((i != slot_count - 1) ? "," : " ]\n");
}
}
std::cout << std::endl;
/*
Restore the old std::cout formatting.
*/
std::cout.copyfmt(old_fmt);
}
/*
Helper function: Prints a matrix of values.
*/
template <typename T>
inline void print_matrix(std::vector<T> matrix, std::size_t row_size)
{
/*
We're not going to print every column of the matrix (there are 2048). Instead
print this many slots from beginning and end of the matrix.
*/
std::size_t print_size = 5;
std::cout << std::endl;
std::cout << " [";
for (std::size_t i = 0; i < print_size; i++)
{
std::cout << std::setw(3) << std::right << matrix[i] << ",";
}
std::cout << std::setw(3) << " ...,";
for (std::size_t i = row_size - print_size; i < row_size; i++)
{
std::cout << std::setw(3) << matrix[i] << ((i != row_size - 1) ? "," : " ]\n");
}
std::cout << " [";
for (std::size_t i = row_size; i < row_size + print_size; i++)
{
std::cout << std::setw(3) << matrix[i] << ",";
}
std::cout << std::setw(3) << " ...,";
for (std::size_t i = 2 * row_size - print_size; i < 2 * row_size; i++)
{
std::cout << std::setw(3) << matrix[i] << ((i != 2 * row_size - 1) ? "," : " ]\n");
}
std::cout << std::endl;
}
/*
Helper function: Print line number.
*/
inline void print_line(int line_number)
{
std::cout << "Line " << std::setw(3) << line_number << " --> ";
}
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
cmake_minimum_required(VERSION 3.10)
set(TEST_NAME generic)
set(GENKEY_BIN ${TEST_NAME}_genkey)
set(ENCR_BIN ${TEST_NAME}_encrypt)
set(DECR_BIN ${TEST_NAME}_decrypt)
set(EVAL_BIN ${TEST_NAME}_evaluate)
set(EVAL_BIN_MUL ${TEST_NAME}_evaluate_multiply)
set(EVAL_BIN_SUB ${TEST_NAME}_evaluate_sub)
set(SCR_ENC encrypt_gen.sh)
set(SCR_DEC decrypt_gen.sh)
set(SCR_GEN genkey_gen.sh)
set(GENKEY_SRCS
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_genkey.cpp
)
set(ENCR_SRCS
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_encrypt.cpp
)
set(DECR_SRCS
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_decrypt.cpp
)
set(EVAL_SRCS
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/add.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_evaluate.cpp
)
set(EVAL_SRCS_SUB
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/sub.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_evaluate_sub.cpp
)
set(EVAL_SRCS_MUL
${CMAKE_CURRENT_LIST_DIR}/seal_api.cpp
${CMAKE_CURRENT_LIST_DIR}/multiply.cpp
${CMAKE_CURRENT_LIST_DIR}/${TEST_NAME}_evaluate_multiply.cpp
)
set(HEADER_DIR ${CMAKE_SOURCE_DIR}/${TEST_NAME})
set(HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/seal_api.h
${CMAKE_CURRENT_LIST_DIR}/add.h
)
add_executable(${GENKEY_BIN} ${GENKEY_SRCS} ${HEADER_FILES})
add_executable(${ENCR_BIN} ${ENCR_SRCS} ${HEADER_FILES})
add_executable(${DECR_BIN} ${DECR_SRCS} ${HEADER_FILES})
add_executable(${EVAL_BIN} ${EVAL_SRCS} ${HEADER_FILES})
add_executable(${EVAL_BIN_SUB} ${EVAL_SRCS_SUB} ${HEADER_FILES})
add_executable(${EVAL_BIN_MUL} ${EVAL_SRCS_MUL} ${HEADER_FILES})
#target_include_directories(${TEST_NAME} PRIVATE ${HEADER_DIR})
# Import Microsoft SEAL
find_package(SEAL 3.5 REQUIRED)
# Link Microsoft SEAL
target_link_libraries(${GENKEY_BIN} SEAL::seal)
target_link_libraries(${ENCR_BIN} SEAL::seal)
target_link_libraries(${DECR_BIN} SEAL::seal)
target_link_libraries(${EVAL_BIN} SEAL::seal)
target_link_libraries(${EVAL_BIN_SUB} SEAL::seal)
target_link_libraries(${EVAL_BIN_MUL} SEAL::seal)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set_target_properties(${GENKEY_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${ENCR_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${DECR_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${EVAL_BIN}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${EVAL_BIN_SUB}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
set_target_properties(${EVAL_BIN_MUL}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME}"
)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_ENC}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_DEC}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${SCR_GEN}
DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TEST_NAME})
#include "add.h"
using namespace std;
using namespace seal;
void add_ciphertext(struct evaluator_t &op_st, Ciphertext &ct, Plaintext &pt, Ciphertext &ct_out)
{
timeval t0, t1;
unsigned long dt = 0;
gettimeofday(&t0, NULL);
op_st.eval->add_plain(ct, pt, ct_out);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] Homomorphic addition (pt+ct) time in (us): " << dt << endl;
}
void add_ciphertext(struct evaluator_t &op_st, Ciphertext &ct1, Ciphertext &ct2, Ciphertext &ct_out)
{
timeval t0, t1;
unsigned long dt = 0;
gettimeofday(&t0, NULL);
op_st.eval->add(ct1, ct2, ct_out);
gettimeofday(&t1, NULL);
dt = 1000000 * (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec);
cout << "[INFO] Homomorphic addition (ct+ct) time in (us): " << dt << endl;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment