Compute square roots in a prime field.
[u/mdw/catacomb] / gfshare.h
1 /* -*-c-*-
2 *
3 * $Id: gfshare.h,v 1.3 2000/06/18 23:12:15 mdw Exp $
4 *
5 * Secret sharing over %$\gf{2^8}$%
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: gfshare.h,v $
33 * Revision 1.3 2000/06/18 23:12:15 mdw
34 * Change typesetting of Galois Field names.
35 *
36 * Revision 1.2 2000/06/17 11:05:27 mdw
37 * Add a commentary on the system.
38 *
39 * Revision 1.1 2000/06/17 10:56:30 mdw
40 * Fast but nonstandard secret sharing system.
41 *
42 */
43
44 /*----- Notes on the system -----------------------------------------------*
45 *
46 * This uses a variant of Shamir's secret sharing system. Shamir's original
47 * system used polynomials modulo a large prime. This implementation instead
48 * uses the field %$\gf{2^8}$%, represented by
49 *
50 * %$\gf{2}[x]/(x^8 + x^4 + x^3 + x^2 + 1)$%
51 *
52 * and shares each byte of the secret independently. It is therefore limited
53 * to 255 players, although this probably isn't a serious limitation in
54 * practice.
55 *
56 * Share creation and reconstruction is extremely efficient. Contrast the
57 * performance of the straightforward implementation based on multiprecision
58 * arithmetic.
59 */
60
61 #ifndef CATACOMB_GFSHARE_H
62 #define CATACOMB_GFSHARE_H
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68 /*----- Header files ------------------------------------------------------*/
69
70 #include <mLib/bits.h>
71
72 #ifndef CATACOMB_GRAND_H
73 # include "grand.h"
74 #endif
75
76 /*----- Data structures ---------------------------------------------------*/
77
78 /* --- A secret sharing context --- */
79
80 typedef struct gfshare_pt {
81 octet x; /* %$x$%-coordinate of the share */
82 octet *y; /* Pointer to share payload */
83 } gfshare_pt;
84
85 typedef struct gfshare {
86 unsigned t; /* Threshold */
87 unsigned n; /* The number of shares to make */
88 unsigned i; /* Next free slot in vector */
89 size_t sz; /* Size of the secret and shares */
90 octet *s; /* The secret */
91 gfshare_pt *v; /* Vector of share information */
92 } gfshare;
93
94 #define GFSHARE_INIT(t, n, sz) { t, n, 0, sz, 0, 0 }
95
96 /*----- Functions provided ------------------------------------------------*/
97
98 /* --- @gfshare_create@ --- *
99 *
100 * Arguments: @gfshare *s@ = pointer to share context to initialize
101 * @unsigned t, n@ = threshold parameters for the system
102 * @size_t sz@ = size of the secret
103 *
104 * Returns: ---
105 *
106 * Use: Initializes a sharing context.
107 */
108
109 extern void gfshare_create(gfshare */*s*/, unsigned /*t*/, unsigned /*n*/,
110 size_t /*sz*/);
111
112 /* --- @gfshare_destroy@ --- *
113 *
114 * Arguments: @gfshare *s@ = pointer to share context to destroy
115 *
116 * Returns: ---
117 *
118 * Use: Disposes of a sharing context. The allocations for the
119 * individual shares and the vector @v@ are freed; the secret is
120 * left alone.
121 */
122
123 extern void gfshare_destroy(gfshare */*s*/);
124
125 /* --- @gfshare_mkshares@ --- *
126 *
127 * Arguments: @gfshare *s@ = pointer to share context to fill in
128 * @grand *r@ = pointer to random number source
129 *
130 * Returns: ---
131 *
132 * Use: Generates @c->n@ secret shares, such that any @c->t@ of them
133 * may be used to recover the secret.
134 *
135 * The context structure is expected to be mostly filled in. In
136 * particular, @t@, @n@, @ssz@ and @s@ must be initialized. If
137 * @v@ is zero, a vector of appropriate size is allocated. You
138 * should use the macro @GFSHARE_INIT@ or @gfshare_create@ to
139 * construct sharing contexts.
140 */
141
142 extern void gfshare_mkshares(gfshare */*s*/, grand */*r*/);
143
144 /* --- @gfshare_add@ --- *
145 *
146 * Arguments: @gfshare *s@ = pointer to sharing context
147 * @unsigned x@ = which share number this is
148 * @const octet *y@ = the share value
149 *
150 * Returns: Number of shares required before recovery may be performed.
151 *
152 * Use: Adds a share to the context. The context must have been
153 * initialized with the correct threshold @t@.
154 */
155
156 extern unsigned gfshare_add(gfshare */*s*/,
157 unsigned /*x*/, const octet */*y*/);
158
159 /* --- @gfshare_combine@ --- *
160 *
161 * Arguments: @gfshare *s@ = pointer to share context
162 * @octet *buf@ = pointer to output buffer for the secret
163 *
164 * Returns: ---
165 *
166 * Use: Reconstructs a secret, given enough shares.
167 */
168
169 extern void gfshare_combine(gfshare */*s*/, octet */*buf*/);
170
171 /*----- That's all, folks -------------------------------------------------*/
172
173 #ifdef __cplusplus
174 }
175 #endif
176
177 #endif