ecc-add-test.c 1.93 KB
Newer Older
1
#include "testutils.h"
2
#include <assert.h>
3 4 5 6 7 8 9 10 11 12 13 14 15

void
test_main (void)
{
  unsigned i;

  for (i = 0; ecc_curves[i]; i++)
    {
      const struct ecc_curve *ecc = ecc_curves[i];
      mp_limb_t *g = xalloc_limbs (ecc_size_j (ecc));
      mp_limb_t *g2 = xalloc_limbs (ecc_size_j (ecc));
      mp_limb_t *g3 = xalloc_limbs (ecc_size_j (ecc));
      mp_limb_t *p = xalloc_limbs (ecc_size_j (ecc));
16 17 18 19 20
      mp_limb_t *scratch = xalloc_limbs (ecc->add_hhh_itch);

      ASSERT (ecc->dup_itch <= ecc->add_hhh_itch);

      ecc_a_to_j (ecc, g, ecc->g);
21

Niels Möller's avatar
Niels Möller committed
22
      if (ecc->p.bit_size == 255)
23 24
	{
	  mp_limb_t *z = xalloc_limbs (ecc_size_j (ecc));
25 26 27 28 29

	  ASSERT (ecc->add_hh == ecc_add_eh);
	  ASSERT (ecc->add_hhh == ecc_add_ehh);
	  ASSERT (ecc->add_hh_itch <= ecc->add_hhh_itch);

30
	  /* Zero point has x = 0, y = 1, z = 1 */
Niels Möller's avatar
Niels Möller committed
31 32
	  mpn_zero (z, 3*ecc->p.size);
	  z[ecc->p.size] = z[2*ecc->p.size] = 1;
33 34

	  ecc->add_hhh (ecc, p, z, z, scratch);
35
	  test_ecc_mul_h (i, 0, p);
36

37
	  ecc->add_hh (ecc, p, z, z, scratch);
38
	  test_ecc_mul_h (i, 0, p);
39

40
	  ecc->add_hhh (ecc, p, g, p, scratch);
41 42
	  test_ecc_mul_h (i, 1, p);

43
	  ecc->add_hh (ecc, p, z, g, scratch);
44 45
	  test_ecc_mul_h (i, 1, p);

46
	  ecc->add_hhh (ecc, g2, g, p, scratch);
47 48
	  test_ecc_mul_h (i, 2, g2);

49
	  ecc->add_hh (ecc, g2, g, g, scratch);
50 51 52 53 54 55
	  test_ecc_mul_h (i, 2, g2);

	  free (z);
	}
      else
	{
56 57 58
	  ASSERT (ecc->add_hhh == ecc_add_jjj);
	  ASSERT (ecc->dup == ecc_dup_jj);
	}
59

60 61
      ecc->dup (ecc, g2, g, scratch);
      test_ecc_mul_h (i, 2, g2);
62

63 64
      ecc->add_hhh (ecc, g3, g, g2, scratch);
      test_ecc_mul_h (i, 3, g3);
65

66 67
      ecc->add_hhh (ecc, g3, g2, g, scratch);
      test_ecc_mul_h (i, 3, g3);
68

69 70
      ecc->add_hhh (ecc, p, g, g3, scratch);
      test_ecc_mul_h (i, 4, p);
71

72 73
      ecc->add_hhh (ecc, p, g3, g, scratch);
      test_ecc_mul_h (i, 4, p);
74

75 76
      ecc->dup (ecc, p, g2, scratch);
      test_ecc_mul_h (i, 4, p);
77 78 79 80 81 82 83 84

      free (g);
      free (g2);
      free (g3);
      free (p);
      free (scratch);
    }
}