/security/nss/lib/freebl/mpi/tests/mptest-4.c

http://github.com/zpao/v8monkey · C · 126 lines · 60 code · 25 blank · 41 comment · 2 complexity · e956cb655f37930898aee0dde026ea83 MD5 · raw file

  1. /*
  2. * Simple test driver for MPI library
  3. *
  4. * Test 4: Modular arithmetic tests
  5. *
  6. * ***** BEGIN LICENSE BLOCK *****
  7. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8. *
  9. * The contents of this file are subject to the Mozilla Public License Version
  10. * 1.1 (the "License"); you may not use this file except in compliance with
  11. * the License. You may obtain a copy of the License at
  12. * http://www.mozilla.org/MPL/
  13. *
  14. * Software distributed under the License is distributed on an "AS IS" basis,
  15. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. * for the specific language governing rights and limitations under the
  17. * License.
  18. *
  19. * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
  20. *
  21. * The Initial Developer of the Original Code is
  22. * Michael J. Fromberger.
  23. * Portions created by the Initial Developer are Copyright (C) 1998
  24. * the Initial Developer. All Rights Reserved.
  25. *
  26. * Contributor(s):
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * either the GNU General Public License Version 2 or later (the "GPL"), or
  30. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. * in which case the provisions of the GPL or the LGPL are applicable instead
  32. * of those above. If you wish to allow use of your version of this file only
  33. * under the terms of either the GPL or the LGPL, and not to allow others to
  34. * use your version of this file under the terms of the MPL, indicate your
  35. * decision by deleting the provisions above and replace them with the notice
  36. * and other provisions required by the GPL or the LGPL. If you do not delete
  37. * the provisions above, a recipient may use your version of this file under
  38. * the terms of any one of the MPL, the GPL or the LGPL.
  39. *
  40. * ***** END LICENSE BLOCK ***** */
  41. /* $Id: mptest-4.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46. #include <limits.h>
  47. #include "mpi.h"
  48. int main(int argc, char *argv[])
  49. {
  50. int ix;
  51. mp_int a, b, c, m;
  52. mp_digit r;
  53. if(argc < 4) {
  54. fprintf(stderr, "Usage: %s <a> <b> <m>\n", argv[0]);
  55. return 1;
  56. }
  57. printf("Test 4: Modular arithmetic\n\n");
  58. mp_init(&a);
  59. mp_init(&b);
  60. mp_init(&m);
  61. mp_read_radix(&a, argv[1], 10);
  62. mp_read_radix(&b, argv[2], 10);
  63. mp_read_radix(&m, argv[3], 10);
  64. printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
  65. printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
  66. printf("m = "); mp_print(&m, stdout); fputc('\n', stdout);
  67. mp_init(&c);
  68. printf("\nc = a (mod m)\n");
  69. mp_mod(&a, &m, &c);
  70. printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
  71. printf("\nc = b (mod m)\n");
  72. mp_mod(&b, &m, &c);
  73. printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
  74. printf("\nc = b (mod 1853)\n");
  75. mp_mod_d(&b, 1853, &r);
  76. printf("c = %04X\n", r);
  77. printf("\nc = (a + b) mod m\n");
  78. mp_addmod(&a, &b, &m, &c);
  79. printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
  80. printf("\nc = (a - b) mod m\n");
  81. mp_submod(&a, &b, &m, &c);
  82. printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
  83. printf("\nc = (a * b) mod m\n");
  84. mp_mulmod(&a, &b, &m, &c);
  85. printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
  86. printf("\nc = (a ** b) mod m\n");
  87. mp_exptmod(&a, &b, &m, &c);
  88. printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
  89. printf("\nIn-place modular squaring test:\n");
  90. for(ix = 0; ix < 5; ix++) {
  91. printf("a = (a * a) mod m a = ");
  92. mp_sqrmod(&a, &m, &a);
  93. mp_print(&a, stdout);
  94. fputc('\n', stdout);
  95. }
  96. mp_clear(&c);
  97. mp_clear(&m);
  98. mp_clear(&b);
  99. mp_clear(&a);
  100. return 0;
  101. }