review commitments code
diff --git a/benchmark/bench_nm_commit.c b/benchmark/bench_nm_commit.c
index 9fa2173..82f7e8c 100644
--- a/benchmark/bench_nm_commit.c
+++ b/benchmark/bench_nm_commit.c
@@ -80,7 +80,7 @@
}
while (elapsed < MIN_TIME || iterations < MIN_ITERS);
- if (!rc)
+ if (rc != COMMITMENTS_OK)
{
printf("FAILURE COMMITMENTS_NM_decommit: %d\n", rc);
exit(EXIT_FAILURE);
diff --git a/examples/example_nm_commit.c b/examples/example_nm_commit.c
index 11c2e8f..6466302 100644
--- a/examples/example_nm_commit.c
+++ b/examples/example_nm_commit.c
@@ -7,7 +7,7 @@
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
+ http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
@@ -58,10 +58,12 @@
printf("\nTransmit R, X to decommit the value.\n");
rc = COMMITMENTS_NM_decommit(&X, &R, &C);
- if (!rc)
+ if (rc != COMMITMENTS_OK)
{
- fprintf(stderr, "FAILURE COMMITMENTS_NM_decommit: %d\n", rc);
- exit(EXIT_FAILURE);
+ printf("\tFailure\n\n");
}
- printf("\tDecommitment successful.\n\n");
+ else
+ {
+ printf("\rSuccess\n\n");
+ }
}
diff --git a/include/amcl/commitments.h b/include/amcl/commitments.h
index 537dfde..25e7c75 100644
--- a/include/amcl/commitments.h
+++ b/include/amcl/commitments.h
@@ -34,6 +34,9 @@
{
#endif
+#define COMMITMENTS_OK 0 /** < Success */
+#define COMMITMENTS_FAIL 81 /** < Invalid Commitment */
+
/* NM Commitment Scheme API */
/*! \brief Generate a commitment for the value X
diff --git a/src/commitments.c b/src/commitments.c
index a534d9b..f9ba973 100644
--- a/src/commitments.c
+++ b/src/commitments.c
@@ -67,13 +67,18 @@
// to make the scheme non malleable
if (R->len != SHA256)
{
- return 0;
+ return COMMITMENTS_FAIL;
}
// Verify the commitment
hash(X, R, &D);
- return OCT_comp(C, &D);
+ if (!OCT_comp(C, &D))
+ {
+ return COMMITMENTS_FAIL;
+ }
+
+ return COMMITMENTS_OK;
}
/* Bit Commitment Setup Definitions */
@@ -175,12 +180,11 @@
}
// If ord(x) = 2p, square it.
- FF_2048_pow(e, x, p, P, n);
+ FF_2048_skpow(e, x, p, P, n, n);
FF_2048_dec(e, 1, n);
if (!FF_2048_iszilch(e, n))
{
FF_2048_power(x, x, 2, P, n);
- FF_2048_mod(x, P, n);
}
}
@@ -271,6 +275,14 @@
FF_2048_skpow(gq, gq, aq, m->Q, HFLEN_2048, HFLEN_2048);
FF_2048_crt(m->b1, gp, gq, m->P, m->Q, HFLEN_2048);
+
+ // Clean memory
+ FF_2048_zero(p, HFLEN_2048);
+ FF_2048_zero(q, HFLEN_2048);
+ FF_2048_zero(gp, HFLEN_2048);
+ FF_2048_zero(gq, HFLEN_2048);
+ FF_2048_zero(ap, HFLEN_2048);
+ FF_2048_zero(aq, HFLEN_2048);
}
void COMMITMENTS_BC_kill_priv_modulus(COMMITMENTS_BC_priv_modulus *m)
diff --git a/test/smoke/test_nm_commit_smoke.c b/test/smoke/test_nm_commit_smoke.c
index 98d052b..548092f 100644
--- a/test/smoke/test_nm_commit_smoke.c
+++ b/test/smoke/test_nm_commit_smoke.c
@@ -45,7 +45,7 @@
COMMITMENTS_NM_commit(&RNG, &X, &R, &C);
rc = COMMITMENTS_NM_decommit(&X, &R, &C);
- if (!rc)
+ if (rc != COMMITMENTS_OK)
{
fprintf(stderr, "FAILURE COMMITMENTS_NM_decommit.\n");
exit(EXIT_FAILURE);
diff --git a/test/unit/test_nm_commit.c b/test/unit/test_nm_commit.c
index c837168..b6ff0d0 100644
--- a/test/unit/test_nm_commit.c
+++ b/test/unit/test_nm_commit.c
@@ -86,7 +86,7 @@
compare_OCT(fp, testNo, "COMMITMENT_NM_commit", &C_GOLDEN, &C);
rc = COMMITMENTS_NM_decommit(&X_GOLDEN, &R_GOLDEN, &C_GOLDEN);
- assert_tv(fp, testNo, "COMMITMENTS_NM_DECOMMIT", rc);
+ assert_tv(fp, testNo, "COMMITMENTS_NM_DECOMMIT", rc == COMMITMENTS_OK);
// Mark that at least one test vector was executed
test_run = 1;
@@ -107,15 +107,15 @@
OCT_copy(&R, &R_GOLDEN);
R.len--;
- rc = !COMMITMENTS_NM_decommit(&X_GOLDEN, &R, &C_GOLDEN);
- assert(NULL, "COMMITMENTS_NM_decommit. Invalid R length", rc);
+ rc = COMMITMENTS_NM_decommit(&X_GOLDEN, &R, &C_GOLDEN);
+ assert(NULL, "COMMITMENTS_NM_decommit. Invalid R length", rc == COMMITMENTS_FAIL);
// Test wrong decommitment
OCT_copy(&R, &R_GOLDEN);
R.val[0]--;
- rc = !COMMITMENTS_NM_decommit(&X_GOLDEN, &R, &C_GOLDEN);
- assert(NULL, "COMMITMENTS_NM_decommit. Invalid R", rc);
+ rc = COMMITMENTS_NM_decommit(&X_GOLDEN, &R, &C_GOLDEN);
+ assert(NULL, "COMMITMENTS_NM_decommit. Invalid R", rc == COMMITMENTS_FAIL);
printf("SUCCESS");
exit(EXIT_SUCCESS);