Skip to content

Commit

Permalink
WIP #15: Add tests for comparisons and the ternary operator
Browse files Browse the repository at this point in the history
Co-authored-by: Dimitris Mouris <[email protected]>
  • Loading branch information
cgouert and jimouris committed Feb 10, 2022
1 parent 0f66479 commit 511dba8
Show file tree
Hide file tree
Showing 21 changed files with 312 additions and 28 deletions.
32 changes: 32 additions & 0 deletions .circleci/test_HElib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ make
diff <(head -n -1 ../test/resources/tests/batching_HElib.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/cmp.t2 --HElib --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/helib-bfv-cmp-int-insecure.config
cp ./src/test/resources/tests/cmp.cpp ./src/HElib/compiled/test.cpp
cd ./src/HElib
make
./bin/test.out > ../test/resources/tests/cmp_HElib.log
diff <(head -n -1 ../test/resources/tests/cmp_HElib.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/cmp.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --HElib --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/helib-bfv-cmp-int-insecure.config
cp ./src/test/resources/tests/ternary.cpp ./src/HElib/compiled/test.cpp
cd ./src/HElib
make
./bin/test.out > ../test/resources/tests/ternary_HElib.log
diff <(head -n -1 ../test/resources/tests/ternary_HElib.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary.res
cd ../..

# Binary Domain Tests
java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/shift.t2 --HElib --w 6
cp ./src/test/resources/tests/shift.cpp ./src/HElib/compiled/test.cpp
Expand All @@ -40,3 +56,19 @@ make
./bin/test.out > ../test/resources/tests/batching_w5_HElib.log
diff <(head -n -1 ../test/resources/tests/batching_w5_HElib.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/cmp.t2 --HElib --w 5
cp ./src/test/resources/tests/cmp.cpp ./src/HElib/compiled/test.cpp
cd ./src/HElib
make
./bin/test.out > ../test/resources/tests/cmp_w5_HElib.log
diff <(head -n -1 ../test/resources/tests/cmp_w5_HElib.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/cmp_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --HElib --w 6
cp ./src/test/resources/tests/ternary.cpp ./src/HElib/compiled/test.cpp
cd ./src/HElib
make
./bin/test.out > ../test/resources/tests/ternary_w6_HElib.log
diff <(head -n -1 ../test/resources/tests/ternary_w6_HElib.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary_w6.res
cd ../..
32 changes: 32 additions & 0 deletions .circleci/test_Lattigo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ make
diff <(head -n -1 ../test/resources/tests/batching_Lattigo.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/cmp.t2 --Lattigo --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/lattigo_insecure_int_cmp.config
cp ./src/test/resources/tests/cmp.go ./src/Lattigo/compiled/test.go
cd ./src/Lattigo
make
./bin/test.out > ../test/resources/tests/cmp_Lattigo.log
diff <(head -n -1 ../test/resources/tests/cmp_Lattigo.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/cmp.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --Lattigo --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/lattigo_insecure_int_cmp.config
cp ./src/test/resources/tests/ternary.go ./src/Lattigo/compiled/test.go
cd ./src/Lattigo
make
./bin/test.out > ../test/resources/tests/ternary_Lattigo.log
diff <(head -n -1 ../test/resources/tests/ternary_Lattigo.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary.res
cd ../..

# Binary Domain Tests
java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/shift.t2 --Lattigo --w 6
cp ./src/test/resources/tests/shift.go ./src/Lattigo/compiled/test.go
Expand All @@ -46,3 +62,19 @@ make
./bin/test.out > ../test/resources/tests/batching_w5_Lattigo.log
diff <(head -n -1 ../test/resources/tests/batching_w5_Lattigo.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/cmp.t2 --Lattigo --w 5 --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/lattigo_insecure_int_cmp.config
cp ./src/test/resources/tests/cmp.go ./src/Lattigo/compiled/test.go
cd ./src/Lattigo
make
./bin/test.out > ../test/resources/tests/cmp_w5_Lattigo.log
diff <(head -n -1 ../test/resources/tests/cmp_w5_Lattigo.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/cmp_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --Lattigo --w 6 --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/lattigo-128-more-levels.config
cp ./src/test/resources/tests/ternary.go ./src/Lattigo/compiled/test.go
cd ./src/Lattigo
make
./bin/test.out > ../test/resources/tests/ternary_w6_Lattigo.log
diff <(head -n -1 ../test/resources/tests/ternary_w6_Lattigo.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary_w6.res
cd ../..
17 changes: 17 additions & 0 deletions .circleci/test_PALISADE.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ make
diff <(head -n -1 ../test/resources/tests/batching_PALISADE.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --PALISADE --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/palisade-bfv-depth-20-int.config
cp ./src/test/resources/tests/ternary.cpp ./src/PALISADE/compiled/test.cpp
cd ./src/PALISADE
make
./bin/test.out > ../test/resources/tests/ternary_PALISADE.log
diff <(head -n -1 ../test/resources/tests/ternary_PALISADE.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary.res
cd ../..


# Binary Domain Tests
java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/shift.t2 --PALISADE --w 6
cp ./src/test/resources/tests/shift.cpp ./src/PALISADE/compiled/test.cpp
Expand All @@ -40,3 +49,11 @@ make
./bin/test.out > ../test/resources/tests/batching_w5_PALISADE.log
diff <(head -n -1 ../test/resources/tests/batching_w5_PALISADE.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --PALISADE --w 6 --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/palisade-bfv-depth-20-bin.config
cp ./src/test/resources/tests/ternary.cpp ./src/PALISADE/compiled/test.cpp
cd ./src/PALISADE
make
./bin/test.out > ../test/resources/tests/ternary_w6_PALISADE.log
diff <(head -n -1 ../test/resources/tests/ternary_w6_PALISADE.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary_w6.res
cd ../..
32 changes: 32 additions & 0 deletions .circleci/test_SEAL.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ make
diff <(head -n -1 ../test/resources/tests/batching_SEAL.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/cmp.t2 --SEAL --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/seal-bfv-comparisons.config
cp ./src/test/resources/tests/cmp.cpp ./src/SEAL/compiled/test.cpp
cd ./src/SEAL
make
./bin/test.out > ../test/resources/tests/cmp_SEAL.log
diff <(head -n -1 ../test/resources/tests/cmp_SEAL.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/cmp.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --SEAL --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/seal-bfv-comparisons.config
cp ./src/test/resources/tests/ternary.cpp ./src/SEAL/compiled/test.cpp
cd ./src/SEAL
make
./bin/test.out > ../test/resources/tests/ternary_SEAL.log
diff <(head -n -1 ../test/resources/tests/ternary_SEAL.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary.res
cd ../..

# Binary Domain Tests
java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/shift.t2 --SEAL --w 6
cp ./src/test/resources/tests/shift.cpp ./src/SEAL/compiled/test.cpp
Expand All @@ -40,3 +56,19 @@ make
./bin/test.out > ../test/resources/tests/batching_w5_SEAL.log
diff <(head -n -1 ../test/resources/tests/batching_w5_SEAL.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/cmp.t2 --SEAL --w 5 --config src/main/java/org/twc/terminator/t2dsl_compiler/configs/seal-bfv-bin-comparisons-insecure.config
cp ./src/test/resources/tests/cmp.cpp ./src/SEAL/compiled/test.cpp
cd ./src/SEAL
make
./bin/test.out > ../test/resources/tests/cmp_w5_SEAL.log
diff <(head -n -1 ../test/resources/tests/cmp_w5_SEAL.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/cmp_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --SEAL --w 6
cp ./src/test/resources/tests/ternary.cpp ./src/SEAL/compiled/test.cpp
cd ./src/SEAL
make
./bin/test.out > ../test/resources/tests/ternary_w6_SEAL.log
diff <(head -n -1 ../test/resources/tests/ternary_w6_SEAL.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary_w6.res
cd ../..
24 changes: 24 additions & 0 deletions .circleci/test_TFHE.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,27 @@ make
./bin/test.out > ../test/resources/tests/batching_w5_TFHE.log
diff <(head -n -1 ../test/resources/tests/batching_w5_TFHE.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/batching_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/cmp.t2 --TFHE --w 5
cp ./src/test/resources/tests/cmp.cpp ./src/TFHE/compiled/test.cpp
cd ./src/TFHE
make
./bin/test.out > ../test/resources/tests/cmp_TFHE.log
diff <(head -n -1 ../test/resources/tests/cmp_TFHE.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/cmp.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/cmp.t2 --TFHE --w 5 --printbin
cp ./src/test/resources/tests/cmp.cpp ./src/TFHE/compiled/test.cpp
cd ./src/TFHE
make
./bin/test.out > ../test/resources/tests/cmp_w5_TFHE.log
diff <(head -n -1 ../test/resources/tests/cmp_w5_TFHE.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/cmp_w5.res
cd ../..

java -jar target/terminator-compiler-1.0.jar src/test/resources/tests/ternary.t2 --TFHE --w 6
cp ./src/test/resources/tests/ternary.cpp ./src/TFHE/compiled/test.cpp
cd ./src/TFHE
make
./bin/test.out > ../test/resources/tests/ternary_TFHE.log
diff <(head -n -1 ../test/resources/tests/ternary_TFHE.log | awk '{$1=$1};1' | cut -d ' ' -f 3-) ../test/resources/tests/ternary.res
cd ../..
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void append_keygen() {
append_idx("rlk := kgen.GenRelinearizationKey(clientSk, 1)\n");
append_idx("evaluator := bfv.NewEvaluator(params, rlwe.EvaluationKey{Rlk: rlk})\n");
append_idx("funits.FunitsInit(&encryptorPk, &encoder, &evaluator, " +
"&params, int(paramDef.T), slots, " + this.word_sz_ + ")\n");
"&params, int(paramDef.T), slots, word_sz)\n");
append_idx("ptxt := bfv.NewPlaintext(params)\n");
append_idx("tmp := make([]int64, slots)\n");
append_idx("encoder.EncodeInt(tmp, ptxt)\n\n");
Expand Down Expand Up @@ -180,6 +180,7 @@ public Var_t visit(MainClass n) throws Exception {
append_idx(")\n\n");
append_idx("func main() {\n");
this.indent_ = 2;
this.append_idx("word_sz := " + this.word_sz_ + "\n");
if (!read_keygen_from_file()) {
append_keygen();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void append_keygen() {
append_idx("size_t plaintext_modulus = 65537;\n");
append_idx("CryptoContext<DCRTPoly> cc = CryptoContextFactory<\n");
append_idx(" DCRTPoly>::genCryptoContextBFVrns(plaintext_modulus,\n"); // BFV
append_idx(" securityLevel, sigma, 0, depth, 0, OPTIMIZED, depth);\n");
append_idx(" securityLevel, sigma, 0, depth, 0, OPTIMIZED, 2);\n");
append_idx("cc->Enable(ENCRYPTION);\n");
append_idx("cc->Enable(SHE);\n");
// append_idx("cc->Enable(LEVELEDSHE);\n"); // for BGV
Expand Down Expand Up @@ -107,6 +107,9 @@ public Var_t visit(MainClass n) throws Exception {
append_idx("using namespace std;\n\n");
append_idx("int main(void) {\n");
this.indent_ = 2;
if (this.is_binary_) {
append_idx("size_t word_sz = " + this.word_sz_ + ";\n");
}
if (!read_keygen_from_file()) {
append_keygen();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public Var_t visit(MainClass n) throws Exception {
append_idx("using namespace std;\n\n");
append_idx("int main(void) {\n");
this.indent_ = 2;
append_idx("size_t word_sz = " + this.word_sz_+ ";\n");
if (!read_keygen_from_file()) {
append_keygen();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
unsigned long p = 19, m = 16109, r = 1, bits = 250;
unsigned long c = 2;
Context context = helib::ContextBuilder<helib::BGV>()
.m(m).p(p).r(r).bits(bits).c(c).build();
SecKey secret_key(context);
secret_key.GenSecKey();
addSome1DMatrices(secret_key);
PubKey& public_key = secret_key;
const EncryptedArray& ea = context.getEA();
long slots = ea.size();

Ptxt<helib::BGV> tmp(context);
Ctxt scratch(public_key);

Ctxt tmp_(public_key);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
unsigned long p = 19, m = 4097, r = 1, bits = 500;
unsigned long c = 2;
Context context = helib::ContextBuilder<helib::BGV>()
.m(m).p(p).r(r).bits(bits).c(c).build();
SecKey secret_key(context);
secret_key.GenSecKey();
addSome1DMatrices(secret_key);
PubKey& public_key = secret_key;
const EncryptedArray& ea = context.getEA();
long slots = ea.size();

Ptxt<helib::BGV> tmp(context);
Ctxt scratch(public_key);

Ctxt tmp_(public_key);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
paramDef := bfv.PN15QP880
paramDef.T = 0x3ee0001
params, err := bfv.NewParametersFromLiteral(paramDef)
slots := 2048
if err != nil {
panic(err)
}
encoder := bfv.NewEncoder(params)
kgen := bfv.NewKeyGenerator(params)
clientSk, clientPk := kgen.GenKeyPair()
encryptorPk := bfv.NewEncryptor(params, clientPk)
encryptorSk := bfv.NewEncryptor(params, clientSk)
decryptor := bfv.NewDecryptor(params, clientSk)
rlk := kgen.GenRelinearizationKey(clientSk, 1)
evaluator := bfv.NewEvaluator(params, rlwe.EvaluationKey{Rlk: rlk})
funits.FunitsInit(&encryptorPk, &encoder, &evaluator, &params, int(paramDef.T), slots, word_sz)
ptxt := bfv.NewPlaintext(params)
tmp := make([]int64, slots)
encoder.EncodeInt(tmp, ptxt)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
customParams := bfv.ParametersLiteral{
LogN: 7,
T: 257,
Q: []uint64{0x7ffffffffe70001, 0x7ffffffffe10001, 0x7ffffffffcc0001, // 59 + 59 + 59 bits
0x400000000270001, 0x400000000350001, 0x400000000360001, // 58 + 58 + 58 bits
0x3ffffffffc10001, 0x3ffffffffbe0001, 0x3ffffffffbd0001, // 58 + 58 + 58 bits
0x4000000004d0001, 0x400000000570001, 0x400000000660001}, // 58 + 58 + 58 bits
P: []uint64{0x7ffffffffb4001},
Sigma: rlwe.DefaultSigma,
}
paramDef := customParams
params, err := bfv.NewParametersFromLiteral(paramDef)
slots := 10
if err != nil {
panic(err)
}
encoder := bfv.NewEncoder(params)
kgen := bfv.NewKeyGenerator(params)
clientSk, clientPk := kgen.GenKeyPair()
encryptorPk := bfv.NewEncryptor(params, clientPk)
encryptorSk := bfv.NewEncryptor(params, clientSk)
decryptor := bfv.NewDecryptor(params, clientSk)
rlk := kgen.GenRelinearizationKey(clientSk, 1)
evaluator := bfv.NewEvaluator(params, rlwe.EvaluationKey{Rlk: rlk})
funits.FunitsInit(&encryptorPk, &encoder, &evaluator, &params, int(paramDef.T), slots, word_sz)
ptxt := bfv.NewPlaintext(params)
tmp := make([]int64, slots)
encoder.EncodeInt(tmp, ptxt)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
uint32_t depth = 20;
double sigma = 3.2;
SecurityLevel securityLevel = HEStd_128_classic;
size_t plaintext_modulus = 65537;
CryptoContext<DCRTPoly> cc = CryptoContextFactory<
DCRTPoly>::genCryptoContextBFVrns(plaintext_modulus,
securityLevel, sigma, 0, depth, 0, OPTIMIZED, 2);
cc->Enable(ENCRYPTION);
cc->Enable(SHE);
auto keyPair = cc->KeyGen();
cc->EvalMultKeyGen(keyPair.secretKey);
size_t slots(cc->GetRingDimension());
vector<int64_t> tmp_vec_(slots);
Plaintext tmp;
vector<vector<int64_t>> tmp_bin_vec_(word_sz, vector<int64_t>(slots, 0));
vector<Ciphertext<DCRTPoly>> tmp_(word_sz);

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
uint32_t depth = 20;
double sigma = 3.2;
SecurityLevel securityLevel = HEStd_128_classic;
size_t plaintext_modulus = 65537;
CryptoContext<DCRTPoly> cc = CryptoContextFactory<
DCRTPoly>::genCryptoContextBFVrns(plaintext_modulus,
securityLevel, sigma, 0, depth, 0, OPTIMIZED, 2);
cc->Enable(ENCRYPTION);
cc->Enable(SHE);
auto keyPair = cc->KeyGen();
cc->EvalMultKeyGen(keyPair.secretKey);
size_t slots(cc->GetRingDimension());
vector<int64_t> tmp_vec_(slots);
Plaintext tmp;
Ciphertext<DCRTPoly> tmp_;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
size_t poly_modulus_degree = 1024;
auto p_mod = PlainModulus::Batching(poly_modulus_degree, 20);
size_t plaintext_modulus = (size_t) p_mod.value();
EncryptionParameters parms(scheme_type::bfv);
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::Create(poly_modulus_degree, {50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50}));
parms.set_plain_modulus(p_mod);
SEALContext context(parms, true, sec_level_type::none);
KeyGenerator keygen(context);
SecretKey secret_key = keygen.secret_key();
PublicKey public_key;
RelinKeys relin_keys;
keygen.create_public_key(public_key);
keygen.create_relin_keys(relin_keys);
Encryptor encryptor(context, public_key);
Evaluator evaluator(context);
Decryptor decryptor(context, secret_key);
BatchEncoder batch_encoder(context);
size_t slots = poly_modulus_degree/2;
Plaintext tmp;
vector<Ciphertext> tmp_(word_sz);
11 changes: 11 additions & 0 deletions src/test/resources/tests/cmp.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
0
1
2
3
4
5
6
6
7
8
8
Loading

0 comments on commit 511dba8

Please sign in to comment.