xref: /freebsd/lib/libmp/tests/legacy_test.c (revision dfed135e5cea0ade1886b98f3df4e68fca9e1fb0)
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 __FBSDID("$FreeBSD$");
29*dfed135eSEnji Cooper 
30*dfed135eSEnji Cooper #include <mp.h>
31*dfed135eSEnji Cooper #include <stdio.h>
32*dfed135eSEnji Cooper #include <string.h>
33*dfed135eSEnji Cooper #include <sysexits.h>
34*dfed135eSEnji Cooper 
35*dfed135eSEnji Cooper MINT *c0, *c1, *c2, *c3, *c5, *c6, *c8, *c10, *c14, *c15, *c25, \
36*dfed135eSEnji Cooper     *c42,*c43, *c44, *c45, *t0, *t1;
37*dfed135eSEnji Cooper static int tnr = 0;
38*dfed135eSEnji Cooper 
39*dfed135eSEnji Cooper static void
40*dfed135eSEnji Cooper testmcmp(const MINT *mp1, const MINT *mp2, const char *tname)
41*dfed135eSEnji Cooper {
42*dfed135eSEnji Cooper 
43*dfed135eSEnji Cooper 	if (mp_mcmp(mp1, mp2) == 0)
44*dfed135eSEnji Cooper 		printf("ok %d - %s\n", ++tnr, tname);
45*dfed135eSEnji Cooper 	else
46*dfed135eSEnji Cooper 		printf("not ok - %d %s\n", ++tnr, tname);
47*dfed135eSEnji Cooper }
48*dfed135eSEnji Cooper 
49*dfed135eSEnji Cooper static void
50*dfed135eSEnji Cooper testsimpel(void)
51*dfed135eSEnji Cooper {
52*dfed135eSEnji Cooper 	const char str42[] = "2a";
53*dfed135eSEnji Cooper 	MINT *t2;
54*dfed135eSEnji Cooper 	char *s;
55*dfed135eSEnji Cooper 
56*dfed135eSEnji Cooper 	mp_madd(c42, c1, t0);
57*dfed135eSEnji Cooper 	testmcmp(c43, t0, "madd0");
58*dfed135eSEnji Cooper 	mp_madd(t0, c1, t0);
59*dfed135eSEnji Cooper 	testmcmp(c44, t0, "madd1");
60*dfed135eSEnji Cooper 	mp_msub(t0, c1, t0);
61*dfed135eSEnji Cooper 	testmcmp(c43, t0, "msub0");
62*dfed135eSEnji Cooper 	mp_msub(t0, c1, t0);
63*dfed135eSEnji Cooper 	testmcmp(c42, t0, "msub1");
64*dfed135eSEnji Cooper 	mp_move(c42, t0);
65*dfed135eSEnji Cooper 	testmcmp(c42, t0, "move0");
66*dfed135eSEnji Cooper 
67*dfed135eSEnji Cooper 	t2 = mp_xtom(str42);
68*dfed135eSEnji Cooper 	testmcmp(c42, t2, "xtom");
69*dfed135eSEnji Cooper 	s = mp_mtox(t2);
70*dfed135eSEnji Cooper 	if (strcmp(str42, s) == 0)
71*dfed135eSEnji Cooper 		printf("ok %d - %s\n", ++tnr, "mtox0");
72*dfed135eSEnji Cooper 	else
73*dfed135eSEnji Cooper 		printf("not ok %d - %s\n", ++tnr, "mtox0");
74*dfed135eSEnji Cooper 	mp_mfree(t2);
75*dfed135eSEnji Cooper }
76*dfed135eSEnji Cooper 
77*dfed135eSEnji Cooper static void
78*dfed135eSEnji Cooper testgcd(void)
79*dfed135eSEnji Cooper {
80*dfed135eSEnji Cooper 
81*dfed135eSEnji Cooper 	mp_gcd(c10, c15, t0);
82*dfed135eSEnji Cooper 	testmcmp(t0, c5, "gcd0");
83*dfed135eSEnji Cooper }
84*dfed135eSEnji Cooper 
85*dfed135eSEnji Cooper static void
86*dfed135eSEnji Cooper testmsqrt(void)
87*dfed135eSEnji Cooper {
88*dfed135eSEnji Cooper 
89*dfed135eSEnji Cooper 	mp_msqrt(c25, t0, t1);
90*dfed135eSEnji Cooper 	testmcmp(t0, c5, "msqrt0");
91*dfed135eSEnji Cooper 	testmcmp(t1, c0, "msqrt1");
92*dfed135eSEnji Cooper 	mp_msqrt(c42, t0, t1);
93*dfed135eSEnji Cooper 	testmcmp(t0, c6, "msqrt2");
94*dfed135eSEnji Cooper 	testmcmp(t1, c6, "msqrt3");
95*dfed135eSEnji Cooper }
96*dfed135eSEnji Cooper 
97*dfed135eSEnji Cooper static void
98*dfed135eSEnji Cooper testdiv(void)
99*dfed135eSEnji Cooper {
100*dfed135eSEnji Cooper 	short ro;
101*dfed135eSEnji Cooper 	MINT *t2;
102*dfed135eSEnji Cooper 
103*dfed135eSEnji Cooper 	mp_mdiv(c42, c5, t0, t1);
104*dfed135eSEnji Cooper 	testmcmp(t0, c8, "mdiv0");
105*dfed135eSEnji Cooper 	testmcmp(t1, c2, "mdiv1");
106*dfed135eSEnji Cooper 
107*dfed135eSEnji Cooper 	mp_mdiv(c10, c8, t0, t1);
108*dfed135eSEnji Cooper 	testmcmp(t0, c1, "mdiv2");
109*dfed135eSEnji Cooper 	testmcmp(t1, c2, "mdiv3");
110*dfed135eSEnji Cooper 
111*dfed135eSEnji Cooper 	mp_sdiv(c42, 5, t0, &ro);
112*dfed135eSEnji Cooper 	testmcmp(t0, c8, "sdiv0");
113*dfed135eSEnji Cooper 	t2 = mp_itom(ro); // Simpler to use common testmcmp()
114*dfed135eSEnji Cooper 	testmcmp(t2, c2, "sdiv1");
115*dfed135eSEnji Cooper 	mp_mfree(t2);
116*dfed135eSEnji Cooper 
117*dfed135eSEnji Cooper 	mp_sdiv(c10, 8, t0, &ro);
118*dfed135eSEnji Cooper 	testmcmp(t0, c1, "sdiv2");
119*dfed135eSEnji Cooper 	t2 = mp_itom(ro); // Simpler to use common testmcmp()
120*dfed135eSEnji Cooper 	testmcmp(t2, c2, "sdiv3");
121*dfed135eSEnji Cooper 	mp_mfree(t2);
122*dfed135eSEnji Cooper }
123*dfed135eSEnji Cooper 
124*dfed135eSEnji Cooper static void
125*dfed135eSEnji Cooper testmult(void)
126*dfed135eSEnji Cooper {
127*dfed135eSEnji Cooper 
128*dfed135eSEnji Cooper 	mp_mult(c5, c2, t0);
129*dfed135eSEnji Cooper 	testmcmp(t0, c10, "mmult0");
130*dfed135eSEnji Cooper 	mp_mult(c3, c14, t0);
131*dfed135eSEnji Cooper 	testmcmp(t0, c42, "mmult1");
132*dfed135eSEnji Cooper }
133*dfed135eSEnji Cooper 
134*dfed135eSEnji Cooper static void
135*dfed135eSEnji Cooper testpow(void)
136*dfed135eSEnji Cooper {
137*dfed135eSEnji Cooper 
138*dfed135eSEnji Cooper 	mp_pow(c2, c3, c10, t0);
139*dfed135eSEnji Cooper 	testmcmp(t0, c8, "pow0");
140*dfed135eSEnji Cooper 	mp_pow(c2, c3, c3, t0);
141*dfed135eSEnji Cooper 	testmcmp(t0, c2, "pow1");
142*dfed135eSEnji Cooper 	mp_rpow(c2, 3, t0);
143*dfed135eSEnji Cooper 	testmcmp(t0, c8, "rpow0");
144*dfed135eSEnji Cooper }
145*dfed135eSEnji Cooper 
146*dfed135eSEnji Cooper /*
147*dfed135eSEnji Cooper  * This program performs some very basic tests of libmp(3).  It is by
148*dfed135eSEnji Cooper  * no means expected to perform a complete test of the library for
149*dfed135eSEnji Cooper  * correctness, but is meant to test the API to make sure libmp (or
150*dfed135eSEnji Cooper  * libcrypto) updates don't totally break the library.
151*dfed135eSEnji Cooper  */
152*dfed135eSEnji Cooper int
153*dfed135eSEnji Cooper main(int argc, char *argv[])
154*dfed135eSEnji Cooper {
155*dfed135eSEnji Cooper 
156*dfed135eSEnji Cooper 	printf("1..25\n");
157*dfed135eSEnji Cooper 
158*dfed135eSEnji Cooper 	/*
159*dfed135eSEnji Cooper 	 * Init "constants" variables - done in this somewhat
160*dfed135eSEnji Cooper 	 * cumbersome way to in theory be able to check for memory
161*dfed135eSEnji Cooper 	 * leaks.
162*dfed135eSEnji Cooper 	 */
163*dfed135eSEnji Cooper 	c0 = mp_itom(0);
164*dfed135eSEnji Cooper 	c1 = mp_itom(1);
165*dfed135eSEnji Cooper 	c2 = mp_itom(2);
166*dfed135eSEnji Cooper 	c3 = mp_itom(3);
167*dfed135eSEnji Cooper 	c5 = mp_itom(5);
168*dfed135eSEnji Cooper 	c6 = mp_itom(6);
169*dfed135eSEnji Cooper 	c8 = mp_itom(8);
170*dfed135eSEnji Cooper 	c10 = mp_itom(10);
171*dfed135eSEnji Cooper 	c14 = mp_itom(14);
172*dfed135eSEnji Cooper 	c15 = mp_itom(15);
173*dfed135eSEnji Cooper 	c25 = mp_itom(25);
174*dfed135eSEnji Cooper 	c42 = mp_itom(42);
175*dfed135eSEnji Cooper 	c43 = mp_itom(43);
176*dfed135eSEnji Cooper 	c44 = mp_itom(44);
177*dfed135eSEnji Cooper 	c45 = mp_itom(45);
178*dfed135eSEnji Cooper 
179*dfed135eSEnji Cooper 	// Init temp variables
180*dfed135eSEnji Cooper 	t0 = mp_itom(0);
181*dfed135eSEnji Cooper 	t1 = mp_itom(0);
182*dfed135eSEnji Cooper 
183*dfed135eSEnji Cooper 	// Run tests
184*dfed135eSEnji Cooper 	testsimpel();
185*dfed135eSEnji Cooper 	testgcd();
186*dfed135eSEnji Cooper 	testdiv();
187*dfed135eSEnji Cooper 	testmult();
188*dfed135eSEnji Cooper 	testpow();
189*dfed135eSEnji Cooper 	testmsqrt();
190*dfed135eSEnji Cooper 
191*dfed135eSEnji Cooper 	// Cleanup
192*dfed135eSEnji Cooper 	mp_mfree(c0);
193*dfed135eSEnji Cooper 	mp_mfree(c1);
194*dfed135eSEnji Cooper 	mp_mfree(c2);
195*dfed135eSEnji Cooper 	mp_mfree(c3);
196*dfed135eSEnji Cooper 	mp_mfree(c5);
197*dfed135eSEnji Cooper 	mp_mfree(c6);
198*dfed135eSEnji Cooper 	mp_mfree(c8);
199*dfed135eSEnji Cooper 	mp_mfree(c10);
200*dfed135eSEnji Cooper 	mp_mfree(c14);
201*dfed135eSEnji Cooper 	mp_mfree(c15);
202*dfed135eSEnji Cooper 	mp_mfree(c25);
203*dfed135eSEnji Cooper 	mp_mfree(c42);
204*dfed135eSEnji Cooper 	mp_mfree(c43);
205*dfed135eSEnji Cooper 	mp_mfree(c44);
206*dfed135eSEnji Cooper 	mp_mfree(c45);
207*dfed135eSEnji Cooper 	mp_mfree(t0);
208*dfed135eSEnji Cooper 	mp_mfree(t1);
209*dfed135eSEnji Cooper 
210*dfed135eSEnji Cooper 	return (EX_OK);
211*dfed135eSEnji Cooper }
212