1*dfed135eSEnji Cooper /*-
2*dfed135eSEnji Cooper * Copyright (c) 2006, Simon L. Nielsen <simon@FreeBSD.org>
3*dfed135eSEnji Cooper * All rights reserved.
4*dfed135eSEnji Cooper *
5*dfed135eSEnji Cooper * Redistribution and use in source and binary forms, with or without
6*dfed135eSEnji Cooper * modification, are permitted provided that the following conditions
7*dfed135eSEnji Cooper * are met:
8*dfed135eSEnji Cooper * 1. Redistributions of source code must retain the above copyright
9*dfed135eSEnji Cooper * notice, this list of conditions and the following disclaimer.
10*dfed135eSEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
11*dfed135eSEnji Cooper * notice, this list of conditions and the following disclaimer in the
12*dfed135eSEnji Cooper * documentation and/or other materials provided with the distribution.
13*dfed135eSEnji Cooper *
14*dfed135eSEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*dfed135eSEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*dfed135eSEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*dfed135eSEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*dfed135eSEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*dfed135eSEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*dfed135eSEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*dfed135eSEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*dfed135eSEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*dfed135eSEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*dfed135eSEnji Cooper * SUCH DAMAGE.
25*dfed135eSEnji Cooper *
26*dfed135eSEnji Cooper */
27*dfed135eSEnji Cooper #include <sys/cdefs.h>
28*dfed135eSEnji Cooper #include <mp.h>
29*dfed135eSEnji Cooper #include <stdio.h>
30*dfed135eSEnji Cooper #include <string.h>
31*dfed135eSEnji Cooper #include <sysexits.h>
32*dfed135eSEnji Cooper
33*dfed135eSEnji Cooper MINT *c0, *c1, *c2, *c3, *c5, *c6, *c8, *c10, *c14, *c15, *c25, \
34*dfed135eSEnji Cooper *c42,*c43, *c44, *c45, *t0, *t1;
35*dfed135eSEnji Cooper static int tnr = 0;
36*dfed135eSEnji Cooper
37*dfed135eSEnji Cooper static void
testmcmp(const MINT * mp1,const MINT * mp2,const char * tname)38*dfed135eSEnji Cooper testmcmp(const MINT *mp1, const MINT *mp2, const char *tname)
39*dfed135eSEnji Cooper {
40*dfed135eSEnji Cooper
41*dfed135eSEnji Cooper if (mp_mcmp(mp1, mp2) == 0)
42*dfed135eSEnji Cooper printf("ok %d - %s\n", ++tnr, tname);
43*dfed135eSEnji Cooper else
44*dfed135eSEnji Cooper printf("not ok - %d %s\n", ++tnr, tname);
45*dfed135eSEnji Cooper }
46*dfed135eSEnji Cooper
47*dfed135eSEnji Cooper static void
testsimpel(void)48*dfed135eSEnji Cooper testsimpel(void)
49*dfed135eSEnji Cooper {
50*dfed135eSEnji Cooper const char str42[] = "2a";
51*dfed135eSEnji Cooper MINT *t2;
52*dfed135eSEnji Cooper char *s;
53*dfed135eSEnji Cooper
54*dfed135eSEnji Cooper mp_madd(c42, c1, t0);
55*dfed135eSEnji Cooper testmcmp(c43, t0, "madd0");
56*dfed135eSEnji Cooper mp_madd(t0, c1, t0);
57*dfed135eSEnji Cooper testmcmp(c44, t0, "madd1");
58*dfed135eSEnji Cooper mp_msub(t0, c1, t0);
59*dfed135eSEnji Cooper testmcmp(c43, t0, "msub0");
60*dfed135eSEnji Cooper mp_msub(t0, c1, t0);
61*dfed135eSEnji Cooper testmcmp(c42, t0, "msub1");
62*dfed135eSEnji Cooper mp_move(c42, t0);
63*dfed135eSEnji Cooper testmcmp(c42, t0, "move0");
64*dfed135eSEnji Cooper
65*dfed135eSEnji Cooper t2 = mp_xtom(str42);
66*dfed135eSEnji Cooper testmcmp(c42, t2, "xtom");
67*dfed135eSEnji Cooper s = mp_mtox(t2);
68*dfed135eSEnji Cooper if (strcmp(str42, s) == 0)
69*dfed135eSEnji Cooper printf("ok %d - %s\n", ++tnr, "mtox0");
70*dfed135eSEnji Cooper else
71*dfed135eSEnji Cooper printf("not ok %d - %s\n", ++tnr, "mtox0");
72*dfed135eSEnji Cooper mp_mfree(t2);
73*dfed135eSEnji Cooper }
74*dfed135eSEnji Cooper
75*dfed135eSEnji Cooper static void
testgcd(void)76*dfed135eSEnji Cooper testgcd(void)
77*dfed135eSEnji Cooper {
78*dfed135eSEnji Cooper
79*dfed135eSEnji Cooper mp_gcd(c10, c15, t0);
80*dfed135eSEnji Cooper testmcmp(t0, c5, "gcd0");
81*dfed135eSEnji Cooper }
82*dfed135eSEnji Cooper
83*dfed135eSEnji Cooper static void
testmsqrt(void)84*dfed135eSEnji Cooper testmsqrt(void)
85*dfed135eSEnji Cooper {
86*dfed135eSEnji Cooper
87*dfed135eSEnji Cooper mp_msqrt(c25, t0, t1);
88*dfed135eSEnji Cooper testmcmp(t0, c5, "msqrt0");
89*dfed135eSEnji Cooper testmcmp(t1, c0, "msqrt1");
90*dfed135eSEnji Cooper mp_msqrt(c42, t0, t1);
91*dfed135eSEnji Cooper testmcmp(t0, c6, "msqrt2");
92*dfed135eSEnji Cooper testmcmp(t1, c6, "msqrt3");
93*dfed135eSEnji Cooper }
94*dfed135eSEnji Cooper
95*dfed135eSEnji Cooper static void
testdiv(void)96*dfed135eSEnji Cooper testdiv(void)
97*dfed135eSEnji Cooper {
98*dfed135eSEnji Cooper short ro;
99*dfed135eSEnji Cooper MINT *t2;
100*dfed135eSEnji Cooper
101*dfed135eSEnji Cooper mp_mdiv(c42, c5, t0, t1);
102*dfed135eSEnji Cooper testmcmp(t0, c8, "mdiv0");
103*dfed135eSEnji Cooper testmcmp(t1, c2, "mdiv1");
104*dfed135eSEnji Cooper
105*dfed135eSEnji Cooper mp_mdiv(c10, c8, t0, t1);
106*dfed135eSEnji Cooper testmcmp(t0, c1, "mdiv2");
107*dfed135eSEnji Cooper testmcmp(t1, c2, "mdiv3");
108*dfed135eSEnji Cooper
109*dfed135eSEnji Cooper mp_sdiv(c42, 5, t0, &ro);
110*dfed135eSEnji Cooper testmcmp(t0, c8, "sdiv0");
111*dfed135eSEnji Cooper t2 = mp_itom(ro); // Simpler to use common testmcmp()
112*dfed135eSEnji Cooper testmcmp(t2, c2, "sdiv1");
113*dfed135eSEnji Cooper mp_mfree(t2);
114*dfed135eSEnji Cooper
115*dfed135eSEnji Cooper mp_sdiv(c10, 8, t0, &ro);
116*dfed135eSEnji Cooper testmcmp(t0, c1, "sdiv2");
117*dfed135eSEnji Cooper t2 = mp_itom(ro); // Simpler to use common testmcmp()
118*dfed135eSEnji Cooper testmcmp(t2, c2, "sdiv3");
119*dfed135eSEnji Cooper mp_mfree(t2);
120*dfed135eSEnji Cooper }
121*dfed135eSEnji Cooper
122*dfed135eSEnji Cooper static void
testmult(void)123*dfed135eSEnji Cooper testmult(void)
124*dfed135eSEnji Cooper {
125*dfed135eSEnji Cooper
126*dfed135eSEnji Cooper mp_mult(c5, c2, t0);
127*dfed135eSEnji Cooper testmcmp(t0, c10, "mmult0");
128*dfed135eSEnji Cooper mp_mult(c3, c14, t0);
129*dfed135eSEnji Cooper testmcmp(t0, c42, "mmult1");
130*dfed135eSEnji Cooper }
131*dfed135eSEnji Cooper
132*dfed135eSEnji Cooper static void
testpow(void)133*dfed135eSEnji Cooper testpow(void)
134*dfed135eSEnji Cooper {
135*dfed135eSEnji Cooper
136*dfed135eSEnji Cooper mp_pow(c2, c3, c10, t0);
137*dfed135eSEnji Cooper testmcmp(t0, c8, "pow0");
138*dfed135eSEnji Cooper mp_pow(c2, c3, c3, t0);
139*dfed135eSEnji Cooper testmcmp(t0, c2, "pow1");
140*dfed135eSEnji Cooper mp_rpow(c2, 3, t0);
141*dfed135eSEnji Cooper testmcmp(t0, c8, "rpow0");
142*dfed135eSEnji Cooper }
143*dfed135eSEnji Cooper
144*dfed135eSEnji Cooper /*
145*dfed135eSEnji Cooper * This program performs some very basic tests of libmp(3). It is by
146*dfed135eSEnji Cooper * no means expected to perform a complete test of the library for
147*dfed135eSEnji Cooper * correctness, but is meant to test the API to make sure libmp (or
148*dfed135eSEnji Cooper * libcrypto) updates don't totally break the library.
149*dfed135eSEnji Cooper */
150*dfed135eSEnji Cooper int
main(int argc,char * argv[])151*dfed135eSEnji Cooper main(int argc, char *argv[])
152*dfed135eSEnji Cooper {
153*dfed135eSEnji Cooper
154*dfed135eSEnji Cooper printf("1..25\n");
155*dfed135eSEnji Cooper
156*dfed135eSEnji Cooper /*
157*dfed135eSEnji Cooper * Init "constants" variables - done in this somewhat
158*dfed135eSEnji Cooper * cumbersome way to in theory be able to check for memory
159*dfed135eSEnji Cooper * leaks.
160*dfed135eSEnji Cooper */
161*dfed135eSEnji Cooper c0 = mp_itom(0);
162*dfed135eSEnji Cooper c1 = mp_itom(1);
163*dfed135eSEnji Cooper c2 = mp_itom(2);
164*dfed135eSEnji Cooper c3 = mp_itom(3);
165*dfed135eSEnji Cooper c5 = mp_itom(5);
166*dfed135eSEnji Cooper c6 = mp_itom(6);
167*dfed135eSEnji Cooper c8 = mp_itom(8);
168*dfed135eSEnji Cooper c10 = mp_itom(10);
169*dfed135eSEnji Cooper c14 = mp_itom(14);
170*dfed135eSEnji Cooper c15 = mp_itom(15);
171*dfed135eSEnji Cooper c25 = mp_itom(25);
172*dfed135eSEnji Cooper c42 = mp_itom(42);
173*dfed135eSEnji Cooper c43 = mp_itom(43);
174*dfed135eSEnji Cooper c44 = mp_itom(44);
175*dfed135eSEnji Cooper c45 = mp_itom(45);
176*dfed135eSEnji Cooper
177*dfed135eSEnji Cooper // Init temp variables
178*dfed135eSEnji Cooper t0 = mp_itom(0);
179*dfed135eSEnji Cooper t1 = mp_itom(0);
180*dfed135eSEnji Cooper
181*dfed135eSEnji Cooper // Run tests
182*dfed135eSEnji Cooper testsimpel();
183*dfed135eSEnji Cooper testgcd();
184*dfed135eSEnji Cooper testdiv();
185*dfed135eSEnji Cooper testmult();
186*dfed135eSEnji Cooper testpow();
187*dfed135eSEnji Cooper testmsqrt();
188*dfed135eSEnji Cooper
189*dfed135eSEnji Cooper // Cleanup
190*dfed135eSEnji Cooper mp_mfree(c0);
191*dfed135eSEnji Cooper mp_mfree(c1);
192*dfed135eSEnji Cooper mp_mfree(c2);
193*dfed135eSEnji Cooper mp_mfree(c3);
194*dfed135eSEnji Cooper mp_mfree(c5);
195*dfed135eSEnji Cooper mp_mfree(c6);
196*dfed135eSEnji Cooper mp_mfree(c8);
197*dfed135eSEnji Cooper mp_mfree(c10);
198*dfed135eSEnji Cooper mp_mfree(c14);
199*dfed135eSEnji Cooper mp_mfree(c15);
200*dfed135eSEnji Cooper mp_mfree(c25);
201*dfed135eSEnji Cooper mp_mfree(c42);
202*dfed135eSEnji Cooper mp_mfree(c43);
203*dfed135eSEnji Cooper mp_mfree(c44);
204*dfed135eSEnji Cooper mp_mfree(c45);
205*dfed135eSEnji Cooper mp_mfree(t0);
206*dfed135eSEnji Cooper mp_mfree(t1);
207*dfed135eSEnji Cooper
208*dfed135eSEnji Cooper return (EX_OK);
209*dfed135eSEnji Cooper }
210