~ubuntu-branches/ubuntu/natty/libgcrypt11/natty-proposed

« back to all changes in this revision

Viewing changes to tests/mpitests.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-05-16 20:13:32 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090516201332-czkobpu32w318i16
Tags: 1.4.4-2ubuntu1
* Merge from Debian unstable (LP: #364535), remaining changes:
  - Add libgcrypt11-udeb for use by cryptsetup-udeb.
  - Add clean-la.mk, and add a symlink for the .la
  - Install to /lib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <string.h>
28
28
#include <stdarg.h>
29
29
 
30
 
#include "../src/gcrypt.h"
31
 
 
 
30
#ifdef _GCRYPT_IN_LIBGCRYPT
 
31
# include "../src/gcrypt.h"
 
32
#else
 
33
# include <gcrypt.h>
 
34
#endif
32
35
 
33
36
static int verbose;
34
37
static int debug;
35
38
 
36
39
 
 
40
static void
 
41
die (const char *format, ...)
 
42
{
 
43
  va_list arg_ptr;
 
44
 
 
45
  va_start (arg_ptr, format);
 
46
  vfprintf (stderr, format, arg_ptr);
 
47
  va_end (arg_ptr);
 
48
  exit (1);
 
49
}
 
50
 
 
51
 
 
52
 
37
53
/* Set up some test patterns */
38
54
 
39
55
/* 48 bytes with value 1: this results in 8 limbs for 64bit limbs, 16limb for 32 bit limbs */
157
173
}
158
174
 
159
175
 
 
176
/* What we test here is that we don't overwrite our args and that
 
177
   using thne same mpi for several args works.  */
 
178
static int
 
179
test_powm (void)
 
180
{
 
181
  int b_int = 17;
 
182
  int e_int = 3;
 
183
  int m_int = 19;  
 
184
  gcry_mpi_t base = gcry_mpi_set_ui (NULL, b_int);
 
185
  gcry_mpi_t exp = gcry_mpi_set_ui (NULL, e_int);
 
186
  gcry_mpi_t mod = gcry_mpi_set_ui (NULL, m_int);
 
187
  gcry_mpi_t res = gcry_mpi_new (0);
 
188
 
 
189
  gcry_mpi_powm (res, base, exp, mod);
 
190
  if (gcry_mpi_cmp_ui (base, b_int))
 
191
    die ("test_powm failed for base at %d\n", __LINE__);
 
192
  if (gcry_mpi_cmp_ui (exp, e_int))
 
193
    die ("test_powm_ui failed for exp at %d\n", __LINE__);
 
194
  if (gcry_mpi_cmp_ui (mod, m_int))
 
195
    die ("test_powm failed for mod at %d\n", __LINE__);
 
196
 
 
197
  /* Check using base for the result.  */
 
198
  gcry_mpi_set_ui (base, b_int);
 
199
  gcry_mpi_set_ui (exp, e_int);
 
200
  gcry_mpi_set_ui(mod, m_int);
 
201
  gcry_mpi_powm (base, base, exp, mod);
 
202
  if (gcry_mpi_cmp (res, base))
 
203
    die ("test_powm failed at %d\n", __LINE__);
 
204
  if (gcry_mpi_cmp_ui (exp, e_int))
 
205
    die ("test_powm_ui failed for exp at %d\n", __LINE__);
 
206
  if (gcry_mpi_cmp_ui (mod, m_int))
 
207
    die ("test_powm failed for mod at %d\n", __LINE__);
 
208
 
 
209
  /* Check using exp for the result.  */
 
210
  gcry_mpi_set_ui (base, b_int);
 
211
  gcry_mpi_set_ui (exp, e_int);
 
212
  gcry_mpi_set_ui(mod, m_int);
 
213
  gcry_mpi_powm (exp, base, exp, mod);
 
214
  if (gcry_mpi_cmp (res, exp))
 
215
    die ("test_powm failed at %d\n", __LINE__);
 
216
  if (gcry_mpi_cmp_ui (base, b_int))
 
217
    die ("test_powm failed for base at %d\n", __LINE__);
 
218
  if (gcry_mpi_cmp_ui (mod, m_int))
 
219
    die ("test_powm failed for mod at %d\n", __LINE__);
 
220
 
 
221
  /* Check using mod for the result.  */
 
222
  gcry_mpi_set_ui (base, b_int);
 
223
  gcry_mpi_set_ui (exp, e_int);
 
224
  gcry_mpi_set_ui(mod, m_int);
 
225
  gcry_mpi_powm (mod, base, exp, mod);
 
226
  if (gcry_mpi_cmp (res, mod))
 
227
    die ("test_powm failed at %d\n", __LINE__);
 
228
  if (gcry_mpi_cmp_ui (base, b_int))
 
229
    die ("test_powm failed for base at %d\n", __LINE__);
 
230
  if (gcry_mpi_cmp_ui (exp, e_int))
 
231
    die ("test_powm_ui failed for exp at %d\n", __LINE__);
 
232
 
 
233
  /* Now check base ^ base mod mod.  */
 
234
  gcry_mpi_set_ui (base, b_int);
 
235
  gcry_mpi_set_ui(mod, m_int);
 
236
  gcry_mpi_powm (res, base, base, mod);
 
237
  if (gcry_mpi_cmp_ui (base, b_int))
 
238
    die ("test_powm failed for base at %d\n", __LINE__);
 
239
  if (gcry_mpi_cmp_ui (mod, m_int))
 
240
    die ("test_powm failed for mod at %d\n", __LINE__);
 
241
 
 
242
  /* Check base ^ base mod mod with base as result.  */
 
243
  gcry_mpi_set_ui (base, b_int);
 
244
  gcry_mpi_set_ui(mod, m_int);
 
245
  gcry_mpi_powm (base, base, base, mod);
 
246
  if (gcry_mpi_cmp (res, base))
 
247
    die ("test_powm failed at %d\n", __LINE__);
 
248
  if (gcry_mpi_cmp_ui (mod, m_int))
 
249
    die ("test_powm failed for mod at %d\n", __LINE__);
 
250
 
 
251
  /* Check base ^ base mod mod with mod as result.  */
 
252
  gcry_mpi_set_ui (base, b_int);
 
253
  gcry_mpi_set_ui(mod, m_int);
 
254
  gcry_mpi_powm (mod, base, base, mod);
 
255
  if (gcry_mpi_cmp (res, mod))
 
256
    die ("test_powm failed at %d\n", __LINE__);
 
257
  if (gcry_mpi_cmp_ui (base, b_int))
 
258
    die ("test_powm failed for base at %d\n", __LINE__);
 
259
 
 
260
  /* Now check base ^ base mod base.  */
 
261
  gcry_mpi_set_ui (base, b_int);
 
262
  gcry_mpi_powm (res, base, base, base);
 
263
  if (gcry_mpi_cmp_ui (base, b_int))
 
264
    die ("test_powm failed for base at %d\n", __LINE__);
 
265
 
 
266
  /* Check base ^ base mod base with base as result.  */
 
267
  gcry_mpi_set_ui (base, b_int);
 
268
  gcry_mpi_powm (base, base, base, base);
 
269
  if (gcry_mpi_cmp (res, base))
 
270
    die ("test_powm failed at %d\n", __LINE__);
 
271
 
 
272
  /* Fixme: We should add the rest of the cases of course.  */
 
273
 
 
274
 
 
275
 
 
276
  return 1;
 
277
}
 
278
 
 
279
 
160
280
int 
161
281
main (int argc, char* argv[])
162
282
{
175
295
  test_add ();
176
296
  test_sub ();
177
297
  test_mul ();
 
298
  test_powm ();
178
299
 
179
300
  return 0;
180
301
}