|
| 1 | +/* Copyright (C) 2020-2021 IBM Corp. |
| 2 | + * This program is Licensed under the Apache License, Version 2.0 |
| 3 | + * (the "License"); you may not use this file except in compliance |
| 4 | + * with the License. You may obtain a copy of the License at |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * Unless required by applicable law or agreed to in writing, software |
| 7 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + * See the License for the specific language governing permissions and |
| 10 | + * limitations under the License. See accompanying LICENSE file. |
| 11 | + */ |
| 12 | + |
| 13 | +// In the CKKS encryption scheme, ciphertexts can encrypt vectors of complex |
| 14 | +// numbers. |
| 15 | + |
| 16 | +#include <helib/helib.h> |
| 17 | + |
| 18 | +#include <helib/matmul.h> |
| 19 | +// This is only needed if you want to do matrix multiplication |
| 20 | + |
| 21 | +using namespace std; |
| 22 | +using namespace helib; |
| 23 | + |
| 24 | +int main(int argc, char* argv[]) |
| 25 | +{ |
| 26 | + Context context = |
| 27 | + ContextBuilder<CKKS>().m(32 * 1024).bits(358).precision(30).c(6).build(); |
| 28 | + |
| 29 | + cout << "securityLevel=" << context.securityLevel() << "\n"; |
| 30 | + |
| 31 | + long n = context.getNSlots(); |
| 32 | + |
| 33 | + SecKey secretKey(context); |
| 34 | + secretKey.GenSecKey(); |
| 35 | + |
| 36 | + addSome1DMatrices(secretKey); |
| 37 | + // This only needs to be done if you want to do matrix multiplication |
| 38 | + |
| 39 | + addSomeFrbMatrices(secretKey); |
| 40 | + // This only needs to be done if you want to do conjugation |
| 41 | + |
| 42 | + const PubKey& publicKey = secretKey; |
| 43 | + |
| 44 | + //=========================================================================== |
| 45 | + |
| 46 | + // Let's encrypt something! |
| 47 | + vector<std::complex<double>> v0(n); |
| 48 | + for (long i = 0; i < n; i++) |
| 49 | + v0[i] = std::complex<double>(cos(2.0 * PI * i / n), sin(2.0 * PI * i / n)); |
| 50 | + |
| 51 | + // A PtxtArray can be initialized with a vector of complex numbers |
| 52 | + PtxtArray p0(context, v0); |
| 53 | + |
| 54 | + // Encryption works the same as with real numbers |
| 55 | + Ctxt c0(publicKey); |
| 56 | + p0.encrypt(c0); |
| 57 | + |
| 58 | + //=========================================================================== |
| 59 | + |
| 60 | + // We next create another ciphertext that encrypts random complex numbers: |
| 61 | + |
| 62 | + PtxtArray p1(context); |
| 63 | + p1.randomComplex(); |
| 64 | + // this fills each entry of p1 with a random number in the complex |
| 65 | + // unit circle |
| 66 | + |
| 67 | + Ctxt c1(publicKey); |
| 68 | + p1.encrypt(c1); |
| 69 | + |
| 70 | + //=========================================================================== |
| 71 | + |
| 72 | + // We can perform homomorphic computations in the same way as we did before: |
| 73 | + |
| 74 | + Ctxt c2 = c0; |
| 75 | + c2 *= 2.5; |
| 76 | + c2 += c1; |
| 77 | + |
| 78 | + // Note that there is no direct support for combining a ciphertext with a |
| 79 | + // complex scalar. This can be achieved, however, by first converting the |
| 80 | + // complex scaler to a PtxtArray. For example: |
| 81 | + |
| 82 | + PtxtArray I(context, std::complex<double>(0.0, 1.0)); |
| 83 | + // I has the imaginary unit in each slot |
| 84 | + |
| 85 | + c2 *= I; |
| 86 | + |
| 87 | + cout << "c2.capacity=" << c2.capacity() << " "; |
| 88 | + cout << "c2.errorBound=" << c2.errorBound() << "\n"; |
| 89 | + |
| 90 | + // Data movement operations, like rotate and shift, work exactly as before. |
| 91 | + |
| 92 | + // There is also support for multiplying a ciphertext by a plaintext matrix |
| 93 | + // of complex numbers. In 04_ckks_matmul.cpp, we showed how you could |
| 94 | + // specify an n x n matrix of real numbers using the class MatMul_CKKS. One |
| 95 | + // can specify an n x n matrix of complex numbers as follows: |
| 96 | + |
| 97 | + MatMul_CKKS_Complex mat(context, [n](long i, long j) { |
| 98 | + return std::complex<double>(i, j) / double(n); |
| 99 | + }); |
| 100 | + |
| 101 | + c2 *= mat; |
| 102 | + |
| 103 | + cout << "c2.capacity=" << c2.capacity() << " "; |
| 104 | + cout << "c2.errorBound=" << c2.errorBound() << "\n"; |
| 105 | + |
| 106 | + //=========================================================================== |
| 107 | + |
| 108 | + // One can homomorphically compute the complex conjugate of each slot |
| 109 | + // of a ciphertext as follows: |
| 110 | + |
| 111 | + conjugate(c2); |
| 112 | + |
| 113 | + cout << "c2.capacity=" << c2.capacity() << " "; |
| 114 | + cout << "c2.errorBound=" << c2.errorBound() << "\n"; |
| 115 | + |
| 116 | + //=========================================================================== |
| 117 | + |
| 118 | + // Let's decrypt the results: |
| 119 | + |
| 120 | + PtxtArray pp2(context); |
| 121 | + pp2.decryptComplex(c2, secretKey); |
| 122 | + |
| 123 | + // Note that if one just writes pp2.decrypt(c2, secretKey) instead of |
| 124 | + // pp2.decryptComplex(c2, secretKey), the imaginary part will be discarded. |
| 125 | + |
| 126 | + // We can store pp2 in a standard vector, as usual: |
| 127 | + |
| 128 | + std::vector<std::complex<double>> v2; |
| 129 | + pp2.store(v2); |
| 130 | + |
| 131 | + //=========================================================================== |
| 132 | + |
| 133 | + // We can also perform the computation on plaintexts and compare: |
| 134 | + |
| 135 | + PtxtArray p2 = p0; |
| 136 | + p2 *= 2.5; |
| 137 | + p2 += p1; |
| 138 | + p2 *= I; |
| 139 | + p2 *= mat; |
| 140 | + conjugate(p2); |
| 141 | + |
| 142 | + double distance = Distance(p2, pp2); |
| 143 | + cout << "distance=" << distance << "\n"; |
| 144 | +} |
0 commit comments