From 07fafae903a45e6527511793895724b4ba858f9b Mon Sep 17 00:00:00 2001
From: Weiqi <weltch1997@gmail.com>
Date: Fri, 24 Feb 2023 19:18:11 -0500
Subject: [PATCH] update field

---
 include/field.h    |  2 ++
 src/field.c        |  6 +++++-
 tests/test_field.c | 29 +++++++++++++++++++----------
 3 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/include/field.h b/include/field.h
index 3e29303..2ffd0e0 100644
--- a/include/field.h
+++ b/include/field.h
@@ -17,6 +17,8 @@ void zp_from_int(zp x, int x_int);
 
 void zp_add(zp r, zp x, zp y);
 
+void zp_neg(zp nx, zp x);
+
 void zp_multiply(zp p, zp x, zp y);
 
 void zp_inverse(zp xi, zp x);
diff --git a/src/field.c b/src/field.c
index c0bb4b5..5f5aaae 100644
--- a/src/field.c
+++ b/src/field.c
@@ -24,6 +24,10 @@ void zp_add(dig_t *r, dig_t *x, dig_t *y) {
     fp_add(r, x, y);
 }
 
+void zp_neg(dig_t *nx, dig_t *x) {
+    fp_neg(nx, x);
+}
+
 void zp_multiply(dig_t *p, dig_t *x, dig_t *y) {
     fp_mul(p, x, y);
 }
@@ -34,4 +38,4 @@ void zp_inverse(dig_t *xi, dig_t *x) {
 
 int zp_is_int(dig_t *x, int x_int) {
     return fp_cmp_dig(x, x_int) == RLC_EQ;
-}
+}
\ No newline at end of file
diff --git a/tests/test_field.c b/tests/test_field.c
index 08411cc..d6e4c93 100644
--- a/tests/test_field.c
+++ b/tests/test_field.c
@@ -16,13 +16,13 @@ int test_zp_copy() {
     zp x, y;
     zp_from_int(x, 3);
     zp_copy(y, x);
-    return fp_cmp_dig(y, 3);
+    return zp_is_int(y, 3);
 }
 
 int test_zp_from_int() {
     zp x;
     zp_from_int(x, 3);
-    return fp_cmp_dig(x, 3);
+    return zp_is_int(x, 3);
 }
 
 int test_zp_add() {
@@ -30,7 +30,15 @@ int test_zp_add() {
     zp_from_int(x, 10);
     zp_from_int(y, 20);
     zp_add(r, x, y);
-    return fp_cmp_dig(r, 30);
+    return zp_is_int(r, 30);
+}
+
+int test_zp_neg() {
+    zp x, y, r;
+    rand_zp(x);
+    zp_neg(y, x);
+    zp_add(r, x, y);
+    return zp_is_int(r, 0);
 }
 
 int test_zp_multiply() {
@@ -38,7 +46,7 @@ int test_zp_multiply() {
     zp_from_int(x, 10);
     zp_from_int(y, 20);
     zp_multiply(r, x, y);
-    return fp_cmp_dig(r, 200);
+    return zp_is_int(r, 200);
 }
 
 int test_zp_inverse() {
@@ -46,7 +54,7 @@ int test_zp_inverse() {
     rand_zp(x);
     zp_inverse(xi, x);
     zp_multiply(r, x, xi);
-    return fp_cmp_dig(r, 1);
+    return zp_is_int(r, 1);
 }
 
 
@@ -58,11 +66,12 @@ int main() {
     // Perform tests.
     if (test_zp_zero() != 1) return 1;
     if (test_zp_one() != 1) return 1;
-    if (test_zp_copy() != RLC_EQ) return 1;
-    if (test_zp_from_int() != RLC_EQ) return 1;
-    if (test_zp_add() != RLC_EQ) return 1;
-    if (test_zp_multiply() != RLC_EQ) return 1;
-    if (test_zp_inverse() != RLC_EQ) return 1;
+    if (test_zp_copy() != 1) return 1;
+    if (test_zp_from_int() != 1) return 1;
+    if (test_zp_add() != 1) return 1;
+    if (test_zp_neg() != 1) return 1;
+    if (test_zp_multiply() != 1) return 1;
+    if (test_zp_inverse() != 1) return 1;
 
     return 0;
 }
\ No newline at end of file
-- 
GitLab