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

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

commit a4570c3942b69be83f1e00670f7c739cb65d3501
Author: Samuele Andreoli <[email protected]>
AuthorDate: Tue Apr 7 22:37:32 2020 +0100

    Add isunity to ff
---
 include/ff.h.in                   |  7 +++++++
 src/ff.c.in                       | 26 ++++++++++++++++++++------
 test/test_ff_consistency_WWW.c.in | 10 +++++++++-
 3 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/include/ff.h.in b/include/ff.h.in
index c6e4f6b..1f2630f 100644
--- a/include/ff.h.in
+++ b/include/ff.h.in
@@ -66,6 +66,13 @@ extern void FF_WWW_zero(BIG_XXX *x,int n);
        @return 1 if zero, else returns 0
  */
 extern int FF_WWW_iszilch(BIG_XXX *x,int n);
+/**    @brief Tests for FF element equal to one
+ *
+       @param x FF number to be tested
+       @param n size of FF in BIGs
+       @return 1 if unity, else returns 0
+ */
+extern int FF_WWW_isunity(BIG_XXX *x,int n);
 /**    @brief  return parity of an FF, that is the least significant bit
  *
        @param x FF number
diff --git a/src/ff.c.in b/src/ff.c.in
index d6a678c..9828305 100644
--- a/src/ff.c.in
+++ b/src/ff.c.in
@@ -79,6 +79,20 @@ int FF_WWW_iszilch(BIG_XXX x[],int n)
     return rc;
 }
 
+/* test equals 1 */
+int FF_WWW_isunity(BIG_XXX x[], int n)
+{
+    int i;
+    int rc;
+
+    rc = BIG_XXX_isunity(x[0]);
+
+    for (i=1; i<n; i++)
+        rc &= BIG_XXX_iszilch(x[i]);
+
+    return rc;
+}
+
 /* shift right by BIGBITS-bit words */
 static void FF_WWW_shrw(BIG_XXX a[],int n)
 {
@@ -1146,9 +1160,9 @@ int FF_WWW_prime(BIG_XXX p[],csprng *rng,int n)
 {
     int i,j,loop,s=0;
 #ifndef C99
-    BIG_XXX d[FFLEN_WWW],x[FFLEN_WWW],unity[FFLEN_WWW],nm1[FFLEN_WWW];
+    BIG_XXX d[FFLEN_WWW],x[FFLEN_WWW],nm1[FFLEN_WWW];
 #else
-    BIG_XXX d[n],x[n],unity[n],nm1[n];
+    BIG_XXX d[n],x[n],nm1[n];
 #endif
     sign32 sf=4849845;/* 3*5*.. *19 */
 
@@ -1156,8 +1170,8 @@ int FF_WWW_prime(BIG_XXX p[],csprng *rng,int n)
 
     if (FF_WWW_cfactor(p,sf,n)) return 0;
 
-    FF_WWW_one(unity,n);
-    FF_WWW_sub(nm1,p,unity,n);
+    FF_WWW_copy(nm1,p,n);
+    FF_WWW_dec(nm1,1,n);
     FF_WWW_norm(nm1,n);
     FF_WWW_copy(d,nm1,n);
     while (FF_WWW_parity(d)==0)
@@ -1171,12 +1185,12 @@ int FF_WWW_prime(BIG_XXX p[],csprng *rng,int n)
     {
         FF_WWW_randomnum(x,p,rng,n);
         FF_WWW_pow(x,x,d,p,n,n);
-        if (FF_WWW_comp(x,unity,n)==0 || FF_WWW_comp(x,nm1,n)==0) continue;
+        if (FF_WWW_isunity(x,n) || FF_WWW_comp(x,nm1,n)==0) continue;
         loop=0;
         for (j=1; j<s; j++)
         {
             FF_WWW_power(x,x,2,p,n);
-            if (FF_WWW_comp(x,unity,n)==0) return 0;
+            if (FF_WWW_isunity(x,n)) return 0;
             if (FF_WWW_comp(x,nm1,n)==0 )
             {
                 loop=1;
diff --git a/test/test_ff_consistency_WWW.c.in 
b/test/test_ff_consistency_WWW.c.in
index f394f9b..0be68c4 100644
--- a/test/test_ff_consistency_WWW.c.in
+++ b/test/test_ff_consistency_WWW.c.in
@@ -73,6 +73,14 @@ int main()
         exit(EXIT_FAILURE);
     }
 
+    /* Testing isunity */
+    FF_WWW_one(A, HFLEN_WWW);
+    if(!FF_WWW_isunity(A, HFLEN_WWW))
+    {
+        printf("ERROR comparing or setting to unity\n");
+        exit(EXIT_FAILURE);
+    }
+
     /* Test parity */
     FF_WWW_one(A,HFLEN_WWW);
     if (!FF_WWW_parity(A) || FF_WWW_parity(B))
@@ -120,7 +128,7 @@ int main()
 
     FF_WWW_add(C,A,B,HFLEN_WWW);
     FF_WWW_add(B,B,B,HFLEN_WWW);
-    if(FF_WWW_comp(A,C,HFLEN_WWW) || !FF_WWW_iszilch(B,HFLEN_WWW))
+    if(!FF_WWW_isunity(A,HFLEN_WWW) || !FF_WWW_iszilch(B,HFLEN_WWW))
     {
         printf("ERROR testing addition/subtraction\n");
         exit(EXIT_FAILURE);

Reply via email to