PageRenderTime 120ms CodeModel.GetById 16ms app.highlight 94ms RepoModel.GetById 1ms app.codeStats 1ms

/security/nss/lib/freebl/mpi/mpi-test.c

http://github.com/zpao/v8monkey
C | 1986 lines | 1411 code | 408 blank | 167 comment | 215 complexity | 7d1c4dee610122234eae24abe881795c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * mpi-test.c
   3 *
   4 * This is a general test suite for the MPI library, which tests
   5 * all the functions in the library with known values.  The program
   6 * exits with a zero (successful) status if the tests pass, or a 
   7 * nonzero status if the tests fail.
   8 *
   9 * ***** BEGIN LICENSE BLOCK *****
  10 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  11 *
  12 * The contents of this file are subject to the Mozilla Public License Version
  13 * 1.1 (the "License"); you may not use this file except in compliance with
  14 * the License. You may obtain a copy of the License at
  15 * http://www.mozilla.org/MPL/
  16 *
  17 * Software distributed under the License is distributed on an "AS IS" basis,
  18 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  19 * for the specific language governing rights and limitations under the
  20 * License.
  21 *
  22 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
  23 *
  24 * The Initial Developer of the Original Code is
  25 * Michael J. Fromberger.
  26 * Portions created by the Initial Developer are Copyright (C) 1999
  27 * the Initial Developer. All Rights Reserved.
  28 *
  29 * Contributor(s):
  30 *   Netscape Communications Corporation
  31 *
  32 * Alternatively, the contents of this file may be used under the terms of
  33 * either the GNU General Public License Version 2 or later (the "GPL"), or
  34 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  35 * in which case the provisions of the GPL or the LGPL are applicable instead
  36 * of those above. If you wish to allow use of your version of this file only
  37 * under the terms of either the GPL or the LGPL, and not to allow others to
  38 * use your version of this file under the terms of the MPL, indicate your
  39 * decision by deleting the provisions above and replace them with the notice
  40 * and other provisions required by the GPL or the LGPL. If you do not delete
  41 * the provisions above, a recipient may use your version of this file under
  42 * the terms of any one of the MPL, the GPL or the LGPL.
  43 *
  44 * ***** END LICENSE BLOCK ***** */
  45/* $Id: mpi-test.c,v 1.13 2005/02/25 04:30:11 julien.pierre.bugs%sun.com Exp $ */
  46
  47#include <stdio.h>
  48#include <stdlib.h>
  49#include <string.h>
  50#include <stdarg.h>
  51#include <limits.h>
  52#include <time.h>
  53
  54#include "mpi.h"
  55#include "mpprime.h"
  56
  57#include "test-info.c"
  58
  59/* ZS means Zero Suppressed (no leading zeros) */
  60#if MP_USE_LONG_DIGIT 
  61#define ZS_DIGIT_FMT         "%lX"
  62#elif MP_USE_LONG_LONG_DIGIT
  63#define ZS_DIGIT_FMT         "%llX"
  64#elif MP_USE_UINT_DIGIT 
  65#define ZS_DIGIT_FMT         "%X"
  66#else
  67#error "unknown type of digit"
  68#endif
  69
  70/*
  71  Test vectors
  72
  73  If you intend to change any of these values, you must also recompute
  74  the corresponding solutions below.  Basically, these are just hex
  75  strings (for the big integers) or integer values (for the digits).
  76
  77  The comparison tests think they know what relationships hold between
  78  these values.  If you change that, you may have to adjust the code
  79  for the comparison tests accordingly.  Most of the other tests
  80  should be fine as long as you re-compute the solutions, though.
  81 */
  82const char   *mp1  = "639A868CDA0C569861B";
  83const char   *mp2  = "AAFC0A3FE45E5E09DBE2C29";
  84const char   *mp3  = "B55AA8DF8A7E83241F38AC7A9E479CAEF2E4D7C5";
  85const char   *mp4  = "-63DBC2265B88268DC801C10EA68476B7BDE0090F";
  86const char   *mp5  = "F595CB42";
  87const char   *mp5a = "-4B597E";
  88const char   *mp6  = "0";
  89const char   *mp7  = "EBFA7121CD838CE6439CC59DDB4CBEF3";
  90const char   *mp8  = "5";
  91const char   *mp9  = "F74A2876A1432698923B0767DA19DCF3D71795EE";
  92const char   *mp10 = "9184E72A000";
  93const char   *mp11 = "54D79A3557E8";
  94const char   *mp12 = "10000000000000000";
  95const char   *mp13 = 
  96"34584F700C15A341E40BF7BFDD88A6630C8FF2B2067469372D391342BDAB6163963C"
  97"D5A5C79F708BDE26E0CCF2DB66CD6D6089E29A877C45F2B050D226E6DA88";
  98const char   *mp14 =
  99"AC3FA0EABAAC45724814D798942A1E28E14C81E0DE8055CED630E7689DA648683645DB6E"
 100"458D9F5338CC3D4E33A5D1C9BF42780133599E60DEE0049AFA8F9489501AE5C9AA2B8C13"
 101"FD21285A538B2CA87A626BB56E0A654C8707535E637FF4E39174157402BDE3AA30C9F134"
 102"0C1307BAA864B075A9CC828B6A5E2B2BF1AE406D920CC5E7657D7C0E697DEE5375773AF9"
 103"E200A1B8FAD7CD141F9EE47ABB55511FEB9A4D99EBA22F3A3FF6792FA7EE9E5DC0EE94F7"
 104"7A631EDF3D7DD7C2DAAAFDF234D60302AB63D5234CEAE941B9AF0ADDD9E6E3A940A94EE5"
 105"5DB45A7C66E61EDD0477419BBEFA44C325129601C4F45671C6A0E64665DF341D17FBC71F"
 106"77418BD9F4375DDB3B9D56126526D8E5E0F35A121FD4F347013DA880020A752324F31DDD"
 107"9BCDB13A3B86E207A2DE086825E6EEB87B3A64232CFD8205B799BC018634AAE193F19531"
 108"D6EBC19A75F27CFFAA03EB5974898F53FD569AA5CE60F431B53B0CDE715A5F382405C9C4"
 109"761A8E24888328F09F7BCE4E8D80C957DF177629C8421ACCD0C268C63C0DD47C3C0D954F"
 110"D79F7D7297C6788DF4B3E51381759864D880ACA246DF09533739B8BB6085EAF7AE8DC2D9"
 111"F224E6874926C8D24D34B457FD2C9A586C6B99582DC24F787A39E3942786CF1D494B6EB4"
 112"A513498CDA0B217C4E80BCE7DA1C704C35E071AC21E0DA9F57C27C3533F46A8D20B04137"
 113"C1B1384BE4B2EB46";
 114const char   *mp15 = 
 115"39849CF7FD65AF2E3C4D87FE5526221103D90BA26A6642FFE3C3ECC0887BBBC57E011BF1"
 116"05D822A841653509C68F79EBE51C0099B8CBB04DEF31F36F5954208A3209AC122F0E11D8"
 117"4AE67A494D78336A2066D394D42E27EF6B03DDAF6D69F5112C93E714D27C94F82FC7EF77"
 118"445768C68EAE1C4A1407BE1B303243391D325090449764AE469CC53EC8012C4C02A72F37"
 119"07ED7275D2CC8D0A14B5BCC6BF264941520EBA97E3E6BAE4EE8BC87EE0DDA1F5611A6ECB"
 120"65F8AEF4F184E10CADBDFA5A2FEF828901D18C20785E5CC63473D638762DA80625003711"
 121"9E984AC43E707915B133543AF9D5522C3E7180DC58E1E5381C1FB7DC6A5F4198F3E88FA6"
 122"CBB6DFA8B2D1C763226B253E18BCCB79A29EE82D2DE735078C8AE3C3C86D476AAA08434C"
 123"09C274BDD40A1D8FDE38D6536C22F44E807EB73DE4FB36C9F51E0BC835DDBE3A8EFCF2FE"
 124"672B525769DC39230EE624D5EEDBD837C82A52E153F37378C3AD68A81A7ADBDF3345DBCE"
 125"8FA18CA1DE618EF94DF72EAD928D4F45B9E51632ACF158CF8332C51891D1D12C2A7E6684"
 126"360C4BF177C952579A9F442CFFEC8DAE4821A8E7A31C4861D8464CA9116C60866C5E72F7"
 127"434ADBED36D54ACDFDFF70A4EFB46E285131FE725F1C637D1C62115EDAD01C4189716327"
 128"BFAA79618B1656CBFA22C2C965687D0381CC2FE0245913C4D8D96108213680BD8E93E821"
 129"822AD9DDBFE4BD04";
 130const char   *mp16 = "4A724340668DB150339A70";
 131const char   *mp17 = "8ADB90F58";
 132const char   *mp18 = "C64C230AB20E5";
 133const char *mp19 = 
 134"F1C9DACDA287F2E3C88DCE2393B8F53DAAAC1196DC36510962B6B59454CFE64B";
 135const char *mp20 = 
 136"D445662C8B6FE394107B867797750C326E0F4A967E135FC430F6CD7207913AC7";
 137const char* mp21 = "2";
 138
 139const mp_digit md1 = 0;
 140const mp_digit md2 = 0x1;
 141const mp_digit md3 = 0x80;
 142const mp_digit md4 = 0x9C97;
 143const mp_digit md5 = 0xF5BF;
 144const mp_digit md6 = 0x14A0;
 145const mp_digit md7 = 0x03E8;
 146const mp_digit md8 = 0x0101;
 147const mp_digit md9 = 0xA;
 148
 149/* 
 150   Solutions of the form x_mpABC, where:
 151
 152   x = (p)roduct, (s)um, (d)ifference, (q)uotient, (r)emainder, (g)cd,
 153       (i)nverse, (e)xponent, square roo(t), (g)cd, (l)cm.  A
 154       leading 'm' indicates a modular operation, e.g. ms_mp12 is the
 155       modular sum of operands 1 and 2
 156
 157   ABC are the operand numbers involved in the computation.  If a 'd'
 158   precedes the number, it is a digit operand; if a 'c' precedes it,
 159   it is a constant; otherwise, it is a full integer.  
 160 */
 161
 162const char *p_mp12   = "4286AD72E095C9FE009938750743174ADDD7FD1E53";
 163const char *p_mp34   = "-46BDBD66CA108C94A8CF46C325F7B6E2F2BA82D35"
 164                       "A1BFD6934C441EE369B60CA29BADC26845E918B";
 165const char *p_mp57   = "E260C265A0A27C17AD5F4E59D6E0360217A2EBA6";
 166const char *p_mp22   = "7233B5C1097FFC77CCF55928FDC3A5D31B712FDE7A1E91";
 167const char *p_mp1d4  = "3CECEA2331F4220BEF68DED";
 168const char *p_mp8d6  = "6720";
 169const char *p_mp1113 =
 170"11590FC3831C8C3C51813142C88E566408DB04F9E27642F6471A1822E0100B12F7F1"
 171"5699A127C0FA9D26DCBFF458522661F30C6ADA4A07C8C90F9116893F6DBFBF24C3A2"
 172"4340";
 173const char *p_mp1415 = 
 174"26B36540DE8B3586699CCEAE218A2842C7D5A01590E70C4A26E789107FBCDB06AA2C"
 175"6DDC39E6FA18B16FCB2E934C9A5F844DAD60EE3B1EA82199EC5E9608F67F860FB965"
 176"736055DF0E8F2540EB28D07F47E309B5F5D7C94FF190AB9C83A6970160CA700B1081"
 177"F60518132AF28C6CEE6B7C473E461ABAC52C39CED50A08DD4E7EA8BA18DAD545126D"
 178"A388F6983C29B6BE3F9DCBC15766E8E6D626A92C5296A9C4653CAE5788350C0E2107"
 179"F57E5E8B6994C4847D727FF1A63A66A6CEF42B9C9E6BD04C92550B85D5527DE8A132"
 180"E6BE89341A9285C7CE7FB929D871BBCBD0ED2863B6B078B0DBB30FCA66D6C64284D6"
 181"57F394A0271E15B6EC7A9D530EBAC6CA262EF6F97E1A29FCE7749240E4AECA591ECF"
 182"272122BC587370F9371B67BB696B3CDC1BC8C5B64B6280994EBA00CDEB8EB0F5D06E"
 183"18F401D65FDCECF23DD7B9BB5B4C5458AEF2CCC09BA7F70EACB844750ACFD027521E"
 184"2E047DE8388B35F8512D3DA46FF1A12D4260213602BF7BFFDB6059439B1BD0676449"
 185"8D98C74F48FB3F548948D5BA0C8ECFCD054465132DC43466D6BBD59FBAF8D6D4E157"
 186"2D612B40A956C7D3E140F3B8562EF18568B24D335707D5BAC7495014DF2444172426"
 187"FD099DED560D30D1F945386604AFC85C64BD1E5F531F5C7840475FC0CF0F79810012"
 188"4572BAF5A9910CDBD02B27FFCC3C7E5E88EF59F3AE152476E33EDA696A4F751E0AE4"
 189"A3D2792DEA78E25B9110E12A19EFD09EA47FF9D6594DA445478BEB6901EAF8A35B2D"
 190"FD59BEE9BF7AA8535B7D326EFA5AA2121B5EBE04DD85827A3D43BD04F4AA6D7B62A2"
 191"B6D7A3077286A511A431E1EF75FCEBA3FAE9D5843A8ED17AA02BBB1B571F904699C5"
 192"A6073F87DDD012E2322AB3F41F2A61F428636FE86914148E19B8EF8314ED83332F2F"
 193"8C2ADE95071E792C0A68B903E060DD322A75FD0C2B992059FCCBB58AFA06B50D1634"
 194"BBD93F187FCE0566609FCC2BABB269C66CEB097598AA17957BB4FDA3E64A1B30402E"
 195"851CF9208E33D52E459A92C63FBB66435BB018E155E2C7F055E0B7AB82CD58FC4889"
 196"372ED9EEAC2A07E8E654AB445B9298D2830D6D4DFD117B9C8ABE3968927DC24B3633"
 197"BAD6E6466DB45DDAE87A0AB00336AC2CCCE176704F7214FCAB55743AB76C2B6CA231"
 198"7984610B27B5786DE55C184DDF556EDFEA79A3652831940DAD941E243F482DC17E50"
 199"284BC2FB1AD712A92542C573E55678878F02DFD9E3A863C7DF863227AEDE14B47AD3"
 200"957190124820ADC19F5353878EDB6BF7D0C77352A6E3BDB53EEB88F5AEF6226D6E68"
 201"756776A8FB49B77564147A641664C2A54F7E5B680CCC6A4D22D894E464DF20537094"
 202"548F1732452F9E7F810C0B4B430C073C0FBCE03F0D03F82630654BCE166AA772E1EE"
 203"DD0C08D3E3EBDF0AF54203B43AFDFC40D8FC79C97A4B0A4E1BEB14D8FCEFDDED8758"
 204"6ED65B18";
 205const char *p_mp2121 = "4";
 206const char *mp_mp345 = "B9B6D3A3";
 207const char *mp_mp335 = "16609C2D";
 208
 209const char *s_mp13   = "B55AA8DF8A7E83241F38B2B446B06A4FB84E5DE0";
 210const char *s_mp34   = "517EE6B92EF65C965736EB6BF7C325F73504CEB6";
 211const char *s_mp46   = "-63DBC2265B88268DC801C10EA68476B7BDE0090F";
 212const char *s_mp5d4  = "F59667D9";
 213const char *s_mp2d5  = "AAFC0A3FE45E5E09DBF21E8";
 214const char *s_mp1415 = 
 215"E5C43DE2B811F4A084625F96E9504039E5258D8348E698CEB9F4D4292622042DB446"
 216"F75F4B65C1FB7A317257FA354BB5A45E789AEC254EAECE11F80A53E3B513822491DB"
 217"D9399DEC4807A2A3A10360129AC93F4A42388D3BF20B310DD0E9E9F4BE07FC88D53A"
 218"78A26091E0AB506A70813712CCBFBDD440A69A906E650EE090FDD6A42A95AC1A414D"
 219"317F1A9F781E6A30E9EE142ECDA45A1E3454A1417A7B9A613DA90831CF88EA1F2E82"
 220"41AE88CC4053220903C2E05BCDD42F02B8CF8868F84C64C5858BAD356143C5494607"
 221"EE22E11650148BAF65A985F6FC4CA540A55697F2B5AA95D6B8CF96EF638416DE1DD6"
 222"3BA9E2C09E22D03E75B60BE456C642F86B82A709253E5E087B507DE3A45F8392423F"
 223"4DBC284E8DC88C43CA77BC8DCEFB6129A59025F80F90FF978116DEBB9209E306FBB9"
 224"1B6111F8B8CFACB7C7C9BC12691C22EE88303E1713F1DFCEB622B8EA102F6365678B"
 225"C580ED87225467AA78E875868BD53B17574BA59305BC1AC666E4B7E9ED72FCFC200E"
 226"189D98FC8C5C7533739C53F52DDECDDFA5A8668BFBD40DABC9640F8FCAE58F532940"
 227"8162261320A25589E9FB51B50F80056471F24B7E1AEC35D1356FC2747FFC13A04B34"
 228"24FCECE10880BD9D97CA8CDEB2F5969BF4F30256EB5ED2BCD1DC64BDC2EE65217848"
 229"48A37FB13F84ED4FB7ACA18C4639EE64309BDD3D552AEB4AAF44295943DC1229A497"
 230"A84A";
 231
 232const char *ms_mp345 = "1E71E292";
 233
 234const char *d_mp12   = "-AAFBA6A55DD183FD854A60E";
 235const char *d_mp34   = "119366B05E606A9B1E73A6D8944CC1366B0C4E0D4";
 236const char *d_mp5d4  = "F5952EAB";
 237const char *d_mp6d2  = "-1";
 238const char *md_mp345 = "26596B86";
 239
 240const char *q_mp42   = "-95825A1FFA1A155D5";
 241const char *r_mp42   = "-6312E99D7700A3DCB32ADF2";
 242const char *q_mp45a  = "15344CDA3D841F661D2B61B6EDF7828CE36";
 243const char *r_mp45a  = "-47C47B";
 244const char *q_mp7c2  = "75FD3890E6C1C67321CE62CEEDA65F79";
 245const char *q_mp3d6  = "8CAFD53C272BD6FE8B0847BDC3B539EFAB5C3";
 246const char *r_mp3d6  = "1E5";
 247const char *r_mp5d5  = "1257";
 248const char *r_mp47   = "B3A9018D970281A90FB729A181D95CB8";
 249const char *q_mp1404 = 
 250"-1B994D869142D3EF6123A3CBBC3C0114FA071CFCEEF4B7D231D65591D32501AD80F"
 251"FF49AE4EC80514CC071EF6B42521C2508F4CB2FEAD69A2D2EF3934087DCAF88CC4C4"
 252"659F1CA8A7F4D36817D802F778F1392337FE36302D6865BF0D4645625DF8BB044E19"
 253"930635BE2609FAC8D99357D3A9F81F2578DE15A300964188292107DAC980E0A08CD7"
 254"E938A2135FAD45D50CB1D8C2D4C4E60C27AB98B9FBD7E4DBF752C57D2674520E4BB2"
 255"7E42324C0EFE84FB3E38CF6950E699E86FD45FE40D428400F2F94EDF7E94FAE10B45"
 256"89329E1BF61E5A378C7B31C9C6A234F8254D4C24823B84D0BF8D671D8BC9154DFAC9"
 257"49BD8ACABD6BD32DD4DC587F22C86153CB3954BDF7C2A890D623642492C482CF3E2C"
 258"776FC019C3BBC61688B485E6FD35D6376089C1E33F880E84C4E51E8ABEACE1B3FB70"
 259"3EAD0E28D2D44E7F1C0A859C840775E94F8C1369D985A3C5E8114B21D68B3CBB75D2"
 260"791C586153C85B90CAA483E57A40E2D97950AAB84920A4396C950C87C7FFFE748358"
 261"42A0BF65445B26D40F05BE164B822CA96321F41D85A289C5F5CD5F438A78704C9683"
 262"422299D21899A22F853B0C93081CC9925E350132A0717A611DD932A68A0ACC6E4C7F"
 263"7F685EF8C1F4910AEA5DC00BB5A36FCA07FFEAA490C547F6E14A08FE87041AB803E1"
 264"BD9E23E4D367A2C35762F209073DFF48F3";
 265const char *r_mp1404 = "12FF98621ABF63144BFFC3207AC8FC10D8D1A09";
 266
 267const char *q_mp13c  = 
 268		"34584F700C15A341E40BF7BFDD88A6630C8FF2B2067469372D391342"
 269		"BDAB6163963CD5A5C79F708BDE26E0CCF2DB66CD6D6089E29A877C45";
 270const char *r_mp13c  = "F2B050D226E6DA88";
 271const char *q_mp9c16 = "F74A2876A1432698923B0767DA19DCF3D71795E";
 272const char *r_mp9c16 = "E";
 273
 274const char *e_mp5d9 = "A8FD7145E727A20E52E73D22990D35D158090307A"
 275		      "13A5215AAC4E9AB1E96BD34E531209E03310400";
 276const char *e_mp78  = "AA5F72C737DFFD8CCD108008BFE7C79ADC01A819B"
 277		      "32B75FB82EC0FB8CA83311DA36D4063F1E57857A2"
 278		      "1AB226563D84A15BB63CE975FF1453BD6750C58D9"
 279		      "D113175764F5D0B3C89B262D4702F4D9640A3";
 280const char *me_mp817 = "E504493ACB02F7F802B327AB13BF25";
 281const char *me_mp5d47 = "1D45ED0D78F2778157992C951DD2734C";
 282const char *me_mp1512 = "FB5B2A28D902B9D9";
 283const char *me_mp161718 = "423C6AC6DBD74";
 284const char *me_mp5114 =
 285"64F0F72807993578BBA3C7C36FFB184028F9EB9A810C92079E1498D8A80FC848E1F0"
 286"25F1DE43B7F6AC063F5CC29D8A7C2D7A66269D72BF5CDC327AF88AF8EF9E601DCB0A"
 287"3F35BFF3525FB1B61CE3A25182F17C0A0633B4089EA15BDC47664A43FEF639748AAC"
 288"19CF58E83D8FA32CD10661D2D4210CC84792937E6F36CB601851356622E63ADD4BD5"
 289"542412C2E0C4958E51FD2524AABDC7D60CFB5DB332EEC9DC84210F10FAE0BA2018F2"
 290"14C9D6867C9D6E49CF28C18D06CE009FD4D04BFC8837C3FAAA773F5CCF6DED1C22DE"
 291"181786AFE188540586F2D74BF312E595244E6936AE52E45742109BAA76C36F2692F5"
 292"CEF97AD462B138BE92721194B163254CBAAEE9B9864B21CCDD5375BCAD0D24132724"
 293"113D3374B4BCF9AA49BA5ACBC12288C0BCF46DCE6CB4A241A91BD559B130B6E9CD3D"
 294"D7A2C8B280C2A278BA9BF5D93244D563015C9484B86D9FEB602501DC16EEBC3EFF19"
 295"53D7999682BF1A1E3B2E7B21F4BDCA3C355039FEF55B9C0885F98DC355CA7A6D8ECF"
 296"5F7F1A6E11A764F2343C823B879B44616B56BF6AE3FA2ACF5483660E618882018E3F"
 297"C8459313BACFE1F93CECC37B2576A5C0B2714BD3EEDEEC22F0E7E3E77B11396B9B99"
 298"D683F2447A4004BBD4A57F6A616CDDFEC595C4FC19884CC2FC21CF5BF5B0B81E0F83"
 299"B9DDA0CF4DFF35BB8D31245912BF4497FD0BD95F0C604E26EA5A8EA4F5EAE870A5BD"
 300"FE8C";
 301
 302const char *e_mpc2d3 = "100000000000000000000000000000000";
 303
 304const char *t_mp9    = "FB9B6E32FF0452A34746";
 305const char *i_mp27   = "B6AD8DCCDAF92B6FE57D062FFEE3A99";
 306const char *i_mp2019 = 
 307"BDF3D88DC373A63EED92903115B03FC8501910AF68297B4C41870AED3EA9F839";
 308/* "15E3FE09E8AE5523AABA197BD2D16318D3CA148EDF4AE1C1C52FC96AFAF5680B"; */
 309
 310
 311const char *t_mp15 =
 312"795853094E59B0008093BCA8DECF68587C64BDCA2F3F7F8963DABC12F1CFFFA9B8C4"
 313"365232FD4751870A0EF6CA619287C5D8B7F1747D95076AB19645EF309773E9EACEA0"
 314"975FA4AE16251A8DA5865349C3A903E3B8A2C0DEA3C0720B6020C7FED69AFF62BB72"
 315"10FAC443F9FFA2950776F949E819260C2AF8D94E8A1431A40F8C23C1973DE5D49AA2"
 316"0B3FF5DA5C1D5324E712A78FF33A9B1748F83FA529905924A31DF38643B3F693EF9B"
 317"58D846BB1AEAE4523ECC843FF551C1B300A130B65C1677402778F98C51C10813250E"
 318"2496882877B069E877B59740DC1226F18A5C0F66F64A5F59A9FAFC5E9FC45AEC0E7A"
 319"BEE244F7DD3AC268CF512A0E52E4F5BE5B94";
 320
 321const char *g_mp71   = "1";
 322const char *g_mp25   = "7";
 323const char *l_mp1011 = "C589E3D7D64A6942A000";
 324
 325/* mp9 in radices from 5 to 64 inclusive */
 326#define LOW_RADIX   5
 327#define HIGH_RADIX  64
 328const char *v_mp9[] = {
 329  "404041130042310320100141302000203430214122130002340212132414134210033",
 330  "44515230120451152500101352430105520150025145320010504454125502",
 331  "644641136612541136016610100564613624243140151310023515322",
 332  "173512120732412062323044435407317550316717172705712756",
 333  "265785018434285762514442046172754680368422060744852",
 334  "1411774500397290569709059837552310354075408897518",
 335  "184064268501499311A17746095910428222A241708032A",
 336  "47706011B225950B02BB45602AA039893118A85950892",
 337  "1A188C826B982353CB58422563AC602B783101671A86",
 338  "105957B358B89B018958908A9114BC3DDC410B77982",
 339  "CB7B3387E23452178846C55DD9D70C7CA9AEA78E8",
 340  "F74A2876A1432698923B0767DA19DCF3D71795EE",
 341  "17BF7C3673B76D7G7A5GA836277296F806E7453A",
 342  "2EBG8HH3HFA6185D6H0596AH96G24C966DD3HG2",
 343  "6G3HGBFEG8I3F25EAF61B904EIA40CFDH2124F",
 344  "10AHC3D29EBHDF3HD97905CG0JA8061855C3FI",
 345  "3BA5A55J5K699B2D09C38A4B237CH51IHA132",
 346  "EDEA90DJ0B5CB3FGG1C8587FEB99D3C143CA",
 347  "31M26JI1BBD56K3I028MML4EEDMAJK60LGLE",
 348  "GGG5M3142FKKG82EJ28111D70EMHC241E4E",
 349  "4446F4D5H10982023N297BF0DKBBHLLJB0I",
 350  "12E9DEEOBMKAKEP0IM284MIP7FO1O521M46",
 351  "85NN0HD48NN2FDDB1F5BMMKIB8CK20MDPK",
 352  "2D882A7A0O0JPCJ4APDRIB77IABAKDGJP2",
 353  "MFMCI0R7S27AAA3O3L2S8K44HKA7O02CN",
 354  "7IGQS73FFSHC50NNH44B6PTTNLC3M6H78",
 355  "2KLUB3U9850CSN6ANIDNIF1LB29MJ43LH",
 356  "UT52GTL18CJ9H4HR0TJTK6ESUFBHF5FE",
 357  "BTVL87QQBMUGF8PFWU4W3VU7U922QTMW",
 358  "4OG10HW0MSWJBIDEE2PDH24GA7RIHIAA",
 359  "1W8W9AX2DRUX48GXOLMK0PE42H0FEUWN",
 360  "SVWI84VBH069WR15W1U2VTK06USY8Z2",
 361  "CPTPNPDa5TYCPPNLALENT9IMX2GL0W2",
 362  "5QU21UJMRaUYYYYYN6GHSMPOYOXEEUY",
 363  "2O2Q7C6RPPB1SXJ9bR4035SPaQQ3H2W",
 364  "18d994IbT4PHbD7cGIPCRP00bbQO0bc",
 365  "NcDUEEWRO7XT76260WGeBHPVa72RdA",
 366  "BbX2WCF9VfSB5LPdJAdeXKV1fd6LC2",
 367  "60QDKW67P4JSQaTdQg7JE9ISafLaVU",
 368  "33ba9XbDbRdNF4BeDB2XYMhAVDaBdA",
 369  "1RIPZJA8gT5L5H7fTcaRhQ39geMMTc",
 370  "d65j70fBATjcDiidPYXUGcaBVVLME",
 371  "LKA9jhPabDG612TXWkhfT2gMXNIP2",
 372  "BgNaYhjfT0G8PBcYRP8khJCR3C9QE",
 373  "6Wk8RhJTAgDh10fYAiUVB1aM0HacG",
 374  "3dOCjaf78kd5EQNViUZWj3AfFL90I",
 375  "290VWkL3aiJoW4MBbHk0Z0bDo22Ni",
 376  "1DbDZ1hpPZNUDBUp6UigcJllEdC26",
 377  "dFSOLBUM7UZX8Vnc6qokGIOiFo1h",
 378  "NcoUYJOg0HVmKI9fR2ag0S8R2hrK",
 379  "EOpiJ5Te7oDe2pn8ZhAUKkhFHlZh",
 380  "8nXK8rp8neV8LWta1WDgd1QnlWsU",
 381  "5T3d6bcSBtHgrH9bCbu84tblaa7r",
 382  "3PlUDIYUvMqOVCir7AtquK5dWanq",
 383  "2A70gDPX2AtiicvIGGk9poiMtgvu",
 384  "1MjiRxjk10J6SVAxFguv9kZiUnIc",
 385  "rpre2vIDeb4h3sp50r1YBbtEx9L",
 386  "ZHcoip0AglDAfibrsUcJ9M1C8fm",
 387  "NHP18+eoe6uU54W49Kc6ZK7+bT2",
 388  "FTAA7QXGoQOaZi7PzePtFFN5vNk"
 389};
 390
 391const unsigned char b_mp4[] = {
 392  0x01, 
 393#if MP_DIGIT_MAX > MP_32BIT_MAX
 394  0x00, 0x00, 0x00, 0x00,
 395#endif
 396  0x63, 0xDB, 0xC2, 0x26, 
 397  0x5B, 0x88, 0x26, 0x8D, 
 398  0xC8, 0x01, 0xC1, 0x0E, 
 399  0xA6, 0x84, 0x76, 0xB7, 
 400  0xBD, 0xE0, 0x09, 0x0F
 401};
 402
 403/* Search for a test suite name in the names table  */
 404int  find_name(char *name);
 405void reason(char *fmt, ...);
 406
 407/*------------------------------------------------------------------------*/
 408/*------------------------------------------------------------------------*/
 409
 410char g_intbuf[4096];  /* buffer for integer comparison   */
 411char a_intbuf[4096];  /* buffer for integer comparison   */
 412int  g_verbose = 1;   /* print out reasons for failure?  */
 413int  res;
 414
 415#define IFOK(x) { if (MP_OKAY > (res = (x))) { \
 416  reason("test %s failed: error %d\n", #x, res); return 1; }}
 417
 418int main(int argc, char *argv[])
 419{
 420  int which, res;
 421
 422  srand((unsigned int)time(NULL));
 423
 424  if (argc < 2) {
 425    fprintf(stderr, "Usage: %s <test-suite> | list\n"
 426	    "Type '%s help' for assistance\n", argv[0], argv[0]);
 427    return 2;
 428  } else if(argc > 2) {
 429    if(strcmp(argv[2], "quiet") == 0)
 430      g_verbose = 0;
 431  }
 432
 433  if(strcmp(argv[1], "help") == 0) {
 434    fprintf(stderr, "Help for mpi-test\n\n"
 435	    "This program is a test driver for the MPI library, which\n"
 436	    "tests all the various functions in the library to make sure\n"
 437	    "they are working correctly.  The syntax is:\n"
 438	    "    %s <suite-name>\n"
 439	    "...where <suite-name> is the name of the test you wish to\n"
 440	    "run.  To get a list of the tests, use '%s list'.\n\n"
 441	    "The program exits with a status of zero if the test passes,\n"
 442	    "or non-zero if it fails.  Ordinarily, failure is accompanied\n"
 443	    "by a diagnostic message to standard error.  To suppress this\n"
 444	    "add the keyword 'quiet' after the suite-name on the command\n"
 445	    "line.\n\n", argv[0], argv[0]);
 446    return 0;
 447  }
 448
 449  if ((which = find_name(argv[1])) < 0) {
 450    fprintf(stderr, "%s: test suite '%s' is not known\n", argv[0], argv[1]);
 451    return 2;
 452  }
 453
 454  if((res = (g_tests[which])()) < 0) {
 455    fprintf(stderr, "%s: test suite not implemented yet\n", argv[0]);
 456    return 2;
 457  } else {
 458    return res; 
 459  }
 460
 461}
 462
 463/*------------------------------------------------------------------------*/
 464
 465int find_name(char *name)
 466{
 467  int ix = 0;
 468  
 469  while(ix < g_count) {
 470    if (strcmp(name, g_names[ix]) == 0)
 471      return ix;
 472    
 473    ++ix;
 474  }
 475  
 476  return -1;
 477}
 478
 479/*------------------------------------------------------------------------*/
 480
 481int test_list(void)
 482{
 483  int ix;
 484  
 485  fprintf(stderr, "There are currently %d test suites available\n",
 486	  g_count);
 487  
 488  for(ix = 1; ix < g_count; ix++)
 489    fprintf(stdout, "%-20s %s\n", g_names[ix], g_descs[ix]);
 490  
 491  return 0;
 492}
 493
 494/*------------------------------------------------------------------------*/
 495
 496int test_copy(void)
 497{
 498  mp_int  a, b;
 499  int     ix;
 500
 501  mp_init(&a); mp_init(&b);
 502
 503  mp_read_radix(&a, mp3, 16);
 504  mp_copy(&a, &b);
 505
 506  if(SIGN(&a) != SIGN(&b) || USED(&a) != USED(&b)) {
 507    if(SIGN(&a) != SIGN(&b)) {
 508      reason("error: sign of original is %d, sign of copy is %d\n", 
 509	     SIGN(&a), SIGN(&b));
 510    } else {
 511      reason("error: original precision is %d, copy precision is %d\n",
 512	     USED(&a), USED(&b));
 513    }
 514    mp_clear(&a); mp_clear(&b);
 515    return 1;
 516  }
 517
 518  for(ix = 0; ix < USED(&b); ix++) {
 519    if(DIGIT(&a, ix) != DIGIT(&b, ix)) {
 520      reason("error: digit %d " DIGIT_FMT " != " DIGIT_FMT "\n",
 521	     ix, DIGIT(&a, ix), DIGIT(&b, ix));
 522      mp_clear(&a); mp_clear(&b);
 523      return 1;
 524    }
 525  }
 526     
 527  mp_clear(&a); mp_clear(&b);
 528  return 0;
 529}
 530
 531/*------------------------------------------------------------------------*/
 532
 533int test_exch(void)
 534{
 535  mp_int  a, b;
 536
 537  mp_init(&a); mp_init(&b);
 538  mp_read_radix(&a, mp7, 16); mp_read_radix(&b, mp1, 16);
 539
 540  mp_exch(&a, &b);
 541  mp_toradix(&a, g_intbuf, 16);
 542
 543  mp_clear(&a);
 544  if(strcmp(g_intbuf, mp1) != 0) {
 545    mp_clear(&b);
 546    reason("error: exchange failed\n");
 547    return 1;
 548  }
 549
 550  mp_toradix(&b, g_intbuf, 16);
 551
 552  mp_clear(&b);
 553  if(strcmp(g_intbuf, mp7) != 0) {
 554    reason("error: exchange failed\n");
 555    return 1;
 556  }
 557
 558  return 0;
 559}
 560
 561/*------------------------------------------------------------------------*/
 562
 563int test_zero(void)
 564{
 565  mp_int   a;
 566
 567  mp_init(&a); mp_read_radix(&a, mp7, 16);
 568  mp_zero(&a);
 569
 570  if(USED(&a) != 1 || DIGIT(&a, 1) != 0) {
 571    mp_toradix(&a, g_intbuf, 16);
 572    reason("error: result is %s\n", g_intbuf);
 573    mp_clear(&a);
 574    return 1;
 575  }
 576
 577  mp_clear(&a);
 578  return 0;
 579}
 580
 581/*------------------------------------------------------------------------*/
 582
 583int test_set(void)
 584{
 585  mp_int   a;
 586
 587  /* Test single digit set */
 588  mp_init(&a); mp_set(&a, 5);
 589  if(DIGIT(&a, 0) != 5) {
 590    mp_toradix(&a, g_intbuf, 16);
 591    reason("error: result is %s, expected 5\n", g_intbuf);
 592    mp_clear(&a);
 593    return 1;
 594  }
 595
 596  /* Test integer set */
 597  mp_set_int(&a, -4938110);
 598  mp_toradix(&a, g_intbuf, 16);
 599  mp_clear(&a);
 600  if(strcmp(g_intbuf, mp5a) != 0) {
 601    reason("error: result is %s, expected %s\n", g_intbuf, mp5a);
 602    return 1;
 603  }
 604
 605  return 0;
 606}
 607
 608/*------------------------------------------------------------------------*/
 609
 610int test_abs(void)
 611{
 612  mp_int  a;
 613
 614  mp_init(&a); mp_read_radix(&a, mp4, 16);
 615  mp_abs(&a, &a);
 616  
 617  if(SIGN(&a) != ZPOS) {
 618    reason("error: sign of result is negative\n");
 619    mp_clear(&a);
 620    return 1;
 621  }
 622
 623  mp_clear(&a);
 624  return 0;
 625}
 626
 627/*------------------------------------------------------------------------*/
 628
 629int test_neg(void)
 630{
 631  mp_int  a;
 632  mp_sign s;
 633
 634  mp_init(&a); mp_read_radix(&a, mp4, 16);
 635
 636  s = SIGN(&a);
 637  mp_neg(&a, &a);
 638  if(SIGN(&a) == s) {
 639    reason("error: sign of result is same as sign of nonzero input\n");
 640    mp_clear(&a);
 641    return 1;
 642  }
 643
 644  mp_clear(&a);
 645  return 0;
 646}
 647
 648/*------------------------------------------------------------------------*/
 649
 650int test_add_d(void)
 651{
 652  mp_int  a;
 653
 654  mp_init(&a);
 655  
 656  mp_read_radix(&a, mp5, 16);
 657  mp_add_d(&a, md4, &a);
 658  mp_toradix(&a, g_intbuf, 16);
 659
 660  if(strcmp(g_intbuf, s_mp5d4) != 0) {
 661    reason("error: computed %s, expected %s\n", g_intbuf, s_mp5d4);
 662    mp_clear(&a);
 663    return 1;
 664  }
 665
 666  mp_read_radix(&a, mp2, 16);
 667  mp_add_d(&a, md5, &a);
 668  mp_toradix(&a, g_intbuf, 16);
 669
 670  if(strcmp(g_intbuf, s_mp2d5) != 0) {
 671    reason("error: computed %s, expected %s\n", g_intbuf, s_mp2d5);
 672    mp_clear(&a);
 673    return 1;
 674  }
 675
 676  mp_clear(&a);
 677  return 0;
 678}
 679
 680/*------------------------------------------------------------------------*/
 681
 682int test_add(void)
 683{
 684  mp_int  a, b;
 685  int     res = 0;
 686
 687  mp_init(&a); mp_init(&b);
 688
 689  mp_read_radix(&a, mp1, 16); mp_read_radix(&b, mp3, 16);
 690  mp_add(&a, &b, &a);
 691  mp_toradix(&a, g_intbuf, 16);
 692
 693  if(strcmp(g_intbuf, s_mp13) != 0) {
 694    reason("error: computed %s, expected %s\n", g_intbuf, s_mp13);
 695    res = 1; goto CLEANUP;
 696  }
 697
 698  mp_read_radix(&a, mp4, 16);
 699  mp_add(&a, &b, &a);
 700  mp_toradix(&a, g_intbuf, 16);
 701
 702  if(strcmp(g_intbuf, s_mp34) != 0) {
 703    reason("error: computed %s, expected %s\n", g_intbuf, s_mp34);
 704    res = 1; goto CLEANUP;
 705  }
 706
 707  mp_read_radix(&a, mp4, 16); mp_read_radix(&b, mp6, 16);
 708  mp_add(&a, &b, &a);
 709  mp_toradix(&a, g_intbuf, 16);
 710
 711  if(strcmp(g_intbuf, s_mp46) != 0) {
 712    reason("error: computed %s, expected %s\n", g_intbuf, s_mp46);
 713    res = 1; goto CLEANUP;
 714  }
 715
 716  mp_read_radix(&a, mp14, 16); mp_read_radix(&b, mp15, 16);
 717  mp_add(&a, &b, &a);
 718  mp_toradix(&a, g_intbuf, 16);
 719
 720  if(strcmp(g_intbuf, s_mp1415) != 0) {
 721    reason("error: computed %s, expected %s\n", g_intbuf, s_mp1415);
 722    res = 1;
 723  }
 724
 725 CLEANUP:
 726  mp_clear(&a); mp_clear(&b);
 727  return res;
 728}
 729
 730/*------------------------------------------------------------------------*/
 731
 732int test_sub_d(void)
 733{
 734  mp_int   a;
 735
 736  mp_init(&a);
 737  mp_read_radix(&a, mp5, 16);
 738
 739  mp_sub_d(&a, md4, &a);
 740  mp_toradix(&a, g_intbuf, 16);
 741
 742  if(strcmp(g_intbuf, d_mp5d4) != 0) {
 743    reason("error: computed %s, expected %s\n", g_intbuf, d_mp5d4);
 744    mp_clear(&a);
 745    return 1;
 746  }
 747
 748  mp_read_radix(&a, mp6, 16);
 749  
 750  mp_sub_d(&a, md2, &a);
 751  mp_toradix(&a, g_intbuf, 16);
 752  
 753  mp_clear(&a);
 754  if(strcmp(g_intbuf, d_mp6d2) != 0) {
 755    reason("error: computed %s, expected %s\n", g_intbuf, d_mp6d2);
 756    return 1;
 757  }
 758
 759  return 0;
 760}
 761
 762/*------------------------------------------------------------------------*/
 763
 764int test_sub(void)
 765{
 766  mp_int  a, b;
 767
 768  mp_init(&a); mp_init(&b);
 769
 770  mp_read_radix(&a, mp1, 16); mp_read_radix(&b, mp2, 16);
 771  mp_sub(&a, &b, &a);
 772  mp_toradix(&a, g_intbuf, 16);
 773
 774  if(strcmp(g_intbuf, d_mp12) != 0) {
 775    reason("error: computed %s, expected %s\n", g_intbuf, d_mp12);
 776    mp_clear(&a); mp_clear(&b);
 777    return 1;
 778  }
 779
 780  mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16);
 781  mp_sub(&a, &b, &a);
 782  mp_toradix(&a, g_intbuf, 16);
 783
 784  if(strcmp(g_intbuf, d_mp34) != 0) {
 785    reason("error: computed %s, expected %s\n", g_intbuf, d_mp34);
 786    mp_clear(&a); mp_clear(&b);
 787    return 1;
 788  }
 789
 790  mp_clear(&a); mp_clear(&b);
 791  return 0;
 792}
 793
 794/*------------------------------------------------------------------------*/
 795
 796int test_mul_d(void)
 797{
 798  mp_int   a;
 799
 800  mp_init(&a);
 801  mp_read_radix(&a, mp1, 16);
 802
 803  IFOK( mp_mul_d(&a, md4, &a) );
 804  mp_toradix(&a, g_intbuf, 16);
 805  
 806  if(strcmp(g_intbuf, p_mp1d4) != 0) {
 807    reason("error: computed %s, expected %s\n", g_intbuf, p_mp1d4);    
 808    mp_clear(&a);
 809    return 1;
 810  }
 811
 812  mp_read_radix(&a, mp8, 16);
 813  IFOK( mp_mul_d(&a, md6, &a) );
 814  mp_toradix(&a, g_intbuf, 16);
 815
 816  mp_clear(&a);
 817  if(strcmp(g_intbuf, p_mp8d6) != 0) {
 818    reason("error: computed %s, expected %s\n", g_intbuf, p_mp8d6); 
 819    return 1;
 820  }
 821
 822  return 0;
 823}
 824
 825/*------------------------------------------------------------------------*/
 826
 827int test_mul(void)
 828{
 829  mp_int   a, b;
 830  int      res = 0;
 831
 832  mp_init(&a); mp_init(&b);
 833  mp_read_radix(&a, mp1, 16); mp_read_radix(&b, mp2, 16);
 834
 835  IFOK( mp_mul(&a, &b, &a) );
 836  mp_toradix(&a, g_intbuf, 16);
 837
 838  if(strcmp(g_intbuf, p_mp12) != 0) {
 839    reason("error: computed %s, expected %s\n", g_intbuf, p_mp12);
 840    res = 1; goto CLEANUP;
 841  }
 842
 843  mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16);
 844  IFOK( mp_mul(&a, &b, &a) );
 845  mp_toradix(&a, g_intbuf, 16);
 846
 847  if(strcmp(g_intbuf, p_mp34) !=0) {
 848    reason("error: computed %s, expected %s\n", g_intbuf, p_mp34);
 849    res = 1; goto CLEANUP;
 850  }
 851
 852  mp_read_radix(&a, mp5, 16); mp_read_radix(&b, mp7, 16);
 853  IFOK( mp_mul(&a, &b, &a) );
 854  mp_toradix(&a, g_intbuf, 16);
 855
 856  if(strcmp(g_intbuf, p_mp57) != 0) {
 857    reason("error: computed %s, expected %s\n", g_intbuf, p_mp57);
 858    res = 1; goto CLEANUP;
 859  }
 860
 861  mp_read_radix(&a, mp11, 16); mp_read_radix(&b, mp13, 16);
 862  IFOK( mp_mul(&a, &b, &a) );
 863  mp_toradix(&a, g_intbuf, 16);
 864
 865  if(strcmp(g_intbuf, p_mp1113) != 0) {
 866    reason("error: computed %s, expected %s\n", g_intbuf, p_mp1113);
 867    res = 1; goto CLEANUP;
 868  }
 869
 870  mp_read_radix(&a, mp14, 16); mp_read_radix(&b, mp15, 16);
 871  IFOK( mp_mul(&a, &b, &a) );
 872  mp_toradix(&a, g_intbuf, 16);
 873
 874  if(strcmp(g_intbuf, p_mp1415) != 0) {
 875    reason("error: computed %s, expected %s\n", g_intbuf, p_mp1415);
 876    res = 1;
 877  }
 878  mp_read_radix(&a, mp21, 10); mp_read_radix(&b, mp21, 10);
 879
 880  IFOK( mp_mul(&a, &b, &a) );
 881  mp_toradix(&a, g_intbuf, 10);
 882
 883  if(strcmp(g_intbuf, p_mp2121) != 0) {
 884    reason("error: computed %s, expected %s\n", g_intbuf, p_mp2121);
 885    res = 1; goto CLEANUP;
 886  }
 887
 888 CLEANUP:
 889  mp_clear(&a); mp_clear(&b);
 890  return res;
 891
 892}
 893
 894/*------------------------------------------------------------------------*/
 895
 896int test_sqr(void)
 897{
 898  mp_int  a;
 899
 900  mp_init(&a); mp_read_radix(&a, mp2, 16);
 901
 902  mp_sqr(&a, &a);
 903  mp_toradix(&a, g_intbuf, 16);
 904
 905  mp_clear(&a);
 906  if(strcmp(g_intbuf, p_mp22) != 0) {
 907    reason("error: computed %s, expected %s\n", g_intbuf, p_mp22);
 908    return 1;
 909  }
 910
 911  return 0;
 912}
 913
 914/*------------------------------------------------------------------------*/
 915
 916int test_div_d(void)
 917{
 918  mp_int    a, q;
 919  mp_digit  r;
 920  int       err = 0;
 921
 922  mp_init(&a); mp_init(&q);
 923  mp_read_radix(&a, mp3, 16);
 924
 925  IFOK( mp_div_d(&a, md6, &q, &r) );
 926  mp_toradix(&q, g_intbuf, 16);
 927
 928  if(strcmp(g_intbuf, q_mp3d6) != 0) {
 929    reason("error: computed q = %s, expected %s\n", g_intbuf, q_mp3d6);
 930    ++err;
 931  }
 932
 933  sprintf(g_intbuf, ZS_DIGIT_FMT, r);
 934
 935  if(strcmp(g_intbuf, r_mp3d6) != 0) {
 936    reason("error: computed r = %s, expected %s\n", g_intbuf, r_mp3d6);
 937    ++err;
 938  }
 939
 940  mp_read_radix(&a, mp9, 16);
 941  IFOK( mp_div_d(&a, 16, &q, &r) );
 942  mp_toradix(&q, g_intbuf, 16);
 943
 944  if(strcmp(g_intbuf, q_mp9c16) != 0) {
 945    reason("error: computed q = %s, expected %s\n", g_intbuf, q_mp9c16);
 946    ++err;
 947  }
 948
 949  sprintf(g_intbuf, ZS_DIGIT_FMT, r);
 950
 951  if(strcmp(g_intbuf, r_mp9c16) != 0) {
 952    reason("error: computed r = %s, expected %s\n", g_intbuf, r_mp9c16);
 953    ++err;
 954  }
 955
 956  mp_clear(&a); mp_clear(&q);
 957  return err;
 958}
 959
 960/*------------------------------------------------------------------------*/
 961
 962int test_div_2(void)
 963{
 964  mp_int  a;
 965
 966  mp_init(&a); mp_read_radix(&a, mp7, 16);
 967  IFOK( mp_div_2(&a, &a) );
 968  mp_toradix(&a, g_intbuf, 16);
 969
 970  mp_clear(&a);
 971  if(strcmp(g_intbuf, q_mp7c2) != 0) {
 972    reason("error: computed %s, expected %s\n", g_intbuf, q_mp7c2);
 973    return 1;
 974  }
 975    
 976  return 0;
 977}
 978
 979/*------------------------------------------------------------------------*/
 980
 981int test_div_2d(void)
 982{
 983  mp_int  a, q, r;
 984
 985  mp_init(&q); mp_init(&r);
 986  mp_init(&a); mp_read_radix(&a, mp13, 16);
 987
 988  IFOK( mp_div_2d(&a, 64, &q, &r) );
 989  mp_clear(&a);
 990
 991  mp_toradix(&q, g_intbuf, 16);
 992
 993  if(strcmp(g_intbuf, q_mp13c) != 0) {
 994    reason("error: computed %s, expected %s\n", g_intbuf, q_mp13c);
 995    mp_clear(&q); mp_clear(&r);
 996    return 1;
 997  }
 998
 999  mp_clear(&q);
1000
1001  mp_toradix(&r, g_intbuf, 16);
1002  if(strcmp(g_intbuf, r_mp13c) != 0) {
1003    reason("error, computed %s, expected %s\n", g_intbuf, r_mp13c);
1004    mp_clear(&r);
1005    return 1;
1006  }
1007
1008  mp_clear(&r);
1009  
1010  return 0;
1011}
1012
1013/*------------------------------------------------------------------------*/
1014
1015int test_div(void)
1016{
1017  mp_int  a, b, r;
1018  int     err = 0;
1019
1020  mp_init(&a); mp_init(&b); mp_init(&r);
1021
1022  mp_read_radix(&a, mp4, 16); mp_read_radix(&b, mp2, 16);
1023  IFOK( mp_div(&a, &b, &a, &r) );
1024  mp_toradix(&a, g_intbuf, 16);
1025
1026  if(strcmp(g_intbuf, q_mp42) != 0) {
1027    reason("error: test 1 computed quot %s, expected %s\n", g_intbuf, q_mp42);
1028    ++err;
1029  }
1030
1031  mp_toradix(&r, g_intbuf, 16);
1032
1033  if(strcmp(g_intbuf, r_mp42) != 0) {
1034    reason("error: test 1 computed rem %s, expected %s\n", g_intbuf, r_mp42);
1035    ++err;
1036  }
1037
1038  mp_read_radix(&a, mp4, 16); mp_read_radix(&b, mp5a, 16);
1039  IFOK( mp_div(&a, &b, &a, &r) );
1040  mp_toradix(&a, g_intbuf, 16);
1041
1042  if(strcmp(g_intbuf, q_mp45a) != 0) {
1043    reason("error: test 2 computed quot %s, expected %s\n", g_intbuf, q_mp45a);
1044    ++err;
1045  }
1046
1047  mp_toradix(&r, g_intbuf, 16);
1048
1049  if(strcmp(g_intbuf, r_mp45a) != 0) {
1050    reason("error: test 2 computed rem %s, expected %s\n", g_intbuf, r_mp45a);
1051    ++err;
1052  }
1053
1054  mp_read_radix(&a, mp14, 16); mp_read_radix(&b, mp4, 16);
1055  IFOK( mp_div(&a, &b, &a, &r) );
1056  mp_toradix(&a, g_intbuf, 16);
1057
1058  if(strcmp(g_intbuf, q_mp1404) != 0) {
1059    reason("error: test 3 computed quot %s, expected %s\n", g_intbuf, q_mp1404);
1060    ++err;
1061  }
1062
1063  mp_toradix(&r, g_intbuf, 16);
1064  
1065  if(strcmp(g_intbuf, r_mp1404) != 0) {
1066    reason("error: test 3 computed rem %s, expected %s\n", g_intbuf, r_mp1404);
1067    ++err;
1068  }
1069
1070  mp_clear(&a); mp_clear(&b); mp_clear(&r);
1071
1072  return err;
1073}
1074
1075/*------------------------------------------------------------------------*/
1076
1077int test_expt_d(void)
1078{
1079  mp_int   a;
1080
1081  mp_init(&a); mp_read_radix(&a, mp5, 16);
1082  mp_expt_d(&a, md9, &a);
1083  mp_toradix(&a, g_intbuf, 16);
1084
1085  mp_clear(&a);
1086  if(strcmp(g_intbuf, e_mp5d9) != 0) {
1087    reason("error: computed %s, expected %s\n", g_intbuf, e_mp5d9);
1088    return 1;
1089  }
1090
1091  return 0;
1092}
1093
1094/*------------------------------------------------------------------------*/
1095
1096int test_expt(void)
1097{
1098  mp_int   a, b;
1099
1100  mp_init(&a); mp_init(&b);
1101  mp_read_radix(&a, mp7, 16); mp_read_radix(&b, mp8, 16);
1102
1103  mp_expt(&a, &b, &a);
1104  mp_toradix(&a, g_intbuf, 16);
1105  mp_clear(&a); mp_clear(&b);
1106
1107  if(strcmp(g_intbuf, e_mp78) != 0) {
1108    reason("error: computed %s, expected %s\n", g_intbuf, e_mp78);
1109    return 1;
1110  }
1111
1112  return 0;
1113}
1114
1115/*------------------------------------------------------------------------*/
1116
1117int test_2expt(void)
1118{
1119  mp_int   a;
1120
1121  mp_init(&a);
1122  mp_2expt(&a, md3);
1123  mp_toradix(&a, g_intbuf, 16);
1124  mp_clear(&a);
1125
1126  if(strcmp(g_intbuf, e_mpc2d3) != 0) {
1127    reason("error: computed %s, expected %s\n", g_intbuf, e_mpc2d3);
1128    return 1;
1129  }
1130
1131  return 0;
1132}
1133
1134/*------------------------------------------------------------------------*/
1135
1136int test_sqrt(void)
1137{
1138  mp_int  a;
1139  int     res = 0;
1140
1141  mp_init(&a); mp_read_radix(&a, mp9, 16);
1142  mp_sqrt(&a, &a);
1143  mp_toradix(&a, g_intbuf, 16);
1144
1145  if(strcmp(g_intbuf, t_mp9) != 0) {
1146    reason("error: computed %s, expected %s\n", g_intbuf, t_mp9);
1147    res = 1; goto CLEANUP;
1148  }
1149
1150  mp_read_radix(&a, mp15, 16);
1151  mp_sqrt(&a, &a);
1152  mp_toradix(&a, g_intbuf, 16);
1153
1154  if(strcmp(g_intbuf, t_mp15) != 0) {
1155    reason("error: computed %s, expected %s\n", g_intbuf, t_mp15);
1156    res = 1;
1157  }
1158
1159 CLEANUP:
1160  mp_clear(&a);
1161  return res;
1162}
1163
1164/*------------------------------------------------------------------------*/
1165
1166int test_mod_d(void)
1167{
1168  mp_int     a;
1169  mp_digit   r;
1170
1171  mp_init(&a); mp_read_radix(&a, mp5, 16);
1172  IFOK( mp_mod_d(&a, md5, &r) );
1173  sprintf(g_intbuf, ZS_DIGIT_FMT, r);
1174  mp_clear(&a);
1175
1176  if(strcmp(g_intbuf, r_mp5d5) != 0) {
1177    reason("error: computed %s, expected %s\n", g_intbuf, r_mp5d5);
1178    return 1;
1179  }
1180
1181  return 0;
1182}
1183
1184/*------------------------------------------------------------------------*/
1185
1186int test_mod(void)
1187{
1188  mp_int  a, m;
1189
1190  mp_init(&a); mp_init(&m);
1191  mp_read_radix(&a, mp4, 16); mp_read_radix(&m, mp7, 16);
1192  IFOK( mp_mod(&a, &m, &a) );
1193  mp_toradix(&a, g_intbuf, 16);
1194  mp_clear(&a); mp_clear(&m);
1195
1196  if(strcmp(g_intbuf, r_mp47) != 0) {
1197    reason("error: computed %s, expected %s\n", g_intbuf, r_mp47);
1198    return 1;
1199  }
1200
1201  return 0;
1202}
1203
1204/*------------------------------------------------------------------------*/
1205
1206int test_addmod(void)
1207{
1208  mp_int a, b, m;
1209
1210  mp_init(&a); mp_init(&b); mp_init(&m);
1211  mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16);
1212  mp_read_radix(&m, mp5, 16);
1213
1214  IFOK( mp_addmod(&a, &b, &m, &a) );
1215  mp_toradix(&a, g_intbuf, 16);
1216  mp_clear(&a); mp_clear(&b); mp_clear(&m);
1217
1218  if(strcmp(g_intbuf, ms_mp345) != 0) {
1219    reason("error: computed %s, expected %s\n", g_intbuf, ms_mp345);
1220    return 1;
1221  }
1222
1223  return 0;
1224}
1225
1226/*------------------------------------------------------------------------*/
1227
1228int test_submod(void)
1229{
1230  mp_int a, b, m;
1231
1232  mp_init(&a); mp_init(&b); mp_init(&m);
1233  mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16);
1234  mp_read_radix(&m, mp5, 16);
1235
1236  IFOK( mp_submod(&a, &b, &m, &a) );
1237  mp_toradix(&a, g_intbuf, 16);
1238  mp_clear(&a); mp_clear(&b); mp_clear(&m);
1239
1240  if(strcmp(g_intbuf, md_mp345) != 0) {
1241    reason("error: computed %s, expected %s\n", g_intbuf, md_mp345);
1242    return 1;
1243  }
1244
1245  return 0;
1246}
1247
1248/*------------------------------------------------------------------------*/
1249
1250int test_mulmod(void)
1251{
1252  mp_int a, b, m;
1253
1254  mp_init(&a); mp_init(&b); mp_init(&m);
1255  mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16);
1256  mp_read_radix(&m, mp5, 16);
1257
1258  IFOK( mp_mulmod(&a, &b, &m, &a) );
1259  mp_toradix(&a, g_intbuf, 16);
1260  mp_clear(&a); mp_clear(&b); mp_clear(&m);
1261
1262  if(strcmp(g_intbuf, mp_mp345) != 0) {
1263    reason("error: computed %s, expected %s\n", g_intbuf, mp_mp345);
1264    return 1;
1265  }
1266
1267  return 0;
1268}
1269
1270/*------------------------------------------------------------------------*/
1271
1272int test_sqrmod(void)
1273{
1274  mp_int a, m;
1275
1276  mp_init(&a); mp_init(&m);
1277  mp_read_radix(&a, mp3, 16); mp_read_radix(&m, mp5, 16);
1278
1279  IFOK( mp_sqrmod(&a, &m, &a) );
1280  mp_toradix(&a, g_intbuf, 16);
1281  mp_clear(&a); mp_clear(&m);
1282
1283  if(strcmp(g_intbuf, mp_mp335) != 0) {
1284    reason("error: computed %s, expected %s\n", g_intbuf, mp_mp335);
1285    return 1;
1286  }
1287
1288  return 0;
1289}
1290
1291/*------------------------------------------------------------------------*/
1292
1293int test_exptmod(void)
1294{
1295  mp_int  a, b, m;
1296  int     res = 0;
1297
1298  mp_init(&a); mp_init(&b); mp_init(&m);
1299  mp_read_radix(&a, mp8, 16); mp_read_radix(&b, mp1, 16);
1300  mp_read_radix(&m, mp7, 16);
1301
1302  IFOK( mp_exptmod(&a, &b, &m, &a) );
1303  mp_toradix(&a, g_intbuf, 16);
1304
1305  if(strcmp(g_intbuf, me_mp817) != 0) {
1306    reason("case 1: error: computed %s, expected %s\n", g_intbuf, me_mp817);
1307    res = 1; goto CLEANUP;
1308  }
1309
1310  mp_read_radix(&a, mp1, 16); mp_read_radix(&b, mp5, 16);
1311  mp_read_radix(&m, mp12, 16);
1312
1313  IFOK( mp_exptmod(&a, &b, &m, &a) );
1314  mp_toradix(&a, g_intbuf, 16);
1315
1316  if(strcmp(g_intbuf, me_mp1512) != 0) {
1317    reason("case 2: error: computed %s, expected %s\n", g_intbuf, me_mp1512);
1318    res = 1; goto CLEANUP;
1319  }
1320
1321  mp_read_radix(&a, mp5, 16); mp_read_radix(&b, mp1, 16);
1322  mp_read_radix(&m, mp14, 16);
1323
1324  IFOK( mp_exptmod(&a, &b, &m, &a) );
1325  mp_toradix(&a, g_intbuf, 16);
1326
1327  if(strcmp(g_intbuf, me_mp5114) != 0) {
1328    reason("case 3: error: computed %s, expected %s\n", g_intbuf, me_mp5114);
1329    res = 1;
1330  }
1331
1332  mp_read_radix(&a, mp16, 16); mp_read_radix(&b, mp17, 16);
1333  mp_read_radix(&m, mp18, 16);
1334
1335  IFOK( mp_exptmod(&a, &b, &m, &a) );
1336  mp_toradix(&a, g_intbuf, 16);
1337
1338  if(strcmp(g_intbuf, me_mp161718) != 0) {
1339    reason("case 4: error: computed %s, expected %s\n", g_intbuf, me_mp161718);
1340    res = 1;
1341  }
1342
1343 CLEANUP:
1344  mp_clear(&a); mp_clear(&b); mp_clear(&m);
1345  return res;
1346}
1347
1348/*------------------------------------------------------------------------*/
1349
1350int test_exptmod_d(void)
1351{
1352  mp_int  a, m;
1353
1354  mp_init(&a); mp_init(&m);
1355  mp_read_radix(&a, mp5, 16); mp_read_radix(&m, mp7, 16);
1356
1357  IFOK( mp_exptmod_d(&a, md4, &m, &a) );
1358  mp_toradix(&a, g_intbuf, 16);
1359  mp_clear(&a); mp_clear(&m);
1360
1361  if(strcmp(g_intbuf, me_mp5d47) != 0) {
1362    reason("error: computed %s, expected %s\n", g_intbuf, me_mp5d47);
1363    return 1;
1364  }
1365
1366  return 0;
1367}
1368
1369/*------------------------------------------------------------------------*/
1370
1371int test_invmod(void)
1372{
1373  mp_int  a, m, c;
1374  mp_int  p1, p2, p3, p4, p5;
1375  mp_int  t1, t2, t3, t4;
1376  mp_err  res;
1377
1378  /* 5 128-bit primes. */
1379  static const char ivp1[] = { "AAD8A5A2A2BEF644BAEE7DB0CA643719" };
1380  static const char ivp2[] = { "CB371AD2B79A90BCC88D0430663E40B9" };
1381  static const char ivp3[] = { "C6C818D4DF2618406CA09280C0400099" };
1382  static const char ivp4[] = { "CE949C04512E68918006B1F0D7E93F27" };
1383  static const char ivp5[] = { "F8EE999B6416645040687440E0B89F51" };
1384
1385  mp_init(&a); mp_init(&m);
1386  mp_read_radix(&a, mp2, 16); mp_read_radix(&m, mp7, 16);
1387
1388  IFOK( mp_invmod(&a, &m, &a) );
1389
1390  mp_toradix(&a, g_intbuf, 16);
1391  mp_clear(&a); mp_clear(&m);
1392
1393  if(strcmp(g_intbuf, i_mp27) != 0) {
1394    reason("error: invmod test 1 computed %s, expected %s\n", g_intbuf, i_mp27);
1395    return 1;
1396  }
1397
1398  mp_init(&a); mp_init(&m);
1399  mp_read_radix(&a, mp20, 16); mp_read_radix(&m, mp19, 16);
1400
1401  IFOK( mp_invmod(&a, &m, &a) );
1402
1403  mp_toradix(&a, g_intbuf, 16);
1404  mp_clear(&a); mp_clear(&m);
1405
1406  if(strcmp(g_intbuf, i_mp2019) != 0) {
1407    reason("error: invmod test 2 computed %s, expected %s\n", g_intbuf, i_mp2019);
1408    return 1;
1409  }
1410
1411/* Need the following test cases:
1412  Odd modulus
1413    - a is odd,      relatively prime to m
1414    - a is odd,  not relatively prime to m
1415    - a is even,     relatively prime to m
1416    - a is even, not relatively prime to m
1417  Even modulus
1418    - a is even  (should fail)
1419    - a is odd,  not relatively prime to m
1420    - a is odd,      relatively prime to m,
1421      m is not a power of 2
1422	- m has factor 2**k, k < 32
1423	- m has factor 2**k, k > 32
1424      m is a power of 2, 2**k
1425	- k < 32
1426	- k > 32
1427*/
1428
1429  mp_init(&a);  mp_init(&m);  mp_init(&c);  
1430  mp_init(&p1); mp_init(&p2); mp_init(&p3); mp_init(&p4); mp_init(&p5); 
1431  mp_init(&t1); mp_init(&t2); mp_init(&t3); mp_init(&t4); 
1432
1433  mp_read_radix(&p1, ivp1, 16);
1434  mp_read_radix(&p2, ivp2, 16);
1435  mp_read_radix(&p3, ivp3, 16);
1436  mp_read_radix(&p4, ivp4, 16);
1437  mp_read_radix(&p5, ivp5, 16);
1438
1439  IFOK( mp_2expt(&t2, 68) );	/* t2 = 2**68 */
1440  IFOK( mp_2expt(&t3, 128) );	/* t3 = 2**128 */
1441  IFOK( mp_2expt(&t4, 31) );	/* t4 = 2**31 */
1442
1443/* test 3: Odd modulus - a is odd, relatively prime to m */
1444
1445  IFOK( mp_mul(&p1, &p2, &a) );
1446  IFOK( mp_mul(&p3, &p4, &m) );
1447  IFOK( mp_invmod(&a, &m, &t1) );
1448  IFOK( mp_invmod_xgcd(&a, &m, &c) );
1449
1450  if (mp_cmp(&t1, &c) != 0) {
1451    mp_toradix(&t1, g_intbuf, 16);
1452    mp_toradix(&c,  a_intbuf, 16);
1453    reason("error: invmod test 3 computed %s, expected %s\n", 
1454           g_intbuf, a_intbuf);
1455    return 1;
1456  }
1457  mp_clear(&a);  mp_clear(&t1); mp_clear(&c);  
1458  mp_init(&a);   mp_init(&t1);  mp_init(&c);   
1459
1460/* test 4: Odd modulus - a is odd, NOT relatively prime to m */
1461
1462  IFOK( mp_mul(&p1, &p3, &a) );
1463  /* reuse same m as before */
1464
1465  res = mp_invmod_xgcd(&a, &m, &c);
1466  if (res != MP_UNDEF) 
1467    goto CLEANUP4;
1468
1469  res = mp_invmod(&a, &m, &t1); /* we expect this to fail. */
1470  if (res != MP_UNDEF) {
1471CLEANUP4:
1472    reason("error: invmod test 4 succeeded, should have failed.\n");
1473    return 1;
1474  }
1475  mp_clear(&a);  mp_clear(&t1); mp_clear(&c);  
1476  mp_init(&a);   mp_init(&t1);  mp_init(&c);   
1477
1478/* test 5: Odd modulus - a is even, relatively prime to m */
1479
1480  IFOK( mp_mul(&p1, &t2, &a) );
1481  /* reuse m */
1482  IFOK( mp_invmod(&a, &m, &t1) );
1483  IFOK( mp_invmod_xgcd(&a, &m, &c) );
1484
1485  if (mp_cmp(&t1, &c) != 0) {
1486    mp_toradix(&t1, g_intbuf, 16);
1487    mp_toradix(&c,  a_intbuf, 16);
1488    reason("error: invmod test 5 computed %s, expected %s\n", 
1489           g_intbuf, a_intbuf);
1490    return 1;
1491  }
1492  mp_clear(&a);  mp_clear(&t1); mp_clear(&c);  
1493  mp_init(&a);   mp_init(&t1);  mp_init(&c);   
1494
1495/* test 6: Odd modulus - a is odd, NOT relatively prime to m */
1496
1497  /* reuse t2 */
1498  IFOK( mp_mul(&t2, &p3, &a) );
1499  /* reuse same m as before */
1500
1501  res = mp_invmod_xgcd(&a, &m, &c);
1502  if (res != MP_UNDEF) 
1503    goto CLEANUP6;
1504
1505  res = mp_invmod(&a, &m, &t1); /* we expect this to fail. */
1506  if (res != MP_UNDEF) {
1507CLEANUP6:
1508    reason("error: invmod test 6 succeeded, should have failed.\n");
1509    return 1;
1510  }
1511  mp_clear(&a);  mp_clear(&m); mp_clear(&c);  mp_clear(&t1); 
1512  mp_init(&a);   mp_init(&m);  mp_init(&c);   mp_init(&t1); 
1513
1514/* test 7: Even modulus, even a, should fail */
1515
1516  IFOK( mp_mul(&p3, &t3, &m) ); /* even m */
1517  /* reuse t2 */
1518  IFOK( mp_mul(&p1, &t2, &a) ); /* even a */
1519
1520  res = mp_invmod_xgcd(&a, &m, &c);
1521  if (res != MP_UNDEF) 
1522    goto CLEANUP7;
1523
1524  res = mp_invmod(&a, &m, &t1); /* we expect this to fail. */
1525  if (res != MP_UNDEF) {
1526CLEANUP7:
1527    reason("error: invmod test 7 succeeded, should have failed.\n");
1528    return 1;
1529  }
1530  mp_clear(&a);  mp_clear(&c);  mp_clear(&t1); 
1531  mp_init(&a);   mp_init(&c);   mp_init(&t1); 
1532
1533/* test 8: Even modulus    - a is odd,  not relatively prime to m */
1534
1535  /* reuse m */
1536  IFOK( mp_mul(&p3, &p1, &a) ); /* even a */
1537
1538  res = mp_invmod_xgcd(&a, &m, &c);
1539  if (res != MP_UNDEF) 
1540    goto CLEANUP8;
1541
1542  res = mp_invmod(&a, &m, &t1); /* we expect this to fail. */
1543  if (res != MP_UNDEF) {
1544CLEANUP8:
1545    reason("error: invmod test 8 succeeded, should have failed.\n");
1546    return 1;
1547  }
1548  mp_clear(&a);  mp_clear(&m); mp_clear(&c);  mp_clear(&t1); 
1549  mp_init(&a);   mp_init(&m);  mp_init(&c);   mp_init(&t1); 
1550
1551/* test 9: Even modulus    - m has factor 2**k, k < 32
1552 *	                   - a is odd, relatively prime to m,
1553 */
1554  IFOK( mp_mul(&p3, &t4, &m) ); /* even m */
1555  IFOK( mp_mul(&p1, &p2, &a) );
1556  IFOK( mp_invmod(&a, &m, &t1) );
1557  IFOK( mp_invmod_xgcd(&a, &m, &c) );
1558
1559  if (mp_cmp(&t1, &c) != 0) {
1560    mp_toradix(&t1, g_intbuf, 16);
1561    mp_toradix(&c,  a_intbuf, 16);
1562    reason("error: invmod test 9 computed %s, expected %s\n", 
1563           g_intbuf, a_intbuf);
1564    return 1;
1565  }
1566  mp_clear(&m);  mp_clear(&t1); mp_clear(&c);  
1567  mp_init(&m);   mp_init(&t1);  mp_init(&c);   
1568
1569/* test 10: Even modulus    - m has factor 2**k, k > 32
1570 *	                    - a is odd, relatively prime to m,
1571 */
1572  IFOK( mp_mul(&p3, &t3, &m) ); /* even m */
1573  /* reuse a */
1574  IFOK( mp_invmod(&a, &m, &t1) );
1575  IFOK( mp_invmod_xgcd(&a, &m, &c) );
1576
1577  if (mp_cmp(&t1, &c) != 0) {
1578    mp_toradix(&t1, g_intbuf, 16);
1579    mp_toradix(&c,  a_intbuf, 16);
1580    reason("error: invmod test 10 computed %s, expected %s\n", 
1581           g_intbuf, a_intbuf);
1582    return 1;
1583  }
1584  mp_clear(&t1); mp_clear(&c);  
1585  mp_init(&t1);  mp_init(&c);   
1586
1587/* test 11: Even modulus    - m is a power of 2, 2**k | k < 32
1588 *                          - a is odd, relatively prime to m,
1589 */
1590  IFOK( mp_invmod(&a, &t4, &t1) );
1591  IFOK( mp_invmod_xgcd(&a, &t4, &c) );
1592
1593  if (mp_cmp(&t1, &c) != 0) {
1594    mp_toradix(&t1, g_intbuf, 16);
1595    mp_toradix(&c,  a_intbuf, 16);
1596    reason("error: invmod test 11 computed %s, expected %s\n", 
1597           g_intbuf, a_intbuf);
1598    return 1;
1599  }
1600  mp_clear(&t1); mp_clear(&c);  
1601  mp_init(&t1);  mp_init(&c);   
1602
1603/* test 12: Even modulus    - m is a power of 2, 2**k | k > 32
1604 *                          - a is odd, relatively prime to m,
1605 */
1606  IFOK( mp_invmod(&a, &t3, &t1) );
1607  IFOK( mp_invmod_xgcd(&a, &t3, &c) );
1608
1609  if (mp_cmp(&t1, &c) != 0) {
1610    mp_toradix(&t1, g_intbuf, 16);
1611    mp_toradix(&c,  a_intbuf, 16);
1612    reason("error: invmod test 12 computed %s, expected %s\n", 
1613           g_intbuf, a_intbuf);
1614    return 1;
1615  }
1616
1617  mp_clear(&a);  mp_clear(&m);  mp_clear(&c);  
1618  mp_clear(&t1); mp_clear(&t2); mp_clear(&t3); mp_clear(&t4); 
1619  mp_clear(&p1); mp_clear(&p2); mp_clear(&p3); mp_clear(&p4); mp_clear(&p5); 
1620
1621  return 0;
1622}
1623
1624/*------------------------------------------------------------------------*/
1625
1626int test_cmp_d(void)
1627{
1628  mp_int  a;
1629
1630  mp_init(&a); mp_read_radix(&a, mp8, 16);
1631
1632  if(mp_cmp_d(&a, md8) >= 0) {
1633    reason("error: %s >= " DIGIT_FMT "\n", mp8, md8);
1634    mp_clear(&a);
1635    return 1;
1636  }
1637
1638  mp_read_radix(&a, mp5, 16);
1639
1640  if(mp_cmp_d(&a, md8) <= 0) {
1641    reason("error: %s <= " DIGIT_FMT "\n", mp5, md8);
1642    mp_clear(&a);
1643    return 1;
1644  }
1645
1646  mp_read_radix(&a, mp6, 16);
1647
1648  if(mp_cmp_d(&a, md1) != 0) {
1649    reason("error: %s != " DIGIT_FMT "\n", mp6, md1);
1650    mp_clear(&a);
1651    return 1;
1652  }
1653
1654  mp_clear(&a);
1655  return 0;
1656
1657}
1658
1659/*------------------------------------------------------------------------*/
1660
1661int test_cmp_z(void)
1662{
1663  mp_int  a;
1664
1665  mp_init(&a); mp_read_radix(&a, mp6, 16);
1666
1667  if(mp_cmp_z(&a) != 0) {
1668    reason("error: someone thinks a zero value is non-zero\n");
1669    mp_clear(&a);
1670    return 1;
1671  }
1672
1673  mp_read_radix(&a, mp1, 16);
1674  
1675  if(mp_cmp_z(&a) <= 0) {
1676    reason("error: someone thinks a positive value is non-positive\n");
1677    mp_clear(&a);
1678    return 1;
1679  }
1680
1681  mp_read_radix(&a, mp4, 16);
1682
1683  if(mp_cmp_z(&a) >= 0) {
1684    reason("error: someone thinks a negative value is non-negative\n");
1685    mp_clear(&a);
1686    return 1;
1687  }
1688
1689  mp_clear(&a);
1690  return 0;
1691}
1692
1693/*------------------------------------------------------------------------*/
1694
1695int test_cmp(void)
1696{
1697  mp_int  a, b;
1698
1699  mp_init(&a); mp_init(&b);
1700  mp_read_radix(&a, mp3, 16); mp_read_radix(&b, mp4, 16);
1701
1702  if(mp_cmp(&a, &b) <= 0) {
1703    reason("error: %s <= %s\n", mp3, mp4);
1704    mp_clear(&a); mp_clear(&b);
1705    return 1;
1706  }
1707
1708  mp_read_radix(&b, mp3, 16);
1709  if(mp_cmp(&a, &b) != 0) {
1710    reason("error: %s != %s\n", mp3, mp3);
1711    mp_clear(&a); mp_clear(&b);
1712    return 1;
1713  }
1714
1715  mp_read_radix(&a, mp5, 16);
1716  if(mp_cmp(&a, &b) >= 0) {
1717    reason("error: %s >= %s\n", mp5, mp3);
1718    mp_clear(&a); mp_clear(&b);
1719    return 1;
1720  }
1721
1722  mp_read_radix(&a, mp5a, 16);
1723  if(mp_cmp_int(&a, 1000000) >= 0 ||
1724     (mp_cmp_int(&a, -5000000) <= 0) ||
1725     (mp_cmp_int(&a, -4938110) != 0)) {
1726    reason("error: long integer comparison failed (%s)", mp5a);
1727    mp_clear(&a); mp_clear(&b

Large files files are truncated, but you can click here to view the full file