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