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.
 
 
 
 
 

88 lines
1.7 KiB

  1. /**
  2. * @cond internal
  3. * @file field.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 High-level arithmetic routines, independent of field (except 3 mod 4).
  9. */
  10. #include "field.h"
  11. #include "ec_point.h"
  12. mask_t
  13. field_eq (
  14. const field_a_t a,
  15. const field_a_t b
  16. ) {
  17. field_a_t ra, rb;
  18. field_copy(ra, a);
  19. field_copy(rb, b);
  20. field_weak_reduce(ra);
  21. field_weak_reduce(rb);
  22. field_sub_RAW(ra, ra, rb);
  23. field_bias(ra, 2);
  24. return field_is_zero(ra);
  25. }
  26. void
  27. field_inverse (
  28. field_a_t a,
  29. const field_a_t x
  30. ) {
  31. field_a_t L0, L1;
  32. field_isr ( L0, x );
  33. field_sqr ( L1, L0 );
  34. field_sqr ( L0, L1 );
  35. field_mul ( a, x, L0 );
  36. }
  37. mask_t
  38. field_is_square (
  39. const field_a_t x
  40. ) {
  41. field_a_t L0, L1;
  42. field_isr ( L0, x );
  43. field_sqr ( L1, L0 );
  44. field_mul ( L0, x, L1 );
  45. field_subw( L0, 1 );
  46. return field_is_zero( L0 ) | field_is_zero( x );
  47. }
  48. void
  49. field_simultaneous_invert (
  50. field_a_t *__restrict__ out,
  51. const field_a_t *in,
  52. unsigned int n
  53. ) {
  54. if (n==0) {
  55. return;
  56. } else if (n==1) {
  57. field_inverse(out[0],in[0]);
  58. return;
  59. }
  60. field_copy(out[1], in[0]);
  61. int i;
  62. for (i=1; i<(int) (n-1); i++) {
  63. field_mul(out[i+1], out[i], in[i]);
  64. }
  65. field_mul(out[0], out[n-1], in[n-1]);
  66. field_a_t tmp;
  67. field_inverse(tmp, out[0]);
  68. field_copy(out[0], tmp);
  69. /* at this point, out[0] = product(in[i]) ^ -1
  70. * out[i] = product(in[0]..in[i-1]) if i != 0
  71. */
  72. for (i=n-1; i>0; i--) {
  73. field_mul(tmp, out[i], out[0]);
  74. field_copy(out[i], tmp);
  75. field_mul(tmp, out[0], in[i]);
  76. field_copy(out[0], tmp);
  77. }
  78. }