/
SecretToBGV.cpp
199 lines (173 loc) · 7.45 KB
/
SecretToBGV.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "include/Conversion/SecretToBGV/SecretToBGV.h"
#include <cassert>
#include <cstdint>
#include <utility>
#include <vector>
#include "include/Dialect/BGV/IR/BGVDialect.h"
#include "include/Dialect/BGV/IR/BGVOps.h"
#include "include/Dialect/LWE/IR/LWEAttributes.h"
#include "include/Dialect/LWE/IR/LWETypes.h"
#include "include/Dialect/Polynomial/IR/Polynomial.h"
#include "include/Dialect/Polynomial/IR/PolynomialAttributes.h"
#include "include/Dialect/Secret/IR/SecretDialect.h"
#include "include/Dialect/Secret/IR/SecretOps.h"
#include "include/Dialect/Secret/IR/SecretTypes.h"
#include "include/Dialect/TensorExt/IR/TensorExtOps.h"
#include "lib/Conversion/Utils.h"
#include "llvm/include/llvm/ADT/TypeSwitch.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project
#include "mlir/include/mlir/IR/BuiltinTypes.h" // from @llvm-project
#include "mlir/include/mlir/IR/PatternMatch.h" // from @llvm-project
#include "mlir/include/mlir/IR/Value.h" // from @llvm-project
#include "mlir/include/mlir/IR/ValueRange.h" // from @llvm-project
#include "mlir/include/mlir/IR/Visitors.h" // from @llvm-project
#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project
#include "mlir/include/mlir/Support/LogicalResult.h" // from @llvm-project
#include "mlir/include/mlir/Transforms/DialectConversion.h" // from @llvm-project
namespace mlir::heir {
#define GEN_PASS_DEF_SECRETTOBGV
#include "include/Conversion/SecretToBGV/SecretToBGV.h.inc"
namespace {
// Returns an RLWE ring given the specified number of bits needed and polynomial
// modulus degree.
// TODO(#536): Integrate a general library to compute appropriate prime moduli
// given any number of bits.
FailureOr<polynomial::RingAttr> getRlweRing(MLIRContext *ctx,
int coefficientModBits,
int polyModDegree) {
std::vector<polynomial::Monomial> monomials;
monomials.emplace_back(1, polyModDegree);
monomials.emplace_back(1, 0);
polynomial::Polynomial xnPlusOne =
polynomial::Polynomial::fromMonomials(monomials, ctx);
switch (coefficientModBits) {
case 29:
return polynomial::RingAttr::get(
APInt(polynomial::APINT_BIT_WIDTH, 463187969), xnPlusOne);
default:
return failure();
}
}
} // namespace
// Remove this class if no type conversions are necessary
class SecretToBGVTypeConverter : public TypeConverter {
public:
SecretToBGVTypeConverter(MLIRContext *ctx, polynomial::RingAttr rlweRing) {
addConversion([](Type type) { return type; });
// Convert secret types to BGV ciphertext types
addConversion([ctx, this](secret::SecretType type) -> Type {
int bitWidth =
llvm::TypeSwitch<Type, int>(type.getValueType())
.Case<RankedTensorType>(
[&](auto ty) -> int { return ty.getElementTypeBitWidth(); })
.Case<IntegerType>([&](auto ty) -> int { return ty.getWidth(); });
return lwe::RLWECiphertextType::get(
ctx,
lwe::PolynomialEvaluationEncodingAttr::get(ctx, bitWidth, bitWidth),
lwe::RLWEParamsAttr::get(ctx, 2, ring_));
});
ring_ = rlweRing;
}
private:
polynomial::RingAttr ring_;
};
template <typename T, typename Y>
class SecretGenericOpConversion
: public OpConversionPattern<secret::GenericOp> {
public:
using OpConversionPattern<secret::GenericOp>::OpConversionPattern;
LogicalResult matchAndRewrite(
secret::GenericOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const final {
if (op.getBody()->getOperations().size() > 2) {
// Each secret.generic should contain at most one instruction -
// secret-distribute-generic can be used to distribute through the
// arithmetic ops.
return failure();
}
auto &innerOp = op.getBody()->getOperations().front();
if (!isa<T>(innerOp)) {
return failure();
}
// Assemble the arguments for the BGV operation.
SmallVector<Value> inputs;
for (OpOperand &operand : innerOp.getOpOperands()) {
if (auto *secretArg = op.getOpOperandForBlockArgument(operand.get())) {
inputs.push_back(
adaptor.getODSOperands(0)[secretArg->getOperandNumber()]);
} else {
inputs.push_back(operand.get());
}
}
// Directly convert the op if all operands are ciphertext.
replaceOp(op, inputs, rewriter);
return success();
}
// Default method for replacing the secret.generic with the target operation.
virtual void replaceOp(secret::GenericOp op, ValueRange inputs,
ConversionPatternRewriter &rewriter) const {
rewriter.replaceOpWithNewOp<Y>(op, inputs);
}
};
class SecretGenericOpMulConversion
: public SecretGenericOpConversion<arith::MulIOp, bgv::MulOp> {
public:
using SecretGenericOpConversion<arith::MulIOp,
bgv::MulOp>::SecretGenericOpConversion;
void replaceOp(secret::GenericOp op, ValueRange inputs,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<bgv::Relinearize>(
op, rewriter.create<bgv::MulOp>(op.getLoc(), inputs),
rewriter.getDenseI32ArrayAttr({0, 1, 2}),
rewriter.getDenseI32ArrayAttr({0, 1}));
}
};
struct SecretToBGV : public impl::SecretToBGVBase<SecretToBGV> {
using SecretToBGVBase::SecretToBGVBase;
void runOnOperation() override {
MLIRContext *context = &getContext();
auto *module = getOperation();
auto rlweRing = getRlweRing(context, coefficientModBits, polyModDegree);
if (failed(rlweRing)) {
return signalPassFailure();
}
// Ensure that all secret types are uniform and matching the ring
// parameter size.
WalkResult compatibleTensors = module->walk([&](Operation *op) {
for (auto value : op->getOperands()) {
if (auto secretTy = dyn_cast<secret::SecretType>(value.getType())) {
auto tensorTy = dyn_cast<RankedTensorType>(secretTy.getValueType());
if (tensorTy &&
tensorTy.getShape() !=
ArrayRef<int64_t>{rlweRing.value().getIdeal().getDegree()}) {
return WalkResult::interrupt();
}
}
}
return WalkResult::advance();
});
if (compatibleTensors.wasInterrupted()) {
module->emitError(
"expected batched secret types to be tensors with dimension "
"matching ring parameter");
return signalPassFailure();
}
SecretToBGVTypeConverter typeConverter(context, rlweRing.value());
RewritePatternSet patterns(context);
ConversionTarget target(*context);
target.addLegalDialect<bgv::BGVDialect>();
target.addIllegalDialect<secret::SecretDialect>();
target.addIllegalOp<secret::GenericOp>();
addStructuralConversionPatterns(typeConverter, patterns, target);
patterns.add<SecretGenericOpConversion<arith::AddIOp, bgv::AddOp>,
SecretGenericOpConversion<arith::SubIOp, bgv::SubOp>,
SecretGenericOpConversion<tensor::ExtractOp, bgv::ExtractOp>,
SecretGenericOpConversion<tensor_ext::RotateOp, bgv::Rotate>,
SecretGenericOpMulConversion>(typeConverter, context);
if (failed(applyPartialConversion(module, target, std::move(patterns)))) {
return signalPassFailure();
}
}
};
} // namespace mlir::heir