gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r11390 - gnunet/src/util


From: gnunet
Subject: [GNUnet-SVN] r11390 - gnunet/src/util
Date: Mon, 17 May 2010 07:02:11 +0200

Author: amatus
Date: 2010-05-17 07:02:11 +0200 (Mon, 17 May 2010)
New Revision: 11390

Modified:
   gnunet/src/util/crypto_ksk.c
Log:
Fix mpz_randomize to always update the "state" of rnd, and optimize the code 
that assumes that mpz_randomize can return a number with more bits than 
requested. This change breaks kblock key generation again.


Modified: gnunet/src/util/crypto_ksk.c
===================================================================
--- gnunet/src/util/crypto_ksk.c        2010-05-15 21:42:02 UTC (rev 11389)
+++ gnunet/src/util/crypto_ksk.c        2010-05-17 05:02:11 UTC (rev 11390)
@@ -156,35 +156,23 @@
 static int no_of_small_prime_numbers = DIM (small_prime_numbers) - 1;
 
 
-     static unsigned int
-     get_nbits (mpz_t a)
+static unsigned int
+get_nbits (mpz_t a)
 {
   return mpz_sizeinbase (a, 2);
 }
 
 
-/**
- * Set bit N of A. and clear all bits above
- */
 static void
-set_highbit (mpz_t a, unsigned int n)
-{
-  unsigned int nbits;
-
-  nbits = get_nbits (a);
-  while (nbits > n)
-    mpz_clrbit (a, nbits--);
-  mpz_setbit (a, n);
-}
-
-static void
 mpz_randomize (mpz_t n, unsigned int nbits, GNUNET_HashCode * rnd)
 {
   GNUNET_HashCode *tmp;
+  int bits_per_hc = sizeof (GNUNET_HashCode) * 8;
   int cnt;
   int i;
 
-  cnt = (nbits / sizeof (GNUNET_HashCode) / 8) + 1;
+  GNUNET_assert (nbits > 0);
+  cnt = (nbits + bits_per_hc - 1) / bits_per_hc;
   tmp = GNUNET_malloc (sizeof (GNUNET_HashCode) * cnt);
 
   tmp[0] = *rnd;
@@ -192,7 +180,7 @@
     {
       GNUNET_CRYPTO_hash (&tmp[i], sizeof (GNUNET_HashCode), &tmp[i + 1]);
     }
-  *rnd = tmp[cnt - 1];
+  GNUNET_CRYPTO_hash (rnd, sizeof (GNUNET_HashCode), &tmp[i + 1]);
   mpz_import (n, cnt * sizeof (GNUNET_HashCode) / sizeof (unsigned int),
               1, sizeof (unsigned int), 1, 0, tmp);
   GNUNET_free (tmp);
@@ -238,19 +226,7 @@
         }
       else
         {
-          mpz_randomize (x, nbits, hc);
-
-          /* Make sure that the number is smaller than the prime and
-             keep the randomness of the high bit. */
-          if (mpz_tstbit (x, nbits - 2))
-            {
-              set_highbit (x, nbits - 2);       /* Clear all higher bits. */
-            }
-          else
-            {
-              set_highbit (x, nbits - 2);
-              mpz_clrbit (x, nbits - 2);
-            }
+          mpz_randomize (x, nbits - 1, hc);
           GNUNET_assert (mpz_cmp (x, nminus1) < 0 && mpz_cmp_ui (x, 1) > 0);
         }
       mpz_powm (y, x, q, n);
@@ -306,7 +282,7 @@
          generating a secret prime we are most probably doing that
          for RSA, to make sure that the modulus does have the
          requested key size we set the 2 high order bits. */
-      set_highbit (prime, nbits - 1);
+      mpz_setbit (prime, nbits - 1);
       mpz_setbit (prime, nbits - 2);
       mpz_setbit (prime, 0);
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]