From 7e8792ed34768f5ea30281130f6c95d2823fba64 Mon Sep 17 00:00:00 2001 From: Quentin Bourgerie Date: Wed, 14 Dec 2022 12:30:43 +0100 Subject: [PATCH] enhance: Refactor the cpp code to be more generic and easy to generate --- Makefile | 12 +++ cpp/compile_test.py | 85 ------------------ cpp/cpp/build/libtest.so | Bin 33511 -> 0 bytes cpp/cpp/build/test.o | Bin 2404 -> 0 bytes cpp/cpp/test.cpp | 52 ----------- cpp/gen_cpp.py | 139 ------------------------------ cpp/gen_header.py | 12 +++ cpp/include/concrete/curves.gen.h | 12 +++ cpp/include/concrete/curves.h | 67 ++++++++++++++ cpp/v0curves.py | 9 -- json/curves.json | 1 + verified_curves.sobj | Bin 670 -> 735 bytes verify_curves.py | 41 ++++++--- 13 files changed, 131 insertions(+), 299 deletions(-) create mode 100644 Makefile delete mode 100644 cpp/compile_test.py delete mode 100755 cpp/cpp/build/libtest.so delete mode 100644 cpp/cpp/build/test.o delete mode 100644 cpp/cpp/test.cpp delete mode 100644 cpp/gen_cpp.py create mode 100644 cpp/gen_header.py create mode 100644 cpp/include/concrete/curves.gen.h create mode 100644 cpp/include/concrete/curves.h delete mode 100644 cpp/v0curves.py create mode 100644 json/curves.json diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..d964915f3 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +CURVES_JSON_PATH=json/curves.json +CURVES_CPP_GEN_H=cpp/include/concrete/curves.gen.h + +$(CURVES_JSON_PATH): verify_curves.py + sage verify_curves.py > $@ + +$(CURVES_CPP_GEN_H): cpp/gen_header.py $(CURVES_JSON_PATH) + cat $(CURVES_JSON_PATH) | python cpp/gen_header.py > $(CURVES_CPP_GEN_H) + +generate-cpp-header: $(CURVES_CPP_GEN_H) + +.PHONY: generate-cpp-header \ No newline at end of file diff --git a/cpp/compile_test.py b/cpp/compile_test.py deleted file mode 100644 index 5fa7b2621..000000000 --- a/cpp/compile_test.py +++ /dev/null @@ -1,85 +0,0 @@ -import subprocess -from ctypes import * -import os -import numpy as np - -v0_parameters_path = "cpp" - -def compile(): - # Creating build directory - try: - os.mkdir(f"{v0_parameters_path}/build") - print("> Successfully created build/ directory") - except FileExistsError: - print("> build/ directory already exists") - # Compile the C++ source as a shared object - subprocess.run( - [ - "g++", - "-c", - "-o", - f"{v0_parameters_path}/build/test.o", - f"{v0_parameters_path}/test.cpp", - ] - ) - subprocess.run( - [ - "gcc", - "-shared", - "-o", - f"{v0_parameters_path}/build/libtest.so", - f"{v0_parameters_path}/build/test.o", - ] - ) - print("> Successfully compiled C++ source") - - -def load_library(): - # Load library in python and define argtype / restype - lib = CDLL(f"{v0_parameters_path}/build/libtest.so") - # defining the structure at python level - class v0curves(Structure): - _fields_ = [ - ("securityLevel", c_int), - ("linearTerm1", c_double), - ("linearTerm2", c_double), - ("nAlpha", c_int), - ("keyFormat", c_int), - ] - - get = lib.security_estimator - get.argtypes = [c_int, c_int] - get.restype = POINTER(v0curves) - print("> Successfully loading shared library") - - return get - - -def stringify_struct(struct): - return f"security_level: {struct.contents.securityLevel}, linear_term1: {struct.contents.linearTerm1}, linear_term2: {struct.contents.linearTerm2} , nAlpha: {struct.contents.nAlpha}, keyFormat: {struct.contents.keyFormat} " - -def check_codegen( - curves_dict -): - # compiling as shared library - compile() - # loading library - security_estimator = load_library() - # checking everything - - for security_level, key_format in curves_dict: - c_struct = security_estimator(security_level, key_format ) - python_struct = curves_dict[(security_level, key_format)] - print(f"(securityLevel, keyFormat) = ({security_level, key_format} : {stringify_struct(c_struct)} ") - assert python_struct[0] == c_struct.contents.linearTerm1, f"linearTerm1: (securityLevel, keyFormat) = ({security_level, key_format} -> (Py) {python_struct[0]} (C++) {c_struct.contents.linearTerm1})" - assert python_struct[1] == c_struct.contents.linearTerm2, f"linearTerm2: (securityLevel, keyFormat) = ({security_level, key_format} -> (Py) {python_struct[1]} (C++) {c_struct.contents.linearTerm2})" - assert python_struct[2] == c_struct.contents.nAlpha, f"nAlpha: (securityLevel, keyFormat) = ({security_level, key_format} -> (Py) {python_struct[2]} (C++) {c_struct.contents.nAlpha})" - print(curves_dict) - print("> Successfully compared C++ array with Python dictionary") - - -if __name__ == "__main__": - from v0curves import curves_dict - compile() - load_library() - check_codegen(curves_dict) diff --git a/cpp/cpp/build/libtest.so b/cpp/cpp/build/libtest.so deleted file mode 100755 index f803f868e8df1d392f7540c2a24e28751585f0ad..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 33511 zcmeI5e{54#6vyvt3tQ7cAxsz(T=^ltGZ0iTL-xkNZ8Epv8nXb&bz2J-))v-oZj4Bm zWg1vam_b0J5&vQ%nh-@p{~&CE88A_qF%nEL8<@EA<1ZFnmy~(Vd#`KPU?BR3fB3!0 zdFP!U_q=mIul@7B`@`tH2N^;rg2rSLJINA@5bdGq>=Ls{)=^YS=jJM>=9+j>Cz`^r zCy|@QY=or@x|@QAK`OQ0-Sv9xfJxVE8@IZ{B0~C&ewDJ;?+YXfrq+8SU(ZvGNo>b^ zFRMWYg(CTa|a2CkI^0G>Os%j+ujpiO% z_=QX(k<#nuraVEpv%yzO_QUI?Q?54DiR-i)?zrOjndmF-B^@&ERYRw!{Jmbk zFP^6N6}L!-^m{{TMlyfx2dAfKSMIBrM9R{VvMrl8mBc~!XkiimUB&DEZoua{$+f1C zy^16&#b3L3@5-76Pn~OJou?+~4g_-p{F=pl>Xh@j?l?oXQ$~7-{s-ja$8zloveQWS zkUm1%Mp8k-!(s^M@_#D1KQbR{JV{ynoc_5jhd#FDWuJ6?^wZ4j-Hz4dbCBpY>8WO$ zcCWFu9yTqb*ff&(p#QekH`n5RCb=@`Z>flt4S{+_!BW{A2)ezw8*^RFWa%>(k>WaP zNY_gZo7c@_E(ymgT7@|NPZr2UFhBqVKmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l z00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l00jQ` z1jfutf2_sa9lIzxPqyWZd|H@uZon>LgEkSnX&2#JHWBV>ljN7=SIMuEUnRd99x$h$ z9x{vQS|N@Onbj!Uv6jMas@d6NmOqbO6#YwtIFqi3&IrYpwkn;sNx!Tpo$0Njb6~Lu z4=xblJB!uu!2)Gp#4e(@Z6Z39CZdBvIENO5!nDWo%u<@as;EtRfa6M%LLP93f8DxfG{$C)bR%n7U(aw5}vR4q~ElB0d@?;>GWT z=PnnaR|n0u)3hJ=i0Chhu~xn-irpmNEs_XvyG`8gq=|6H*w}G?XFP9v7JN0+Cjp@LBe}_X;^r_Jnv6t@fds6`JVR$XV5iEn<>N&DiP!&tMsW( zBbPiFAOHd&00JNY0w4eaAOHd&00JNY0w4eaAOHd&00JNY0w4eaAOHd&00JNY0w4ea zAOHd&00JNY0w4eaAOHd&00JNY0w4eaAOHd&00JNY0w4eaAOHd&00JNY0{;<#>GQ-^ z?a_*)PdbB8oEBEgam(&fJX@l+Zh`)Q%F)%Df@N9ZZz!pcL=zyolYp{^VoSzC9zX} z@r#1xsnVi?zMvsmD;y^JOv7Uhbv!HSBzw<;ymJ?>k2n|Q zY;7OC+-JJ)yfU*pb4K2sWq(M^jqLO^&*>&H3;ynBOsqN)^KCstV?sxgWZ>qfV zUSyNa9dfOBX|GErRRRkI@a-8 QsC4azc^ekq*dDO|1?m2HDF6Tf diff --git a/cpp/cpp/build/test.o b/cpp/cpp/build/test.o deleted file mode 100644 index da76ef12b969456dc068f49357926271efd3712f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2404 zcmbVOUuauZ7(chEO+s|3T5ShPqZ@2PD_Q5CK6u&b+UcrMr{-aZaGE7?2J>&pZH>e~ z8ATjUp}{=}zI3pMWDNAp=0WV313^KH;-9yq+e6af+YmFn&fj z`Of#9b8mjQ{>MMJ3!w=N`sv54^x-FfiLo7XH}RW{rps1*%QmkwcSB5bfc=y*;st{V zK&kM-BjGN0o(wA!At)Y&3?jg7r0f?Z0%s_dOc?Q8BspPAHpeS)9LWw68{^b03GK6Y zDD|=tF%r?4bi&GVbG&7a=V2@%0OfZC`3+N`E%`+=sZ=KItfAEOWO&kHLO69QbwwNU z<7GNprBa!gN+3J++FUw{`fZH2!u9vC$4L~8_B)6sP%LjJlKr3*^2JgFyS z^MA$-m$qsaT~+g84AziJQf#Dq@ybTlUPSN9E$H@~(8}w;XEd$sIU&mRU82_5A!=85 z$=Z99+VPfOwAX#2-E@m~Lx^y5N2!MTFYbZ=?t46^n!}=vdo|RA{ixP=`~usw+_LTH z@9gMZUA?cXM?CiL@9Oo$<<=DIXt*Fr8{jdapPgUveXgF|Ds zP$TblQEQ=2-ut1)zMYuVr|_llb?^S2(s=6hp98=jTV( z81Iu0AKG{Ca~}*!s;ybT1;*HMVMvnp0nC{f9*9rH0FQj_iddO+pR0 z2H9oI7HYqa+Fx~x8ugU=PJYj_AN3*td%_3E&y8~cKdb+!G1eS6FeqA+PqD*+%_GAl^@*?`_nV zfn;|c*bjNdM*S=h?;C49p#Bltm4JBlhTgVOF97k12^lu(vp|Y>2#E7xz1)Dh1V`UP zPui%%#@Y|QfrL6h;CWjKi2Md-fhgI4WDGC@Ed5WwZCq25T_TQ+I^Un)H}UV7m)Pek z<|mnd&U}&i8Rnld|AhGx^Y@uon7_mP3+7)E$3~oUMdDsQ!QAP4l{gyUe4joej>50- zx%ikk3h#U$j)D8N75v)cMF8;tuRlV(C*Dp=FNAYW@gY;uLO~r%W{yUZYCe*)K5{~3 zBe_T_j!%4EsC+y+mrEFLsd(N JWEP~V{0s6y$8-Py diff --git a/cpp/cpp/test.cpp b/cpp/cpp/test.cpp deleted file mode 100644 index 6ccffe2b7..000000000 --- a/cpp/cpp/test.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include -using namespace std; - -const int num_sec_levels = 4; -const int num_key_format = 1; - -typedef struct v0curves -{ - int securityLevel; - double linearTerm1; - double linearTerm2; - int nAlpha; - int keyFormat; - - v0curves( int securityLevel_, - double linearTerm1_, - double linearTerm2_, - int nAlpha_, - int keyFormat_) - { - securityLevel = securityLevel_; - linearTerm1 = linearTerm1_; - linearTerm2 = linearTerm2_; - nAlpha = nAlpha_; - keyFormat = keyFormat_; - } - -} v0curves; - -v0curves parameters[num_sec_levels][num_key_format] = { - {v0curves(1, 4.13213, 7.123123, 1, 1)}, - {v0curves(2, 5.123123, 8.123123, 1, 2)}, - {v0curves(3, 6.123123, 9.1231223, 1, 3)}, - {v0curves(4, 10.1231, 10.123123, 1, 4)} -}; - -extern "C" v0curves *security_estimator(int securityLevel, int keyFormat) -{ - if (securityLevel == 80 ){ - return ¶meters[0][keyFormat]; - } - else if (securityLevel == 128 ){ - return ¶meters[1][keyFormat]; - } - else if (securityLevel == 192 ){ - return ¶meters[2][keyFormat]; - } - else if (securityLevel == 256 ){ - return ¶meters[3][keyFormat]; - } -} - \ No newline at end of file diff --git a/cpp/gen_cpp.py b/cpp/gen_cpp.py deleted file mode 100644 index d24b63a57..000000000 --- a/cpp/gen_cpp.py +++ /dev/null @@ -1,139 +0,0 @@ -from v0curves import curves - -# define the number of security levels in curves -num_sec_levels = len(curves) - -import_string = f""" - -#include -using namespace std;""" - - -constant_string = f""" -const int num_sec_levels = {num_sec_levels};""" - - -struct_string = """ -typedef struct v0curves -{ - int rlweDimension; - int polynomialSize; - int ciphertextModulus; - int keyFormat; - - v0curves(int rlweDimension, - int polynomialSize_, - int ciphertextModulus, - int keyFormat) - { - rlweDimension = rlweDimension_; - polynomialSize = polynomialSize_; - ciphertextModulus = ciphertextModulus_; - keyFormat = keyFormat_; - } - -} v0curves;""" - - -table_string = """ -v0curves parameters[num_sec_levels] = """ - - -get_string = """ -extern "C" int security_estimator(int securityLevel, int keyFormat) -{ - return ¶meters[securityLevel][keyFormat]; -}""" - - -def constructor(rlweDimension, polynomialSize, ciphertextModulus, keyFormat): - return f"v0curves({rlweDimension}, {polynomialSize}, {ciphertextModulus}, {keyFormat})," - - -def fill_parameters( - # Return a string with parameters for the c++ array initialization - polynomial_size_results, - rlwe_dimension_results, - ciphertext_modulus_results, - key_format_results -): - parameters = "{}{{".format(table_string) - for security_level in range(num_sec_levels): - print(security_level) - line = "{" - - try: - line += constructor( - int(polynomial_size_results[security_level]), - int(rlwe_dimension_results[security_level]), - int(ciphertext_modulus_results[security_level]), - int(key_format_results[security_level]), - ) - except ValueError: - line += constructor(0, 0, 0, 0) - line = line[:-1] - line += "}," - parameters += line - parameters = parameters[:-1] - parameters += "} ;" - return parameters - - -def codegen( - polynomial_size_results, - rlwe_dimension_results, - ciphertext_modulus_results, - key_format_results, -): - # Generate the C++ file as a string - code = f""" - {import_string} - {constant_string} - {struct_string} - {fill_parameters( - polynomial_size_results, - rlwe_dimension_results, - ciphertext_modulus_results, - key_format_results - )} - {get_string} - """ - return code - - -def write_codegen( - polynomial_size_results, - rlwe_dimension_results, - ciphertext_modulus_results, - key_format_results, -): - # Create the c++ source - code = codegen( - polynomial_size_results, - rlwe_dimension_results, - ciphertext_modulus_results, - key_format_results - ) - # TODO: insert correct filename here with a path - with open(f"test.cpp", "w") as f: - f.write(code) - print("> Successfully wrote C++ source to disk") - - -def main_codegen(): - # finding parameters for V0 - ( - polynomial_size_results, - rlwe_dimension_results, - ciphertext_modulus_results, - key_format_results, - - ) = main_optimization_v0() - - # code generation - write_codegen( - polynomial_size_results, - rlwe_dimension_results, - ciphertext_modulus_results, - key_format_results - ) diff --git a/cpp/gen_header.py b/cpp/gen_header.py new file mode 100644 index 000000000..38b0a5b73 --- /dev/null +++ b/cpp/gen_header.py @@ -0,0 +1,12 @@ +import sys, json; + +def print_curve(data): + print(f'\tSecurityCurve({data["security_level"]},{data["slope"]}, {data["bias"]}, {data["minimal_lwe_dimension"]}, KeyFormat::BINARY),') + +def print_cpp_curves_declaration(datas): + print("std::vector curves {") + for data in datas: + print_curve(data) + print("}\n") + +print_cpp_curves_declaration(json.load(sys.stdin)) \ No newline at end of file diff --git a/cpp/include/concrete/curves.gen.h b/cpp/include/concrete/curves.gen.h new file mode 100644 index 000000000..05ff21944 --- /dev/null +++ b/cpp/include/concrete/curves.gen.h @@ -0,0 +1,12 @@ +std::vector curves { + SecurityCurve(80,-0.0404263311936459, 1.660978864143658, 450, KeyFormat::BINARY), + SecurityCurve(96,-0.03414780360867054, 2.0173102586603733, 450, KeyFormat::BINARY), + SecurityCurve(112,-0.02967013708113588, 2.16246371408387, 450, KeyFormat::BINARY), + SecurityCurve(128,-0.026405028765226296, 2.482642269104389, 450, KeyFormat::BINARY), + SecurityCurve(144,-0.023821437305989134, 2.7177789440636673, 450, KeyFormat::BINARY), + SecurityCurve(160,-0.021743582187160406, 2.9388105484933504, 498, KeyFormat::BINARY), + SecurityCurve(176,-0.019904056582117705, 2.8161252801542673, 551, KeyFormat::BINARY), + SecurityCurve(192,-0.018610403247590064, 3.2996236848399008, 606, KeyFormat::BINARY), + SecurityCurve(256,-0.014606812351714961, 3.8493629234693145, 826, KeyFormat::BINARY), +} + diff --git a/cpp/include/concrete/curves.h b/cpp/include/concrete/curves.h new file mode 100644 index 000000000..0b73ff750 --- /dev/null +++ b/cpp/include/concrete/curves.h @@ -0,0 +1,67 @@ +// Part of the Concrete Compiler Project, under the BSD3 License with Zama +// Exceptions. See +// https://github.com/zama-ai/concrete-compiler-internal/blob/main/LICENSE.txt +// for license information. + +#ifndef CONCRETELANG_SUPPORT_V0CURVES_H_ +#define CONCRETELANG_SUPPORT_V0CURVES_H_ + +#include +#include +#include +#include + +namespace concrete { + +enum KeyFormat { + BINARY, +}; + +/// @brief SecurityCurves represents a curves of security +struct SecurityCurve { + /// @brief Number of bits of security + int bits; + /// @brief A term of the curve + double slope; + /// @brief A term of the curve + double bias; + /// @brief The minimal secure n + int minimalLweDimension; + /// @brief The format of the key + int keyFormat; + + SecurityCurve() = delete; + + SecurityCurve(int bits, double slope, double bias, int minimalLweDimension, + KeyFormat keyFormat) + : bits(bits), slope(slope), bias(bias), + minimalLweDimension(minimalLweDimension), keyFormat(keyFormat) {} + + /// @brief Returns the secure encryption variance for glwe ciphertexts + /// @param glweDimension The dimension of the glwe + /// @param polynomialSize The size of the polynom of the glwe + /// @param logQ The log of q + /// @return The secure encryption variances + double getVariance(int glweDimension, int polynomialSize, int logQ) { + auto a = std::pow( + 2, (slope * glweDimension * polynomialSize + bias) * 2); + auto b = std::pow(2, -2 * (logQ - 2)); + return a > b ? a : b; + } +}; + +#include "curves.gen.h" + +/// @brief Return the security curve for a given level and a key format. +/// @param bitsOfSecurity The number of bits of security +/// @param keyFormat The format of the key +/// @return The security curve or nullptr if the curve is not found. +SecurityCurve *getSecurtityCurve(int bitsOfSecurity, KeyFormat keyFormat) { + std::find_if(curves.begin(), curves.end(), [&](SecurityCurve c) { + return c.bits == bitsOfSecurity && c.keyFormat == keyFormat; + }); +} + +} // namespace concrete + +#endif diff --git a/cpp/v0curves.py b/cpp/v0curves.py deleted file mode 100644 index f9d30a71a..000000000 --- a/cpp/v0curves.py +++ /dev/null @@ -1,9 +0,0 @@ - -curves = [ - (80, -0.04047677865612648, 1.1433465085639063, 160, 0), - (128, -0.026374888765705498, 2.012143923330495, 256, 0), - (192, -0.018504919354426233, 2.6634073426215843, 384, 0), - (256, -0.014327640360322604, 2.899270827311091, 781, 0), -] - -curves_dict = {(tuple[0], tuple[-1]): tuple[1:4] for tuple in curves} diff --git a/json/curves.json b/json/curves.json new file mode 100644 index 000000000..4c621f7ac --- /dev/null +++ b/json/curves.json @@ -0,0 +1 @@ +[{"slope": -0.0404263311936459, "bias": 1.660978864143658, "security_level": 80, "minimal_lwe_dimension": 450}, {"slope": -0.03414780360867054, "bias": 2.0173102586603733, "security_level": 96, "minimal_lwe_dimension": 450}, {"slope": -0.02967013708113588, "bias": 2.16246371408387, "security_level": 112, "minimal_lwe_dimension": 450}, {"slope": -0.026405028765226296, "bias": 2.482642269104389, "security_level": 128, "minimal_lwe_dimension": 450}, {"slope": -0.023821437305989134, "bias": 2.7177789440636673, "security_level": 144, "minimal_lwe_dimension": 450}, {"slope": -0.021743582187160406, "bias": 2.9388105484933504, "security_level": 160, "minimal_lwe_dimension": 498}, {"slope": -0.019904056582117705, "bias": 2.8161252801542673, "security_level": 176, "minimal_lwe_dimension": 551}, {"slope": -0.018610403247590064, "bias": 3.2996236848399008, "security_level": 192, "minimal_lwe_dimension": 606}, {"slope": -0.014606812351714961, "bias": 3.8493629234693145, "security_level": 256, "minimal_lwe_dimension": 826}] diff --git a/verified_curves.sobj b/verified_curves.sobj index 546a32e51814a52cf0cb7db3b53badd39bc666ce..331cd44ee0d303d0e057d9f1d8858e331d16d232 100644 GIT binary patch literal 735 zcmV<50wDc(oL!RXa|}@w#b=R(M(hc(uNhm2eNWq$k>J|emeHAMy6IWYEYy^W8c`l9 zlqJ@(#a62LVEsZVQTP2v{sG=YF#5x*uKUh8zk6?YDRc;dNIQZNt0E8x}KXws z*b3v-#OvshpQiVPrE03+)&caYMf${gt+{ zy-+W&pW?@=!8(l?O&B9EmN3p8@{eu;zu;SZB{w->V!YN(AWRgPM40T>Mi0Bc)2IAR z*A2>Z{E|O5%DXl{RDh}4IE_#*5Ft!22Q21WQt-#EK@&5yZYE)tz-&T;0%UptbN&MK zjz;ns`T3N5)sOF~{JGlKNSG%upRmCFcbl>#&&p1|l#}m)xxcWW`P#MHU;Qb zfNvVG{l5X8$xGX~n?KonLlJgpBN28A>>}(g+Yy)ezI?!Md_G@kVvkntC0GLc2>TV{ zjY8Ci6yiYnPejeDEBIbDcd7h?+I5J~Di9+aF8jt@k{_1W@ngp@VQXC*Auf<0I4b?C zN^jNaN6I%eksry<8MEa}zO3?(YM)E+1db7om-pW-pU9i?;%V7gZNk^eBq1e`CS=^& R=0H#(mW2yW!w>icZz|vxZZ7}; literal 670 zcmV;P0%84loK=$9Pg6k@hr0mE1?q;Vh$|>np^6&_C>M9RQ1IYVH>9@3qGkD&@<337 zm<%Es6>tlP;P#*o;_{*~)|~l|{0DSKYI3~~lbbW&@0&9xGvK-ahu0hG?Ct38^M$(N z5no4dEYTi}$Af)FPbe4*#tk^<_Kk3&uRCJE6>vEmjEjYMYyp+rZ8Q zmU>g!ELAVL-dik3`*}tkmZy`;2+IXl5b{%0wxZ-Tzvah#zo*b)Vx`6k2&)8E6V_O* z(K+whe_nkLImGYyYngl&3XppsCP0u4Rzyj=Op#Xr(bRBLE2VV^(^!L%Nv#I^R8 z<((paxI%tDpk|oFejPbLI4E$4aM(7B)-&=+K7ZiBc_xl%>L}ruKpnxdrj_`Y64(FN z@szyV$kY5S$m7b~pfi3#qd*hkxP2HrDWCIfjE6Ezkj9z`Cj?FsPFb@`UF=HWwEbZO z8fw#d!oy?A8_gtG$Y2= table_sd) + #print(n, table_sd, model_sd, model_sd >= table_sd) if table_sd > model_sd: - print("MODEL FAILS at n = {}".format(n)) - return "FAIL" + #print("MODEL FAILS at n = {}".format(n)) + return False - return "PASS", n_min + return True, n_min def generate_and_verify(security_levels, log_q, name="verified_curves"): - data = [] + success = [] + + fail = [] for sec in security_levels: - print("WE GO FOR {}".format(sec)) + #print("WE GO FOR {}".format(sec)) # generate the model for security level sec (a_sec, b_sec) = generate_curve(sec) # verify the model for security level sec - res = verify_curve(sec, a_sec, b_sec) + (status, n_alpha) = verify_curve(sec, a_sec, b_sec) # append the information into a list - data.append((a_sec, b_sec - log_q, sec, res[0], res[1])) - save(data, "{}.sobj".format(name)) + x = {"slope": a_sec, "bias": b_sec - log_q, "security_level": sec, "minimal_lwe_dimension": n_alpha} + if status: + success.append(x) + else: + fail.append(x) - return data + save(success, "{}.sobj".format(name)) + + return success, fail -data = generate_and_verify([80, 96, 112, 128, 144, 160, 176, 192, 256], log_q=64) -print(data) +(success, fail) = generate_and_verify([80, 96, 112, 128, 144, 160, 176, 192, 256], log_q=64) +if (fail): + print("FAILURE: Fail to verify the following curves") + print(json.dumps(fail)) + exit(1) + +print(json.dumps(success))