You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

52 lines
1.3 KiB

  1. /**
  2. * @cond internal
  3. * @file f_arithmetic.c
  4. * @copyright
  5. * Copyright (c) 2014 Cryptography Research, Inc. \n
  6. * Released under the MIT License. See LICENSE.txt for license information.
  7. * @author Mike Hamburg
  8. * @brief Field-specific arithmetic.
  9. */
  10. #include "field.h"
  11. #include "constant_time.h"
  12. /* Guarantee: a^2 x = 0 if x = 0; else a^2 x = 1 or SQRT_MINUS_ONE; */
  13. mask_t gf_isr (gf a, const gf x) {
  14. gf L0, L1, L2, L3;
  15. gf_sqr (L0, x);
  16. gf_mul (L1, L0, x);
  17. gf_sqr (L0, L1);
  18. gf_mul (L1, L0, x);
  19. gf_sqrn(L0, L1, 3);
  20. gf_mul (L2, L0, L1);
  21. gf_sqrn(L0, L2, 6);
  22. gf_mul (L1, L2, L0);
  23. gf_sqr (L2, L1);
  24. gf_mul (L0, L2, x);
  25. gf_sqrn(L2, L0, 12);
  26. gf_mul (L0, L2, L1);
  27. gf_sqrn(L2, L0, 25);
  28. gf_mul (L3, L2, L0);
  29. gf_sqrn(L2, L3, 25);
  30. gf_mul (L1, L2, L0);
  31. gf_sqrn(L2, L1, 50);
  32. gf_mul (L0, L2, L3);
  33. gf_sqrn(L2, L0, 125);
  34. gf_mul (L3, L2, L0);
  35. gf_sqrn(L2, L3, 2);
  36. gf_mul (L0, L2, x);
  37. gf_sqr (L2, L0);
  38. gf_mul (L3, L2, x);
  39. gf_add(L1,L3,ONE);
  40. mask_t one = gf_eq(L3,ONE);
  41. mask_t succ = one | gf_eq(L1,ZERO);
  42. mask_t qr = one | gf_eq(L3,SQRT_MINUS_ONE);
  43. constant_time_select(L2, SQRT_MINUS_ONE, ONE, sizeof(L2), qr, 0);
  44. gf_mul (a,L2,L0);
  45. return succ;
  46. }