3 * Shamir's secret sharing
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 #include "mpbarrett.h"
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @share_create@ --- *
47 * Arguments: @share *s@ = pointer to share context to initialize
48 * @unsigned t@ = threshold for the system
52 * Use: Initializes a sharing context.
55 void share_create(share
*s
, unsigned t
)
63 /* --- @share_destroy@ --- *
65 * Arguments: @share *s@ = pointer to share context to destroy
69 * Use: Disposes of a sharing context. All memory is freed, all
70 * integers are dropped.
73 void share_destroy(share
*s
)
77 /* --- Dispose of the share vector --- */
80 for (i
= 0; i
< s
->t
; i
++)
85 /* --- Other stuff --- */
90 /* --- @share_mkshares@ --- *
92 * Arguments: @share *s@ = pointer to share context to fill in
93 * @grand *r@ = pointer to random number source
94 * @mp *n@ = the secret to share
98 * Use: Initializes a sharing context to be able to create shares.
99 * The context structure is expected to be mostly filled in. In
100 * particular, @t@ must be initialized. If @p@ is zero, a prime
101 * number of appropriate size is generated automatically. If
102 * @v@ is zero, a vector of appropriate size is allocated. You
103 * should use the macro @SHARE_INIT@ or @share_create@ to
104 * construct sharing contexts.
107 void share_mkshares(share
*s
, grand
*r
, mp
*n
)
111 /* --- If there's no prime, construct one --- */
116 unsigned bits
= (mp_octets(n
) + 1) * 8;
119 p
= mprand(MP_NEW
, bits
, r
, 1);
120 s
->p
= pgen("p", p
, p
, 0, 0, 0, pgen_filter
, &pf
,
121 PGEN_BAILLIEPSWNTESTS
, pgen_bailliepswtest
, 0);
124 /* --- Construct the polynomial --- */
127 s
->v
= xmalloc(s
->t
* sizeof(share_pt
));
128 for (i
= 0; i
< s
->t
- 1; i
++)
129 s
->v
[i
].y
= mprand_range(MP_NEWSEC
, s
->p
, r
, 0);
130 s
->v
[s
->t
- 1].y
= mp_copy(n
);
133 /* --- @share_get@ --- *
135 * Arguments: @share *s@ = pointer to share conext
136 * @mp *d@ = destination for the share
137 * @unsigned x@ = share index to fetch
139 * Returns: The share, as requested.
141 * Use: Extracts a share from the system. You may extract @MPW_MAX@
142 * shares, or @s->p@ shares from the system, whichever is
143 * smaller. Shares are indexed from 0.
146 mp
*share_get(share
*s
, mp
*d
, unsigned x
)
153 /* --- Various bits of initialization --- */
155 mp_build(&u
, &uw
, &uw
+ 1);
158 /* --- Evaluate the polynomial at %$x = i + 1$% --- */
161 mpbarrett_create(&mb
, s
->p
);
162 for (i
= 0; i
< s
->t
; i
++) {
163 d
= mp_mul(d
, d
, &u
);
164 d
= mp_add(d
, d
, s
->v
[i
].y
);
165 d
= mpbarrett_reduce(&mb
, d
, d
);
167 mpbarrett_destroy(&mb
);
172 /* --- @share_addedp@ --- *
174 * Arguments: @share *s@ = pointer to sharing context
175 * @unsigned x@ = which share number to check
177 * Returns: Nonzero if share @x@ has been added already, zero if it
181 int share_addedp(share
*s
, unsigned x
)
185 for (i
= 0; i
< s
->i
; i
++) {
186 if (s
->v
[i
].x
== x
+ 1)
192 /* --- @share_add@ --- *
194 * Arguments: @share *s@ = pointer to sharing context
195 * @unsigned x@ = which share number this is
196 * @mp *y@ = the share value
198 * Returns: Number of shares required before recovery may be performed.
200 * Use: Adds a share to the context. The context must have been
201 * initialized with the correct prime @p@ and threshold @t@.
204 unsigned share_add(share
*s
, unsigned x
, mp
*y
)
206 assert(((void)"Share context is full", s
->i
< s
->t
));
207 assert(((void)"Share already present", !share_addedp(s
, x
)));
209 /* --- If no vector has been allocated, create one --- */
213 s
->v
= xmalloc(s
->t
* sizeof(share_pt
));
215 for (i
= 0; i
< s
->t
; i
++)
219 /* --- Store the share in the vector --- */
221 s
->v
[s
->i
].x
= x
+ 1;
222 s
->v
[s
->i
].y
= mp_copy(y
);
227 return (s
->t
- s
->i
);
230 /* --- @share_combine@ --- *
232 * Arguments: @share *s@ = pointer to share context
234 * Returns: The secret, as a multiprecision integer.
236 * Use: Reconstructs a secret, given enough shares.
239 mp
*share_combine(share
*s
)
248 /* --- Sanity checking --- */
250 assert(((void)"Not enough shares yet", s
->i
== s
->t
));
252 /* --- Initialization --- */
254 mpbarrett_create(&mb
, s
->p
);
255 mp_build(&ii
, &iiw
, &iiw
+ 1);
256 mp_build(&jj
, &jjw
, &jjw
+ 1);
258 /* --- Grind through the shares --- */
260 for (i
= 0; i
< s
->t
; i
++) {
264 for (j
= 0; j
< s
->t
; j
++) {
268 if (s
->v
[j
].x
>= s
->v
[i
].x
)
269 m
= mp_sub(m
, &jj
, &ii
);
271 m
= mp_sub(m
, &ii
, &jj
);
272 m
= mp_sub(m
, s
->p
, m
);
274 m
= mp_modinv(m
, m
, s
->p
);
275 c
= mp_mul(c
, c
, &jj
);
276 c
= mpbarrett_reduce(&mb
, c
, c
);
278 c
= mpbarrett_reduce(&mb
, c
, c
);
280 c
= mp_mul(c
, c
, s
->v
[i
].y
);
281 c
= mpbarrett_reduce(&mb
, c
, c
);
286 a
= mpbarrett_reduce(&mb
, a
, a
);
288 mpbarrett_destroy(&mb
);
292 /*----- Test rig ----------------------------------------------------------*/
298 static int verify(grand
*r
)
300 unsigned n
= r
->ops
->range(r
, 16) + 8;
301 unsigned t
= r
->ops
->range(r
, n
- 1) + 1;
302 unsigned len
= r
->ops
->range(r
, 160);
304 mp
**v
= xmalloc(t
* sizeof(mp
*));
305 unsigned *p
= xmalloc(n
* sizeof(unsigned));
306 mp
*sec
= mprand(MP_NEW
, len
, r
, 0);
314 for (i
= 0; i
< n
; i
++)
316 for (i
= 0; i
< t
; i
++) {
317 unsigned long j
= r
->ops
->range(r
, n
- i
);
324 share_mkshares(&s
, r
, sec
);
325 for (i
= 0; i
< t
; i
++)
326 v
[i
] = share_get(&s
, MP_NEW
, p
[i
]);
329 assert(mparena_count(MPARENA_GLOBAL
) + mparena_count(MPARENA_SECURE
) == t
+ 2);
333 for (i
= 0; i
< t
; i
++)
334 share_add(&s
, p
[i
], v
[i
]);
335 ss
= share_combine(&s
);
338 if (!MP_EQ(sec
, ss
)) {
340 fprintf(stderr
, "\nbad recombination of shares\n");
346 for (i
= 0; i
< t
; i
++)
352 assert(mparena_count(MPARENA_GLOBAL
) + mparena_count(MPARENA_SECURE
) == 0);
358 grand
*r
= fibrand_create(0);
362 fputs("share: ", stdout
);
363 for (i
= 0; i
< 40; i
++) {
371 fputs(" ok\n", stdout
);
373 fputs(" failed\n", stdout
);
374 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
379 /*----- That's all, folks -------------------------------------------------*/