test-libmp.c revision 267654
168349Sobrien/*-
2133359Sobrien * Copyright (c) 2006, Simon L. Nielsen <simon@FreeBSD.org>
3133359Sobrien * All rights reserved.
4133359Sobrien *
5133359Sobrien * Redistribution and use in source and binary forms, with or without
6133359Sobrien * modification, are permitted provided that the following conditions
7133359Sobrien * are met:
8133359Sobrien * 1. Redistributions of source code must retain the above copyright
9133359Sobrien *    notice, this list of conditions and the following disclaimer.
10133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright
11133359Sobrien *    notice, this list of conditions and the following disclaimer in the
12133359Sobrien *    documentation and/or other materials provided with the distribution.
13133359Sobrien *
14133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17133359Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18133359Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24133359Sobrien * SUCH DAMAGE.
25133359Sobrien *
26133359Sobrien */
27133359Sobrien#include <sys/cdefs.h>
28133359Sobrien__FBSDID("$FreeBSD: releng/9.3/tools/regression/lib/libmp/test-libmp.c 189092 2009-02-26 21:43:15Z ed $");
2968349Sobrien
3068349Sobrien#include <mp.h>
31103373Sobrien#include <stdio.h>
32103373Sobrien#include <string.h>
33133359Sobrien#include <sysexits.h>
34133359Sobrien
35133359SobrienMINT *c0, *c1, *c2, *c3, *c5, *c6, *c8, *c10, *c14, *c15, *c25, \
3668349Sobrien    *c42,*c43, *c44, *c45, *t0, *t1;
3769216Sobrienstatic int tnr = 0;
3868349Sobrien
39133359Sobrienstatic void
4068349Sobrientestmcmp(const MINT *mp1, const MINT *mp2, const char *tname)
41133359Sobrien{
4268349Sobrien
4368349Sobrien	if (mp_mcmp(mp1, mp2) == 0)
4468349Sobrien		printf("ok %d - %s\n", ++tnr, tname);
4568349Sobrien	else
4668349Sobrien		printf("not ok - %d %s\n", ++tnr, tname);
4768349Sobrien}
4868349Sobrien
4968349Sobrienstatic int
5068349Sobrientestsimpel(void)
5168349Sobrien{
5268349Sobrien	const char str42[] = "2a";
5368349Sobrien	MINT *t2;
5468349Sobrien	char *s;
5568349Sobrien
5668349Sobrien	mp_madd(c42, c1, t0);
5768349Sobrien	testmcmp(c43, t0, "madd0");
5868349Sobrien	mp_madd(t0, c1, t0);
59133359Sobrien	testmcmp(c44, t0, "madd1");
60133359Sobrien	mp_msub(t0, c1, t0);
61133359Sobrien	testmcmp(c43, t0, "msub0");
6268349Sobrien	mp_msub(t0, c1, t0);
63103373Sobrien	testmcmp(c42, t0, "msub1");
64186690Sobrien	mp_move(c42, t0);
65133359Sobrien	testmcmp(c42, t0, "move0");
66186690Sobrien
67103373Sobrien	t2 = mp_xtom(str42);
68186690Sobrien	testmcmp(c42, t2, "xtom");
69186690Sobrien	s = mp_mtox(t2);
70186690Sobrien	if (strcmp(str42, s) == 0)
71103373Sobrien		printf("ok %d - %s\n", ++tnr, "mtox0");
7268349Sobrien	else
7368349Sobrien		printf("not ok %d - %s\n", ++tnr, "mtox0");
7468349Sobrien	mp_mfree(t2);
7568349Sobrien}
7668349Sobrien
77186690Sobrienstatic int
7868349Sobrientestgcd(void)
7968349Sobrien{
8068349Sobrien
8168349Sobrien	mp_gcd(c10, c15, t0);
82159764Sobrien	testmcmp(t0, c5, "gcd0");
8368349Sobrien}
84133359Sobrien
8568349Sobrienstatic int
8668349Sobrientestmsqrt(void)
87169962Sobrien{
88103373Sobrien
8968349Sobrien	mp_msqrt(c25, t0, t1);
9068349Sobrien	testmcmp(t0, c5, "msqrt0");
9168349Sobrien	testmcmp(t1, c0, "msqrt1");
9268349Sobrien	mp_msqrt(c42, t0, t1);
93133359Sobrien	testmcmp(t0, c6, "msqrt2");
9468349Sobrien	testmcmp(t1, c6, "msqrt3");
95110949Sobrien}
96169942Sobrien
97169942Sobrienstatic int
9868349Sobrientestdiv(void)
99133359Sobrien{
100133359Sobrien	short ro;
101159764Sobrien	MINT *t2;
10268349Sobrien
103133359Sobrien	mp_mdiv(c42, c5, t0, t1);
10468349Sobrien	testmcmp(t0, c8, "mdiv0");
105133359Sobrien	testmcmp(t1, c2, "mdiv1");
10668349Sobrien
107133359Sobrien	mp_mdiv(c10, c8, t0, t1);
108133359Sobrien	testmcmp(t0, c1, "mdiv2");
109133359Sobrien	testmcmp(t1, c2, "mdiv3");
11068349Sobrien
111103373Sobrien	mp_sdiv(c42, 5, t0, &ro);
112133359Sobrien	testmcmp(t0, c8, "sdiv0");
113133359Sobrien	t2 = mp_itom(ro); // Simpler to use common testmcmp()
11468349Sobrien	testmcmp(t2, c2, "sdiv1");
115133359Sobrien	mp_mfree(t2);
11668349Sobrien
11768349Sobrien	mp_sdiv(c10, 8, t0, &ro);
11868349Sobrien	testmcmp(t0, c1, "sdiv2");
11968349Sobrien	t2 = mp_itom(ro); // Simpler to use common testmcmp()
120133359Sobrien	testmcmp(t2, c2, "sdiv3");
12168349Sobrien	mp_mfree(t2);
122175296Sobrien}
123175296Sobrien
124133359Sobrienstatic int
125133359Sobrientestmult(void)
126133359Sobrien{
12780588Sobrien
128169942Sobrien	mp_mult(c5, c2, t0);
129169962Sobrien	testmcmp(t0, c10, "mmult0");
130103373Sobrien	mp_mult(c3, c14, t0);
131169942Sobrien	testmcmp(t0, c42, "mmult1");
132103373Sobrien}
133175296Sobrien
134175296Sobrienstatic int
135175296Sobrientestpow(void)
136175296Sobrien{
137175296Sobrien
138175296Sobrien	mp_pow(c2, c3, c10, t0);
139175296Sobrien	testmcmp(t0, c8, "pow0");
140175296Sobrien	mp_pow(c2, c3, c3, t0);
141175296Sobrien	testmcmp(t0, c2, "pow1");
14268349Sobrien	mp_rpow(c2, 3, t0);
143169962Sobrien	testmcmp(t0, c8, "rpow0");
144169962Sobrien}
145169962Sobrien
146169962Sobrien/*
147169962Sobrien * This program performs some very basic tests of libmp(3).  It is by
148169962Sobrien * no means expected to perform a complete test of the library for
149169962Sobrien * correctness, but is meant to test the API to make sure libmp (or
150169962Sobrien * libcrypto) updates don't totally break the library.
151169962Sobrien */
152169962Sobrienint
153169962Sobrienmain(int argc, char *argv[])
154169962Sobrien{
155169962Sobrien
156159764Sobrien	printf("1..25\n");
157159764Sobrien
15868349Sobrien	/*
159103373Sobrien	 * Init "constants" variables - done in this somewhat
160103373Sobrien	 * cumbersome way to in theory be able to check for memory
161103373Sobrien	 * leaks.
162103373Sobrien	 */
163103373Sobrien	c0 = mp_itom(0);
16468349Sobrien	c1 = mp_itom(1);
16568349Sobrien	c2 = mp_itom(2);
16668349Sobrien	c3 = mp_itom(3);
16768349Sobrien	c5 = mp_itom(5);
16868349Sobrien	c6 = mp_itom(6);
16980588Sobrien	c8 = mp_itom(8);
17080588Sobrien	c10 = mp_itom(10);
17180588Sobrien	c14 = mp_itom(14);
17280588Sobrien	c15 = mp_itom(15);
17384685Sobrien	c25 = mp_itom(25);
174169942Sobrien	c42 = mp_itom(42);
175169942Sobrien	c43 = mp_itom(43);
17680588Sobrien	c44 = mp_itom(44);
177169942Sobrien	c45 = mp_itom(45);
17880588Sobrien
17980588Sobrien	// Init temp variables
18080588Sobrien	t0 = mp_itom(0);
18180588Sobrien	t1 = mp_itom(0);
18280588Sobrien
18380588Sobrien	// Run tests
18468349Sobrien	testsimpel();
185159764Sobrien	testgcd();
186159764Sobrien	testdiv();
187159764Sobrien	testmult();
188103373Sobrien	testpow();
189103373Sobrien	testmsqrt();
19068349Sobrien
191103373Sobrien	// Cleanup
192175296Sobrien	mp_mfree(c0);
193175296Sobrien	mp_mfree(c1);
194103373Sobrien	mp_mfree(c2);
195175296Sobrien	mp_mfree(c3);
196175296Sobrien	mp_mfree(c5);
197175296Sobrien	mp_mfree(c6);
198175296Sobrien	mp_mfree(c8);
199175296Sobrien	mp_mfree(c10);
200175296Sobrien	mp_mfree(c14);
201175296Sobrien	mp_mfree(c15);
202175296Sobrien	mp_mfree(c25);
203103373Sobrien	mp_mfree(c42);
204169942Sobrien	mp_mfree(c43);
205169942Sobrien	mp_mfree(c44);
206169942Sobrien	mp_mfree(c45);
20768349Sobrien	mp_mfree(t0);
208175296Sobrien	mp_mfree(t1);
20968349Sobrien
21068349Sobrien	return (EX_OK);
211133359Sobrien}
21268349Sobrien