|
| 1 | +#include "lib/Dialect/LWE/Transforms/ImplementTrivialEncryptionAsAddition.h" |
| 2 | + |
| 3 | +#include <cassert> |
| 4 | +#include <cstdint> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +#include "lib/Dialect/FuncUtils.h" |
| 8 | +#include "lib/Dialect/LWE/IR/LWEAttributes.h" |
| 9 | +#include "lib/Dialect/LWE/IR/LWEOps.h" |
| 10 | +#include "lib/Dialect/LWE/IR/LWETypes.h" |
| 11 | +#include "lib/Dialect/ModuleAttributes.h" |
| 12 | +#include "llvm/include/llvm/Support/Debug.h" // from @llvm-project |
| 13 | +#include "llvm/include/llvm/Support/Format.h" // from @llvm-project |
| 14 | +#include "llvm/include/llvm/Support/raw_ostream.h" // from @llvm-project |
| 15 | +#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project |
| 16 | +#include "mlir/include/mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project |
| 17 | +#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project |
| 18 | +#include "mlir/include/mlir/IR/Block.h" // from @llvm-project |
| 19 | +#include "mlir/include/mlir/IR/Builders.h" // from @llvm-project |
| 20 | +#include "mlir/include/mlir/IR/BuiltinOps.h" // from @llvm-project |
| 21 | +#include "mlir/include/mlir/IR/BuiltinTypes.h" // from @llvm-project |
| 22 | +#include "mlir/include/mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project |
| 23 | +#include "mlir/include/mlir/IR/Operation.h" // from @llvm-project |
| 24 | +#include "mlir/include/mlir/IR/PatternMatch.h" // from @llvm-project |
| 25 | +#include "mlir/include/mlir/IR/TypeRange.h" // from @llvm-project |
| 26 | +#include "mlir/include/mlir/IR/TypeUtilities.h" // from @llvm-project |
| 27 | +#include "mlir/include/mlir/IR/Types.h" // from @llvm-project |
| 28 | +#include "mlir/include/mlir/IR/Value.h" // from @llvm-project |
| 29 | +#include "mlir/include/mlir/IR/Visitors.h" // from @llvm-project |
| 30 | +#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project |
| 31 | +#include "mlir/include/mlir/Support/LogicalResult.h" // from @llvm-project |
| 32 | +#include "mlir/include/mlir/Support/WalkResult.h" // from @llvm-project |
| 33 | +#include "mlir/include/mlir/Transforms/WalkPatternRewriteDriver.h" // from @llvm-project |
| 34 | + |
| 35 | +// IWYU pragma: begin_keep |
| 36 | +#include "mlir/include/mlir/Transforms/Passes.h" // from @llvm-project |
| 37 | +// IWYU pragma: end_keep |
| 38 | + |
| 39 | +#define DEBUG_TYPE "implement-trivial-encryption-as-addition" |
| 40 | + |
| 41 | +namespace mlir { |
| 42 | +namespace heir { |
| 43 | +namespace lwe { |
| 44 | + |
| 45 | +#define GEN_PASS_DEF_IMPLEMENTTRIVIALENCRYPTIONASADDITION |
| 46 | +#include "lib/Dialect/LWE/Transforms/Passes.h.inc" |
| 47 | + |
| 48 | +using func::FuncOp; |
| 49 | + |
| 50 | +Type findEncryptionKeyTypeFromHelper(func::FuncOp funcOp, ModuleOp module) { |
| 51 | + Type foundType; |
| 52 | + |
| 53 | + // First try looking for public key types, since the result decryption func |
| 54 | + // always has a secret key type and we don't want to accidentally visit it |
| 55 | + // first. |
| 56 | + auto result = module.walk([&](func::FuncOp func) { |
| 57 | + if (isClientHelper(func.getOperation())) { |
| 58 | + for (Type ty : func.getArgumentTypes()) |
| 59 | + if (isa<LWEPublicKeyType>(ty)) { |
| 60 | + foundType = ty; |
| 61 | + return WalkResult::interrupt(); |
| 62 | + } |
| 63 | + } |
| 64 | + return WalkResult::advance(); |
| 65 | + }); |
| 66 | + |
| 67 | + if (result.wasInterrupted()) { |
| 68 | + return foundType; |
| 69 | + } |
| 70 | + |
| 71 | + module.walk([&](func::FuncOp func) { |
| 72 | + if (isClientHelper(func.getOperation())) { |
| 73 | + for (Type ty : func.getArgumentTypes()) |
| 74 | + if (isa<LWESecretKeyType>(ty)) { |
| 75 | + foundType = ty; |
| 76 | + return WalkResult::interrupt(); |
| 77 | + } |
| 78 | + } |
| 79 | + return WalkResult::advance(); |
| 80 | + }); |
| 81 | + |
| 82 | + return foundType; |
| 83 | +} |
| 84 | + |
| 85 | +uint64_t hashTypeStringFormat(Type type) { |
| 86 | + SmallString<16> typeString; |
| 87 | + llvm::raw_svector_ostream typeOS(typeString); |
| 88 | + typeOS << type; |
| 89 | + return llvm::hash_value(typeString.str()); |
| 90 | +} |
| 91 | + |
| 92 | +// Creates a function that returns a single ciphertext encrypting zero. A new |
| 93 | +// function is created for each ciphertext type returned by originalOp, and |
| 94 | +// otherwise duplicate functions are looked up by symbol name. Created functions |
| 95 | +// are tagged with client.enc_zero_func. |
| 96 | +func::FuncOp getOrCreateEncryptionOfZerosFunc(func::FuncOp parentFunc, |
| 97 | + TrivialEncryptOp originalOp, |
| 98 | + ModuleOp module) { |
| 99 | + LWEPlaintextType plaintextType = cast<LWEPlaintextType>( |
| 100 | + getElementTypeOrSelf(originalOp.getInput().getType())); |
| 101 | + LWECiphertextType ciphertextType = cast<LWECiphertextType>( |
| 102 | + getElementTypeOrSelf(originalOp.getResult().getType())); |
| 103 | + SmallString<16> buffer; |
| 104 | + llvm::raw_svector_ostream os(buffer); |
| 105 | + os << parentFunc.getSymName() << "__encrypt__zero__" |
| 106 | + << llvm::format_hex_no_prefix(hashTypeStringFormat(ciphertextType), 16); |
| 107 | + SmallString<16> buffer2; |
| 108 | + std::string encFuncName = std::string(sanitizeIdentifier(buffer, buffer2)); |
| 109 | + |
| 110 | + if (auto existingFunc = module.lookupSymbol<func::FuncOp>(encFuncName)) { |
| 111 | + return existingFunc; |
| 112 | + } |
| 113 | + |
| 114 | + ImplicitLocOpBuilder builder = |
| 115 | + ImplicitLocOpBuilder::atBlockEnd(module.getLoc(), module.getBody()); |
| 116 | + builder.setInsertionPointAfter(parentFunc); |
| 117 | + |
| 118 | + Type keyTy = findEncryptionKeyTypeFromHelper(parentFunc, module); |
| 119 | + if (!keyTy) { |
| 120 | + keyTy = lwe::LWESecretKeyType::get( |
| 121 | + module.getContext(), lwe::KeyAttr::get(module.getContext(), 0), |
| 122 | + ciphertextType.getCiphertextSpace().getRing()); |
| 123 | + } |
| 124 | + |
| 125 | + FunctionType encFuncType = |
| 126 | + FunctionType::get(builder.getContext(), {keyTy}, {ciphertextType}); |
| 127 | + auto encFuncOp = func::FuncOp::create(builder, encFuncName, encFuncType); |
| 128 | + |
| 129 | + encFuncOp->setAttr(kClientEncZeroFuncAttrName, builder.getUnitAttr()); |
| 130 | + Block* entryBlock = encFuncOp.addEntryBlock(); |
| 131 | + builder.setInsertionPointToEnd(entryBlock); |
| 132 | + |
| 133 | + int numSlots; |
| 134 | + auto numSlotsAttr = dyn_cast_or_null<IntegerAttr>( |
| 135 | + module->getAttr(kRequestedSlotCountAttrName)); |
| 136 | + if (numSlotsAttr) { |
| 137 | + numSlots = numSlotsAttr.getInt(); |
| 138 | + } else { |
| 139 | + module->emitWarning() |
| 140 | + << "Encountered module op with no requested_slots " |
| 141 | + "attribute; defaulting to polynomial modulus ring " |
| 142 | + "degree / 2, which will be off by a factor of 2 for BGV/BFV.\n"; |
| 143 | + numSlots = plaintextType.getPlaintextSpace() |
| 144 | + .getRing() |
| 145 | + .getPolynomialModulus() |
| 146 | + .getPolynomial() |
| 147 | + .getDegree() / |
| 148 | + 2; |
| 149 | + } |
| 150 | + |
| 151 | + Type coeffType = |
| 152 | + plaintextType.getPlaintextSpace().getRing().getCoefficientType(); |
| 153 | + Type cleartextElementType = builder.getIntegerType( |
| 154 | + llvm::TypeSwitch<Type, int>(coeffType) |
| 155 | + .Case<IntegerType, FloatType>( |
| 156 | + [&](auto ty) { return ty.getIntOrFloatBitWidth(); }) |
| 157 | + .Case<mod_arith::ModArithType>([&](auto ty) { |
| 158 | + return ty.getModulus().getType().getIntOrFloatBitWidth(); |
| 159 | + }) |
| 160 | + .Default([&](auto ty) { |
| 161 | + originalOp->emitOpError() |
| 162 | + << "has unsupported plaintext coefficient type; can't " |
| 163 | + "determine what constant type to use for encryption of " |
| 164 | + "zero."; |
| 165 | + return 0; |
| 166 | + })); |
| 167 | + |
| 168 | + RankedTensorType zeroType = |
| 169 | + RankedTensorType::get({numSlots}, cleartextElementType); |
| 170 | + auto zeroAttr = builder.getZeroAttr(zeroType); |
| 171 | + arith::ConstantOp constantOp = arith::ConstantOp::create(builder, zeroAttr); |
| 172 | + |
| 173 | + auto plaintextSpace = plaintextType.getPlaintextSpace(); |
| 174 | + auto encodeOp = RLWEEncodeOp::create( |
| 175 | + builder, plaintextType, constantOp.getResult(), |
| 176 | + plaintextSpace.getEncoding(), plaintextSpace.getRing()); |
| 177 | + auto encrypted = RLWEEncryptOp::create( |
| 178 | + builder, ciphertextType, encodeOp.getResult(), encFuncOp.getArgument(0)); |
| 179 | + |
| 180 | + for (auto attr : originalOp->getAttrs()) { |
| 181 | + if (attr.getName().strref().contains('.')) { |
| 182 | + encrypted->setAttr(attr.getName(), attr.getValue()); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + func::ReturnOp::create(builder, encrypted.getResult()); |
| 187 | + return encFuncOp; |
| 188 | +} |
| 189 | + |
| 190 | +// Creates a new function arg containing a ciphertext encrypting zero |
| 191 | +// with the attribute client.enc_zero_arg |
| 192 | +Value getOrCreateNewFuncArg(func::FuncOp func, LWECiphertextType type, |
| 193 | + PatternRewriter& rewriter) { |
| 194 | + for (unsigned i = 0; i < func.getNumArguments(); ++i) { |
| 195 | + if (func.getArgument(i).getType() == type && |
| 196 | + func.getArgAttr(i, kClientEncZeroArgAttrName)) { |
| 197 | + return func.getArgument(i); |
| 198 | + } |
| 199 | + } |
| 200 | + auto context = func.getContext(); |
| 201 | + auto oldType = func.getFunctionType(); |
| 202 | + SmallVector<Type> newInputs(oldType.getInputs().begin(), |
| 203 | + oldType.getInputs().end()); |
| 204 | + newInputs.push_back(type); |
| 205 | + auto newType = FunctionType::get(context, newInputs, oldType.getResults()); |
| 206 | + func.setType(newType); |
| 207 | + |
| 208 | + auto newArg = func.getBody().addArgument(type, func.getLoc()); |
| 209 | + func.setArgAttr(newArg.getArgNumber(), kClientEncZeroArgAttrName, |
| 210 | + rewriter.getUnitAttr()); |
| 211 | + return newArg; |
| 212 | +} |
| 213 | + |
| 214 | +struct TrivialEncryptionRewritePattern |
| 215 | + : public OpRewritePattern<TrivialEncryptOp> { |
| 216 | + using OpRewritePattern<TrivialEncryptOp>::OpRewritePattern; |
| 217 | + |
| 218 | + LogicalResult matchAndRewrite(TrivialEncryptOp op, |
| 219 | + PatternRewriter& rewriter) const override { |
| 220 | + auto func = op->getParentOfType<FuncOp>(); |
| 221 | + auto module = func->getParentOfType<ModuleOp>(); |
| 222 | + getOrCreateEncryptionOfZerosFunc(func, op, module); |
| 223 | + Type resultType = op.getResult().getType(); |
| 224 | + LWECiphertextType ctTy = |
| 225 | + cast<LWECiphertextType>(getElementTypeOrSelf(resultType)); |
| 226 | + Value newFuncArg = getOrCreateNewFuncArg(func, ctTy, rewriter); |
| 227 | + |
| 228 | + // The newFuncArg is a single ciphertext, so we may need to splat it |
| 229 | + // into a tensor of the appropriate shape |
| 230 | + Value operand = newFuncArg; |
| 231 | + if (isa<ShapedType>(op.getResult().getType())) { |
| 232 | + operand = |
| 233 | + tensor::SplatOp::create(rewriter, op.getLoc(), resultType, operand); |
| 234 | + } |
| 235 | + auto newAddOp = |
| 236 | + RAddPlainOp::create(rewriter, op.getLoc(), operand, op.getInput()); |
| 237 | + rewriter.replaceOp(op, newAddOp); |
| 238 | + return success(); |
| 239 | + } |
| 240 | +}; |
| 241 | + |
| 242 | +struct ImplementTrivialEncryptionAsAddition |
| 243 | + : impl::ImplementTrivialEncryptionAsAdditionBase< |
| 244 | + ImplementTrivialEncryptionAsAddition> { |
| 245 | + using ImplementTrivialEncryptionAsAdditionBase:: |
| 246 | + ImplementTrivialEncryptionAsAdditionBase; |
| 247 | + |
| 248 | + void runOnOperation() override { |
| 249 | + MLIRContext* context = &getContext(); |
| 250 | + RewritePatternSet patterns(context); |
| 251 | + patterns.add<TrivialEncryptionRewritePattern>(context); |
| 252 | + walkAndApplyPatterns(getOperation(), std::move(patterns)); |
| 253 | + } |
| 254 | +}; |
| 255 | + |
| 256 | +} // namespace lwe |
| 257 | +} // namespace heir |
| 258 | +} // namespace mlir |
0 commit comments