bigtest.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <mp.h>
  12. #include <libsec.h>
  13. char *sfactors[] =
  14. { "3", "5", "17", "257", "641", "65537", "274177", "2424833", "6700417", "45592577",
  15. "6487031809", "67280421310721", "1238926361552897", "59649589127497217",
  16. "5704689200685129054721", "4659775785220018543264560743076778192897",
  17. "7455602825647884208337395736200454918783366342657",
  18. "93461639715357977769163558199606896584051237541638188580280321",
  19. "741640062627530801524787141901937474059940781097519023905821316144415759504705008092818711693940737",
  20. "130439874405488189727484768796509903946608530841611892186895295776832416251471863574140227977573104895898783928842923844831149032913798729088601617946094119449010595906710130531906171018354491609619193912488538116080712299672322806217820753127014424577"
  21. };
  22. int32_t start;
  23. void
  24. printmp(mpint *b, char *tag)
  25. {
  26. int n;
  27. char *p;
  28. print("%s (%d) ", tag, b->top);
  29. p = mptoa(b, 10, nil, 0);
  30. write(1, p, strlen(p));
  31. free(p);
  32. print("\n");
  33. }
  34. int
  35. timing(void)
  36. {
  37. int32_t now, span;
  38. now = time(0);
  39. span = now-start;
  40. start = now;
  41. return span;
  42. }
  43. int expdebug;
  44. void
  45. main(int argc, char **argv)
  46. {
  47. mpint *p, *k, *d, *b, *e, *x, *r;
  48. int i;
  49. start = time(0);
  50. fmtinstall('B', mpconv);
  51. mpsetminbits(2*Dbits);
  52. x = mpnew(0);
  53. e = mpnew(0);
  54. r = mpnew(0);
  55. p = mpnew(0);
  56. // b = 2^32
  57. b = mpcopy(mpone);
  58. mpleft(b, 32, b);
  59. // 2^29440
  60. p = mpcopy(mpone);
  61. mpleft(p, 29440, p);
  62. // 2^27392
  63. k = mpcopy(mpone);
  64. mpleft(k, 27392, k);
  65. // k = 2^29440 - 2^27392
  66. mpsub(p, k, k);
  67. // p = 2^29440 - 2^27392 + 1
  68. mpadd(k, mpone, p);
  69. // if(!probably_prime(p, 18)){
  70. // print("not a prime\n");
  71. // exits(0);
  72. // }
  73. // print("probably prime\n");
  74. mpright(k, 10, k);
  75. printmp(k, "k =");
  76. expdebug = 1;
  77. mpexp(b, k, p, x);
  78. printmp(x, "x =");
  79. print("timing %d\n", timing());
  80. for(i = 0; i < nelem(sfactors); i++){
  81. d = strtomp(sfactors[i], nil, 10, nil);
  82. // e = k/d
  83. mpdiv(k, d, e, r);
  84. printmp(r, "r =");
  85. // x = b^e mod p
  86. mpexp(b, e, p, x);
  87. printmp(x, "x =");
  88. print("timing %d\n", timing());
  89. }
  90. }