Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/Examples/openfhe/bgv/dot_product_128_debug/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See README.md for setup required to run these tests

load("@heir//tests/Examples/openfhe:test.bzl", "openfhe_end_to_end_test")

package(default_applicable_licenses = ["@heir//:license"])

openfhe_end_to_end_test(
name = "dot_product_128_debug_test",
generated_lib_header = "dot_product_128_debug_lib.h",
heir_opt_flags = [
"--mlir-to-bgv=ciphertext-degree=128 annotate-noise-bound=true plaintext-modulus=4194305",
"--scheme-to-openfhe=insert-debug-handler-calls=true",
],
mlir_src = "dot_product_128.mlir",
tags = ["notap"],
test_src = "dot_product_128_debug_test.cpp",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func.func @dot_product(%arg0: tensor<128xi32> {secret.secret}, %arg1: tensor<128xi32> {secret.secret}) -> i32 {
%c0 = arith.constant 0 : index
%c0_si32 = arith.constant 0 : i32
%0 = affine.for %arg2 = 0 to 128 iter_args(%iter = %c0_si32) -> (i32) {
%1 = tensor.extract %arg0[%arg2] : tensor<128xi32>
%2 = tensor.extract %arg1[%arg2] : tensor<128xi32>
%3 = arith.muli %1, %2 : i32
%4 = arith.addi %iter, %3 : i32
affine.yield %4 : i32
}
return %0 : i32
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <map>
#include <ostream>
#include <string>
#include <vector>

#include "gtest/gtest.h" // from @googletest
#include "src/core/include/lattice/hal/lat-backend.h" // from @openfhe
#include "src/core/include/utils/inttypes.h" // from @openfhe
#include "src/pke/include/key/privatekey-fwd.h" // from @openfhe

// Generated headers (block clang-format from messing up order)
#include "tests/Examples/openfhe/bgv/dot_product_128_debug/dot_product_128_debug_lib.h"

// DecryptCore not accessible from CryptoContext
// so copy from @openfhe//src/pke/lib/schemerns/rns-pke.cpp
DCRTPoly DecryptCore(const std::vector<DCRTPoly>& cv,
const PrivateKey<DCRTPoly> privateKey) {
const DCRTPoly& s = privateKey->GetPrivateElement();

size_t sizeQ = s.GetParams()->GetParams().size();
size_t sizeQl = cv[0].GetParams()->GetParams().size();

size_t diffQl = sizeQ - sizeQl;

auto scopy(s);
scopy.DropLastElements(diffQl);

DCRTPoly sPower(scopy);

DCRTPoly b(cv[0]);
b.SetFormat(Format::EVALUATION);

DCRTPoly ci;
for (size_t i = 1; i < cv.size(); i++) {
ci = cv[i];
ci.SetFormat(Format::EVALUATION);

b += sPower * ci;
sPower *= scopy;
}
return b;
}

#define OP
#define DECRYPT
#define NOISE

void __heir_debug(CryptoContextT cc, PrivateKeyT sk, CiphertextT ct,
const std::map<std::string, std::string>& debugAttrMap) {
#ifdef OP
auto isBlockArgument = debugAttrMap.at("asm.is_block_arg");
if (isBlockArgument == "1") {
std::cout << "Input" << std::endl;
} else {
std::cout << debugAttrMap.at("asm.op_name") << std::endl;
}
#endif

#ifdef DECRYPT
PlaintextT ptxt;
cc->Decrypt(sk, ct, &ptxt);
ptxt->SetLength(std::stod(debugAttrMap.at("message.size")));
std::cout << " " << ptxt << std::endl;
#endif

#ifdef NOISE
auto cv = ct->GetElements();
size_t sizeQl = cv[0].GetParams()->GetParams().size();

auto b = DecryptCore(cv, sk);
b.SetFormat(Format::COEFFICIENT);

double noise = (log2(b.Norm()));

double logQ = 0;
std::vector<double> logqi_v;
for (usint i = 0; i < sizeQl; i++) {
double logqi =
log2(cv[0].GetParams()->GetParams()[i]->GetModulus().ConvertToInt());
logqi_v.push_back(logqi);
logQ += logqi;
}

std::cout << " cv " << cv.size() << " Ql " << sizeQl << " logQ: " << logQ
<< " logqi: " << logqi_v << " budget " << logQ - noise - 1
<< " noise: " << noise << std::endl;

// print the predicted bound by analysis
if (debugAttrMap.find("noise.bound") != debugAttrMap.end()) {
double noiseBound = std::stod(debugAttrMap.at("noise.bound"));

std::cout << " noise bound: " << noiseBound
<< " gap: " << noiseBound - noise << std::endl;
}
#endif
}

namespace mlir {
namespace heir {
namespace openfhe {

int32_t DotProductPlaintext(std::vector<int32_t> arg0,
std::vector<int32_t> arg1) {
int32_t dot_product = 0;
for (int i = 0; i < arg0.size(); ++i) {
dot_product += arg0[i] * arg1[i];
}
return dot_product;
}

TEST(DotProduct128Test, RunTest) {
auto cryptoContext = dot_product__generate_crypto_context();
auto keyPair = cryptoContext->KeyGen();
auto publicKey = keyPair.publicKey;
auto secretKey = keyPair.secretKey;
cryptoContext =
dot_product__configure_crypto_context(cryptoContext, secretKey);
std::cout << *cryptoContext->GetCryptoParameters() << std::endl;

std::vector<int32_t> arg0;
std::vector<int32_t> arg1;
for (int i = 0; i < 128; ++i) {
arg0.push_back(22);
arg1.push_back((22));
}

int32_t expected = DotProductPlaintext(arg0, arg1);

auto arg0Encrypted =
dot_product__encrypt__arg0(cryptoContext, arg0, publicKey);
auto arg1Encrypted =
dot_product__encrypt__arg1(cryptoContext, arg1, publicKey);
auto outputEncrypted =
dot_product(cryptoContext, secretKey, arg0Encrypted, arg1Encrypted);
auto actual =
dot_product__decrypt__result0(cryptoContext, outputEncrypted, secretKey);

EXPECT_EQ(expected, actual);
}

} // namespace openfhe
} // namespace heir
} // namespace mlir
Loading