Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secret to BGV to OpenFHE e2e pipeline #624

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

j2kun
Copy link
Collaborator

@j2kun j2kun commented Apr 17, 2024

This PR runs an end-to-end example (simple_sum) through the heir-simd-vectorizer pipeline, with the new bgv-add-client-interface, down to OpenFHE codegen, and runs the generated code in a test.

Deficiencies (still need to file TODOs):

  • OpenFHE's MakePackedPlaintext will zero-pad to the ring degree N, and this breaks the rotate-and-reduce trick, so instead I have to manually repeat the coefficients cyclically until it fills the whole ciphertext. I'd like to add this to the encode helper, and it suggests a bunch more work to do if we have an input cleartext tensor that is not divisible by the OpenFHE's ring degree (rotate-and-reduce tricks won't work in that case, or will have to be tweaked to account for the true underlying ring degree).
  • Support non-tensor input types to the encode codegen
  • move int64_t casting to a higher-level lowering (say, from BGV to OpenFHE)

The output code generated for simple_sum

#include "src/pke/include/openfhe.h" // from @openfhe

using namespace lbcrypto;
using CiphertextT = ConstCiphertext<DCRTPoly>;
using CryptoContextT = CryptoContext<DCRTPoly>;
using EvalKeyT = EvalKey<DCRTPoly>;
using PlaintextT = Plaintext;
using PrivateKeyT = PrivateKey<DCRTPoly>;
using PublicKeyT = PublicKey<DCRTPoly>;

CiphertextT simple_sum(CryptoContextT v0, CiphertextT v1) {
  size_t v2 = 1;
  size_t v3 = 2;
  size_t v4 = 4;
  size_t v5 = 8;
  size_t v6 = 16;
  size_t v7 = 31;
  int64_t v8 = static_cast<int64_t>(v6);
  const auto& v9 = v0->EvalRotate(v1, v8);
  const auto& v10 = v0->EvalAdd(v1, v9);
  int64_t v11 = static_cast<int64_t>(v5);
  const auto& v12 = v0->EvalRotate(v10, v11);
  const auto& v13 = v0->EvalAdd(v10, v12);
  int64_t v14 = static_cast<int64_t>(v4);
  const auto& v15 = v0->EvalRotate(v13, v14);
  const auto& v16 = v0->EvalAdd(v13, v15);
  int64_t v17 = static_cast<int64_t>(v3);
  const auto& v18 = v0->EvalRotate(v16, v17);
  const auto& v19 = v0->EvalAdd(v16, v18);
  int64_t v20 = static_cast<int64_t>(v2);
  const auto& v21 = v0->EvalRotate(v19, v20);
  const auto& v22 = v0->EvalAdd(v19, v21);
  std::vector<int16_t> v23 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
  std::vector<int64_t> v23_cast(std::begin(v23), std::end(v23));
  const auto& v24 = v0->MakePackedPlaintext(v23_cast);
  const auto& v25 = v0->EvalMult(v22, v24);
  int64_t v26 = static_cast<int64_t>(v7);
  const auto& v27 = v0->EvalRotate(v25, v26);
  const auto& v28 = v27;
  return v28;
}

CiphertextT simple_sum__encrypt(CryptoContextT v29, std::vector<int16_t> v30, PublicKeyT v31) {
  std::vector<int64_t> v30_cast(std::begin(v30), std::end(v30));
  const auto& v32 = v29->MakePackedPlaintext(v30_cast);
  const auto& v33 = v29->Encrypt(v31, v32);
  return v33;
}

int16_t simple_sum__decrypt(CryptoContextT v34, CiphertextT v35, PrivateKeyT v36) {
  PlaintextT v37;
  v34->Decrypt(v36, v35, &v37);
  int16_t v38 = v37->GetPackedValue()[0];
  return v38;
}

@j2kun j2kun marked this pull request as draft April 17, 2024 23:13
@j2kun
Copy link
Collaborator Author

j2kun commented Apr 24, 2024

OK this monster PR is working now, EXCEPT for one tiny detail: I can't figure out how to get the new //tests/openfhe/end_to_end:simple_sum_test to run with yosys enabled by default. It seems to run heir-opt with yosys dependencies missing by default, and to make it pass I have to run bazel test with --//:enable_yosys=0

Even if I run bazel test --//:enable_yosys=1 //tests/openfhe/end_to_end:simple_sum_test, it still complains that yosys deps/env vars are not found.

Edit: I think I understand now: the env variables set by the cc_binary build rule only apply when that target is bazel run, not when it is used in some other rule, nor when it is run from bazel-bin/. I will try to tweak that so that it can be used in all situations.

@j2kun j2kun marked this pull request as ready for review April 24, 2024 00:44
@j2kun j2kun requested a review from asraa April 24, 2024 00:44
@j2kun j2kun force-pushed the openfhe-pke branch 2 times, most recently from 4b586ba to ca04b23 Compare April 24, 2024 05:54
Copy link
Collaborator

@asraa asraa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all the fixes to the config settings and adding the compilation flag!

Sub changes:

- add public/private key types, encrypt/decrypt, and associated
  lowerings for BGV & OpenFHE
- Add heir-opt.bzl for running heir-opt as part of a bazel macro
- Improve the yosys enabling flags, env var override
- Add OpenFHE codegen for index_cast, tensor-shaped arith.constant,
  tensor types, and encrypt/decrypt/encode/decode
- Avoid unnecessary copies in generated OpenFHE code
@j2kun
Copy link
Collaborator Author

j2kun commented Apr 24, 2024

Squashed and ready to pull

@j2kun j2kun added the pull_ready Indicates whether a PR is ready to pull. The copybara worker will import for internal testing label Apr 24, 2024
@copybara-service copybara-service bot merged commit 3908365 into google:main Apr 25, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pull_ready Indicates whether a PR is ready to pull. The copybara worker will import for internal testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants