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

« back to all changes in this revision

Viewing changes to tests/t-mpi-bit.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-05-16 20:13:32 UTC
  • mfrom: (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20090516201332-382lmffc3put5wtm
Tags: upstream-1.4.4
ImportĀ upstreamĀ versionĀ 1.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
  return buf;
101
101
}
102
102
 
 
103
/* Allocate a bit string consisting of '0' and '1' from the MPI A.  Do
 
104
   not return any leading zero bits. Caller needs to xfree the
 
105
   result. */
 
106
static char *
 
107
mpi2bitstr_nlz (gcry_mpi_t a)
 
108
{
 
109
  char *p, *buf;
 
110
  size_t length = gcry_mpi_get_nbits (a);
 
111
  
 
112
  buf = p = xmalloc (length + 1);
 
113
  while (length-- > 1)
 
114
    *p++ = gcry_mpi_test_bit (a, length) ? '1':'0';
 
115
  *p++ = gcry_mpi_test_bit (a, 0) ? '1':'0';
 
116
  *p = 0;
 
117
 
 
118
  return buf;
 
119
}
 
120
 
103
121
/* Shift a bit string to the right. */
104
122
static void
105
123
rshiftbitstring (char *string, size_t n)
113
131
  memset (string, '0', n);
114
132
}
115
133
 
 
134
/* Shift a bit string to the left. Caller needs to free the result. */
 
135
static char *
 
136
lshiftbitstring (const char *string, size_t n)
 
137
{
 
138
  size_t len = strlen (string);
 
139
  char *result;
 
140
 
 
141
  if (len+n+1 < len)
 
142
    die ("internal overflow\n");
 
143
  /* Allocate enough space. */ 
 
144
  result = xmalloc (len+n+1);
 
145
  for (; *string == '0' && string[1]; string++, len--)
 
146
    ;
 
147
  memcpy (result, string, len);
 
148
  if (*string == '0' && !string[1])
 
149
    n = 0; /* Avoid extra nulls for an only 0 string.  */
 
150
  else
 
151
    memset (result+len, '0', n);
 
152
  result[len+n] = 0;
 
153
  return result;
 
154
}
 
155
 
116
156
 
117
157
/* This is to check a bug reported by bpgcrypt at itaparica.org on
118
158
   2006-07-31 against libgcrypt 1.2.2.  */
141
181
    fail ("failed to clear a bit\n");
142
182
  result = mpi2bitstr (a, 70);
143
183
  assert (strlen (result) == 70);
144
 
  show ("r=%s\n", result);
145
184
  for (i=0; result[i]; i++)
146
185
    if ( result[i] != '0' )
147
186
      break;
210
249
  gcry_mpi_release (a);
211
250
}
212
251
 
 
252
/* Check that the left shifting.  */
 
253
static void
 
254
test_lshift (int pass)
 
255
{
 
256
  static int size_list[] = {1, 31, 32, 63, 64, 65, 70, 0};
 
257
  int size_idx;
 
258
  gcry_mpi_t a, b;
 
259
  char *tmpstr, *result, *result2;
 
260
  int i;
 
261
 
 
262
  wherestr = "test_lshift";
 
263
  show ("checking that lshift works as expected (pass %d)\n", pass);
 
264
 
 
265
  for (size_idx=0; size_list[size_idx]; size_idx++)
 
266
    {
 
267
      a = gcry_mpi_new (0);
 
268
      b = gcry_mpi_new (0);
 
269
 
 
270
      /* gcry_mpi_randomize rounds up to full bytes, thus we need to
 
271
         use gcry_mpi_clear_highbit to fix that.  */
 
272
      gcry_mpi_randomize (a, size_list[size_idx], GCRY_WEAK_RANDOM);
 
273
      gcry_mpi_clear_highbit (a, size_list[size_idx]);
 
274
 
 
275
      for (i=0; i < 75; i++)
 
276
        {
 
277
          gcry_mpi_lshift (b, a, i);
 
278
          
 
279
          result = mpi2bitstr_nlz (b);
 
280
          tmpstr = mpi2bitstr_nlz (a);
 
281
          result2 = lshiftbitstring (tmpstr, i);
 
282
          xfree (tmpstr);
 
283
          if (strcmp (result, result2))
 
284
            {
 
285
              show ("got =%s\n", result);
 
286
              show ("want=%s\n", result2);
 
287
              fail ("lshift by %d failed\n", i);
 
288
            }
 
289
          xfree (result);
 
290
          xfree (result2);
 
291
        }
 
292
      
 
293
      /* Again. This time using in-place operation. */
 
294
      gcry_mpi_randomize (a, size_list[size_idx], GCRY_WEAK_RANDOM);
 
295
      gcry_mpi_clear_highbit (a, size_list[size_idx]);
 
296
      
 
297
      for (i=0; i < 75; i++)
 
298
        {
 
299
          gcry_mpi_release (b);
 
300
          b = gcry_mpi_copy (a);
 
301
          gcry_mpi_lshift (b, b, i);
 
302
 
 
303
          result = mpi2bitstr_nlz (b);
 
304
          tmpstr = mpi2bitstr_nlz (a);
 
305
          result2 = lshiftbitstring (tmpstr, i);
 
306
          xfree (tmpstr);
 
307
          if (strcmp (result, result2))
 
308
            {
 
309
              show ("got =%s\n", result);
 
310
              show ("want=%s\n", result2);
 
311
              fail ("in-place lshift by %d failed\n", i);
 
312
            }
 
313
          xfree (result2);
 
314
          xfree (result);
 
315
        }
 
316
 
 
317
      gcry_mpi_release (b);
 
318
      gcry_mpi_release (a);
 
319
    }
 
320
}
 
321
 
213
322
 
214
323
int
215
324
main (int argc, char **argv)
226
335
    die ("version mismatch\n");
227
336
 
228
337
  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
229
 
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
 
338
  gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
230
339
  if (debug)
231
340
    gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
232
 
  gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
 
341
 
 
342
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
233
343
 
234
344
  one_bit_only (0);
235
345
  one_bit_only (1);
236
346
  for (i=0; i < 5; i++)
237
347
    test_rshift (i); /* Run several times due to random initializations. */
 
348
 
 
349
  for (i=0; i < 5; i++)
 
350
    test_lshift (i); /* Run several times due to random initializations. */
238
351
  
239
352
  show ("All tests completed. Errors: %d\n", error_count);
240
353
  return error_count ? 1 : 0;