You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@milagro.apache.org by sa...@apache.org on 2020/02/13 11:00:40 UTC

[incubator-milagro-MPC] 01/05: review commitments code

This is an automated email from the ASF dual-hosted git repository.

sandreoli pushed a commit to branch review
in repository https://gitbox.apache.org/repos/asf/incubator-milagro-MPC.git

commit ba6a54d83afb74031fab0219652b677244fe56d6
Author: Samuele Andreoli <sa...@yahoo.it>
AuthorDate: Mon Feb 10 12:30:34 2020 +0000

    review commitments code
---
 benchmark/bench_nm_commit.c       |  2 +-
 examples/example_nm_commit.c      | 12 +++++++-----
 include/amcl/commitments.h        |  3 +++
 src/commitments.c                 | 20 ++++++++++++++++----
 test/smoke/test_nm_commit_smoke.c |  2 +-
 test/unit/test_nm_commit.c        | 10 +++++-----
 6 files changed, 33 insertions(+), 16 deletions(-)

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 @@ int main()
     }
     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 @@ to you under the Apache License, Version 2.0 (the
 "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 @@ int main()
 
     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");
+    }
+    else
+    {
+        printf("\rSuccess\n\n");
     }
-    printf("\tDecommitment successful.\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 @@ extern "C"
 {
 #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 @@ int COMMITMENTS_NM_decommit(octet *X, octet *R, octet *C)
     // 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 @@ void bc_generator(csprng *RNG, BIG_1024_58* x, BIG_1024_58 *p, BIG_1024_58 *P, i
     }
 
     // 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 @@ void COMMITMENTS_BC_setup(csprng *RNG, COMMITMENTS_BC_priv_modulus *m, octet *P,
     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 @@ int main()
     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 @@ int main(int argc, char **argv)
             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 @@ int main(int argc, char **argv)
     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);