|
1 | 1 | #include "lib/Parameters/CKKS/Params.h" |
2 | 2 |
|
| 3 | +#include <algorithm> |
3 | 4 | #include <cassert> |
| 5 | +#include <cmath> |
| 6 | +#include <cstddef> |
| 7 | +#include <cstdint> |
4 | 8 | #include <utility> |
5 | 9 | #include <vector> |
6 | 10 |
|
| 11 | +#include "lib/Dialect/CKKS/IR/CKKSAttributes.h" |
| 12 | +#include "lib/Dialect/CKKS/IR/CKKSEnums.h" |
7 | 13 | #include "lib/Parameters/RLWEParams.h" |
| 14 | +#include "lib/Parameters/RLWESecurityParams.h" |
8 | 15 | #include "llvm/include/llvm/Support/raw_ostream.h" // from @llvm-project |
| 16 | +#include "src/core/include/openfhecore.h" // from @openfhe |
9 | 17 |
|
10 | 18 | namespace mlir { |
11 | 19 | namespace heir { |
12 | 20 | namespace ckks { |
13 | 21 |
|
| 22 | +/// By original we mean the method in RNS-CKKS implementation |
| 23 | +/// Corresponds to FIXED* in OpenFHE |
| 24 | +static std::vector<int64_t> moduliQGenerationOpenFHEFixed(int logFirstMod, |
| 25 | + int logDefaultScale, |
| 26 | + int numLevel, |
| 27 | + int ringDim) { |
| 28 | + auto cyclOrder = ringDim * 2; |
| 29 | + std::vector<int64_t> moduliQ(numLevel); |
| 30 | + lbcrypto::NativeInteger q = |
| 31 | + lbcrypto::FirstPrime<NativeInteger>(logDefaultScale, cyclOrder); |
| 32 | + moduliQ[numLevel - 1] = q.ConvertToInt(); |
| 33 | + |
| 34 | + auto maxPrime{q}; |
| 35 | + auto minPrime{q}; |
| 36 | + |
| 37 | + auto qPrev = q; |
| 38 | + auto qNext = q; |
| 39 | + if (numLevel > 2) { |
| 40 | + for (size_t i = numLevel - 2, cnt = 0; i >= 1; --i, ++cnt) { |
| 41 | + if ((cnt % 2) == 0) { |
| 42 | + qPrev = PreviousPrime(qPrev, cyclOrder); |
| 43 | + moduliQ[i] = qPrev.ConvertToInt(); |
| 44 | + } else { |
| 45 | + qNext = NextPrime(qNext, cyclOrder); |
| 46 | + moduliQ[i] = qNext.ConvertToInt(); |
| 47 | + } |
| 48 | + |
| 49 | + if (moduliQ[i] > maxPrime) |
| 50 | + maxPrime = moduliQ[i]; |
| 51 | + else if (moduliQ[i] < minPrime) |
| 52 | + minPrime = moduliQ[i]; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (logFirstMod == logDefaultScale) { // this requires dcrtBits < 60 |
| 57 | + moduliQ[0] = |
| 58 | + lbcrypto::NextPrime<NativeInteger>(maxPrime, cyclOrder).ConvertToInt(); |
| 59 | + } else { |
| 60 | + moduliQ[0] = lbcrypto::LastPrime<NativeInteger>(logFirstMod, cyclOrder) |
| 61 | + .ConvertToInt(); |
| 62 | + |
| 63 | + // find if the value of moduliQ[0] is already in the vector starting with |
| 64 | + // moduliQ[1] and if there is, then get another prime for moduliQ[0] |
| 65 | + const auto pos = std::find(moduliQ.begin() + 1, moduliQ.end(), moduliQ[0]); |
| 66 | + if (pos != moduliQ.end()) { |
| 67 | + moduliQ[0] = lbcrypto::NextPrime<NativeInteger>(maxPrime, cyclOrder) |
| 68 | + .ConvertToInt(); |
| 69 | + } |
| 70 | + } |
| 71 | + return moduliQ; |
| 72 | +} |
| 73 | + |
| 74 | +/// See "Reduced Error" paper https://eprint.iacr.org/2020/1118 |
| 75 | +/// Corresponds to FLEXIBLE* in OpenFHE |
| 76 | +static std::vector<int64_t> moduliQGenerationReducedError(int logFirstMod, |
| 77 | + int logDefaultScale, |
| 78 | + int numLevel, |
| 79 | + int ringDim) { |
| 80 | + auto cyclOrder = ringDim * 2; |
| 81 | + std::vector<int64_t> moduliQ(numLevel); |
| 82 | + lbcrypto::NativeInteger q = |
| 83 | + lbcrypto::FirstPrime<lbcrypto::NativeInteger>(logDefaultScale, cyclOrder); |
| 84 | + moduliQ[numLevel - 1] = q.ConvertToInt(); |
| 85 | + |
| 86 | + auto maxPrime{q}; |
| 87 | + auto minPrime{q}; |
| 88 | + |
| 89 | + if (numLevel > 2) { |
| 90 | + for (size_t i = numLevel - 2, cnt = 0; i >= 1; --i, ++cnt) { |
| 91 | + // Comments from OpenFHE ckksrns-parametergeneration.cpp |
| 92 | + /* Scaling factors in FLEXIBLEAUTO are a bit fragile, |
| 93 | + * in the sense that once one scaling factor gets far enough from the |
| 94 | + * original scaling factor, subsequent level scaling factors quickly |
| 95 | + * diverge to either 0 or infinity. To mitigate this problem to a certain |
| 96 | + * extend, we have a special prime selection process in place. The goal is |
| 97 | + * to maintain the scaling factor of all levels as close to the original |
| 98 | + * scale factor of level 0 as possible. |
| 99 | + */ |
| 100 | + double sf = static_cast<double>(moduliQ[numLevel - 1]); |
| 101 | + for (size_t i = numLevel - 2, cnt = 0; i >= 1; --i, ++cnt) { |
| 102 | + sf = pow(sf, 2) / static_cast<double>(moduliQ[i + 1]); |
| 103 | + NativeInteger sfInt = std::llround(sf); |
| 104 | + NativeInteger sfRem = sfInt.Mod(cyclOrder); |
| 105 | + bool hasSameMod = true; |
| 106 | + if ((cnt % 2) == 0) { |
| 107 | + NativeInteger qPrev = |
| 108 | + sfInt - NativeInteger(cyclOrder) - sfRem + NativeInteger(1); |
| 109 | + while (hasSameMod) { |
| 110 | + hasSameMod = false; |
| 111 | + qPrev = lbcrypto::PreviousPrime(qPrev, cyclOrder); |
| 112 | + for (size_t j = i + 1; j < numLevel; j++) { |
| 113 | + if (qPrev == moduliQ[j]) { |
| 114 | + hasSameMod = true; |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + moduliQ[i] = qPrev.ConvertToInt(); |
| 120 | + } else { |
| 121 | + NativeInteger qNext = |
| 122 | + sfInt + NativeInteger(cyclOrder) - sfRem + NativeInteger(1); |
| 123 | + while (hasSameMod) { |
| 124 | + hasSameMod = false; |
| 125 | + qNext = lbcrypto::NextPrime(qNext, cyclOrder); |
| 126 | + for (size_t j = i + 1; j < numLevel; j++) { |
| 127 | + if (qNext == moduliQ[j]) { |
| 128 | + hasSameMod = true; |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + moduliQ[i] = qNext.ConvertToInt(); |
| 134 | + } |
| 135 | + if (moduliQ[i] > maxPrime) |
| 136 | + maxPrime = moduliQ[i]; |
| 137 | + else if (moduliQ[i] < minPrime) |
| 138 | + minPrime = moduliQ[i]; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (logFirstMod == logDefaultScale) { // this requires dcrtBits < 60 |
| 144 | + moduliQ[0] = |
| 145 | + lbcrypto::NextPrime<lbcrypto::NativeInteger>(maxPrime, cyclOrder) |
| 146 | + .ConvertToInt(); |
| 147 | + } else { |
| 148 | + moduliQ[0] = |
| 149 | + lbcrypto::LastPrime<lbcrypto::NativeInteger>(logFirstMod, cyclOrder) |
| 150 | + .ConvertToInt(); |
| 151 | + |
| 152 | + // find if the value of moduliQ[0] is already in the vector starting with |
| 153 | + // moduliQ[1] and if there is, then get another prime for moduliQ[0] |
| 154 | + const auto pos = std::find(moduliQ.begin() + 1, moduliQ.end(), moduliQ[0]); |
| 155 | + if (pos != moduliQ.end()) { |
| 156 | + moduliQ[0] = |
| 157 | + lbcrypto::NextPrime<lbcrypto::NativeInteger>(maxPrime, cyclOrder) |
| 158 | + .ConvertToInt(); |
| 159 | + } |
| 160 | + } |
| 161 | + return moduliQ; |
| 162 | +} |
| 163 | + |
| 164 | +// numScaleMod is L |
14 | 165 | SchemeParam SchemeParam::getConcreteSchemeParam( |
15 | | - std::vector<double> logqi, int logDefaultScale, int slotNumber, |
16 | | - bool usePublicKey, bool encryptionTechniqueExtended) { |
| 166 | + int logFirstMod, int logDefaultScale, int numScaleMod, int slotNumber, |
| 167 | + bool usePublicKey, bool encryptionTechniqueExtended, bool reducedError) { |
17 | 168 | // CKKS slot number = ringDim / 2 |
18 | | - return SchemeParam(RLWESchemeParam::getConcreteRLWESchemeParam( |
19 | | - std::move(logqi), 2 * slotNumber, usePublicKey, |
20 | | - encryptionTechniqueExtended), |
21 | | - logDefaultScale); |
| 169 | + auto minRingDim = 2 * slotNumber; |
| 170 | + |
| 171 | + auto dnum = computeDnum(numScaleMod); |
| 172 | + |
| 173 | + // q0 + (q1 + ... + qL) = firstModBits + scalingModBits * L |
| 174 | + double logQ = logFirstMod + logDefaultScale * numScaleMod; |
| 175 | + // pi can be large |
| 176 | + auto sizePi = 60; |
| 177 | + |
| 178 | + // make P > Q / dnum |
| 179 | + auto logP = ceil(logQ / dnum); |
| 180 | + auto numPi = ceil(logP / sizePi); |
| 181 | + // update logP |
| 182 | + logP = numPi * sizePi; |
| 183 | + |
| 184 | + auto logPQ = logQ + logP; |
| 185 | + |
| 186 | + // ringDim will change if newLogPQ is too large |
| 187 | + auto ringDim = computeRingDim(logPQ, minRingDim); |
| 188 | + bool redo = false; |
| 189 | + std::vector<int64_t> qiImpl; |
| 190 | + std::vector<int64_t> piImpl; |
| 191 | + do { |
| 192 | + redo = false; |
| 193 | + qiImpl.clear(); |
| 194 | + piImpl.clear(); |
| 195 | + |
| 196 | + if (reducedError) { |
| 197 | + qiImpl = moduliQGenerationReducedError(logFirstMod, logDefaultScale, |
| 198 | + numScaleMod + 1, ringDim); |
| 199 | + } else { |
| 200 | + qiImpl = moduliQGenerationOpenFHEFixed(logFirstMod, logDefaultScale, |
| 201 | + numScaleMod + 1, ringDim); |
| 202 | + } |
| 203 | + std::vector<int64_t> existingPrimes = qiImpl; |
| 204 | + for (size_t i = 0; i < numPi; ++i) { |
| 205 | + auto prime = findPrime(sizePi, ringDim, existingPrimes); |
| 206 | + piImpl.push_back(prime); |
| 207 | + existingPrimes.push_back(prime); |
| 208 | + } |
| 209 | + |
| 210 | + // if generated primes are too large, increase ringDim |
| 211 | + double newLogPQ = 0; |
| 212 | + for (auto qi : qiImpl) { |
| 213 | + newLogPQ += log2(qi); |
| 214 | + } |
| 215 | + for (auto pi : piImpl) { |
| 216 | + newLogPQ += log2(pi); |
| 217 | + } |
| 218 | + auto newRingDim = computeRingDim(newLogPQ, minRingDim); |
| 219 | + if (newRingDim != ringDim) { |
| 220 | + ringDim = newRingDim; |
| 221 | + redo = true; |
| 222 | + } |
| 223 | + } while (redo); |
| 224 | + |
| 225 | + std::vector<double> logqi; |
| 226 | + std::vector<double> logpi; |
| 227 | + for (auto qi : qiImpl) { |
| 228 | + logqi.push_back(log2(qi)); |
| 229 | + } |
| 230 | + for (auto pi : piImpl) { |
| 231 | + logpi.push_back(log2(pi)); |
| 232 | + } |
| 233 | + |
| 234 | + return SchemeParam( |
| 235 | + RLWESchemeParam(ringDim, numScaleMod + 1, logqi, qiImpl, dnum, logpi, |
| 236 | + piImpl, usePublicKey, encryptionTechniqueExtended), |
| 237 | + logDefaultScale); |
22 | 238 | } |
23 | 239 |
|
24 | 240 | SchemeParam SchemeParam::getSchemeParamFromAttr(SchemeParamAttr attr) { |
|
0 commit comments