1160786Ssimon/*-
2160786Ssimon * Copyright (c) 2006, Simon L. Nielsen <simon@FreeBSD.org>
3160786Ssimon * All rights reserved.
4160786Ssimon *
5160786Ssimon * Redistribution and use in source and binary forms, with or without
6160786Ssimon * modification, are permitted provided that the following conditions
7160786Ssimon * are met:
8160786Ssimon * 1. Redistributions of source code must retain the above copyright
9160786Ssimon *    notice, this list of conditions and the following disclaimer.
10160786Ssimon * 2. Redistributions in binary form must reproduce the above copyright
11160786Ssimon *    notice, this list of conditions and the following disclaimer in the
12160786Ssimon *    documentation and/or other materials provided with the distribution.
13160786Ssimon *
14160786Ssimon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15160786Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16160786Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17160786Ssimon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18160786Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19160786Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20160786Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21160786Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22160786Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23160786Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24160786Ssimon * SUCH DAMAGE.
25160786Ssimon *
26160786Ssimon */
27160786Ssimon#include <sys/cdefs.h>
28160786Ssimon__FBSDID("$FreeBSD$");
29160786Ssimon
30160786Ssimon#include <mp.h>
31160786Ssimon#include <stdio.h>
32160786Ssimon#include <string.h>
33160786Ssimon#include <sysexits.h>
34160786Ssimon
35160786SsimonMINT *c0, *c1, *c2, *c3, *c5, *c6, *c8, *c10, *c14, *c15, *c25, \
36160786Ssimon    *c42,*c43, *c44, *c45, *t0, *t1;
37160786Ssimonstatic int tnr = 0;
38160786Ssimon
39160786Ssimonstatic void
40160786Ssimontestmcmp(const MINT *mp1, const MINT *mp2, const char *tname)
41160786Ssimon{
42160786Ssimon
43189092Sed	if (mp_mcmp(mp1, mp2) == 0)
44160786Ssimon		printf("ok %d - %s\n", ++tnr, tname);
45160786Ssimon	else
46160786Ssimon		printf("not ok - %d %s\n", ++tnr, tname);
47160786Ssimon}
48160786Ssimon
49269534Sngiestatic void
50160786Ssimontestsimpel(void)
51160786Ssimon{
52160786Ssimon	const char str42[] = "2a";
53160786Ssimon	MINT *t2;
54160786Ssimon	char *s;
55160786Ssimon
56189092Sed	mp_madd(c42, c1, t0);
57160786Ssimon	testmcmp(c43, t0, "madd0");
58189092Sed	mp_madd(t0, c1, t0);
59160786Ssimon	testmcmp(c44, t0, "madd1");
60189092Sed	mp_msub(t0, c1, t0);
61160786Ssimon	testmcmp(c43, t0, "msub0");
62189092Sed	mp_msub(t0, c1, t0);
63160786Ssimon	testmcmp(c42, t0, "msub1");
64189092Sed	mp_move(c42, t0);
65160786Ssimon	testmcmp(c42, t0, "move0");
66160786Ssimon
67189092Sed	t2 = mp_xtom(str42);
68160786Ssimon	testmcmp(c42, t2, "xtom");
69189092Sed	s = mp_mtox(t2);
70160786Ssimon	if (strcmp(str42, s) == 0)
71160786Ssimon		printf("ok %d - %s\n", ++tnr, "mtox0");
72160786Ssimon	else
73160786Ssimon		printf("not ok %d - %s\n", ++tnr, "mtox0");
74189092Sed	mp_mfree(t2);
75160786Ssimon}
76160786Ssimon
77269534Sngiestatic void
78160786Ssimontestgcd(void)
79160786Ssimon{
80160786Ssimon
81189092Sed	mp_gcd(c10, c15, t0);
82160786Ssimon	testmcmp(t0, c5, "gcd0");
83160786Ssimon}
84160786Ssimon
85269534Sngiestatic void
86160786Ssimontestmsqrt(void)
87160786Ssimon{
88160786Ssimon
89189092Sed	mp_msqrt(c25, t0, t1);
90160786Ssimon	testmcmp(t0, c5, "msqrt0");
91160786Ssimon	testmcmp(t1, c0, "msqrt1");
92189092Sed	mp_msqrt(c42, t0, t1);
93160786Ssimon	testmcmp(t0, c6, "msqrt2");
94160786Ssimon	testmcmp(t1, c6, "msqrt3");
95160786Ssimon}
96160786Ssimon
97269534Sngiestatic void
98160786Ssimontestdiv(void)
99160786Ssimon{
100160786Ssimon	short ro;
101160786Ssimon	MINT *t2;
102160786Ssimon
103189092Sed	mp_mdiv(c42, c5, t0, t1);
104160786Ssimon	testmcmp(t0, c8, "mdiv0");
105160786Ssimon	testmcmp(t1, c2, "mdiv1");
106160786Ssimon
107189092Sed	mp_mdiv(c10, c8, t0, t1);
108160786Ssimon	testmcmp(t0, c1, "mdiv2");
109160786Ssimon	testmcmp(t1, c2, "mdiv3");
110160786Ssimon
111189092Sed	mp_sdiv(c42, 5, t0, &ro);
112160786Ssimon	testmcmp(t0, c8, "sdiv0");
113189092Sed	t2 = mp_itom(ro); // Simpler to use common testmcmp()
114160786Ssimon	testmcmp(t2, c2, "sdiv1");
115189092Sed	mp_mfree(t2);
116160786Ssimon
117189092Sed	mp_sdiv(c10, 8, t0, &ro);
118160786Ssimon	testmcmp(t0, c1, "sdiv2");
119189092Sed	t2 = mp_itom(ro); // Simpler to use common testmcmp()
120160786Ssimon	testmcmp(t2, c2, "sdiv3");
121189092Sed	mp_mfree(t2);
122160786Ssimon}
123160786Ssimon
124269534Sngiestatic void
125160786Ssimontestmult(void)
126160786Ssimon{
127160786Ssimon
128189092Sed	mp_mult(c5, c2, t0);
129160786Ssimon	testmcmp(t0, c10, "mmult0");
130189092Sed	mp_mult(c3, c14, t0);
131160786Ssimon	testmcmp(t0, c42, "mmult1");
132160786Ssimon}
133160786Ssimon
134269534Sngiestatic void
135160786Ssimontestpow(void)
136160786Ssimon{
137160786Ssimon
138189092Sed	mp_pow(c2, c3, c10, t0);
139160786Ssimon	testmcmp(t0, c8, "pow0");
140189092Sed	mp_pow(c2, c3, c3, t0);
141160786Ssimon	testmcmp(t0, c2, "pow1");
142189092Sed	mp_rpow(c2, 3, t0);
143160786Ssimon	testmcmp(t0, c8, "rpow0");
144160786Ssimon}
145160786Ssimon
146160786Ssimon/*
147160786Ssimon * This program performs some very basic tests of libmp(3).  It is by
148160786Ssimon * no means expected to perform a complete test of the library for
149160786Ssimon * correctness, but is meant to test the API to make sure libmp (or
150160786Ssimon * libcrypto) updates don't totally break the library.
151160786Ssimon */
152160786Ssimonint
153160786Ssimonmain(int argc, char *argv[])
154160786Ssimon{
155160786Ssimon
156160786Ssimon	printf("1..25\n");
157160786Ssimon
158160786Ssimon	/*
159160786Ssimon	 * Init "constants" variables - done in this somewhat
160160786Ssimon	 * cumbersome way to in theory be able to check for memory
161160786Ssimon	 * leaks.
162160786Ssimon	 */
163189092Sed	c0 = mp_itom(0);
164189092Sed	c1 = mp_itom(1);
165189092Sed	c2 = mp_itom(2);
166189092Sed	c3 = mp_itom(3);
167189092Sed	c5 = mp_itom(5);
168189092Sed	c6 = mp_itom(6);
169189092Sed	c8 = mp_itom(8);
170189092Sed	c10 = mp_itom(10);
171189092Sed	c14 = mp_itom(14);
172189092Sed	c15 = mp_itom(15);
173189092Sed	c25 = mp_itom(25);
174189092Sed	c42 = mp_itom(42);
175189092Sed	c43 = mp_itom(43);
176189092Sed	c44 = mp_itom(44);
177189092Sed	c45 = mp_itom(45);
178160786Ssimon
179160786Ssimon	// Init temp variables
180189092Sed	t0 = mp_itom(0);
181189092Sed	t1 = mp_itom(0);
182160786Ssimon
183160786Ssimon	// Run tests
184160786Ssimon	testsimpel();
185160786Ssimon	testgcd();
186160786Ssimon	testdiv();
187160786Ssimon	testmult();
188160786Ssimon	testpow();
189160786Ssimon	testmsqrt();
190160786Ssimon
191160786Ssimon	// Cleanup
192189092Sed	mp_mfree(c0);
193189092Sed	mp_mfree(c1);
194189092Sed	mp_mfree(c2);
195189092Sed	mp_mfree(c3);
196189092Sed	mp_mfree(c5);
197189092Sed	mp_mfree(c6);
198189092Sed	mp_mfree(c8);
199189092Sed	mp_mfree(c10);
200189092Sed	mp_mfree(c14);
201189092Sed	mp_mfree(c15);
202189092Sed	mp_mfree(c25);
203189092Sed	mp_mfree(c42);
204189092Sed	mp_mfree(c43);
205189092Sed	mp_mfree(c44);
206189092Sed	mp_mfree(c45);
207189092Sed	mp_mfree(t0);
208189092Sed	mp_mfree(t1);
209160786Ssimon
210160786Ssimon	return (EX_OK);
211160786Ssimon}
212