|
| 1 | +/* Copyright (C) 2019 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 | +#include <benchmark/benchmark.h> |
| 14 | +#include <iostream> |
| 15 | + |
| 16 | +#include <helib/helib.h> |
| 17 | +#include <helib/debugging.h> |
| 18 | + |
| 19 | +void squareWithFatBoot(FHEPubKey& pk, Ctxt& c) { |
| 20 | + if (c.bitCapacity() <= 50) { |
| 21 | + pk.reCrypt(c); |
| 22 | + } |
| 23 | + c.square(); |
| 24 | +} |
| 25 | + |
| 26 | +static void BM_fatboot(benchmark::State& state, |
| 27 | + long m, |
| 28 | + long p, |
| 29 | + long r, |
| 30 | + long c, |
| 31 | + long bits, |
| 32 | + long t, |
| 33 | + int c_m, |
| 34 | + std::vector<long> mvector, |
| 35 | + std::vector<long> gens, |
| 36 | + std::vector<long> ords) |
| 37 | +{ |
| 38 | + NTL::Vec<long> mvec = convert<NTL::Vec<long>>(mvector); |
| 39 | + std::cout << "m=" << m |
| 40 | + << ", p=" << p |
| 41 | + << ", r=" << r |
| 42 | + << ", bits=" << bits |
| 43 | + << ", c=" << c |
| 44 | + << ", skHwt=" << t |
| 45 | + << ", c_m=" << c_m |
| 46 | + << ", mvec=" << mvec |
| 47 | + << ", gens=" << gens |
| 48 | + << ", ords=" << ords |
| 49 | + << std::endl; |
| 50 | + std::cout << "Initialising context object..." << std::endl; |
| 51 | + FHEcontext context(m, p, r, gens, ords); |
| 52 | + context.zMStar.set_cM(c_m/100.0); |
| 53 | + |
| 54 | + std::cout << "Building modulus chain..." << std::endl; |
| 55 | + buildModChain(context, bits, c, /*willBeBootstrappable=*/true, /*skHwt*/t); |
| 56 | + |
| 57 | + // Make bootstrappable |
| 58 | + context.makeBootstrappable(mvec, t, /*build_cache=*/0, /*alsoThick=*/true); |
| 59 | + |
| 60 | + // Print the context |
| 61 | + context.zMStar.printout(); |
| 62 | + std::cout << std::endl; |
| 63 | + std::cout << "Security: " << context.securityLevel() << std::endl; |
| 64 | + |
| 65 | + std::cout << "Creating secret key..." << std::endl; |
| 66 | + FHESecKey secret_key(context); |
| 67 | + secret_key.GenSecKey(); |
| 68 | + std::cout << "Generating key-switching matrices..." << std::endl; |
| 69 | + addSome1DMatrices(secret_key); |
| 70 | + addFrbMatrices(secret_key); |
| 71 | + |
| 72 | + // Generate bootstrapping data |
| 73 | + secret_key.genRecryptData(); |
| 74 | + |
| 75 | + // NOTE: For some reason the reCrypt method is not marked const so |
| 76 | + // I had to remove the const from the public key |
| 77 | + FHEPubKey& public_key = secret_key; |
| 78 | + const EncryptedArray& ea = *(context.ea); |
| 79 | + |
| 80 | + long nslots = ea.size(); |
| 81 | + std::cout << "Number of slots: " << nslots << std::endl; |
| 82 | + |
| 83 | + std::vector<long> ptxt(nslots); |
| 84 | + for (int i = 0; i < nslots; ++i) { |
| 85 | + ptxt[i] = std::rand() % 2; // Random 0s and 1s |
| 86 | + } |
| 87 | + |
| 88 | + Ctxt ctxt(public_key); |
| 89 | + ea.encrypt(ctxt, public_key, ptxt); |
| 90 | + for (auto _ : state) |
| 91 | + squareWithFatBoot(public_key, ctxt); |
| 92 | + std::cout << "Multiplications performed = " << state.iterations() <<std::endl; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +BENCHMARK_CAPTURE(BM_fatboot, tiny_params, |
| 97 | + /*m =*/ 31*41, |
| 98 | + /*p =*/ 2, |
| 99 | + /*r =*/ 1, |
| 100 | + /*c =*/ 2, |
| 101 | + /*bits =*/ 580, |
| 102 | + /*t =*/ 64, |
| 103 | + /*c_m =*/ 100, |
| 104 | + /*mvec =*/ std::vector<long> {31, 41}, |
| 105 | + /*gens =*/ std::vector<long> {1026, 249}, |
| 106 | + /*ords =*/ std::vector<long> {30, -2} |
| 107 | + )->Unit(benchmark::kMillisecond)->Iterations(200); |
| 108 | + |
| 109 | +BENCHMARK_CAPTURE(BM_fatboot, small_params, |
| 110 | + /*m =*/ 31775, |
| 111 | + /*p =*/ 2, |
| 112 | + /*r =*/ 1, |
| 113 | + /*c =*/ 2, |
| 114 | + /*bits =*/ 580, |
| 115 | + /*t =*/ 64, |
| 116 | + /*c_m =*/ 100, |
| 117 | + /*mvec =*/ std::vector<long> {41, 775}, |
| 118 | + /*gens =*/ std::vector<long> {6976, 24806}, |
| 119 | + /*ords =*/ std::vector<long> {40, 30} |
| 120 | + )->Unit(benchmark::kMillisecond)->MinTime(200); |
| 121 | + |
| 122 | +BENCHMARK_CAPTURE(BM_fatboot, big_params, |
| 123 | + /*m =*/ 35113, |
| 124 | + /*p =*/ 2, |
| 125 | + /*r =*/ 1, |
| 126 | + /*c =*/ 2, |
| 127 | + /*bits =*/ 580, |
| 128 | + /*t =*/ 64, |
| 129 | + /*c_m =*/ 100, |
| 130 | + /*mvec =*/ std::vector<long> {37, 949}, |
| 131 | + /*gens =*/ std::vector<long> {16134, 8548}, |
| 132 | + /*ords =*/ std::vector<long> {36, 24} |
| 133 | + )->Unit(benchmark::kMillisecond)->MinTime(200); |
0 commit comments