Fork of the official github repository of the framework Leaky-LWE-Estimator, a Sage Toolkit to attack and estimate the hardness of LWE with Side Information. https://github.com/lducas/leaky-LWE-Estimator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

check_consistency.sage 6.6KB

4 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/sage -python
  2. # -*- coding: latin-1 -*-
  3. load("../framework/instance_gen.sage")
  4. def check_all_equal(du, du_):
  5. assert du.dim() == du_.dim()
  6. assert abs(du.dvol - du_.dvol) < 1e-6
  7. assert abs(du.beta - du_.beta) < 1e-6
  8. if du.delta == oo:
  9. assert du_.delta == oo
  10. else:
  11. assert abs(du.delta - du_.delta) < 1e-6
  12. def test_dbdd_ppred(n, m, q, D_e, D_s, nb_hints, hint_weight):
  13. A, b, dbdd = initialize_from_LWE_instance(DBDD, n, q, m,
  14. D_e, D_s,
  15. verbosity=2)
  16. dbdd_p = DBDD_predict(dbdd.B, matrix(RR, dbdd.S), dbdd.u, verbosity=2)
  17. hw = hint_weight
  18. if hw == 1:
  19. dbdd_d = DBDD_predict_diag(
  20. dbdd.B, matrix(RR, dbdd.S), dbdd.u, verbosity=2)
  21. d = n + m
  22. dbdd.estimate_attack()
  23. dbdd_p.estimate_attack()
  24. check_all_equal(dbdd, dbdd_p)
  25. print("Ok so far.")
  26. for h in range(nb_hints):
  27. v = vec(d * [0])
  28. for w in range(hint_weight):
  29. p = randint(0, d - 1)
  30. v[0, p] = randint(- 1, 1) if w < hint_weight - 1 else 1
  31. try:
  32. leak = dbdd.leak(v)
  33. force = bool(randint(0, 1))
  34. if (h % 3) == 0:
  35. dbdd.integrate_perfect_hint(v, leak,
  36. non_primitive_action="fail",
  37. force=force,
  38. catch_invalid_hint=False)
  39. dbdd_p.integrate_perfect_hint(v, leak, force=force)
  40. if hw == 1:
  41. dbdd_d.integrate_perfect_hint(v, leak, force=force)
  42. elif (h % 3) == 1:
  43. k = randint(2, 12)
  44. opt = True # bool(randint(0, 1))
  45. dbdd.integrate_modular_hint(v, leak % k, k,
  46. smooth=True,
  47. non_primitive_action="fail",
  48. force=force,
  49. catch_invalid_hint=False)
  50. dbdd_p.integrate_modular_hint(v, leak % k, k,
  51. smooth=True,
  52. force=force)
  53. if hw == 1:
  54. dbdd_d.integrate_modular_hint(v, leak % k, k,
  55. smooth=True,
  56. force=force)
  57. else:
  58. sigma = randint(1, 10) / 10
  59. apost = bool(randint(0, 1))
  60. dbdd.integrate_approx_hint(v, leak, sigma,
  61. aposteriori=apost,
  62. force=force,
  63. catch_invalid_hint=False)
  64. dbdd_p.integrate_approx_hint(v, leak, sigma,
  65. aposteriori=apost,
  66. force=force)
  67. if hw == 1:
  68. dbdd_d.integrate_approx_hint(v, leak, sigma,
  69. aposteriori=apost,
  70. force=force)
  71. except InvalidHint as e:
  72. logging(str(e) + " Skipping both real and predict. \n",
  73. style="REJECT")
  74. pass
  75. dbdd.estimate_attack(silent=True)
  76. dbdd_p.estimate_attack(silent=True)
  77. if hw == 1:
  78. dbdd_d.estimate_attack(silent=True)
  79. check_all_equal(dbdd, dbdd_p)
  80. if hw == 1:
  81. check_all_equal(dbdd, dbdd_d)
  82. dbdd.integrate_q_vectors(q)
  83. dbdd_p.integrate_q_vectors(q)
  84. check_all_equal(dbdd, dbdd_p)
  85. if hw == 1:
  86. dbdd_d.integrate_q_vectors(q)
  87. check_all_equal(dbdd, dbdd_d)
  88. dbdd.estimate_attack()
  89. dbdd_p.estimate_attack()
  90. if hw == 1:
  91. dbdd_d.estimate_attack()
  92. dbdd.attack()
  93. logging("Passed that batch of tests !", style="SUCCESS")
  94. def random_PSD(d, variance, repeat=5, diag=False):
  95. S = matrix(d * [d * [0]])
  96. noise = vec(d * [0])
  97. for x in range(repeat * d):
  98. if not diag:
  99. V = vec([randint(- 10, 10) + randint(- 10, 10) for i in range(d)])
  100. else:
  101. V = vec(d * [0])
  102. V[0, randint(0, d - 1)] = 1
  103. f = (variance / (repeat * scal(V * V.T)))
  104. S += f * V.T * V
  105. noise += round_to_rational(gauss(0, sqrt(f))) * V
  106. return S, noise
  107. def test_dbdd_ppred_fulldimapprox(n, m, q, D_e, D_s, nb_hints, diag=False):
  108. A, b, dbdd = initialize_from_LWE_instance(DBDD,
  109. n, q,
  110. m, D_e,
  111. D_s,
  112. verbosity=2)
  113. dbdd_p = DBDD_predict(dbdd.B, matrix(RR, dbdd.S), dbdd.u, verbosity=2)
  114. if diag:
  115. dbdd_d = DBDD_predict_diag(dbdd.B, matrix(RR, dbdd.S),
  116. dbdd.u, verbosity=2)
  117. d = n + m
  118. dbdd.estimate_attack()
  119. dbdd_p.estimate_attack()
  120. check_all_equal(dbdd, dbdd_p)
  121. if diag:
  122. dbdd_d.estimate_attack()
  123. check_all_equal(dbdd, dbdd_d)
  124. for h in range(nb_hints):
  125. S, noise = random_PSD(d, 10, repeat=10, diag=diag)
  126. dbdd.integrate_approx_hint_fulldim(dbdd.u[:, :-1] + noise, S)
  127. dbdd_p.integrate_approx_hint_fulldim(None, S)
  128. if diag:
  129. dbdd_d.integrate_approx_hint_fulldim(None, S)
  130. dbdd.estimate_attack()
  131. dbdd_p.estimate_attack()
  132. check_all_equal(dbdd, dbdd_p)
  133. if diag:
  134. dbdd_d.estimate_attack()
  135. check_all_equal(dbdd, dbdd_d)
  136. dbdd.attack()
  137. logging("Passed that batch of tests !", style="SUCCESS")
  138. """
  139. Starting the tests
  140. """
  141. n = 8
  142. m = 16
  143. q = 90 # 2 ** 13
  144. D_s = build_centered_binomial_law(4)
  145. D_e = build_centered_binomial_law(4)
  146. test_dbdd_ppred_fulldimapprox(n, m, q, D_e, D_s, 3, diag=True)
  147. test_dbdd_ppred_fulldimapprox(n, m, q, D_e, D_s, 3)
  148. n = 8
  149. m = 16
  150. q = 90 # 2 ** 13
  151. D_s = build_centered_binomial_law(3)
  152. D_e = build_centered_binomial_law(5)
  153. test_dbdd_ppred_fulldimapprox(n, m, q, D_e, D_s, 3, diag=True)
  154. test_dbdd_ppred_fulldimapprox(n, m, q, D_e, D_s, 3)
  155. n = 30
  156. m = 45
  157. q = 90 # 2 ** 13
  158. D_s = build_centered_binomial_law(4)
  159. D_e = build_centered_binomial_law(4)
  160. test_dbdd_ppred(n, m, q, D_e, D_s, 40, 1)
  161. test_dbdd_ppred(n, m, q, D_e, D_s, 40, 2)
  162. test_dbdd_ppred(n, m, q, D_e, D_s, 40, 3)
  163. D_s = build_centered_binomial_law(3)
  164. D_e = build_centered_binomial_law(5)
  165. test_dbdd_ppred(n, m, q, D_e, D_s, 40, 1)
  166. test_dbdd_ppred(n, m, q, D_e, D_s, 40, 2)
  167. test_dbdd_ppred(n, m, q, D_e, D_s, 40, 3)
  168. logging("All test pass !", style="SUCCESS")