Add an internal-representation no-op function.
[u/mdw/catacomb] / share.c
diff --git a/share.c b/share.c
index 4f5942e..d0fd0f5 100644 (file)
--- a/share.c
+++ b/share.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: share.c,v 1.4 2000/10/08 12:16:17 mdw Exp $
+ * $Id: share.c,v 1.6 2001/02/03 16:05:41 mdw Exp $
  *
  * Shamir's secret sharing
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: share.c,v $
+ * Revision 1.6  2001/02/03 16:05:41  mdw
+ * Now @mp_drop@ checks its argument is non-NULL before attempting to free
+ * it.  Note that the macro version @MP_DROP@ doesn't do this.
+ *
+ * Revision 1.5  2000/12/06 20:30:10  mdw
+ * Change secret sharing interface: present the secret at share
+ * construction time.
+ *
  * Revision 1.4  2000/10/08 12:16:17  mdw
  * Use @MP_EQ@ instead of @MP_CMP@.
  *
@@ -77,7 +85,6 @@ void share_create(share *s, unsigned t)
 {
   s->t = t;
   s->i = 0;
-  s->s = 0;
   s->p = 0;
   s->v = 0;
 }
@@ -99,38 +106,34 @@ void share_destroy(share *s)
   /* --- Dispose of the share vector --- */
 
   if (s->v) {
-    for (i = 0; i < s->t; i++) {
-      if (s->v[i].y)
-       mp_drop(s->v[i].y);
-    }
+    for (i = 0; i < s->t; i++)
+      mp_drop(s->v[i].y);
     xfree(s->v);
   }
 
   /* --- Other stuff --- */
 
-  if (s->p)
-    mp_drop(s->p);
-  if (s->s)
-    mp_drop(s->s);
+  mp_drop(s->p);
 }
 
 /* --- @share_mkshares@ --- *
  *
  * Arguments:  @share *s@ = pointer to share context to fill in
  *             @grand *r@ = pointer to random number source
+ *             @mp *n@ = the secret to share
  *
  * Returns:    ---
  *
  * Use:                Initializes a sharing context to be able to create shares.
  *             The context structure is expected to be mostly filled in.  In
- *             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.
+ *             particular, @t@ 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.
  */
 
-void share_mkshares(share *s, grand *r)
+void share_mkshares(share *s, grand *r, mp *n)
 {
   unsigned i;
 
@@ -140,7 +143,7 @@ void share_mkshares(share *s, grand *r)
     pgen_filterctx pf;
     rabin pr;
     mp *p;
-    unsigned bits = (mp_octets(s->s) + 1) * 8;
+    unsigned bits = (mp_octets(n) + 1) * 8;
 
     pf.step = 2;
     p = mprand(MP_NEW, bits, r, 1);
@@ -154,7 +157,7 @@ void share_mkshares(share *s, grand *r)
     s->v = xmalloc(s->t * sizeof(share_pt));
   for (i = 0; i < s->t - 1; i++)
     s->v[i].y = mprand_range(MP_NEWSEC, s->p, r, 0);
-  s->v[s->t - 1].y = mp_copy(s->s);
+  s->v[s->t - 1].y = mp_copy(n);
 }
 
 /* --- @share_get@ --- *
@@ -180,8 +183,7 @@ mp *share_get(share *s, mp *d, unsigned x)
   /* --- Various bits of initialization --- */
 
   mp_build(&u, &uw, &uw + 1);
-  if (d)
-    mp_drop(d);
+  mp_drop(d);
 
   /* --- Evaluate the polynomial at %$x = i + 1$% --- */
 
@@ -291,9 +293,7 @@ mp *share_combine(share *s)
   }
 
   a = mpbarrett_reduce(&mb, a, a);
-  s->s = mp_copy(a);
-  if (m)
-    mp_drop(m);
+  mp_drop(m);
   mpbarrett_destroy(&mb);
   return (a);
 }
@@ -330,8 +330,7 @@ static int verify(grand *r)
   }
 
   share_create(&s, t);
-  s.s = mp_copy(sec);
-  share_mkshares(&s, r);
+  share_mkshares(&s, r, sec);
   for (i = 0; i < t; i++)
     v[i] = share_get(&s, MP_NEW, p[i]);
   pp = mp_copy(s.p);