summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--gmp-h.in4
-rw-r--r--mpn/generic/sec_div.c55
-rw-r--r--tests/mpn/t-div.c4
4 files changed, 43 insertions, 27 deletions
diff --git a/ChangeLog b/ChangeLog
index f2c48655d..1f428949d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2014-01-01 Torbjorn Granlund <tege@gmplib.org>
+
+ * mpn/generic/sec_div.c: Rewrite to make mpn_sec_div_qr return high
+ quotient limb.
+ * gmp-h.in (mpn_sec_div_qr): Update declaration.
+ * tests/mpn/t-div.c: Adapt.
+
2013-12-31 Niels Möller <nisse@lysator.liu.se>
* doc/gmp.texi (Low-level Functions for cryptography): Document
diff --git a/gmp-h.in b/gmp-h.in
index 5fb004437..670eeb44f 100644
--- a/gmp-h.in
+++ b/gmp-h.in
@@ -1,6 +1,6 @@
/* Definitions for GNU multiple precision functions. -*- mode: c -*-
-Copyright 1991, 1993-1997, 1999-2013 Free Software Foundation, Inc.
+Copyright 1991, 1993-1997, 1999-2014 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
@@ -1645,7 +1645,7 @@ __GMP_DECLSPEC mp_size_t mpn_sec_powm_itch (mp_size_t, mp_size_t, mp_size_t);
__GMP_DECLSPEC void mpn_sec_tabselect (volatile mp_limb_t *, volatile mp_limb_t *, mp_size_t, mp_size_t, mp_size_t);
#define mpn_sec_div_qr __MPN(sec_div_qr)
-__GMP_DECLSPEC void mpn_sec_div_qr (mp_ptr, mp_ptr, mp_size_t, mp_srcptr, mp_size_t, mp_ptr);
+__GMP_DECLSPEC mp_limb_t mpn_sec_div_qr (mp_ptr, mp_ptr, mp_size_t, mp_srcptr, mp_size_t, mp_ptr);
#define mpn_sec_div_qr_itch __MPN(sec_div_qr_itch)
__GMP_DECLSPEC mp_size_t mpn_sec_div_qr_itch (mp_size_t, mp_size_t);
#define mpn_sec_div_r __MPN(sec_div_r)
diff --git a/mpn/generic/sec_div.c b/mpn/generic/sec_div.c
index 0d84f4e45..ccbe233de 100644
--- a/mpn/generic/sec_div.c
+++ b/mpn/generic/sec_div.c
@@ -1,14 +1,10 @@
-/* mpn_sec_div_qr, mpn_sec_div_r -- Compute Q = floor(U / V), U = U mod
- V. Side-channel silent under the assumption that the used instructions are
+/* mpn_sec_div_qr, mpn_sec_div_r -- Compute Q = floor(U / V), U = U mod V.
+ Side-channel silent under the assumption that the used instructions are
side-channel silent.
Contributed to the GNU project by Torbjörn Granlund.
- THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
- SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
- GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
-
-Copyright 2011-2013 Free Software Foundation, Inc.
+Copyright 2011-2014 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
@@ -33,11 +29,13 @@ with the GNU MP Library. If not, see https://www.gnu.org/licenses/. */
#define FNAME mpn_sec_div_qr
#define FNAME_itch mpn_sec_div_qr_itch
#define Q(q) q,
+#define RETTYPE mp_limb_t
#endif
#if OPERATION_sec_div_r
#define FNAME mpn_sec_div_r
#define FNAME_itch mpn_sec_div_r_itch
#define Q(q)
+#define RETTYPE void
#endif
mp_size_t
@@ -56,7 +54,7 @@ FNAME_itch (mp_size_t nn, mp_size_t dn)
#endif
}
-void
+RETTYPE
FNAME (Q(mp_ptr qp)
mp_ptr np, mp_size_t nn,
mp_srcptr dp, mp_size_t dn,
@@ -84,6 +82,23 @@ FNAME (Q(mp_ptr qp)
np2 = tp + dn; /* (nn + 1) limbs */
cy = mpn_lshift (np2, np, nn, cnt);
np2[nn++] = cy;
+
+ d0 = dp2[dn - 1];
+ d0 += (~d0 != 0);
+ invert_limb (inv32, d0);
+
+ /* We add nn + dn to tp here, not nn + 1 + dn, as expected. This is
+ since nn here will have been incremented. */
+#if OPERATION_sec_div_qr
+ qh = mpn_sec_pi1_div_qr (np2 + dn, np2, nn, dp2, dn, inv32, tp + nn + dn);
+ ASSERT (qh == 0); /* FIXME: this indicates inefficiency! */
+ MPN_COPY (qp, np2 + dn, nn - dn - 1);
+ qh = np2[nn - 1];
+#else
+ mpn_sec_pi1_div_r (np2, nn, dp2, dn, inv32, tp + nn + dn);
+#endif
+
+ mpn_rshift (np, np2, dn, cnt);
}
else
{
@@ -92,27 +107,21 @@ FNAME (Q(mp_ptr qp)
assume nn > dn. */
dp2 = (mp_ptr) dp;
np2 = np;
- }
- d0 = dp2[dn - 1];
- d0 += (~d0 != 0);
- invert_limb (inv32, d0);
+ d0 = dp2[dn - 1];
+ d0 += (~d0 != 0);
+ invert_limb (inv32, d0);
- /* We add nn + dn to tp here, not nn + 1 + dn, as expected. This is since nn
- here will have been incremented. */
+ /* We add nn + dn to tp here, not nn + 1 + dn, as expected. This is
+ since nn here will have been incremented. */
#if OPERATION_sec_div_qr
- qh = mpn_sec_pi1_div_qr (qp, np2, nn, dp2, dn, inv32, tp + nn + dn);
+ qh = mpn_sec_pi1_div_qr (qp, np2, nn, dp2, dn, inv32, tp + nn + dn);
#else
- mpn_sec_pi1_div_r (np2, nn, dp2, dn, inv32, tp + nn + dn);
+ mpn_sec_pi1_div_r (np2, nn, dp2, dn, inv32, tp + nn + dn);
#endif
-
- if (cnt == 0)
- ; /* we have np = np2 here. */
- else
- mpn_rshift (np, np2, dn, cnt);
+ }
#if OPERATION_sec_div_qr
- if (cnt == 0)
- qp[nn - dn] = qh;
+ return qh;
#endif
}
diff --git a/tests/mpn/t-div.c b/tests/mpn/t-div.c
index db50aa75c..477f902b4 100644
--- a/tests/mpn/t-div.c
+++ b/tests/mpn/t-div.c
@@ -1,4 +1,4 @@
-/* Copyright 2006, 2007, 2009, 2010, 2013 Free Software Foundation, Inc.
+/* Copyright 2006, 2007, 2009, 2010, 2013, 2014 Free Software Foundation, Inc.
This file is part of the GNU MP Library test suite.
@@ -306,7 +306,7 @@ main (int argc, char **argv)
MPN_COPY (rp, np, nn);
if (nn >= dn)
MPN_COPY (qp, junkp, nn - dn + 1);
- mpn_sec_div_qr (qp, rp, nn, dup, dn, scratch);
+ qp[nn - dn] = mpn_sec_div_qr (qp, rp, nn, dup, dn, scratch);
ASSERT_ALWAYS (ran == scratch[itch]);
check_one (qp, rp, np, nn, dup, dn, "mpn_sec_div_qr", 0);