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 /*----- Header files ------------------------------------------------------*/
39 #include "mpbarrett.h"
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @share_create@ --- *
49 * Arguments: @share *s@ = pointer to share context to initialize
50 * @unsigned t@ = threshold for the system
54 * Use: Initializes a sharing context.
57 void share_create(share
*s
, unsigned t
)
65 /* --- @share_destroy@ --- *
67 * Arguments: @share *s@ = pointer to share context to destroy
71 * Use: Disposes of a sharing context. All memory is freed, all
72 * integers are dropped.
75 void share_destroy(share
*s
)
79 /* --- Dispose of the share vector --- */
82 for (i
= 0; i
< s
->t
; i
++)
87 /* --- Other stuff --- */
92 /* --- @share_mkshares@ --- *
94 * Arguments: @share *s@ = pointer to share context to fill in
95 * @grand *r@ = pointer to random number source
96 * @mp *n@ = the secret to share
100 * Use: Initializes a sharing context to be able to create shares.
101 * The context structure is expected to be mostly filled in. In
102 * particular, @t@ must be initialized. If @p@ is zero, a prime
103 * number of appropriate size is generated automatically. If
104 * @v@ is zero, a vector of appropriate size is allocated. You
105 * should use the macro @SHARE_INIT@ or @share_create@ to
106 * construct sharing contexts.
109 void share_mkshares(share
*s
, grand
*r
, mp
*n
)
113 /* --- If there's no prime, construct one --- */
119 unsigned bits
= (mp_octets(n
) + 1) * 8;
122 p
= mprand(MP_NEW
, bits
, r
, 1);
123 s
->p
= pgen("p", p
, p
, 0, 0, 0, pgen_filter
, &pf
,
124 rabin_iters(bits
), pgen_test
, &pr
);
127 /* --- Construct the polynomial --- */
130 s
->v
= xmalloc(s
->t
* sizeof(share_pt
));
131 for (i
= 0; i
< s
->t
- 1; i
++)
132 s
->v
[i
].y
= mprand_range(MP_NEWSEC
, s
->p
, r
, 0);
133 s
->v
[s
->t
- 1].y
= mp_copy(n
);
136 /* --- @share_get@ --- *
138 * Arguments: @share *s@ = pointer to share conext
139 * @mp *d@ = destination for the share
140 * @unsigned x@ = share index to fetch
142 * Returns: The share, as requested.
144 * Use: Extracts a share from the system. You may extract @MPW_MAX@
145 * shares, or @s->p@ shares from the system, whichever is
146 * smaller. Shares are indexed from 0.
149 mp
*share_get(share
*s
, mp
*d
, unsigned x
)
156 /* --- Various bits of initialization --- */
158 mp_build(&u
, &uw
, &uw
+ 1);
161 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
164 mpbarrett_create(&mb
, s
->p
);
165 for (i
= 0; i
< s
->t
; i
++) {
166 d
= mp_mul(d
, d
, &u
);
167 d
= mp_add(d
, d
, s
->v
[i
].y
);
168 d
= mpbarrett_reduce(&mb
, d
, d
);
170 mpbarrett_destroy(&mb
);
175 /* --- @share_addedp@ --- *
177 * Arguments: @share *s@ = pointer to sharing context
178 * @unsigned x@ = which share number to check
180 * Returns: Nonzero if share @x@ has been added already, zero if it
184 int share_addedp(share
*s
, unsigned x
)
188 for (i
= 0; i
< s
->i
; i
++) {
189 if (s
->v
[i
].x
== x
+ 1)
195 /* --- @share_add@ --- *
197 * Arguments: @share *s@ = pointer to sharing context
198 * @unsigned x@ = which share number this is
199 * @mp *y@ = the share value
201 * Returns: Number of shares required before recovery may be performed.
203 * Use: Adds a share to the context. The context must have been
204 * initialized with the correct prime @p@ and threshold @t@.
207 unsigned share_add(share
*s
, unsigned x
, mp
*y
)
209 assert(((void)"Share context is full", s
->i
< s
->t
));
210 assert(((void)"Share already present", !share_addedp(s
, x
)));
212 /* --- If no vector has been allocated, create one --- */
216 s
->v
= xmalloc(s
->t
* sizeof(share_pt
));
218 for (i
= 0; i
< s
->t
; i
++)
222 /* --- Store the share in the vector --- */
224 s
->v
[s
->i
].x
= x
+ 1;
225 s
->v
[s
->i
].y
= mp_copy(y
);
230 return (s
->t
- s
->i
);
233 /* --- @share_combine@ --- *
235 * Arguments: @share *s@ = pointer to share context
237 * Returns: The secret, as a multiprecision integer.
239 * Use: Reconstructs a secret, given enough shares.
242 mp
*share_combine(share
*s
)
251 /* --- Sanity checking --- */
253 assert(((void)"Not enough shares yet", s
->i
== s
->t
));
255 /* --- Initialization --- */
257 mpbarrett_create(&mb
, s
->p
);
258 mp_build(&ii
, &iiw
, &iiw
+ 1);
259 mp_build(&jj
, &jjw
, &jjw
+ 1);
261 /* --- Grind through the shares --- */
263 for (i
= 0; i
< s
->t
; i
++) {
267 for (j
= 0; j
< s
->t
; j
++) {
271 if (s
->v
[j
].x
>= s
->v
[i
].x
)
272 m
= mp_sub(m
, &jj
, &ii
);
274 m
= mp_sub(m
, &ii
, &jj
);
275 m
= mp_sub(m
, s
->p
, m
);
277 m
= mp_modinv(m
, m
, s
->p
);
278 c
= mp_mul(c
, c
, &jj
);
279 c
= mpbarrett_reduce(&mb
, c
, c
);
281 c
= mpbarrett_reduce(&mb
, c
, c
);
283 c
= mp_mul(c
, c
, s
->v
[i
].y
);
284 c
= mpbarrett_reduce(&mb
, c
, c
);
289 a
= mpbarrett_reduce(&mb
, a
, a
);
291 mpbarrett_destroy(&mb
);
295 /*----- Test rig ----------------------------------------------------------*/
301 static int verify(grand
*r
)
303 unsigned n
= r
->ops
->range(r
, 16) + 8;
304 unsigned t
= r
->ops
->range(r
, n
- 1) + 1;
305 unsigned len
= r
->ops
->range(r
, 160);
307 mp
**v
= xmalloc(t
* sizeof(mp
*));
308 unsigned *p
= xmalloc(n
* sizeof(unsigned));
309 mp
*sec
= mprand(MP_NEW
, len
, r
, 0);
317 for (i
= 0; i
< n
; i
++)
319 for (i
= 0; i
< t
; i
++) {
320 unsigned long j
= r
->ops
->range(r
, n
- i
);
327 share_mkshares(&s
, r
, sec
);
328 for (i
= 0; i
< t
; i
++)
329 v
[i
] = share_get(&s
, MP_NEW
, p
[i
]);
332 assert(mparena_count(MPARENA_GLOBAL
) + mparena_count(MPARENA_SECURE
) == t
+ 2);
336 for (i
= 0; i
< t
; i
++)
337 share_add(&s
, p
[i
], v
[i
]);
338 ss
= share_combine(&s
);
341 if (!MP_EQ(sec
, ss
)) {
343 fprintf(stderr
, "\nbad recombination of shares\n");
349 for (i
= 0; i
< t
; i
++)
355 assert(mparena_count(MPARENA_GLOBAL
) + mparena_count(MPARENA_SECURE
) == 0);
361 grand
*r
= fibrand_create(0);
365 fputs("share: ", stdout
);
366 for (i
= 0; i
< 40; i
++) {
374 fputs(" ok\n", stdout
);
376 fputs(" failed\n", stdout
);
377 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
382 /*----- That's all, folks -------------------------------------------------*/