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.

112 lines
2.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #include "zqfield.h"
  2. ZqField::ZqField(PBigInt ap) {
  3. mpz_init2(tmp, 1024);
  4. mpz_init_set(p, *ap);
  5. mpz_init_set_ui(zero, 0);
  6. mpz_init_set_ui(one, 1);
  7. }
  8. ZqField::~ZqField() {
  9. mpz_clear(tmp);
  10. mpz_clear(p);
  11. mpz_clear(zero);
  12. mpz_clear(one);
  13. }
  14. void ZqField::add(PBigInt r, PBigInt a, PBigInt b) {
  15. mpz_add(*r,*a,*b);
  16. if (mpz_cmp(*r, p) >= 0) {
  17. mpz_sub(*r, *r, p);
  18. }
  19. }
  20. void ZqField::sub(PBigInt r, PBigInt a, PBigInt b) {
  21. if (mpz_cmp(*a, *b) >= 0) {
  22. mpz_sub(*r, *a, *b);
  23. } else {
  24. mpz_sub(tmp, *b, *a);
  25. mpz_sub(*r, p, tmp);
  26. }
  27. }
  28. void ZqField::mul(PBigInt r, PBigInt a, PBigInt b) {
  29. mpz_mul(tmp,*a,*b);
  30. mpz_fdiv_r(*r, tmp, p);
  31. }
  32. void ZqField::div(PBigInt r, PBigInt a, PBigInt b) {
  33. mpz_invert(tmp, *b, p);
  34. mpz_mul(tmp,*a,tmp);
  35. mpz_fdiv_r(*r, tmp, p);
  36. }
  37. void ZqField::idiv(PBigInt r, PBigInt a, PBigInt b) {
  38. mpz_fdiv_q(*r, *a, *b);
  39. }
  40. void ZqField::mod(PBigInt r, PBigInt a, PBigInt b) {
  41. mpz_fdiv_r(*r, *a, *b);
  42. }
  43. void ZqField::lt(PBigInt r, PBigInt a, PBigInt b) {
  44. int c = mpz_cmp(*a, *b);
  45. if (c<0) {
  46. mpz_set(*r, one);
  47. } else {
  48. mpz_set(*r, zero);
  49. }
  50. }
  51. void ZqField::eq(PBigInt r, PBigInt a, PBigInt b) {
  52. int c = mpz_cmp(*a, *b);
  53. if (c==0) {
  54. mpz_set(*r, one);
  55. } else {
  56. mpz_set(*r, zero);
  57. }
  58. }
  59. void ZqField::gt(PBigInt r, PBigInt a, PBigInt b) {
  60. int c = mpz_cmp(*a, *b);
  61. if (c>0) {
  62. mpz_set(*r, one);
  63. } else {
  64. mpz_set(*r, zero);
  65. }
  66. }
  67. void ZqField::leq(PBigInt r, PBigInt a, PBigInt b) {
  68. int c = mpz_cmp(*a, *b);
  69. if (c<=0) {
  70. mpz_set(*r, one);
  71. } else {
  72. mpz_set(*r, zero);
  73. }
  74. }
  75. void ZqField::geq(PBigInt r, PBigInt a, PBigInt b) {
  76. int c = mpz_cmp(*a, *b);
  77. if (c>=0) {
  78. mpz_set(*r, one);
  79. } else {
  80. mpz_set(*r, zero);
  81. }
  82. }
  83. void ZqField::neq(PBigInt r, PBigInt a, PBigInt b) {
  84. int c = mpz_cmp(*a, *b);
  85. if (c!=0) {
  86. mpz_set(*r, one);
  87. } else {
  88. mpz_set(*r, zero);
  89. }
  90. }
  91. int ZqField::isTrue(PBigInt a) {
  92. return mpz_sgn(*a);
  93. }
  94. void ZqField::copyn(PBigInt a, PBigInt b, int n) {
  95. for (int i=0;i<n; i++) mpz_set(a[i], b[i]);
  96. }