PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 43#include <stdio.h>
 44#include <stdlib.h>
 45#include <string.h>
 46#include <ctype.h>
 47#include <limits.h>
 48
 49#include "mpi.h"
 50
 51int main(int argc, char *argv[])
 52{
 53  int      ix;
 54  mp_int   a, b, c, m;
 55  mp_digit r;
 56
 57  if(argc < 4) {
 58    fprintf(stderr, "Usage: %s <a> <b> <m>\n", argv[0]);
 59    return 1;
 60  }
 61
 62  printf("Test 4: Modular arithmetic\n\n");
 63
 64  mp_init(&a);
 65  mp_init(&b);
 66  mp_init(&m);
 67
 68  mp_read_radix(&a, argv[1], 10);
 69  mp_read_radix(&b, argv[2], 10);
 70  mp_read_radix(&m, argv[3], 10);
 71  printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
 72  printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
 73  printf("m = "); mp_print(&m, stdout); fputc('\n', stdout);
 74  
 75  mp_init(&c);
 76  printf("\nc = a (mod m)\n");
 77
 78  mp_mod(&a, &m, &c);
 79  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
 80
 81  printf("\nc = b (mod m)\n");
 82
 83  mp_mod(&b, &m, &c);
 84  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
 85
 86  printf("\nc = b (mod 1853)\n");
 87
 88  mp_mod_d(&b, 1853, &r);
 89  printf("c = %04X\n", r);
 90
 91  printf("\nc = (a + b) mod m\n");
 92
 93  mp_addmod(&a, &b, &m, &c);
 94  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
 95
 96  printf("\nc = (a - b) mod m\n");
 97
 98  mp_submod(&a, &b, &m, &c);
 99  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
100
101  printf("\nc = (a * b) mod m\n");
102
103  mp_mulmod(&a, &b, &m, &c);
104  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
105
106  printf("\nc = (a ** b) mod m\n");
107
108  mp_exptmod(&a, &b, &m, &c);
109  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
110
111  printf("\nIn-place modular squaring test:\n");
112  for(ix = 0; ix < 5; ix++) {
113    printf("a = (a * a) mod m   a = ");
114    mp_sqrmod(&a, &m, &a);
115    mp_print(&a, stdout);
116    fputc('\n', stdout);
117  }
118  
119
120  mp_clear(&c);
121  mp_clear(&m);
122  mp_clear(&b);
123  mp_clear(&a);
124
125  return 0;
126}