fprime.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Arithmetic in prime fields
  2. * Daniel Beer <dlbeer@gmail.com>, 10 Jan 2014
  3. *
  4. * This file is in the public domain.
  5. */
  6. #ifndef FPRIME_H_
  7. #define FPRIME_H_
  8. #include <stdint.h>
  9. #include <string.h>
  10. /* Maximum size of a field element (or a prime). Field elements are
  11. * always manipulated and stored in normalized form, with 0 <= x < p.
  12. * You can use normalize() to convert a denormalized bitstring to normal
  13. * form.
  14. *
  15. * Operations are constant with respect to the value of field elements,
  16. * but not with respect to the modulus.
  17. *
  18. * The modulus is a number p, such that 2p-1 fits in FPRIME_SIZE bytes.
  19. */
  20. #define FPRIME_SIZE 32
  21. /* Load a large constant */
  22. void fprime_from_bytes(uint8_t *x,
  23. const uint8_t *in, size_t len,
  24. const uint8_t *modulus);
  25. /* Copy an element */
  26. static inline void fprime_copy(uint8_t *x, const uint8_t *a)
  27. {
  28. memcpy(x, a, FPRIME_SIZE);
  29. }
  30. /* Compare two field points in constant time. Return one if equal, zero
  31. * otherwise. This should be performed only on normalized values.
  32. */
  33. uint8_t fprime_eq(const uint8_t *x, const uint8_t *y);
  34. /* Conditional copy. If condition == 0, then zero is copied to dst. If
  35. * condition == 1, then one is copied to dst. Any other value results in
  36. * undefined behaviour.
  37. */
  38. void fprime_select(uint8_t *dst,
  39. const uint8_t *zero, const uint8_t *one,
  40. uint8_t condition);
  41. /* Add one value to another. The two pointers must be distinct. */
  42. void fprime_add(uint8_t *r, const uint8_t *a, const uint8_t *modulus);
  43. /* Multiply two values to get a third. r must be distinct from a and b */
  44. void fprime_mul(uint8_t *r, const uint8_t *a, const uint8_t *b,
  45. const uint8_t *modulus);
  46. #endif