Пример #1
0
/**
 * Do some DLOG operations for testing.
 *
 * @param edc context for ECC operations
 * @param do_dlog #GNUNET_YES if we want to actually do the bencharked operation
 */
static void
test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc, 
           int do_dlog)
{
  gcry_mpi_t fact;
  gcry_mpi_t n;
  gcry_ctx_t ctx;
  gcry_mpi_point_t q;
  gcry_mpi_point_t g;
  unsigned int i;
  int x;
  int iret;

  GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
  g = gcry_mpi_ec_get_point ("g", ctx, 0);
  GNUNET_assert (NULL != g);
  n = gcry_mpi_ec_get_mpi ("n", ctx, 0);
  q = gcry_mpi_point_new (0);
  fact = gcry_mpi_new (0);
  for (i=0;i<TEST_ITER;i++)
  {
    fprintf (stderr, ".");
    x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
				  MAX_FACT);
    if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
				       2))
    {
      gcry_mpi_set_ui (fact, x);
      gcry_mpi_sub (fact, n, fact);
      x = - x;
    }
    else 
    {
      gcry_mpi_set_ui (fact, x);
    }
    gcry_mpi_ec_mul (q, fact, g, ctx);
    if ( (GNUNET_YES == do_dlog) &&
	 (x !=
	  (iret = GNUNET_CRYPTO_ecc_dlog (edc,
					  q))) )
    {
      fprintf (stderr, 
	       "DLOG failed for value %d (%d)\n", 
	       x,
	       iret);
      GNUNET_assert (0);
    }
  }
  gcry_mpi_release (fact);
  gcry_mpi_release (n);
  gcry_mpi_point_release (g);
  gcry_mpi_point_release (q);
  gcry_ctx_release (ctx);
  fprintf (stderr, "\n");
}
Пример #2
0
/**
 * Do some arithmetic operations for testing.
 *
 * @param edc context for ECC operations
 */
static void
test_math (struct GNUNET_CRYPTO_EccDlogContext *edc)
{
  int i;
  int j;
  gcry_mpi_point_t ip;
  gcry_mpi_point_t jp;
  gcry_mpi_point_t r;
  gcry_mpi_point_t ir;
  gcry_mpi_point_t irj;
  gcry_mpi_point_t r_inv;
  gcry_mpi_point_t sum;

  for (i=-MATH_MAX;i<MATH_MAX;i++)
  {
    ip = GNUNET_CRYPTO_ecc_dexp (edc, i);
    for (j=-MATH_MAX;j<MATH_MAX;j++)
    {
      fprintf (stderr, ".");
      jp = GNUNET_CRYPTO_ecc_dexp (edc, j);
      GNUNET_CRYPTO_ecc_rnd (edc,
			     &r,
			     &r_inv);
      ir = GNUNET_CRYPTO_ecc_add (edc, ip, r);
      irj = GNUNET_CRYPTO_ecc_add (edc, ir, jp);
      sum = GNUNET_CRYPTO_ecc_add (edc, irj, r_inv);
      GNUNET_assert (i + j ==
		     GNUNET_CRYPTO_ecc_dlog (edc,
					     sum));
      GNUNET_CRYPTO_ecc_free (jp);
      GNUNET_CRYPTO_ecc_free (ir);
      GNUNET_CRYPTO_ecc_free (irj);
      GNUNET_CRYPTO_ecc_free (r);
      GNUNET_CRYPTO_ecc_free (r_inv);
      GNUNET_CRYPTO_ecc_free (sum);
    }
    GNUNET_CRYPTO_ecc_free (ip);
  }
  fprintf (stderr, "\n");
}