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.

39 lines
990 B

  1. #include "fr.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <gmp.h>
  5. void Fr_str2element(PFrElement pE, char *s) {
  6. mpz_t r;
  7. mpz_init(r);
  8. mpz_set_str(r, s, 10);
  9. pE->type = Fr_LONG;
  10. for (int i=0; i<Fr_N64; i++) pE->longVal[i] = 0;
  11. mpz_export((void *)pE->longVal, NULL, -1, 8, -1, 0, r);
  12. }
  13. char *Fr_element2str(PFrElement pE) {
  14. mpz_t r;
  15. mpz_t q;
  16. if (pE->type == Fr_SHORT) {
  17. if (pE->shortVal>=0) {
  18. char *r = new char[32];
  19. sprintf(r, "%d", pE->shortVal);
  20. return r;
  21. } else {
  22. mpz_init(q);
  23. mpz_import(q, Fr_N64, -1, 8, -1, 0, (const void *)Fr_q.longVal);
  24. mpz_init_set_si(r, pE->shortVal);
  25. mpz_add(r, r, q);
  26. mpz_clear(q);
  27. }
  28. } else {
  29. Fr_toNormal(pE);
  30. mpz_init(r);
  31. mpz_import(r, Fr_N64, -1, 8, -1, 0, (const void *)pE->longVal);
  32. }
  33. char *res = mpz_get_str (0, 10, r);
  34. mpz_clear(r);
  35. return res;
  36. }