Further comment changes for reformat
[openssl.git] / crypto / bn / bn_asm.c
index 85b203d25dc65b56d9e55c0133fc743ecaaa3326..a33b63411b7ffe95fbb921ddd6cc374d61c14535 100644 (file)
@@ -68,7 +68,7 @@
 
 #if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
 
-BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w)
+BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
        {
        BN_ULONG c1=0;
 
@@ -94,7 +94,7 @@ BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG
        return(c1);
        } 
 
-BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w)
+BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
        {
        BN_ULONG c1=0;
 
@@ -119,7 +119,7 @@ BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w)
        return(c1);
        } 
 
-void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, size_t n)
+void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
         {
        assert(n >= 0);
        if (n <= 0) return;
@@ -143,7 +143,7 @@ void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, size_t n)
 
 #else /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
 
-BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w)
+BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
        {
        BN_ULONG c=0;
        BN_ULONG bl,bh;
@@ -172,7 +172,7 @@ BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG
        return(c);
        } 
 
-BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w)
+BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
        {
        BN_ULONG carry=0;
        BN_ULONG bl,bh;
@@ -201,7 +201,7 @@ BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, size_t num, BN_ULONG w)
        return(carry);
        } 
 
-void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, size_t n)
+void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
         {
        assert(n >= 0);
        if (n <= 0) return;
@@ -303,7 +303,7 @@ BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
 #endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
 
 #ifdef BN_LLONG
-BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t n)
+BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
         {
        BN_ULLONG ll=0;
 
@@ -338,7 +338,7 @@ BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t
        return((BN_ULONG)ll);
        }
 #else /* !BN_LLONG */
-BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t n)
+BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
         {
        BN_ULONG c,l,t;
 
@@ -390,7 +390,7 @@ BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t
        }
 #endif /* !BN_LLONG */
 
-BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t n)
+BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
         {
        BN_ULONG t1,t2;
        int c=0;
@@ -438,6 +438,10 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t
 /* sqr_add_c(a,i,c0,c1,c2)  -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
 /* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */
 
+/*
+ * Keep in mind that carrying into high part of multiplication result
+ * can not overflow, because it cannot be all-ones.
+ */
 #ifdef BN_LLONG
 #define mul_add_c(a,b,c0,c1,c2) \
        t=(BN_ULLONG)a*b; \
@@ -478,10 +482,10 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t
 #define mul_add_c2(a,b,c0,c1,c2) {     \
        BN_ULONG ta=(a),tb=(b),t0;      \
        BN_UMULT_LOHI(t0,t1,ta,tb);     \
-       t2 = t1+t1; c2 += (t2<t1)?1:0;  \
-       t1 = t0+t0; t2 += (t1<t0)?1:0;  \
-       c0 += t1; t2 += (c0<t1)?1:0;    \
+       c0 += t0; t2 = t1+((c0<t0)?1:0);\
        c1 += t2; c2 += (c1<t2)?1:0;    \
+       c0 += t0; t1 += (c0<t0)?1:0;    \
+       c1 += t1; c2 += (c1<t1)?1:0;    \
        }
 
 #define sqr_add_c(a,i,c0,c1,c2)        {       \
@@ -508,10 +512,10 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, size_t
        BN_ULONG ta=(a),tb=(b),t0;      \
        t1 = BN_UMULT_HIGH(ta,tb);      \
        t0 = ta * tb;                   \
-       t2 = t1+t1; c2 += (t2<t1)?1:0;  \
-       t1 = t0+t0; t2 += (t1<t0)?1:0;  \
-       c0 += t1; t2 += (c0<t1)?1:0;    \
+       c0 += t0; t2 = t1+((c0<t0)?1:0);\
        c1 += t2; c2 += (c1<t2)?1:0;    \
+       c0 += t0; t1 += (c0<t0)?1:0;    \
+       c1 += t1; c2 += (c1<t1)?1:0;    \
        }
 
 #define sqr_add_c(a,i,c0,c1,c2)        {       \
@@ -860,13 +864,35 @@ int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_U
 
        n0 = *n0p;
 
-       tp[num]   = bn_mul_words(tp,ap,num,bp[0]);
+       c0 = 0;
+       ml = bp[0];
+#ifdef mul64
+       mh = HBITS(ml);
+       ml = LBITS(ml);
+       for (j=0;j<num;++j)
+               mul(tp[j],ap[j],ml,mh,c0);
+#else
+       for (j=0;j<num;++j)
+               mul(tp[j],ap[j],ml,c0);
+#endif
+
+       tp[num]   = c0;
        tp[num+1] = 0;
        goto enter;
 
        for(i=0;i<num;i++)
                {
-               c0 = bn_mul_add_words(tp,ap,num,bp[i]);
+               c0 = 0;
+               ml = bp[i];
+#ifdef mul64
+               mh = HBITS(ml);
+               ml = LBITS(ml);
+               for (j=0;j<num;++j)
+                       mul_add(tp[j],ap[j],ml,mh,c0);
+#else
+               for (j=0;j<num;++j)
+                       mul_add(tp[j],ap[j],ml,c0);
+#endif
                c1 = (tp[num] + c0)&BN_MASK2;
                tp[num]   = c1;
                tp[num+1] = (c1<c0?1:0);