From a1665c2ddc307e96855d89eb39adcc738a21d996 Mon Sep 17 00:00:00 2001 From: "hilder.vitor" Date: Fri, 11 Mar 2022 10:48:48 +0100 Subject: [PATCH] NTRU scheme: homomorphic XOR gate implemented --- include/ntruhe.h | 11 ++++++++++- src/ntruhe.cpp | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/ntruhe.h b/include/ntruhe.h index 1bd85a8..b822b27 100644 --- a/include/ntruhe.h +++ b/include/ntruhe.h @@ -164,6 +164,15 @@ class SchemeNTRU * @param[in] ct_2 encryption of the second input bit */ void or_gate(Ctxt_NTRU& ct_res, const Ctxt_NTRU& ct1, const Ctxt_NTRU& ct2) const; + + /** + * Computes the XOR gate of two given ciphertexts ct1 and ct2 + * @param[out] ct_res encryptions of the outuput of the XOR gate + * @param[in] ct_1 encryption of the first input bit + * @param[in] ct_2 encryption of the second input bit + */ + void xor_gate(Ctxt_NTRU& ct_res, const Ctxt_NTRU& ct1, const Ctxt_NTRU& ct2) const; + }; -#endif \ No newline at end of file +#endif diff --git a/src/ntruhe.cpp b/src/ntruhe.cpp index 012536a..5e53391 100644 --- a/src/ntruhe.cpp +++ b/src/ntruhe.cpp @@ -282,4 +282,18 @@ void SchemeNTRU::or_gate(Ctxt_NTRU& ct_res, const Ctxt_NTRU& ct1, const Ctxt_NTR ct_res = ct_or_const - ct1 - ct2; bootstrap(ct_res); //cout << "OR: " << float(clock()-start)/CLOCKS_PER_SEC << endl; -} \ No newline at end of file +} + +void SchemeNTRU::xor_gate(Ctxt_NTRU& ct_res, const Ctxt_NTRU& ct1, const Ctxt_NTRU& ct2) const +{ + if (ct_res.data.size() != parNTRU.n) + ct_res.data = vector(parNTRU.n); + for (size_t i = 0; i < parNTRU.n; i++){ + ct_res.data[i] = 2*(ct1.data[i] + ct2.data[i]); + ct_res.data[i] = parNTRU.mod_q_base(ct_res.data[i]); // res = 2*(ct1 + ct2) % q + } + + bootstrap(ct_res); +} + +