diff --git a/include/field.h b/include/field.h index 3e29303929a38ad75e527823cc0ab987f9b7cfaf..2ffd0e0541069ba643fa710e13b6cabdfedb068d 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 c0bb4b5e8f8c9bfc75cd817ea51ab6c3568a3823..5f5aaae617a0baa3d439a70b64d32cfbc93ec039 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 08411cc39ea320b86ab2e33240d8c73e54fa4cf4..d6e4c93e8e88ae9815075cab1b985844b7ff3ee1 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