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