Beispiel #1
0
int
main (void)
{
  b2n_t k;
  ec2np_t p, q, r;
  ec2ng_t g;
  char buf[BUFSIZE];

  b2n_init (k);
  ec2np_init (p);
  ec2np_init (q);
  ec2np_init (r);
  ec2ng_init (g);

  printf ("Testing: ec2ng_set* :");
  /* Init Group */
  ec2ng_set_p_str (g, "0x0800000000000000000000004000000000000001");
  CMP_FAIL (g->p, "0x0800000000000000000000004000000000000001");
  ec2ng_set_a_ui (g, 0);
  CMP_FAIL (g->a, "0x00");
  ec2ng_set_b_str (g, "0x07338f");
  CMP_FAIL (g->b, "0x07338f");

  printf ("\nTesting: ec2np_find_y: ");
  /* Init Point */
  ec2np_set_x_ui (p, 0x7b);
  ec2np_find_y (p, g);

  CMP_FAIL (p->y, "0x01c8");

  printf ("\nTesting: ec2np_ison: ");
  if (ec2np_ison (p, g))
    printf ("OKAY ");
  else
    printf ("FAILED ");

  ec2np_set_x_ui (q, 0x4);
  ec2np_find_y (q, g);
  if (ec2np_ison (q, g))
    printf ("OKAY ");
  else
    printf ("FAILED ");

  printf ("\nTesting: ec2np_add: ");
  ec2np_set (r, p);
  b2n_add (r->y, r->y, r->x);
  ec2np_add (r, r, p, g);
  if (!r->inf)
    printf ("FAILED ");
  else
    printf ("OKAY ");

  ec2np_add (q, p, q, g);
  CMP_FAIL (q->x, "0x06f32d7cc82cec8612a87a86e026350fb7595469");
  CMP_FAIL (q->y, "0x4ab92e21e51358ca8deab3fbbc9f7d8a7d1575");
  if (ec2np_ison (q, g))
    printf ("OKAY ");
  else
    printf ("FAILED ");

  ec2np_add (p, q, q, g);
  CMP_FAIL (p->x, "0x0390001461385559a22ac9b6181c1e1889b38451");
  CMP_FAIL (p->y, "0x0188e61f38d747d7813c6a8b33d14dfb7418b04c");
  if (ec2np_ison (p, g))
    printf ("OKAY ");
  else
    printf ("FAILED ");

  printf ("\nTesting: ec2np_mul: ");
  b2n_set_ui (k, 57);
  ec2np_set (q, p);
  ec2np_mul (q, q, k, g);
  if (ec2np_ison (q, g))
    printf ("OKAY ");
  else
    printf ("FAILED ");
  CMP_FAIL (q->x, "0x06bcf88caab88f99399350c46559da3b91afbf9d");

  b2n_set_str (k, "0x0800000000000000000057db5698537193aef943");
  ec2np_set (q, p);
  ec2np_mul (q, q, k, g);
  if (ec2np_ison (q, g))
    printf ("OKAY ");
  else
    printf ("FAILED ");
  CMP_FAIL (q->x, "0x0390001461385559a22ac9b6181c1e1889b38451");

  printf ("\n");
  ec2np_clear (p);
  ec2np_clear (q);
  ec2np_clear (r);
  ec2ng_clear (g);
  b2n_clear (k);
  return 1;
}
Beispiel #2
0
int
main (void)
{
  int i;
  b2n_t n, m, d, r;
  char buf[BUFSIZE];

  b2n_init (n);
  b2n_init (m);
  b2n_init (d);
  b2n_init (r);

  printf ("Arithimetic Tests for GF(2)[x]:\n");
  printf ("Testing: b2n_set*: ");
  b2n_set_ui (n, 0xffc0);
  CMP_FAIL (n, "0xffc0");

  b2n_set_str (m, "0x180c0");
  CMP_FAIL (m, "0x0180c0");
  b2n_set_str (m, "0x808b8080c0");
  CMP_FAIL (m, "0x808b8080c0");

  printf ("\nTesting: b2n_add: ");
  b2n_add (d, n, m);
  CMP_FAIL (d, "0x808b807f00");
  b2n_add (n, n, m);
  CMP_FAIL (n, "0x808b807f00");
  b2n_add (n, n, n);
  CMP_FAIL (n, "0x00");
  b2n_set_str (n, "0x9090900000000000000000");
  b2n_set_ui (m, 0);
  b2n_add (n, n, m);
  CMP_FAIL (n, "0x9090900000000000000000");

  printf ("\nTesting: b2n_lshift: ");
  b2n_set_str (m, "0x808b8080c0");
  b2n_lshift (n, m, 3);
  CMP_FAIL (n, "0x04045c040600");
  b2n_lshift (n, m, 11);
  CMP_FAIL (n, "0x04045c04060000");
  b2n_set (n, m);
  for (i = 0; i < 11; i++)
    b2n_lshift (n, n, 1);
  CMP_FAIL (n, "0x04045c04060000");
  b2n_lshift (d, m, 12);
  CMP_FAIL (d, "0x0808b8080c0000");
  b2n_set_str (m, "0xdeadbeef");
  b2n_lshift (d, m, 103);
  CMP_FAIL (d, "0x6f56df7780000000000000000000000000");

  printf ("\nTesting: b2n_rshift: ");
  b2n_rshift (m, n, 3);
  CMP_FAIL (m, "0x808b8080c000");
  b2n_rshift (m, m, 11);
  CMP_FAIL (m, "0x1011701018");
  b2n_set_str (m, "0x12381998713258186712365");
  b2n_rshift (m, m, 23);
  CMP_FAIL (m, "0x024703330e264b030c");
  b2n_set_str (m, "0x12381998713258186712365");
  for (i=0; i<23; i++)
    b2n_rshift (m, m, 1);
  CMP_FAIL (m, "0x024703330e264b030c");

  printf ("\nTesting: b2n_mul: 0x9 o 0x5: ");
  b2n_set_ui (n, 9);
  b2n_set_ui (m, 5);
  b2n_mul (d, n, m);
  CMP_FAIL (d, "0x2d");
  b2n_mul (n, n, m);
  CMP_FAIL (d, "0x2d");

  printf ("\nTesting: b2n_mul: 0x9 o 0x0: ");
  b2n_set_ui (n, 9);
  b2n_set_ui (m, 0);
  b2n_mul (d, n, m);
  CMP_FAIL (d, "0x00");
  b2n_set_ui (n, 0);
  b2n_set_ui (m, 9);
  b2n_mul (d, n, m);
  CMP_FAIL (d, "0x00");

  printf ("\nTesting: b2n_mul: 0x9 o 0x1: ");
  b2n_set_ui (n, 9);
  b2n_set_ui (m, 1);
  b2n_mul (d, n, m);
  CMP_FAIL (d, "0x09");

  printf ("\nTesting: b2n_mul: 0x12329 o 0x1235: ");
  b2n_set_str (n, "0x12329");
  b2n_set_str (m, "0x1235");
  b2n_mul (d, n, m);
  CMP_FAIL (d, "0x10473a3d");
  b2n_mul (n, n, m);
  CMP_FAIL (d, "0x10473a3d");

  printf ("\nTesting: b2n_square: 0x1235 o 0x1235: ");
  b2n_set_str (m, "0x1235");
  b2n_square (n, m);
  CMP_FAIL (n, "0x01040511");

  printf ("\nTesting: b2n_square: 0x80c1235 o 0x80c1235: ");
  b2n_set_str (m, "0x80c1235");
  b2n_square (n, m);
  CMP_FAIL (n, "0x40005001040511");

  b2n_set_str (m, "0x12329");
  printf ("\nTesting: sigbit: 0x12329: %d, %s",
	  b2n_sigbit(m), b2n_sigbit(m) == 17 ? "OKAY" : "FAILED");
  b2n_set_ui (m, 0);
  printf ("\nTesting: sigbit: 0x0: %d, %s",
	  b2n_sigbit(m), b2n_sigbit(m) == 0 ? "OKAY" : "FAILED");
  b2n_set_str (m, "0x7f3290000");
  printf ("\nTesting: sigbit: 0x7f3290000: %d, %s",
	  b2n_sigbit(m), b2n_sigbit(m) == 35 ? "OKAY" : "FAILED");

  printf ("\nTesting: b2n_cmp: ");
  b2n_set_str (m, "0x2234");
  b2n_set_str (n, "0x1234");
  printf ("%d <-> %d, ", b2n_sigbit (m), b2n_sigbit(n));
  printf ("%d, %d ,%d: ", b2n_cmp (m,m), b2n_cmp (m,n), b2n_cmp (n,m));
  if (b2n_cmp (m,m) || b2n_cmp (m,n) != 1 || b2n_cmp (n,m) != -1)
    printf ("FAILED");
  else
    printf ("OKAY");
  printf ("\nTesting: b2n_cmp_null: ");
  b2n_set_str (m, "0x2234");
  b2n_set_ui (n, 0);
  printf ("%d, %d: ", b2n_cmp_null (m), b2n_cmp_null (n));
  if (b2n_cmp_null (m) != 1 || b2n_cmp_null (n))
    printf ("FAILED");
  else
    printf ("OKAY");

  printf ("\nTesting: b2n_div: 0x2d / 0x5: ");
  b2n_set_str (n, "0x2d");
  b2n_set_ui (m, 5);
  b2n_div (n, m, n, m);
  CMP_FAIL (n, "0x09");
  CMP_FAIL (m, "0x00");
  printf ("\nTesting: b2n_div: 0x2d / 0x1: ");
  b2n_set_str (n, "0x2d");
  b2n_set_ui (m, 1);
  b2n_div (n, m, n, m);
  CMP_FAIL (n, "0x2d");
  CMP_FAIL (m, "0x00");

  printf ("\nTesting: b2n_div: 0x10473a3d / 0x1235: ");
  b2n_set_str (n, "0x10473a3d");
  b2n_set_str (m, "0x1235");
  b2n_div (n, m, n, m);
  CMP_FAIL (n, "0x012329");
  CMP_FAIL (m, "0x00");

  printf ("\nTesting: b2n_div: 0x10473a3d / 0x1536: ");
  b2n_set_str (n, "0x10473a3d");
  b2n_set_str (m, "0x1536");
  b2n_div (n, m, n, m);
  CMP_FAIL (n, "0x014331");
  CMP_FAIL (m, "0xab");
  b2n_set_str (n, "0x10473a3d");
  b2n_set_str (m, "0x1536");
  b2n_div_q (d, n, m);
  CMP_FAIL (d, "0x014331");
  b2n_div_r (d, n, m);
  CMP_FAIL (d, "0xab");

  printf ("\nTesting: b2n_div: "
	  "0x0800000000000000000000004000000000000001 / 0xffab09909a00: ");
  b2n_set_str (n, "0x0800000000000000000000004000000000000001");
  b2n_set_str (m, "0xffab09909a00");
  b2n_div_q (d, n, m);
  CMP_FAIL (d, "0x18083e83a98647cedae0b3e69a5e");
  b2n_div_r (d, n, m);
  CMP_FAIL (d, "0x5b8bf98cac01");
  b2n_set (d, m);
  b2n_div (n, m, n, m);
  CMP_FAIL (n, "0x18083e83a98647cedae0b3e69a5e");
  CMP_FAIL (m, "0x5b8bf98cac01");

  printf ("\nTesting: b2n_div: "
	  "0x0800000000000000000000004000000000000001 / 0x7b: ");
  b2n_set_str (n, "0x0800000000000000000000004000000000000001");
  b2n_set_str (m, "0x7b");
  b2n_div (n, m, n, m);
  CMP_FAIL (n, "0x32dea27065bd44e0cb7a89c000000000000000");
  CMP_FAIL (m, "0x01");

  printf ("\n\nArithimetic Tests for GF(2**m) ~= GF(2)[x]/p(x):\n");
  printf ("Testing: b2n_gcd: ");
  b2n_set_str (d, "0x771");
  b2n_set_str (m, "0x26d");
  b2n_gcd (n, m, d);
  CMP_FAIL (n, "0x0b");
  b2n_set_str (d, "0x0800000000000000000000004000000000000001");
  b2n_set_str (m, "0xffab09909a00");
  b2n_gcd (n, m, d);
  CMP_FAIL (n, "0x01");
  b2n_set_str (d, "0x0800000000000000000000004000000000000001");
  b2n_set_str (m, "0x7b");
  b2n_gcd (n, m, d);
  CMP_FAIL (n, "0x01");

  printf ("\nTesting: b2n_mul_inv: ");
  b2n_set_str (d, "0x0800000000000000000000004000000000000001");
  b2n_set_str (m, "0xffab09909a00");
  b2n_mul_inv (n, m, d);
  CMP_FAIL (n, "0x074029149f69304174d28858ae5c60df208a22a8");
  b2n_set_str (n, "0xffab09909a00");
  b2n_mul_inv (n, n, d);
  CMP_FAIL (n, "0x074029149f69304174d28858ae5c60df208a22a8");
  b2n_mul (n, n, m);
  b2n_mod (n, n, d);
  CMP_FAIL (n, "0x01");
  b2n_set_str (d, "0x0800000000000000000000004000000000000001");
  b2n_set_str (m, "0x7b");
  b2n_mul_inv (n, m, d);
  CMP_FAIL (n, "0x32dea27065bd44e0cb7a89c000000000000000");
  b2n_mul (n, n, m);
  b2n_mod (n, n, d);
  CMP_FAIL (n, "0x01");

  printf ("\nTesting: b2n_random: ");
  b2n_random (m, 155);
  b2n_snprint (buf, BUFSIZE, m);
  printf ("%s, %d", buf, b2n_sigbit(m));

  printf ("\nTesting: b2n_sqrt: ");
  b2n_set_str (n, "0x0800000000000000000000004000000000000001");
  b2n_set_ui (d, 2);
  b2n_sqrt (m, d, n);
  b2n_square (d, m);
  b2n_add (d, d, m);
  b2n_mod (d, d, n);
  CMP_FAIL (d, "0x02");

  /* x**3 + b */
  b2n_set_ui (n, 0x7b);
  b2n_square (d, n);
  b2n_mul (d, d, n);
  b2n_set_str (n, "0x07338f");
  b2n_add (d, d, n);
  b2n_set_str (n, "0x0800000000000000000000004000000000000001");
  b2n_mod (d, d, n);
  /* \alpha = x**3 + b - end */

  /* \beta = x**(-2)*\alpha */
  b2n_set_ui (m, 0x7b);
  b2n_mul_inv (m, m, n);
  b2n_square (m, m);
  b2n_mod (m, m, n);
  b2n_mul (d, d, m);
  b2n_mod (d, d, n);
  b2n_set (r, d);
  /* \beta = x**(-2)*\alpha - end */

  b2n_sqrt (m, d, n);
  CMP_FAIL (m, "0x0690aec7cd215d8f9a42bb1f0000000000000004");
  b2n_square (d, m);
  b2n_mod (d, d, n);
  b2n_add (d, d, m);
  b2n_mod (d, d, n);
  printf ("Squaring Check: ");
  CMP_FAIL (d, "0x03d5af92c8311d9e8f56be4b3e690aec7cd215cc");

  printf ("\nTesting: b2n_trace: ");
  b2n_set_ui (m, 2);
  b2n_trace (d, m, n);
  CMP_FAIL (d, "0x00");
  b2n_set_ui (m, 0x11223);
  b2n_trace (d, m, n);
  CMP_FAIL (d, "0x01");

  printf ("\nTesting: b2n_exp_mod: ");
  b2n_set_ui (m, 0x7b);
  b2n_exp_mod (d, m, 5, n);
  CMP_FAIL (d, "0x7cccb7cb");
  b2n_set_str (m, "0x123456789abcdef");
  b2n_exp_mod (d, m, 13, n);
  CMP_FAIL (d, "0x043f0a8550cb69b3c50d0340d1c6d5c97ecd60d4");

  printf ("\nTesting: b2n_3mul: ");
  b2n_set_ui (m, 0x7b);
  b2n_3mul (m, m);
  CMP_FAIL (m, "0x0171");

  b2n_set_ui (m, 0x7fffffff);
  b2n_3mul (m, m);
  CMP_FAIL (m, "0x017ffffffd");

  printf ("\nTesting: b2n_nadd: ");
  b2n_set_str (m, "0x7fffffff");
  b2n_set_str (n, "0x10203045");
  b2n_nadd (d, n, m);
  CMP_FAIL (d, "0x90203044");

  b2n_set_str (m, "0x9a4a54d8b8dfa566112849991214329a233d");
  b2n_set_str (n, "0x70ee40dd60c8657e58eda9a17ad9176e28b4b457e5a34a0948e335");
  b2n_nadd (d, n, m);
  CMP_FAIL (d, "0x70ee40dd60c8657e5987f3f65391f7138ec5dca17eb55e3be30672");

  printf ("\nTesting: b2n_nsub: ");
  b2n_set_str (n, "0x90203044");
  b2n_set_str (m, "0x10203045");
  b2n_nsub (d, n, m);
  CMP_FAIL (d, "0x7fffffff");

  b2n_set_str (n, "0x70ee40dd60c8657e5987f3f65391f7138ec5dca17eb55e3be30672");
  b2n_set_str (m, "0x70ee40dd60c8657e58eda9a17ad9176e28b4b457e5a34a0948e335");
  b2n_nsub (d, n, m);
  CMP_FAIL (d, "0x9a4a54d8b8dfa566112849991214329a233d");

  b2n_clear (n);
  b2n_clear (m);
  b2n_clear (d);
  b2n_clear (r);

  printf ("\n");
  return 0;
}