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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial