Skip to content

Commit

Permalink
[update] ckks example
Browse files Browse the repository at this point in the history
  • Loading branch information
ppppbamzy committed Oct 24, 2022
1 parent 5677754 commit fc5e6dd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,23 @@ int main() {
int precision_bits = 30;
auto params = ckks::create_params(4096, precision_bits);
CkksSk sk(params);
auto relin_key = get_relin_key(sk);
auto relin_key = get_relin_key(sk, params.additional_mod);

auto pt = ckks::encode(1, params);
auto ct_sum = ckks::encrypt(pt, sk);
for (int i = 2; i <= 100000; i++) {
CkksCt ct_sum;
for (int i = 1; i <= 100000; i++) {
auto pt = ckks::encode(1.0 / i, params);
auto ct = ckks::encrypt(pt, sk);
auto ct_squared = ckks::mult(ct, ct, relin_key);
ct_sum = ckks::add(ct_sum, ct_squared);

if (i == 1) {
ct_sum = ct_squared;
} else {
ct_sum = ckks::add(ct_sum, ct_squared);
}
}

double sum = ckks::decode(ckks::decrypt(ct_sum, sk));
std::cout << "(" << sum << ", "
<< M_PI * M_PI / 6 << ")" << std::end;
std::cout << "(" << sum << ", " << M_PI * M_PI / 6 << ")" << std::endl;
}

```
Expand Down
13 changes: 9 additions & 4 deletions examples/ckks_example.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ckks/ckks.h"
#include <cmath>
#include <iostream>
#include <numeric>

using namespace hehub;

Expand All @@ -10,13 +11,17 @@ int main() {
CkksSk sk(params);
auto relin_key = get_relin_key(sk, params.additional_mod);

auto pt = ckks::encode(1, params);
auto ct_sum = ckks::encrypt(pt, sk);
for (int i = 2; i <= 100000; i++) {
CkksCt ct_sum;
for (int i = 1; i <= 100000; i++) {
auto pt = ckks::encode(1.0 / i, params);
auto ct = ckks::encrypt(pt, sk);
auto ct_squared = ckks::mult(ct, ct, relin_key);
ct_sum = ckks::add(ct_sum, ct_squared);

if (i == 1) {
ct_sum = ct_squared;
} else {
ct_sum = ckks::add(ct_sum, ct_squared);
}
}

double sum = ckks::decode(ckks::decrypt(ct_sum, sk));
Expand Down
1 change: 1 addition & 0 deletions src/fhe/bgv/basics.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "bgv.h"
#include "common/mod_arith.h"
#include "common/ntt.h"
#include "common/rns_transform.h"
#include <algorithm>
#include <cmath>
Expand Down
1 change: 1 addition & 0 deletions src/fhe/primitives/keys.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "keys.h"
#include "common/mod_arith.h"
#include "common/ntt.h"
#include "common/rns_transform.h"

namespace hehub {
Expand Down
1 change: 1 addition & 0 deletions tests/bgv_t.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "catch2/catch.hpp"
#include "common/mod_arith.h"
#include "common/ntt.h"
#include "common/sampling.h"
#include "bgv/bgv.h"
#include <iostream>
Expand Down
1 change: 1 addition & 0 deletions tests/ckks_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ckks/ckks.h"
#include "common/bigint.h"
#include "common/mod_arith.h"
#include "common/ntt.h"
#include "common/permutation.h"
#include "common/sampling.h"
#include <type_traits>
Expand Down

0 comments on commit fc5e6dd

Please sign in to comment.