rather than building them all up-front.
/* -*-c-*-
*
- * $Id: gfshare.c,v 1.3 2000/06/22 18:04:13 mdw Exp $
+ * $Id: gfshare.c,v 1.4 2000/06/24 18:29:05 mdw Exp $
*
* Secret sharing over %$\gf(2^8)$%
*
/*----- Revision history --------------------------------------------------*
*
* $Log: gfshare.c,v $
+ * Revision 1.4 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.3 2000/06/22 18:04:13 mdw
* Improve secret reconstruction -- compute coefficients as needed rather
* than making a big array of them.
/* --- @gfshare_create@ --- *
*
* Arguments: @gfshare *s@ = pointer to share context to initialize
- * @unsigned t, n@ = threshold parameters for the system
+ * @unsigned t@ = threshold for the system
* @size_t sz@ = size of the secret
*
* Returns: ---
* Use: Initializes a sharing context.
*/
-void gfshare_create(gfshare *s, unsigned t, unsigned n, size_t sz)
+void gfshare_create(gfshare *s, unsigned t, size_t sz)
{
s->t = t;
- s->n = n;
s->i = 0;
s->sz = sz;
s->s = 0;
void gfshare_destroy(gfshare *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++) {
- if (s->v[i].y)
- XS_FREE(s->v[i].y);
- }
- xfree(s->v);
- }
+ if (s->v)
+ XS_FREE(s->v);
}
/* --- @gfshare_mkshares@ --- *
*
* 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@, @ssz@ and @s@ must be initialized. If
- * @v@ is zero, a vector of appropriate size is allocated. You
- * should use the macro @GFSHARE_INIT@ or @gfshare_create@ to
- * construct sharing contexts.
+ * particular, @t@ and @s@ must be initialized. If @v@ is zero,
+ * a vector of appropriate size is allocated. You should use
+ * the macro @GFSHARE_INIT@ or @gfshare_create@ to construct
+ * sharing contexts.
*/
void gfshare_mkshares(gfshare *s, grand *r)
{
- octet *v;
- unsigned i;
-
- /* --- Construct the coefficients --- */
-
- v = XS_ALLOC(s->sz * s->t);
- r->ops->fill(r, v, s->sz * (s->t - 1));
- memcpy(v + s->sz * (s->t - 1), s->s, s->sz);
-
-
- /* --- Construct the shares --- */
-
- if (!s->v)
- s->v = xmalloc(s->n * sizeof(gfshare_pt));
-
- for (i = 0; i < s->n; i++) {
- unsigned j;
- const octet *p = v;
- unsigned ilog = gflog[i + 1];
+ s->v = XS_ALLOC(s->sz * s->t);
+ r->ops->fill(r, s->v, s->sz * (s->t - 1));
+ memcpy(s->v + s->sz * (s->t - 1), s->s, s->sz);
+}
- /* --- Evaluate the polynomial at %$x = i + 1$% --- */
+/* --- @gfshare_get@ --- *
+ *
+ * Arguments: @gfshare *s@ = pointer to share conext
+ * @unsigned x@ = share index to fetch
+ * @void *buf@ = pointer to output buffer
+ *
+ * Returns: The share, as requested.
+ *
+ * Use: Extracts a share from the system. You may extract up to 255
+ * shares from the system. Shares are indexed from 0.
+ */
- s->v[i].y = XS_ALLOC(s->sz);
- memcpy(s->v[i].y, p, s->sz);
- p += s->sz;
- for (j = 1; j < s->t; j++) {
- octet *q = s->v[i].y;
- unsigned k;
- for (k = 0; k < s->sz; k++) {
- unsigned qq = *q;
- if (qq)
- qq = gfexp[ilog + gflog[qq]];
- *q++ = qq ^ *p++;
- }
+void gfshare_get(gfshare *s, unsigned x, void *buf)
+{
+ unsigned i;
+ const octet *p = s->v;
+ unsigned ilog = gflog[x + 1];
+
+ /* --- Evaluate the polynomial at %$x = i + 1$% --- */
+
+ memcpy(buf, p, s->sz);
+ p += s->sz;
+
+ for (i = 1; i < s->t; i++) {
+ octet *q = buf;
+ unsigned k;
+ for (k = 0; k < s->sz; k++) {
+ unsigned qq = *q;
+ if (qq)
+ qq = gfexp[ilog + gflog[qq]];
+ *q++ = qq ^ *p++;
}
- s->v[i].x = i;
}
-
- /* --- Dispose of various bits of old rubbish --- */
-
- XS_FREE(v);
}
/* --- @gfshare_add@ --- *
*
* Arguments: @gfshare *s@ = pointer to sharing context
* @unsigned x@ = which share number this is
- * @const octet *y@ = the share value
+ * @const void *y@ = the share value
*
* Returns: Number of shares required before recovery may be performed.
*
* initialized with the correct threshold @t@.
*/
-unsigned gfshare_add(gfshare *s, unsigned x, const octet *y)
+unsigned gfshare_add(gfshare *s, unsigned x, const void *y)
{
+ octet *p;
+
/* --- If no vector has been allocated, create one --- */
if (!s->v) {
- s->v = xmalloc(s->t * sizeof(gfshare_pt));
+ s->v = XS_ALLOC(s->t * (s->sz + 1));
s->i = 0;
}
/* --- Store the share in the vector --- */
- s->v[s->i].x = x;
- s->v[s->i].y = XS_ALLOC(s->sz);
- memcpy(s->v[s->i].y, y, s->sz);
+ p = s->v + s->i * (s->sz + 1);
+ *p++ = x + 1;
+ memcpy(p, y, s->sz);
s->i++;
/* --- Done --- */
/* --- @gfshare_combine@ --- *
*
* Arguments: @gfshare *s@ = pointer to share context
- * @octet *buf@ = pointer to output buffer for the secret
+ * @void *buf@ = pointer to output buffer for the secret
*
* Returns: ---
*
* Use: Reconstructs a secret, given enough shares.
*/
-void gfshare_combine(gfshare *s, octet *buf)
+void gfshare_combine(gfshare *s, void *buf)
{
unsigned i, j;
+ unsigned xi, xj;
/* --- Sanity checking --- */
memset(buf, 0, s->sz);
for (i = 0; i < s->t; i++) {
+ octet *p = buf;
+ octet *q = s->v + i * (s->sz + 1);
unsigned c = 0, ci = 0;
/* --- Compute the magic coefficient --- */
+ xi = *q++;
for (j = 0; j < s->t; j++) {
if (i == j)
continue;
- c += gflog[s->v[j].x + 1];
+ xj = s->v[j * (s->sz + 1)];
+ c += gflog[xj];
if (c >= 0xff)
c -= 0xff;
- ci += gflog[(s->v[i].x + 1) ^ (s->v[j].x + 1)];
+ ci += gflog[xi ^ xj];
if (ci >= 0xff)
ci -= 0xff;
}
/* --- Work out another layer of the secret --- */
+ p = buf;
for (j = 0; j < s->sz; j++) {
- if (s->v[i].y[j])
- buf[j] ^= gfexp[c + gflog[s->v[i].y[j]]];
+ if (*q)
+ *p ^= gfexp[c + gflog[*q]];
+ p++, q++;
}
}
}
unsigned t = r->ops->range(r, n - 1) + 1;
unsigned len = r->ops->range(r, 32) + 1;
- gfshare_pt *v = xmalloc(t * sizeof(gfshare_pt));
+ octet *v = xmalloc(t * len);
unsigned *p = xmalloc(n * sizeof(unsigned));
octet *sec = xmalloc(len), *sbuf = xmalloc(len);
gfshare s;
r->ops->fill(r, sec, len);
- gfshare_create(&s, t, n, len);
+ gfshare_create(&s, t, len);
s.s = sec;
gfshare_mkshares(&s, r);
- for (i = 0; i < t; i++) {
- v[i].x = s.v[p[i]].x;
- v[i].y = xmalloc(len);
- memcpy(v[i].y, s.v[p[i]].y, len);
- }
+ for (i = 0; i < t; i++)
+ gfshare_get(&s, p[i], v + (i * len));
gfshare_destroy(&s);
- gfshare_create(&s, t, n, len);
- for (i = 0; i < t; i++) {
- gfshare_add(&s, v[i].x, v[i].y);
- }
+ gfshare_create(&s, t, len);
+ for (i = 0; i < t; i++)
+ gfshare_add(&s, p[i], v + (i * len));
gfshare_combine(&s, sbuf);
gfshare_destroy(&s);
xfree(sec);
xfree(sbuf);
- for (i = 0; i < t; i++)
- xfree(v[i].y);
xfree(v);
xfree(p);
/* -*-c-*-
*
- * $Id: gfshare.h,v 1.3 2000/06/18 23:12:15 mdw Exp $
+ * $Id: gfshare.h,v 1.4 2000/06/24 18:29:05 mdw Exp $
*
* Secret sharing over %$\gf{2^8}$%
*
/*----- Revision history --------------------------------------------------*
*
* $Log: gfshare.h,v $
+ * Revision 1.4 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.3 2000/06/18 23:12:15 mdw
* Change typesetting of Galois Field names.
*
/* --- A secret sharing context --- */
-typedef struct gfshare_pt {
- octet x; /* %$x$%-coordinate of the share */
- octet *y; /* Pointer to share payload */
-} gfshare_pt;
-
typedef struct gfshare {
unsigned t; /* Threshold */
- unsigned n; /* The number of shares to make */
unsigned i; /* Next free slot in vector */
size_t sz; /* Size of the secret and shares */
- octet *s; /* The secret */
- gfshare_pt *v; /* Vector of share information */
+ void *s; /* The secret */
+ octet *v; /* Vector of share information */
} gfshare;
-#define GFSHARE_INIT(t, n, sz) { t, n, 0, sz, 0, 0 }
+#define GFSHARE_INIT(t, sz) { t, 0, sz, 0, 0 }
/*----- Functions provided ------------------------------------------------*/
/* --- @gfshare_create@ --- *
*
* Arguments: @gfshare *s@ = pointer to share context to initialize
- * @unsigned t, n@ = threshold parameters for the system
+ * @unsigned t@ = threshold for the system
* @size_t sz@ = size of the secret
*
* Returns: ---
* Use: Initializes a sharing context.
*/
-extern void gfshare_create(gfshare */*s*/, unsigned /*t*/, unsigned /*n*/,
- size_t /*sz*/);
+extern void gfshare_create(gfshare */*s*/, unsigned /*t*/, size_t /*sz*/);
/* --- @gfshare_destroy@ --- *
*
*
* 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@, @ssz@ and @s@ must be initialized. If
- * @v@ is zero, a vector of appropriate size is allocated. You
- * should use the macro @GFSHARE_INIT@ or @gfshare_create@ to
- * construct sharing contexts.
+ * particular, @t@ and @s@ must be initialized. If @v@ is zero,
+ * a vector of appropriate size is allocated. You should use
+ * the macro @GFSHARE_INIT@ or @gfshare_create@ to construct
+ * sharing contexts.
*/
extern void gfshare_mkshares(gfshare */*s*/, grand */*r*/);
+/* --- @gfshare_get@ --- *
+ *
+ * Arguments: @gfshare *s@ = pointer to share conext
+ * @unsigned x@ = share index to fetch
+ * @void *buf@ = pointer to output buffer
+ *
+ * Returns: The share, as requested.
+ *
+ * Use: Extracts a share from the system. You may extract up to 255
+ * shares from the system. Shares are indexed from 0.
+ */
+
+extern void gfshare_get(gfshare */*s*/, unsigned /*x*/, void */*buf*/);
+
/* --- @gfshare_add@ --- *
*
* Arguments: @gfshare *s@ = pointer to sharing context
* @unsigned x@ = which share number this is
- * @const octet *y@ = the share value
+ * @const void *y@ = the share value
*
* Returns: Number of shares required before recovery may be performed.
*
*/
extern unsigned gfshare_add(gfshare */*s*/,
- unsigned /*x*/, const octet */*y*/);
+ unsigned /*x*/, const void */*y*/);
/* --- @gfshare_combine@ --- *
*
* Arguments: @gfshare *s@ = pointer to share context
- * @octet *buf@ = pointer to output buffer for the secret
+ * @void *buf@ = pointer to output buffer for the secret
*
* Returns: ---
*
* Use: Reconstructs a secret, given enough shares.
*/
-extern void gfshare_combine(gfshare */*s*/, octet */*buf*/);
+extern void gfshare_combine(gfshare */*s*/, void */*buf*/);
/*----- That's all, folks -------------------------------------------------*/
/* -*-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;
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);
}
*
* 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.
void share_mkshares(share *s, grand *r)
{
- mp **v;
unsigned i;
- mp u;
- mpw uw;
- mpbarrett mb;
/* --- If there's no prime, construct one --- */
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@ --- *
/* --- 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++;
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 {
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;
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);
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);
}
/* -*-c-*-
*
- * $Id: share.h,v 1.1 2000/06/17 12:09:38 mdw Exp $
+ * $Id: share.h,v 1.2 2000/06/24 18:29:05 mdw Exp $
*
* Shamir's secret sharing
*
/*----- Revision history --------------------------------------------------*
*
* $Log: share.h,v $
+ * Revision 1.2 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.1 2000/06/17 12:09:38 mdw
* Shamir's secret sharing system.
*
typedef struct share {
unsigned t; /* Threshold */
- unsigned n; /* The number of shares to make */
unsigned i; /* Next free slot in the vector */
mp *s; /* The secret */
mp *p; /* Modulus for arithmetic */
share_pt *v; /* Vector of share information */
} share;
-#define SHARE_INIT(t, n) { t, n, 0, 0, 0, 0 }
+#define SHARE_INIT(t) { t, 0, 0, 0, 0 }
/*----- Functions provided ------------------------------------------------*/
/* --- @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.
*/
-extern void share_create(share */*s*/, unsigned /*t*/, unsigned /*n*/);
+extern void share_create(share */*s*/, unsigned /*t*/);
/* --- @share_destroy@ --- *
*
*
* 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.
extern void share_mkshares(share */*s*/, grand */*r*/);
+/* --- @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.
+ */
+
+extern mp *share_get(share */*s*/, mp */*d*/, unsigned /*x*/);
+
/* --- @share_add@ --- *
*
* Arguments: @share *s@ = pointer to sharing context