modexp: Implement simple mp_modexp function.
authorMark Wooding <mdw@distorted.org.uk>
Tue, 16 Jan 2007 22:09:51 +0000 (22:09 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 16 Jan 2007 22:23:02 +0000 (22:23 +0000)
This has been a serious omission for rather too long.

Makefile.m4
mp-modexp.c [new file with mode: 0644]
mp.h
tests/mp

index 6f75ebc..f5d42d2 100644 (file)
@@ -190,7 +190,7 @@ define(`MP_BASE',
 define(`MP_SOURCES',
        `qdparse.c \
        mp-test.c mplimits.c \
 define(`MP_SOURCES',
        `qdparse.c \
        mp-test.c mplimits.c \
-       mp-sqrt.c mp-gcd.c mp-jacobi.c mp-modsqrt.c mp-exp.c \
+       mp-sqrt.c mp-gcd.c mp-jacobi.c mp-modsqrt.c mp-exp.c mp-modexp.c \
        mpint.c mptext-file.c mptext-dstr.c \
        mptext-len.c \
        exp.c mpcrt.c mpmul.c mprand.c \
        mpint.c mptext-file.c mptext-dstr.c \
        mptext-len.c \
        exp.c mpcrt.c mpmul.c mprand.c \
@@ -413,6 +413,7 @@ CTESTRIG(mp-modsqrt)
 CTESTRIG(mp-gcd)
 CTESTRIG(mp-jacobi)
 CTESTRIG(mp-sqrt)
 CTESTRIG(mp-gcd)
 CTESTRIG(mp-jacobi)
 CTESTRIG(mp-sqrt)
+CTESTRIG(mp-modexp)
 CTESTRIG(mptext)
 CTESTRIG(mpint)
 CTESTRIG(mpbarrett)
 CTESTRIG(mptext)
 CTESTRIG(mpint)
 CTESTRIG(mpbarrett)
diff --git a/mp-modexp.c b/mp-modexp.c
new file mode 100644 (file)
index 0000000..de20a4d
--- /dev/null
@@ -0,0 +1,113 @@
+/* -*-c-*-
+ *
+ * $Id$
+ *
+ * General-purpose modular exponentiation
+ *
+ * (c) 2006 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "mp.h"
+#include "mpbarrett.h"
+#include "mpmont.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @mp_modexp@ --- *
+ *
+ * Arguments:  @mp *d@ = fake destination
+ *             @mp *x@ = base of exponentiation
+ *             @mp *e@ = exponent
+ *             @mp *n@ = modulus (must be positive)
+ *
+ * Returns:    The value %$x^e \bmod n$%.
+ */
+
+mp *mp_modexp(mp *d, mp *x, mp *e, mp *n)
+{
+  if (MP_ODDP(n)) {
+    mpmont mm;
+    mpmont_create(&mm, n);
+    d = mpmont_exp(&mm, d, x, e);
+    mpmont_destroy(&mm);
+  } else {
+    mpbarrett mb;
+    mpbarrett_create(&mb, n);
+    d = mpbarrett_exp(&mb, d, x, e);
+    mpbarrett_destroy(&mb);
+  }
+  return (d);
+}
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+static int tmodexp(dstr *v)
+{
+  mp *a = *(mp **)v[0].buf;
+  mp *b = *(mp **)v[1].buf;
+  mp *m = *(mp **)v[2].buf;
+  mp *r = *(mp **)v[3].buf;
+  mp *mr;
+  int ok = 1;
+
+  mr = mp_modexp(MP_NEW, a, b, m);
+
+  if (!MP_EQ(mr, r)) {
+    fputs("\n*** modexp failed", stderr);
+    fputs("\n a = ", stderr); mp_writefile(a, stderr, 10);
+    fputs("\n e = ", stderr); mp_writefile(b, stderr, 10);
+    fputs("\n m = ", stderr); mp_writefile(m, stderr, 10);
+    fputs("\n r = ", stderr); mp_writefile(r, stderr, 10);
+    fputs("\nmr = ", stderr); mp_writefile(mr, stderr, 10);
+    fputc('\n', stderr);
+    ok = 0;
+  }
+
+  MP_DROP(m);
+  MP_DROP(a);
+  MP_DROP(b);
+  MP_DROP(r);
+  MP_DROP(mr);
+  assert(mparena_count(MPARENA_GLOBAL) == 0);
+  return ok;
+}
+
+static test_chunk tests[] = {
+  { "modexp", tmodexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
+  { 0, 0, { 0 } }
+};
+
+int main(int argc, char *argv[])
+{
+  sub_init();
+  test_run(argc, argv, tests, SRCDIR "/tests/mp");
+  return (0);
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/mp.h b/mp.h
index 74b6473..7f96746 100644 (file)
--- a/mp.h
+++ b/mp.h
@@ -975,6 +975,18 @@ extern int mp_jacobi(mp */*a*/, mp */*n*/);
 
 extern mp *mp_modsqrt(mp */*d*/, mp */*a*/, mp */*p*/);
 
 
 extern mp *mp_modsqrt(mp */*d*/, mp */*a*/, mp */*p*/);
 
+/* --- @mp_modexp@ --- *
+ *
+ * Arguments:  @mp *d@ = fake destination
+ *             @mp *x@ = base of exponentiation
+ *             @mp *e@ = exponent
+ *             @mp *n@ = modulus (must be positive)
+ *
+ * Returns:    The value %$x^e \bmod n$%.
+ */
+
+extern mp *mp_modexp(mp */*d*/, mp */*x*/, mp */*e*/, mp */*n*/);
+
 /*----- Test harness support ----------------------------------------------*/
 
 #include <mLib/testrig.h>
 /*----- Test harness support ----------------------------------------------*/
 
 #include <mLib/testrig.h>
index ab2e775..a7c5d50 100644 (file)
--- a/tests/mp
+++ b/tests/mp
@@ -223,6 +223,23 @@ modsqrt {
   660864223630638896 1729533840094059799 671335997718840076;
 }
 
   660864223630638896 1729533840094059799 671335997718840076;
 }
 
+modexp {
+
+  # --- Montgomery exponentiation ---
+
+  435365332435654643667 8745435676786567758678547 
+    4325987397987458979875737589783
+    2439674515119108242643169132064;
+  0xfffffffdfffffffffffffffffffffffe 0 0xfffffffdffffffffffffffffffffffff 1;
+  1804289383 -8939035539979879765 8939489893434234331 6139425926295484741;
+
+  # --- Barrett exponentiation ---
+
+  435365332435654643667 8745435676786567758678547
+    4325987397987458979875737589782
+    2425191520487853884024972777945;
+}
+
 factorial {
   0 1;
   1 1;
 factorial {
   0 1;
   1 1;