From 07fa6efac8ac2ca20f717189110ac272ef0c7c45 Mon Sep 17 00:00:00 2001 From: "hilder.vitor" Date: Fri, 11 Mar 2022 13:50:59 +0100 Subject: [PATCH] Fixed bug when on SchemeNTRU::not_gate When the same variable was passed as the first and the second argument of the not_gate, the result was always zero, i.e., after SchemeNTRU::not_gate(c, c), c was an encryption of 0 --- src/ntruhe.cpp | 3 +- test.cpp | 103 +++++++++++++++++++++++-------------------------- 2 files changed, 49 insertions(+), 57 deletions(-) diff --git a/src/ntruhe.cpp b/src/ntruhe.cpp index 4862dee..9de8576 100644 --- a/src/ntruhe.cpp +++ b/src/ntruhe.cpp @@ -298,7 +298,6 @@ void SchemeNTRU::xor_gate(Ctxt_NTRU& ct_res, const Ctxt_NTRU& ct1, const Ctxt_NT void SchemeNTRU::not_gate(Ctxt_NTRU& ct_res, const Ctxt_NTRU& ct) const { - ct_res = ct_not_const; - ct_res -= ct; + ct_res = ct_not_const - ct; } diff --git a/test.cpp b/test.cpp index 1733756..90dbbfe 100644 --- a/test.cpp +++ b/test.cpp @@ -333,53 +333,50 @@ void test_ntruhe_gate_helper(int in1, int in2, const SchemeNTRU& s, GateType g) cout << "Avg. time: " << avg_time/N_TESTS << endl; } -void test_ntru_gate(GateType g) +void test_ntru_gate(SchemeNTRU& s, GateType g) { - SchemeNTRU s; - test_ntruhe_gate_helper(0, 0, s, g); test_ntruhe_gate_helper(0, 1, s, g); test_ntruhe_gate_helper(1, 0, s, g); test_ntruhe_gate_helper(1, 1, s, g); } -void test_ntruhe_nand() +void test_ntruhe_nand(SchemeNTRU& s) { GateType g = NAND; - test_ntru_gate(g); + test_ntru_gate(s, g); cout << "NAND IS OK" << endl; } -void test_ntruhe_and() +void test_ntruhe_and(SchemeNTRU& s) { GateType g = AND; - test_ntru_gate(g); + test_ntru_gate(s, g); cout << "AND IS OK" << endl; } -void test_ntruhe_or() +void test_ntruhe_or(SchemeNTRU& s) { GateType g = OR; - test_ntru_gate(g); + test_ntru_gate(s, g); cout << "OR IS OK" << endl; } -void test_ntruhe_xor() +void test_ntruhe_xor(SchemeNTRU& s) { GateType g = XOR; - test_ntru_gate(g); + test_ntru_gate(s, g); cout << "XOR IS OK" << endl; } -void test_ntruhe_not() +void test_ntruhe_not(SchemeNTRU& s) { - SchemeNTRU s; GateType g = NOT; for(int i = 0; i < 5; i++){ @@ -391,8 +388,6 @@ void test_ntruhe_not() } - - void test_ntruhe_gate_composition_helper(SchemeNTRU& s, GateType g) { float avg_time = 0.0; @@ -414,36 +409,30 @@ void test_ntruhe_gate_composition_helper(SchemeNTRU& s, GateType g) s.nand_gate(ct_res, ct_res, ct);// ct_res should encrypt NAND(exp_out, in2) avg_time += float(clock()-start)/CLOCKS_PER_SEC; exp_out = !(exp_out & in2); // exp_out = NAND(exp_out, in2) - - //cout << "NAND output: " << output << endl; } else if (g == AND) { auto start = clock(); s.and_gate(ct_res, ct_res, ct); avg_time += float(clock()-start)/CLOCKS_PER_SEC; exp_out = (exp_out & in2); // exp_out = AND(exp_out, in2) - //cout << "AND output: " << output << endl; } else if (g == OR) { auto start = clock(); s.or_gate(ct_res, ct_res, ct); avg_time += float(clock()-start)/CLOCKS_PER_SEC; exp_out = (exp_out | in2); // exp_out = OR(exp_out, in2) - //cout << "OR output: " << output << endl; } else if (g == XOR) { auto start = clock(); s.xor_gate(ct_res, ct_res, ct); avg_time += float(clock()-start)/CLOCKS_PER_SEC; exp_out = (exp_out ^ in2); // exp_out = XOR(exp_out, in2) - //cout << "XOR output: " << output << endl; } else if (g == NOT) { auto start = clock(); s.not_gate(ct_res, ct_res); avg_time += float(clock()-start)/CLOCKS_PER_SEC; exp_out = (1 - exp_out); // exp_out = NOT(exp_out) - //cout << "NOT output: " << output << endl; } int output = s.decrypt(ct_res); @@ -452,10 +441,8 @@ void test_ntruhe_gate_composition_helper(SchemeNTRU& s, GateType g) cout << "Avg. time: " << avg_time/N_TESTS << endl; } -void test_ntruhe_composition_of_gates() +void test_ntruhe_composition_of_gates(SchemeNTRU& s) { - SchemeNTRU s; - test_ntruhe_gate_composition_helper(s, NAND); cout << "COMPOSING NAND IS OK" << endl; @@ -545,58 +532,54 @@ void test_lwehe_gate_helper(int in1, int in2, SchemeLWE& s, GateType g) cout << "Avg. time: " << avg_time/100.0 << endl; } -void test_lwe_gate(GateType g) +void test_lwe_gate(SchemeLWE& s, GateType g) { - SchemeLWE s; - test_lwehe_gate_helper(0, 0, s, g); test_lwehe_gate_helper(0, 1, s, g); test_lwehe_gate_helper(1, 0, s, g); test_lwehe_gate_helper(1, 1, s, g); } -void test_lwehe_nand() +void test_lwehe_nand(SchemeLWE& s) { GateType g = NAND; - test_lwe_gate(g); + test_lwe_gate(s, g); cout << "NAND IS OK" << endl; } -void test_lwehe_and() +void test_lwehe_and(SchemeLWE& s) { GateType g = AND; - test_lwe_gate(g); + test_lwe_gate(s, g); cout << "AND IS OK" << endl; } -void test_lwehe_or() +void test_lwehe_or(SchemeLWE& s) { GateType g = OR; - test_lwe_gate(g); + test_lwe_gate(s, g); cout << "OR IS OK" << endl; } -void test_lwehe_xor() +void test_lwehe_xor(SchemeLWE& s) { GateType g = XOR; - test_lwe_gate(g); + test_lwe_gate(s, g); cout << "XOR IS OK" << endl; } -void test_lwehe_not() +void test_lwehe_not(SchemeLWE& s) { - SchemeLWE s; GateType g = NOT; for(int i = 0; i < 4; i++){ int bit = binary_sampler(rand_engine); - cout << "bit = " << bit << endl; test_lwehe_gate_helper(bit, 0, s, g); } @@ -607,7 +590,7 @@ void test_lwehe_not() void test_lwehe_gate_composition_helper(SchemeLWE& s, GateType g) { float avg_time = 0.0; - int N_TESTS = 120; + int N_TESTS = 110; int in1, in2, exp_out; in1 = binary_sampler(rand_engine); @@ -655,6 +638,13 @@ void test_lwehe_gate_composition_helper(SchemeLWE& s, GateType g) exp_out = (exp_out ^ in2); // exp_out = XOR(exp_out, in2) //cout << "XOR output: " << output << endl; } + else if (g == NOT) { + auto start = clock(); + s.not_gate(ct_res, ct_res); + avg_time += float(clock()-start)/CLOCKS_PER_SEC; + exp_out = (1 - exp_out); // exp_out = NOT(exp_out) + } + int output = s.decrypt(ct_res); @@ -663,10 +653,8 @@ void test_lwehe_gate_composition_helper(SchemeLWE& s, GateType g) cout << "Avg. time: " << avg_time/N_TESTS << endl; } -void test_lwehe_composition_of_gates() +void test_lwehe_composition_of_gates(SchemeLWE& s) { - SchemeLWE s; - test_lwehe_gate_composition_helper(s, NAND); cout << "COMPOSING NAND IS OK" << endl; @@ -678,6 +666,9 @@ void test_lwehe_composition_of_gates() test_lwehe_gate_composition_helper(s, XOR); cout << "COMPOSING XOR IS OK" << endl; + + test_lwehe_gate_composition_helper(s, NOT); + cout << "COMPOSING NOT GATE IS OK" << endl; } @@ -690,24 +681,26 @@ int main() cout << endl; cout << "-------------------------" << endl; cout << "NTRU tests" << endl; - test_ntruhe_nand(); - test_ntruhe_and(); - test_ntruhe_or(); - test_ntruhe_xor(); - test_ntruhe_not(); - test_ntruhe_composition_of_gates(); + SchemeNTRU s_ntru; + test_ntruhe_nand(s_ntru); + test_ntruhe_and(s_ntru); + test_ntruhe_or(s_ntru); + test_ntruhe_xor(s_ntru); + test_ntruhe_not(s_ntru); + test_ntruhe_composition_of_gates(s_ntru); cout << "NTRU tests PASSED" << endl; cout << endl; cout << "-------------------------" << endl; cout << "LWE tests" << endl; - test_lwehe_not(); - test_lwehe_nand(); - test_lwehe_and(); - test_lwehe_or(); - test_lwehe_xor(); - test_lwehe_not(); - test_lwehe_composition_of_gates(); + SchemeLWE s_lwe; + test_lwehe_not(s_lwe); + test_lwehe_nand(s_lwe); + test_lwehe_and(s_lwe); + test_lwehe_or(s_lwe); + test_lwehe_xor(s_lwe); + test_lwehe_not(s_lwe); + test_lwehe_composition_of_gates(s_lwe); cout << "LWE tests PASSED" << endl; return 0;