3 * $Id: share.c,v 1.3 2000/06/24 18:29:05 mdw Exp $
5 * Shamir's secret sharing
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2000/06/24 18:29:05 mdw
34 * Interface change: allow shares to be extracted from a context on demand,
35 * rather than building them all up-front.
37 * Revision 1.2 2000/06/18 23:05:19 mdw
38 * Minor performance tweak: use Barrett reduction rather than Montgomery.
39 * Fast secret sharing isn't done here, though: see `gfshare' instead.
41 * Revision 1.1 2000/06/17 12:09:38 mdw
42 * Shamir's secret sharing system.
46 /*----- Header files ------------------------------------------------------*/
55 #include "mpbarrett.h"
61 /*----- Main code ---------------------------------------------------------*/
63 /* --- @share_create@ --- *
65 * Arguments: @share *s@ = pointer to share context to initialize
66 * @unsigned t@ = threshold for the system
70 * Use: Initializes a sharing context.
73 void share_create(share
*s
, unsigned t
)
82 /* --- @share_destroy@ --- *
84 * Arguments: @share *s@ = pointer to share context to destroy
88 * Use: Disposes of a sharing context. All memory is freed, all
89 * integers are dropped.
92 void share_destroy(share
*s
)
96 /* --- Dispose of the share vector --- */
99 for (i
= 0; i
< s
->t
; i
++) {
106 /* --- Other stuff --- */
114 /* --- @share_mkshares@ --- *
116 * Arguments: @share *s@ = pointer to share context to fill in
117 * @grand *r@ = pointer to random number source
121 * Use: Initializes a sharing context to be able to create shares.
122 * The context structure is expected to be mostly filled in. In
123 * particular, @t@ and @s@ must be initialized. If @p@ is zero,
124 * a prime number of appropriate size is generated
125 * automatically. If @v@ is zero, a vector of appropriate size
126 * is allocated. You should use the macro @SHARE_INIT@ or
127 * @share_create@ to construct sharing contexts.
130 void share_mkshares(share
*s
, grand
*r
)
134 /* --- If there's no prime, construct one --- */
140 unsigned bits
= (mp_octets(s
->s
) + 1) * 8;
143 p
= mprand(MP_NEW
, bits
, r
, 1);
144 s
->p
= pgen("p", p
, p
, 0, 0, 0, pgen_filter
, &pf
,
145 rabin_iters(bits
), pgen_test
, &pr
);
148 /* --- Construct the polynomial --- */
151 s
->v
= xmalloc(s
->t
* sizeof(share_pt
));
152 for (i
= 0; i
< s
->t
- 1; i
++)
153 s
->v
[i
].y
= mprand_range(MP_NEWSEC
, s
->p
, r
, 0);
154 s
->v
[s
->t
- 1].y
= mp_copy(s
->s
);
157 /* --- @share_get@ --- *
159 * Arguments: @share *s@ = pointer to share conext
160 * @mp *d@ = destination for the share
161 * @unsigned x@ = share index to fetch
163 * Returns: The share, as requested.
165 * Use: Extracts a share from the system. You may extract @MPW_MAX@
166 * shares, or @s->p@ shares from the system, whichever is
167 * smaller. Shares are indexed from 0.
170 mp
*share_get(share
*s
, mp
*d
, unsigned x
)
177 /* --- Various bits of initialization --- */
179 mp_build(&u
, &uw
, &uw
+ 1);
183 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
186 mpbarrett_create(&mb
, s
->p
);
187 for (i
= 0; i
< s
->t
; i
++) {
188 d
= mp_mul(d
, d
, &u
);
189 d
= mp_add(d
, d
, s
->v
[i
].y
);
190 d
= mpbarrett_reduce(&mb
, d
, d
);
192 mpbarrett_destroy(&mb
);
197 /* --- @share_add@ --- *
199 * Arguments: @share *s@ = pointer to sharing context
200 * @unsigned x@ = which share number this is
201 * @mp *y@ = the share value
203 * Returns: Number of shares required before recovery may be performed.
205 * Use: Adds a share to the context. The context must have been
206 * initialized with the correct prime @p@ and threshold @t@.
209 unsigned share_add(share
*s
, unsigned x
, mp
*y
)
211 /* --- If no vector has been allocated, create one --- */
215 s
->v
= xmalloc(s
->t
* sizeof(share_pt
));
217 for (i
= 0; i
< s
->t
; i
++)
221 assert(((void)"Share context is full", s
->i
< s
->t
));
223 /* --- Store the share in the vector --- */
225 s
->v
[s
->i
].x
= x
+ 1;
226 s
->v
[s
->i
].y
= mp_copy(y
);
231 return (s
->t
- s
->i
);
234 /* --- @share_combine@ --- *
236 * Arguments: @share *s@ = pointer to share context
238 * Returns: The secret, as a multiprecision integer.
240 * Use: Reconstructs a secret, given enough shares.
243 mp
*share_combine(share
*s
)
252 /* --- Sanity checking --- */
254 assert(((void)"Not enough shares yet", s
->i
== s
->t
));
256 /* --- Initialization --- */
258 mpbarrett_create(&mb
, s
->p
);
259 mp_build(&ii
, &iiw
, &iiw
+ 1);
260 mp_build(&jj
, &jjw
, &jjw
+ 1);
262 /* --- Grind through the shares --- */
264 for (i
= 0; i
< s
->t
; i
++) {
268 for (j
= 0; j
< s
->t
; j
++) {
272 if (s
->v
[j
].x
>= s
->v
[i
].x
)
273 m
= mp_sub(m
, &jj
, &ii
);
275 m
= mp_sub(m
, &ii
, &jj
);
276 m
= mp_sub(m
, s
->p
, m
);
278 mp_gcd(0, 0, &m
, s
->p
, m
);
279 c
= mp_mul(c
, c
, &jj
);
280 c
= mpbarrett_reduce(&mb
, c
, c
);
282 c
= mpbarrett_reduce(&mb
, c
, c
);
284 c
= mp_mul(c
, c
, s
->v
[i
].y
);
285 c
= mpbarrett_reduce(&mb
, c
, c
);
290 a
= mpbarrett_reduce(&mb
, a
, a
);
294 mpbarrett_destroy(&mb
);
298 /*----- Test rig ----------------------------------------------------------*/
304 static int verify(grand
*r
)
306 unsigned n
= r
->ops
->range(r
, 16) + 8;
307 unsigned t
= r
->ops
->range(r
, n
- 1) + 1;
308 unsigned len
= r
->ops
->range(r
, 160);
310 mp
**v
= xmalloc(t
* sizeof(mp
*));
311 unsigned *p
= xmalloc(n
* sizeof(unsigned));
312 mp
*sec
= mprand(MP_NEW
, len
, r
, 0);
320 for (i
= 0; i
< n
; i
++)
322 for (i
= 0; i
< t
; i
++) {
323 unsigned long j
= r
->ops
->range(r
, n
- i
);
331 share_mkshares(&s
, r
);
332 for (i
= 0; i
< t
; i
++)
333 v
[i
] = share_get(&s
, MP_NEW
, p
[i
]);
336 assert(mparena_count(MPARENA_GLOBAL
) + mparena_count(MPARENA_SECURE
) == t
+ 2);
340 for (i
= 0; i
< t
; i
++)
341 share_add(&s
, p
[i
], v
[i
]);
342 ss
= share_combine(&s
);
345 if (MP_CMP(sec
, !=, ss
)) {
347 fprintf(stderr
, "\nbad recombination of shares\n");
353 for (i
= 0; i
< t
; i
++)
359 assert(mparena_count(MPARENA_GLOBAL
) + mparena_count(MPARENA_SECURE
) == 0);
365 grand
*r
= fibrand_create(0);
369 fputs("share: ", stdout
);
370 for (i
= 0; i
< 40; i
++) {
378 fputs(" ok\n", stdout
);
380 fputs(" failed\n", stdout
);
381 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
386 /*----- That's all, folks -------------------------------------------------*/