NFLLWESecurityEstimator.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # -*- coding: utf-8 -*-
  2. from sage.rings.real_mpfr import RRtoRR # Import RealField from SageMath
  3. import numpy as np
  4. import os
  5. import re
  6. import datetime
  7. import sys
  8. # Get the current path
  9. pathScript = os.getcwd()
  10. # Initialize the path of Martin Albrecht script
  11. pathModule = pathScript + "/lwe-estimator"
  12. sys.path.append(pathModule)
  13. # Import lwe estimator of Martin Albrecht
  14. from estimator import *
  15. print "Estimate the complexity of solving LWE with XPIR parameters\n"
  16. # Precision of 100 (high)
  17. RR = RealField(100)
  18. # Initialize the path of the NFLParams.cpp file
  19. pathNFLParameters = pathScript + "/NFLParams.cpp"
  20. # Chech that the file exits
  21. if (os.path.isfile(pathNFLParameters)):
  22. print "Please wait...\n"
  23. # Open the data file which contains NFL parameters
  24. with open(pathNFLParameters) as paramsFile:
  25. # Check all lines
  26. for line in paramsFile:
  27. # Check the line that contains kMinPolyDegree
  28. if 'const unsigned int kMinPolyDegree' in line:
  29. # Find the index of caracters before and after kMinPolyDegree
  30. index1 = line.find('=')
  31. index2 = line.find('\n', index1+1)
  32. # Set kMinPolyDegree
  33. kMinPolyDegree = int(line[index1 + 2 : index2 - 1])
  34. # Check the line that contains kMaxPolyDegree
  35. if 'const unsigned int kMaxPolyDegree' in line:
  36. # Find the index of caracters before and after kMaxPolyDegree
  37. index1 = line.find('=')
  38. index2 = line.find('\n', index1+1)
  39. # Set kMaxPolyDegree
  40. kMaxPolyDegree = int(line[index1 + 2 : index2 - 1])
  41. # Check the line that contains kMaxAggregatedModulusBitsize
  42. if 'const unsigned int kModulusBitsize' in line:
  43. # Find the index of caracters before and after kModulusBitsize
  44. index1 = line.find('=')
  45. index2 = line.find('\n', index1+1)
  46. # Set kModulusBitsize
  47. kModulusBitsize = int(line[index1 + 2 : index2 - 1])
  48. # Check the line that contains kMaxAggregatedModulusBitsize
  49. if 'const unsigned int kMaxAggregatedModulusBitsize' in line:
  50. # Find the index of caracters before and after kMaxAggregatedModulusBitsize
  51. index1 = line.find('=')
  52. index2 = line.find('\n', index1+1)
  53. # Set kMaxAggregatedModulusBitsize
  54. kMaxAggregatedModulusBitsize = int(line[index1 + 2 : index2 - 1])
  55. # Initialize the path of the NFLLWESecurityEstimated.hpp file
  56. pathNFLLWESecurityEstimatedHPP = pathScript + "/../NFLLWESecurityEstimated.hpp"
  57. # Open NFLLWESecurityEstimated.hpp, if it does not exist, it will create it
  58. paramsSecure = open(pathNFLLWESecurityEstimatedHPP, 'w')
  59. paramsSecure.write('#pragma once\n')
  60. paramsSecure.write("#include <string>\n")
  61. paramsSecure.write('\n')
  62. paramsSecure.write("using namespace std;\n")
  63. paramsSecure.write('\n')
  64. paramsSecure.write('string securityParameters = "')
  65. # Initialize the number of estimations
  66. i =0
  67. # Scan n from kMinPolyDegree to kMaxPolyDegree
  68. for log2n in range(int(np.log2(kMinPolyDegree)), int(np.log2(kMaxPolyDegree)) + 1, 1):
  69. n = 2 ** log2n
  70. # Scan log2q from kModulusBitsize to kMaxAggregatedModulusBitsize
  71. for log2q in range(kModulusBitsize, kMaxAggregatedModulusBitsize + 1, 60):
  72. # Increment the number of estimations
  73. i += 1
  74. # Compute the number of bits for each parameters with the Martin Albrecht algortihm
  75. security = estimate_lwe(n, RR(80 / RR((2 ** log2q)) ) , 2 ** log2q, skip=("mitm", "bkw", "arora-gb"))
  76. # Select the security and return the number of bits
  77. nbrBits = int(np.log2(min(security['sis']['bkz2'], security['dec']['bkz2'], security['kannan']['bkz2'])))
  78. # Write security parameters
  79. paramsSecure.write(str(n))
  80. paramsSecure.write(":")
  81. paramsSecure.write(str(log2q))
  82. paramsSecure.write(":")
  83. paramsSecure.write(str(nbrBits))
  84. paramsSecure.write('\\n')
  85. paramsSecure.write('\\')
  86. paramsSecure.write('\n')
  87. # Print to the user the number of the last estimation done
  88. print "estimate parameters ", i, " : done"
  89. paramsSecure.write('";\n')
  90. # Close the NFLLWESecurityEstimated.hpp file
  91. paramsSecure.close()
  92. print "\nResults of the estimation written\n\nScript finished !"
  93. else:
  94. # Error if XPIR file not found
  95. print "ERROR : XPIR files not found !"