Pollard's rho algorithm for computing discrete logs.
[u/mdw/catacomb] / share.c
diff --git a/share.c b/share.c
index d427706..0d61a78 100644 (file)
--- a/share.c
+++ b/share.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: share.c,v 1.2 2000/06/18 23:05:19 mdw Exp $
+ * $Id: share.c,v 1.3 2000/06/24 18:29:05 mdw Exp $
  *
  * Shamir's secret sharing
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: share.c,v $
+ * Revision 1.3  2000/06/24 18:29:05  mdw
+ * Interface change: allow shares to be extracted from a context on demand,
+ * rather than building them all up-front.
+ *
  * Revision 1.2  2000/06/18 23:05:19  mdw
  * Minor performance tweak: use Barrett reduction rather than Montgomery.
  * Fast secret sharing isn't done here, though: see `gfshare' instead.
 /* --- @share_create@ --- *
  *
  * Arguments:  @share *s@ = pointer to share context to initialize
- *             @unsigned t, n@ = threshold parameters for the system
+ *             @unsigned t@ = threshold for the system
  *
  * Returns:    ---
  *
  * Use:                Initializes a sharing context.
  */
 
-void share_create(share *s, unsigned t, unsigned n)
+void share_create(share *s, unsigned t)
 {
   s->t = t;
-  s->n = n;
   s->i = 0;
   s->s = 0;
   s->p = 0;
@@ -88,19 +91,12 @@ void share_create(share *s, unsigned t, unsigned n)
 
 void share_destroy(share *s)
 {
-  unsigned n;
   unsigned i;
 
   /* --- Dispose of the share vector --- */
 
   if (s->v) {
-    if (s->i)
-      n = s->i;
-    else if (s->n)
-      n = s->n;
-    else
-      n = s->t;
-    for (i = 0; i < n; i++) {
+    for (i = 0; i < s->t; i++) {
       if (s->v[i].y)
        mp_drop(s->v[i].y);
     }
@@ -122,12 +118,10 @@ void share_destroy(share *s)
  *
  * Returns:    ---
  *
- * Use:                Generates @c->n@ secret shares, such that any @c->t@ of them
- *             may be used to recover the secret.
- *
+ * Use:                Initializes a sharing context to be able to create shares.
  *             The context structure is expected to be mostly filled in.  In
- *             particular, @t@, @n@ and @s@ must be initialized.  If @p@ is
- *             zero, a prime number of appropriate size is generated
+ *             particular, @t@ and @s@ must be initialized.  If @p@ is zero,
+ *             a prime number of appropriate size is generated
  *             automatically.  If @v@ is zero, a vector of appropriate size
  *             is allocated.  You should use the macro @SHARE_INIT@ or
  *             @share_create@ to construct sharing contexts.
@@ -135,11 +129,7 @@ void share_destroy(share *s)
 
 void share_mkshares(share *s, grand *r)
 {
-  mp **v;
   unsigned i;
-  mp u;
-  mpw uw;
-  mpbarrett mb;
 
   /* --- If there's no prime, construct one --- */
 
@@ -155,46 +145,53 @@ void share_mkshares(share *s, grand *r)
                rabin_iters(bits), pgen_test, &pr);
   }
 
-  /* --- Construct the coefficients --- */
+  /* --- Construct the polynomial --- */
 
-  mpbarrett_create(&mb, s->p);
-  v = xmalloc(s->t * sizeof(mp *));
+  if (!s->v)
+    s->v = xmalloc(s->t * sizeof(share_pt));
   for (i = 0; i < s->t - 1; i++)
-    v[i] = mprand_range(MP_NEW, s->p, r, 0);
-  v[s->t - 1] = s->s;
-
-  /* --- Construct the shares --- */
+    s->v[i].y = mprand_range(MP_NEWSEC, s->p, r, 0);
+  s->v[s->t - 1].y = mp_copy(s->s);
+}
 
-  if (!s->v)
-    s->v = xmalloc(s->n * sizeof(share_pt));
+/* --- @share_get@ --- *
+ *
+ * Arguments:  @share *s@ = pointer to share conext
+ *             @mp *d@ = destination for the share
+ *             @unsigned x@ = share index to fetch
+ *
+ * Returns:    The share, as requested.
+ *
+ * Use:                Extracts a share from the system.  You may extract @MPW_MAX@
+ *             shares, or @s->p@ shares from the system, whichever is
+ *             smaller.  Shares are indexed from 0.
+ */
 
-  mp_build(&u, &uw, &uw + 1);
-  for (uw = 1; uw <= s->n; uw++) {
-    mp *m = MP_ZERO;
-    unsigned j;
+mp *share_get(share *s, mp *d, unsigned x)
+{
+  mpbarrett mb;
+  mpw uw = x + 1;
+  mp u;
+  unsigned i;
 
-    /* --- Evaluate the polynomial at %$x = i + 1$% --- */
+  /* --- Various bits of initialization --- */
 
-    for (j = 0; j < s->t; j++) {
-      m = mp_mul(m, m, &u);
-      m = mpbarrett_reduce(&mb, m, m);
-      m = mp_add(m, m, v[j]);
-      if (MP_CMP(m, >=, s->p))
-       m = mp_sub(m, m, s->p);
-    }
+  mp_build(&u, &uw, &uw + 1);
+  if (d)
+    mp_drop(d);
 
-    /* --- Reduce the final result --- */
+  /* --- Evaluate the polynomial at %$x = i + 1$% --- */
 
-    s->v[uw - 1].x = uw - 1;
-    s->v[uw - 1].y = m;
+  d = MP_ZERO;
+  mpbarrett_create(&mb, s->p);
+  for (i = 0; i < s->t; i++) {
+    d = mp_mul(d, d, &u);
+    d = mp_add(d, d, s->v[i].y);
+    d = mpbarrett_reduce(&mb, d, d);
   }
   mpbarrett_destroy(&mb);
 
-  /* --- Dispose of various bits of old rubbish --- */
-
-  for (i = 0; i < s->t - 1; i++)
-    mp_drop(v[i]);
-  xfree(v);
+  return (d);
 }
 
 /* --- @share_add@ --- *
@@ -214,15 +211,18 @@ unsigned share_add(share *s, unsigned x, mp *y)
   /* --- If no vector has been allocated, create one --- */
 
   if (!s->v) {
+    unsigned i;
     s->v = xmalloc(s->t * sizeof(share_pt));
     s->i = 0;
+    for (i = 0; i < s->t; i++)
+      s->v[i].y = 0;
   }
 
   assert(((void)"Share context is full", s->i < s->t));
 
   /* --- Store the share in the vector --- */
 
-  s->v[s->i].x = x;
+  s->v[s->i].x = x + 1;
   s->v[s->i].y = mp_copy(y);
   s->i++;
 
@@ -264,11 +264,11 @@ mp *share_combine(share *s)
   for (i = 0; i < s->t; i++) {
     mp *c = MP_ONE;
 
-    iiw = s->v[i].x + 1;
+    iiw = s->v[i].x;
     for (j = 0; j < s->t; j++) {
       if (i == j)
        continue;
-      jjw = s->v[j].x + 1;
+      jjw = s->v[j].x;
       if (s->v[j].x >= s->v[i].x)
        m = mp_sub(m, &jj, &ii);
       else {
@@ -307,7 +307,7 @@ static int verify(grand *r)
   unsigned t = r->ops->range(r, n - 1) + 1;
   unsigned len = r->ops->range(r, 160);
 
-  share_pt *v = xmalloc(t * sizeof(share_pt));
+  mp **v = xmalloc(t * sizeof(mp *));
   unsigned *p = xmalloc(n * sizeof(unsigned));
   mp *sec = mprand(MP_NEW, len, r, 0);
   share s;
@@ -326,23 +326,19 @@ static int verify(grand *r)
     p[i + j] = x;
   }
 
-  share_create(&s, t, n);
+  share_create(&s, t);
   s.s = mp_copy(sec);
   share_mkshares(&s, r);
-  for (i = 0; i < t; i++) {
-    v[i].x = s.v[p[i]].x;
-    v[i].y = mp_copy(s.v[p[i]].y);
-  }
+  for (i = 0; i < t; i++)
+    v[i] = share_get(&s, MP_NEW, p[i]);
   pp = mp_copy(s.p);
   share_destroy(&s);
+  assert(mparena_count(MPARENA_GLOBAL) + mparena_count(MPARENA_SECURE) == t + 2);
 
-  assert(mparena_count(MPARENA_GLOBAL) == t + 2);
-
-  share_create(&s, t, n);
+  share_create(&s, t);
   s.p = pp;
-  for (i = 0; i < t; i++) {
-    share_add(&s, v[i].x, v[i].y);
-  }
+  for (i = 0; i < t; i++)
+    share_add(&s, p[i], v[i]);
   ss = share_combine(&s);
   share_destroy(&s);
 
@@ -355,12 +351,12 @@ static int verify(grand *r)
   mp_drop(ss);
 
   for (i = 0; i < t; i++)
-    mp_drop(v[i].y);
+    mp_drop(v[i]);
 
   xfree(v);
   xfree(p);
 
-  assert(mparena_count(MPARENA_GLOBAL) == 0);
+  assert(mparena_count(MPARENA_GLOBAL) + mparena_count(MPARENA_SECURE) == 0);
   return (ok);
 }