ModularFunctions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Random.h>
  28. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  29. //#define NT_DEBUG
  30. namespace Crypto {
  31. namespace NumberTheory {
  32. static auto ModularInverse(const UnsignedBigInteger& a_, const UnsignedBigInteger& b) -> UnsignedBigInteger
  33. {
  34. if (b == 1)
  35. return { 1 };
  36. UnsignedBigInteger one { 1 };
  37. UnsignedBigInteger temp_1;
  38. UnsignedBigInteger temp_2;
  39. UnsignedBigInteger temp_3;
  40. UnsignedBigInteger temp_4;
  41. UnsignedBigInteger temp_plus;
  42. UnsignedBigInteger temp_minus;
  43. UnsignedBigInteger temp_quotient;
  44. UnsignedBigInteger temp_remainder;
  45. UnsignedBigInteger d;
  46. auto a = a_;
  47. auto u = a;
  48. if (a.words()[0] % 2 == 0) {
  49. // u += b
  50. UnsignedBigInteger::add_without_allocation(u, b, temp_plus);
  51. u.set_to(temp_plus);
  52. }
  53. auto v = b;
  54. UnsignedBigInteger x { 0 };
  55. // d = b - 1
  56. UnsignedBigInteger::subtract_without_allocation(b, one, d);
  57. while (!(v == 1)) {
  58. while (v < u) {
  59. // u -= v
  60. UnsignedBigInteger::subtract_without_allocation(u, v, temp_minus);
  61. u.set_to(temp_minus);
  62. // d += x
  63. UnsignedBigInteger::add_without_allocation(d, x, temp_plus);
  64. d.set_to(temp_plus);
  65. while (u.words()[0] % 2 == 0) {
  66. if (d.words()[0] % 2 == 1) {
  67. // d += b
  68. UnsignedBigInteger::add_without_allocation(d, b, temp_plus);
  69. d.set_to(temp_plus);
  70. }
  71. // u /= 2
  72. UnsignedBigInteger::divide_u16_without_allocation(u, 2, temp_quotient, temp_remainder);
  73. u.set_to(temp_quotient);
  74. // d /= 2
  75. UnsignedBigInteger::divide_u16_without_allocation(d, 2, temp_quotient, temp_remainder);
  76. d.set_to(temp_quotient);
  77. }
  78. }
  79. // v -= u
  80. UnsignedBigInteger::subtract_without_allocation(v, u, temp_minus);
  81. v.set_to(temp_minus);
  82. // x += d
  83. UnsignedBigInteger::add_without_allocation(x, d, temp_plus);
  84. x.set_to(temp_plus);
  85. while (v.words()[0] % 2 == 0) {
  86. if (x.words()[0] % 2 == 1) {
  87. // x += b
  88. UnsignedBigInteger::add_without_allocation(x, b, temp_plus);
  89. x.set_to(temp_plus);
  90. }
  91. // v /= 2
  92. UnsignedBigInteger::divide_u16_without_allocation(v, 2, temp_quotient, temp_remainder);
  93. v.set_to(temp_quotient);
  94. // x /= 2
  95. UnsignedBigInteger::divide_u16_without_allocation(x, 2, temp_quotient, temp_remainder);
  96. x.set_to(temp_quotient);
  97. }
  98. }
  99. // x % b
  100. UnsignedBigInteger::divide_without_allocation(x, b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  101. return temp_remainder;
  102. }
  103. static auto ModularPower(const UnsignedBigInteger& b, const UnsignedBigInteger& e, const UnsignedBigInteger& m) -> UnsignedBigInteger
  104. {
  105. if (m == 1)
  106. return 0;
  107. UnsignedBigInteger ep { e };
  108. UnsignedBigInteger base { b };
  109. UnsignedBigInteger exp { 1 };
  110. UnsignedBigInteger temp_1;
  111. UnsignedBigInteger temp_2;
  112. UnsignedBigInteger temp_3;
  113. UnsignedBigInteger temp_4;
  114. UnsignedBigInteger temp_multiply;
  115. UnsignedBigInteger temp_quotient;
  116. UnsignedBigInteger temp_remainder;
  117. while (!(ep < 1)) {
  118. #ifdef NT_DEBUG
  119. dbg() << ep.to_base10();
  120. #endif
  121. if (ep.words()[0] % 2 == 1) {
  122. // exp = (exp * base) % m;
  123. UnsignedBigInteger::multiply_without_allocation(exp, base, temp_1, temp_2, temp_3, temp_4, temp_multiply);
  124. UnsignedBigInteger::divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  125. exp.set_to(temp_remainder);
  126. }
  127. // ep = ep / 2;
  128. UnsignedBigInteger::divide_u16_without_allocation(ep, 2, temp_quotient, temp_remainder);
  129. ep.set_to(temp_quotient);
  130. // base = (base * base) % m;
  131. UnsignedBigInteger::multiply_without_allocation(base, base, temp_1, temp_2, temp_3, temp_4, temp_multiply);
  132. UnsignedBigInteger::divide_without_allocation(temp_multiply, m, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  133. base.set_to(temp_remainder);
  134. }
  135. return exp;
  136. }
  137. static void GCD_without_allocation(
  138. const UnsignedBigInteger& a,
  139. const UnsignedBigInteger& b,
  140. UnsignedBigInteger& temp_a,
  141. UnsignedBigInteger& temp_b,
  142. UnsignedBigInteger& temp_1,
  143. UnsignedBigInteger& temp_2,
  144. UnsignedBigInteger& temp_3,
  145. UnsignedBigInteger& temp_4,
  146. UnsignedBigInteger& temp_quotient,
  147. UnsignedBigInteger& temp_remainder,
  148. UnsignedBigInteger& output)
  149. {
  150. temp_a.set_to(a);
  151. temp_b.set_to(b);
  152. for (;;) {
  153. if (temp_a == 0) {
  154. output.set_to(temp_b);
  155. return;
  156. }
  157. // temp_b %= temp_a
  158. UnsignedBigInteger::divide_without_allocation(temp_b, temp_a, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  159. temp_b.set_to(temp_remainder);
  160. if (temp_b == 0) {
  161. output.set_to(temp_a);
  162. return;
  163. }
  164. // temp_a %= temp_b
  165. UnsignedBigInteger::divide_without_allocation(temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  166. temp_a.set_to(temp_remainder);
  167. }
  168. }
  169. static UnsignedBigInteger GCD(const UnsignedBigInteger& a, const UnsignedBigInteger& b)
  170. {
  171. UnsignedBigInteger temp_a;
  172. UnsignedBigInteger temp_b;
  173. UnsignedBigInteger temp_1;
  174. UnsignedBigInteger temp_2;
  175. UnsignedBigInteger temp_3;
  176. UnsignedBigInteger temp_4;
  177. UnsignedBigInteger temp_quotient;
  178. UnsignedBigInteger temp_remainder;
  179. UnsignedBigInteger output;
  180. GCD_without_allocation(a, b, temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder, output);
  181. return output;
  182. }
  183. static auto LCM(const UnsignedBigInteger& a, const UnsignedBigInteger& b) -> UnsignedBigInteger
  184. {
  185. UnsignedBigInteger temp_a;
  186. UnsignedBigInteger temp_b;
  187. UnsignedBigInteger temp_1;
  188. UnsignedBigInteger temp_2;
  189. UnsignedBigInteger temp_3;
  190. UnsignedBigInteger temp_4;
  191. UnsignedBigInteger temp_quotient;
  192. UnsignedBigInteger temp_remainder;
  193. UnsignedBigInteger gcd_output;
  194. UnsignedBigInteger output { 0 };
  195. GCD_without_allocation(a, b, temp_a, temp_b, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder, gcd_output);
  196. if (gcd_output == 0) {
  197. #ifdef NT_DEBUG
  198. dbg() << "GCD is zero";
  199. #endif
  200. return output;
  201. }
  202. // output = (a / gcd_output) * b
  203. UnsignedBigInteger::divide_without_allocation(a, gcd_output, temp_1, temp_2, temp_3, temp_4, temp_quotient, temp_remainder);
  204. UnsignedBigInteger::multiply_without_allocation(temp_quotient, b, temp_1, temp_2, temp_3, temp_4, output);
  205. #ifdef NT_DEBUG
  206. dbg() << "quot: " << temp_quotient << " rem: " << temp_remainder << " out: " << output;
  207. #endif
  208. return output;
  209. }
  210. template<size_t test_count>
  211. static bool MR_primality_test(UnsignedBigInteger n, const Vector<UnsignedBigInteger, test_count>& tests)
  212. {
  213. auto prev = n.minus({ 1 });
  214. auto b = prev;
  215. auto r = 0;
  216. auto div_result = b.divided_by(2);
  217. while (div_result.quotient == 0) {
  218. div_result = b.divided_by(2);
  219. b = div_result.quotient;
  220. ++r;
  221. }
  222. for (size_t i = 0; i < tests.size(); ++i) {
  223. auto return_ = true;
  224. if (n < tests[i])
  225. continue;
  226. auto x = ModularPower(tests[i], b, n);
  227. if (x == 1 || x == prev)
  228. continue;
  229. for (auto d = r - 1; d != 0; --d) {
  230. x = ModularPower(x, 2, n);
  231. if (x == 1)
  232. return false;
  233. if (x == prev) {
  234. return_ = false;
  235. break;
  236. }
  237. }
  238. if (return_)
  239. return false;
  240. }
  241. return true;
  242. }
  243. static UnsignedBigInteger random_number(const UnsignedBigInteger& min, const UnsignedBigInteger& max)
  244. {
  245. ASSERT(min < max);
  246. auto range = max.minus(min);
  247. UnsignedBigInteger base;
  248. // FIXME: Need a cryptographically secure rng
  249. auto size = range.trimmed_length() * sizeof(u32);
  250. u8 buf[size];
  251. AK::fill_with_random(buf, size);
  252. Vector<u32> vec;
  253. for (size_t i = 0; i < size / sizeof(u32); ++i) {
  254. vec.append(*(u32*)buf + i);
  255. }
  256. UnsignedBigInteger offset { move(vec) };
  257. return offset.plus(min);
  258. }
  259. static bool is_probably_prime(const UnsignedBigInteger& p)
  260. {
  261. if (p == 2 || p == 3 || p == 5)
  262. return true;
  263. if (p < 49)
  264. return true;
  265. Vector<UnsignedBigInteger, 256> tests;
  266. UnsignedBigInteger seven { 7 };
  267. for (size_t i = 0; i < tests.size(); ++i)
  268. tests.append(random_number(seven, p.minus(2)));
  269. return MR_primality_test(p, tests);
  270. }
  271. static UnsignedBigInteger random_big_prime(size_t bits)
  272. {
  273. ASSERT(bits >= 33);
  274. UnsignedBigInteger min = UnsignedBigInteger::from_base10("6074001000").shift_left(bits - 33);
  275. UnsignedBigInteger max = UnsignedBigInteger { 1 }.shift_left(bits).minus(1);
  276. for (;;) {
  277. auto p = random_number(min, max);
  278. if (is_probably_prime(p))
  279. return p;
  280. }
  281. }
  282. }
  283. }