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