|
| 1 | +//================================================================================== |
| 2 | +// BSD 2-Clause License |
| 3 | +// |
| 4 | +// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors |
| 5 | +// |
| 6 | +// All rights reserved. |
| 7 | +// |
| 8 | +// Author TPOC: contact@openfhe.org |
| 9 | +// |
| 10 | +// Redistribution and use in source and binary forms, with or without |
| 11 | +// modification, are permitted provided that the following conditions are met: |
| 12 | +// |
| 13 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 14 | +// list of conditions and the following disclaimer. |
| 15 | +// |
| 16 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 17 | +// this list of conditions and the following disclaimer in the documentation |
| 18 | +// and/or other materials provided with the distribution. |
| 19 | +// |
| 20 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 24 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +//================================================================================== |
| 31 | + |
| 32 | +#include "benchmark/benchmark.h" |
| 33 | +#include "config_core.h" |
| 34 | +#include "cryptocontext.h" |
| 35 | +#include "gen-cryptocontext.h" |
| 36 | +#include "gtest/gtest.h" |
| 37 | +#include "scheme/ckksrns/ckksrns-fhe.h" |
| 38 | +#include "scheme/ckksrns/ckksrns-utils.h" |
| 39 | +#include "scheme/ckksrns/gen-cryptocontext-ckksrns.h" |
| 40 | + |
| 41 | +#include <vector> |
| 42 | + |
| 43 | +using namespace lbcrypto; |
| 44 | + |
| 45 | +struct boot_config { |
| 46 | + uint32_t ringDim; |
| 47 | + uint32_t slots; |
| 48 | + uint32_t dcrtBits; |
| 49 | + uint32_t firstMod; |
| 50 | + uint32_t numDigits; |
| 51 | + uint32_t lvlsAfter; |
| 52 | + uint32_t iters; |
| 53 | + std::vector<uint32_t> lvlb; |
| 54 | + SecretKeyDist skdst; |
| 55 | + ScalingTechnique stech; |
| 56 | +}; |
| 57 | + |
| 58 | +// clang-format off |
| 59 | +[[maybe_unused]] std::vector<boot_config> boot_configs = { |
| 60 | + // ringDm, slots, dcrtBits, firstMod, numDigits, lvlsAfter, iters, lvlb, skdst, stech |
| 61 | + { 1 << 16, 1 << 15, 54, 60, 15, 9, 1, {3, 3}, UNIFORM_TERNARY, FLEXIBLEAUTO}, |
| 62 | + { 1 << 16, 1 << 15, 50, 57, 11, 9, 2, {3, 3}, UNIFORM_TERNARY, FLEXIBLEAUTO}, |
| 63 | + { 1 << 16, 1 << 15, 50, 57, 16, 10, 2, {3, 3}, UNIFORM_TERNARY, FLEXIBLEAUTO}, |
| 64 | + { 1 << 16, 1 << 15, 52, 57, 10, 8, 2, {3, 3}, UNIFORM_TERNARY, FIXEDMANUAL}, |
| 65 | + { 1 << 16, 1 << 15, 52, 57, 16, 9, 2, {3, 3}, UNIFORM_TERNARY, FIXEDMANUAL}, |
| 66 | + { 1 << 17, 1 << 16, 59, 60, 0, 5, 1, {4, 4}, SPARSE_TERNARY, FLEXIBLEAUTO}, |
| 67 | + { 1 << 17, 1 << 16, 59, 60, 0, 5, 1, {4, 4}, SPARSE_ENCAPSULATED, FLEXIBLEAUTO}, |
| 68 | + { 1 << 16, 1 << 5, 59, 60, 0, 5, 1, {1, 1}, SPARSE_TERNARY, FLEXIBLEAUTO}, |
| 69 | + { 1 << 16, 1 << 5, 59, 60, 0, 5, 1, {1, 1}, SPARSE_ENCAPSULATED, FLEXIBLEAUTO}, |
| 70 | + { 1 << 17, 1 << 5, 59, 60, 0, 5, 1, {1, 1}, SPARSE_TERNARY, FLEXIBLEAUTO}, |
| 71 | + { 1 << 17, 1 << 5, 59, 60, 0, 5, 1, {1, 1}, SPARSE_ENCAPSULATED, FLEXIBLEAUTO}, |
| 72 | + { 1 << 17, 1 << 16, 59, 60, 0, 10, 1, {4, 4}, SPARSE_ENCAPSULATED, FLEXIBLEAUTO}, |
| 73 | + { 1 << 17, 1 << 5, 59, 60, 0, 10, 1, {1, 1}, SPARSE_ENCAPSULATED, FLEXIBLEAUTO}, |
| 74 | + { 1 << 17, 1 << 16, 59, 60, 0, 10, 2, {4, 4}, SPARSE_ENCAPSULATED, FLEXIBLEAUTO}, |
| 75 | + { 1 << 17, 1 << 5, 59, 60, 0, 10, 2, {1, 1}, SPARSE_ENCAPSULATED, FLEXIBLEAUTO}, |
| 76 | + // TODO: enable following once STC Composite Scaling operational |
| 77 | + // { 1 << 17, 1 << 16, 78, 96, 0, 10, 2, {4, 4}, SPARSE_TERNARY, COMPOSITESCALINGAUTO}, |
| 78 | +}; |
| 79 | +// clang-format on |
| 80 | + |
| 81 | +[[maybe_unused]] static void BootConfigs(benchmark::internal::Benchmark* b) { |
| 82 | + for (uint32_t i = 0; i < boot_configs.size(); ++i) |
| 83 | + b->ArgName("Config")->Arg(i); |
| 84 | +} |
| 85 | + |
| 86 | +[[maybe_unused]] static void CKKSBoot(benchmark::State& state) { |
| 87 | + auto t = boot_configs[state.range(0)]; |
| 88 | + |
| 89 | + CCParams<CryptoContextCKKSRNS> parameters; |
| 90 | + parameters.SetSecurityLevel(HEStd_128_classic); |
| 91 | + parameters.SetRingDim(t.ringDim); |
| 92 | + parameters.SetScalingModSize(t.dcrtBits); |
| 93 | + parameters.SetFirstModSize(t.firstMod); |
| 94 | + parameters.SetNumLargeDigits(t.numDigits); |
| 95 | + parameters.SetSecretKeyDist(t.skdst); |
| 96 | + parameters.SetScalingTechnique(t.stech); |
| 97 | + parameters.SetKeySwitchTechnique(HYBRID); |
| 98 | + uint32_t depth = t.lvlsAfter + FHECKKSRNS::GetBootstrapDepth(t.lvlb, t.skdst) + (t.iters - 1); |
| 99 | + parameters.SetMultiplicativeDepth(depth); |
| 100 | + |
| 101 | + auto cc = GenCryptoContext(parameters); |
| 102 | + cc->Enable(PKE); |
| 103 | + cc->Enable(KEYSWITCH); |
| 104 | + cc->Enable(LEVELEDSHE); |
| 105 | + cc->Enable(ADVANCEDSHE); |
| 106 | + cc->Enable(FHE); |
| 107 | + |
| 108 | + cc->EvalBootstrapSetup(t.lvlb, {0, 0}, t.slots); |
| 109 | + |
| 110 | + auto keyPair = cc->KeyGen(); |
| 111 | + cc->EvalMultKeyGen(keyPair.secretKey); |
| 112 | + cc->EvalBootstrapKeyGen(keyPair.secretKey, t.slots); |
| 113 | + |
| 114 | + std::vector<double> x = {0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0}; |
| 115 | + |
| 116 | + auto ptxt = cc->MakeCKKSPackedPlaintext(x, 1, depth - 1, nullptr, t.slots); |
| 117 | + ptxt->SetLength(t.slots); |
| 118 | + |
| 119 | + auto ctxt = cc->Encrypt(keyPair.publicKey, ptxt); |
| 120 | + |
| 121 | + while (state.KeepRunning()) |
| 122 | + auto ctxtAfter = cc->EvalBootstrap(ctxt, t.iters); |
| 123 | + |
| 124 | + cc->ClearStaticMapsAndVectors(); |
| 125 | +} |
| 126 | + |
| 127 | +[[maybe_unused]] static void CKKSBootStC(benchmark::State& state) { |
| 128 | + auto t = boot_configs[state.range(0)]; |
| 129 | + |
| 130 | + CCParams<CryptoContextCKKSRNS> parameters; |
| 131 | + parameters.SetSecurityLevel(HEStd_128_classic); |
| 132 | + parameters.SetRingDim(t.ringDim); |
| 133 | + parameters.SetScalingModSize(t.dcrtBits); |
| 134 | + parameters.SetFirstModSize(t.firstMod); |
| 135 | + parameters.SetNumLargeDigits(t.numDigits); |
| 136 | + parameters.SetSecretKeyDist(t.skdst); |
| 137 | + parameters.SetScalingTechnique(t.stech); |
| 138 | + parameters.SetKeySwitchTechnique(HYBRID); |
| 139 | + uint32_t depth = t.lvlsAfter + FHECKKSRNS::GetBootstrapDepth(t.lvlb, t.skdst) + (t.iters - 1); |
| 140 | + parameters.SetMultiplicativeDepth(depth); |
| 141 | + |
| 142 | + auto cc = GenCryptoContext(parameters); |
| 143 | + cc->Enable(PKE); |
| 144 | + cc->Enable(KEYSWITCH); |
| 145 | + cc->Enable(LEVELEDSHE); |
| 146 | + cc->Enable(ADVANCEDSHE); |
| 147 | + cc->Enable(FHE); |
| 148 | + |
| 149 | + cc->EvalBootstrapSetup(t.lvlb, {0, 0}, t.slots, 0, true, true); |
| 150 | + |
| 151 | + auto keyPair = cc->KeyGen(); |
| 152 | + cc->EvalMultKeyGen(keyPair.secretKey); |
| 153 | + cc->EvalBootstrapKeyGen(keyPair.secretKey, t.slots); |
| 154 | + |
| 155 | + std::vector<double> x = {0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0}; |
| 156 | + |
| 157 | + auto ptxt = cc->MakeCKKSPackedPlaintext(x, 1, depth - 1 - t.lvlb[1], nullptr, t.slots); |
| 158 | + ptxt->SetLength(t.slots); |
| 159 | + |
| 160 | + auto ctxt = cc->Encrypt(keyPair.publicKey, ptxt); |
| 161 | + |
| 162 | + while (state.KeepRunning()) |
| 163 | + auto ctxtAfter = cc->EvalBootstrap(ctxt, t.iters); |
| 164 | + |
| 165 | + cc->ClearStaticMapsAndVectors(); |
| 166 | +} |
| 167 | + |
| 168 | +BENCHMARK(CKKSBoot)->Unit(benchmark::kSecond)->Iterations(4)->Apply(BootConfigs); |
| 169 | +BENCHMARK(CKKSBootStC)->Unit(benchmark::kSecond)->Iterations(4)->Apply(BootConfigs); |
| 170 | + |
| 171 | +BENCHMARK_MAIN(); |
0 commit comments