libtc  20160415
Threshold Cryptography functions library
algorithms_generate_keys.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <gmp.h>
3 #include <stdio.h>
4 
5 #include "mathutils.h"
6 #include "tc.h"
7 #include "tc_internal.h"
8 
9 
10 /* Fast safe prime generation,
11  * if it finds a prime it tries the next probably safe prime or the previous */
12 void generate_safe_prime(mpz_t out, int bit_len, random_fn random) {
13  assert(random != NULL);
14  static const int c = 25; /* Number of Miller-Rabbin tests */
15 
16  mpz_t p, q, r, t1;
17  mpz_inits(p, q, r, t1, NULL);
18  int q_composite, r_composite;
19 
20  do {
21  random_prime(p, bit_len, random);
22  mpz_sub_ui(t1, p, 1);
23  mpz_fdiv_q_ui(q, t1, 2);
24 
25  mpz_mul_ui(t1, p, 2);
26  mpz_add_ui(r, t1, 1);
27 
28  /* r > p > q */
29 
30  q_composite = mpz_probab_prime_p(q, c) == 0;
31  r_composite = mpz_probab_prime_p(r, c) == 0;
32  } while (q_composite && r_composite);
33 
34  mpz_set(out, q_composite ? r : p);
35 
36  mpz_clears(p, q, r, t1, NULL);
37 }
38 
42 key_share_t **tc_generate_keys(key_metainfo_t **out, size_t bit_size, uint16_t k, uint16_t l, bytes_t *public_e) {
43  /* Preconditions */
44  assert(out != NULL);
45  assert(bit_size >= 512 && bit_size <= 8192);
46  assert(0 < k);
47  assert(k <= l);
48  assert(l / 2 + 1 <= k);
49 
50  key_metainfo_t *info = *out = tc_init_key_metainfo(k, l);
51  key_share_t **ks = tc_init_key_shares(info);
52 
53  static const int F4 = 65537; // Fermat fourth number.
54 
55  size_t prime_size = bit_size / 2;
56 
57  mpz_t pr, qr, p, q, d, e, ll, m, n, delta_inv, divisor, r, vk_v, vk_u, s_i, vk_i;
58  mpz_inits(pr, qr, p, q, d, e, ll, m, n, delta_inv, divisor, r, vk_v, vk_u, s_i, vk_i, NULL);
59 
60  generate_safe_prime(p, prime_size, random_dev);
61  generate_safe_prime(q, prime_size, random_dev);
62 
63  // p' = (p-1)/2
64  mpz_sub_ui(pr, p, 1);
65  mpz_fdiv_q_ui(pr, pr, 2);
66 
67  // q' = (q-1)/2
68  mpz_sub_ui(qr, q, 1);
69  mpz_fdiv_q_ui(qr, qr, 2);
70 
71  // n = p * q, m = p' * q'
72  mpz_mul(n, p, q);
73  mpz_mul(m, pr, qr);
74  TC_MPZ_TO_BYTES(info->public_key->n, n);
75 
76  mpz_set_ui(ll, l);
77 
78  int e_set = 0;
79  if (public_e != NULL) {
80  TC_BYTES_TO_MPZ(e, public_e);
81  if (mpz_probab_prime_p(e, 25) && mpz_cmp(ll, e) < 0) {
82  e_set = !e_set;
83  }
84  }
85 
86  if (!e_set){
87  mpz_set_ui(e, F4); // l is always less than 65537 (l is an uint16_t)
88  }
89 
90  TC_MPZ_TO_BYTES(info->public_key->e, e);
91 
92  // d = e^{-1} mod m
93  mpz_invert(d, e, m);
94 
95  // Generate v
96  do {
97  random_dev(r, mpz_sizeinbase(n, 2));
98  mpz_gcd(divisor, r, n);
99  } while (mpz_cmp_ui(divisor, 1) != 0);
100  mpz_powm_ui(vk_v, r, 2, n);
101  TC_MPZ_TO_BYTES(info->vk_v, vk_v);
102 
103  // Generate u
104  do {
105  random_dev(vk_u, mpz_sizeinbase(n, 2));
106  mpz_mod(vk_u, vk_u, n);
107  } while (mpz_jacobi(vk_u, n) != -1);
108  TC_MPZ_TO_BYTES(info->vk_u, vk_u);
109 
110  // Delta = l!
111  mpz_fac_ui(delta_inv, l);
112  mpz_invert(delta_inv, delta_inv, m);
113 
114  // Generate Polynomial with random coefficients
115  poly_t *poly = create_random_poly(d, info->k-1, m);
116 
117  // Calculate Key Shares
118  for(int i=1; i <= info->l; i++) {
120  key_share->id = i;
121  poly_eval_ui(s_i, poly, i);
122 
123  mpz_mul(s_i, s_i, delta_inv);
124  mpz_mod(s_i, s_i, m);
125 
126  TC_MPZ_TO_BYTES(key_share->s_i, s_i);
127  TC_MPZ_TO_BYTES(key_share->n, n);
128 
129  mpz_powm(vk_i, vk_v, s_i, n);
130  TC_MPZ_TO_BYTES(&info->vk_i[TC_ID_TO_INDEX(i)], vk_i);
131  }
132 
133  clear_poly(poly);
134  mpz_clears(pr, qr, p, q, d, e, ll, m, n, delta_inv, divisor, r, vk_v, vk_u, s_i, vk_i, NULL);
135 
136  assert(ks != NULL);
137 #ifndef NDEBUG
138  for (int i = 0; i < info->l; i++) {
139  assert(ks[i] != NULL);
140  }
141 #endif
142  assert(*out != NULL);
143 
144  return ks;
145 }
#define TC_ID_TO_INDEX(id)
Definition: tc_internal.h:38
public_key_t * public_key
Definition: tc_internal.h:14
void random_prime(mpz_t rop, int bit_len, random_fn random)
Definition: random.c:26
bytes_t * s_i
Definition: tc_internal.h:23
void generate_safe_prime(mpz_t out, int bit_len, random_fn random)
bytes_t * n
Definition: tc_internal.h:24
Structure that&#39;s stores a pointer that points to data_len bytes.
Definition: tc.h:14
bytes_t * n
Definition: tc_internal.h:9
Structure that represents the data about a key share, including its public key.
Definition: tc_internal.h:13
bytes_t * vk_i
Definition: tc_internal.h:19
uint16_t id
Definition: tc_internal.h:25
void random_dev(mpz_t rop, int bit_len)
Definition: random.c:8
bytes_t * vk_v
Definition: tc_internal.h:17
uint16_t k
Definition: tc_internal.h:15
uint16_t l
Definition: tc_internal.h:16
void(* random_fn)(mpz_t rop, int bit_len)
Definition: mathutils.h:6
key_metainfo_t * tc_init_key_metainfo(uint16_t k, uint16_t l)
Definition: structs_init.c:90
#define TC_BYTES_TO_MPZ(z, bytes)
Definition: tc_internal.h:42
bytes_t * vk_u
Definition: tc_internal.h:18
Structure that represents one key share, to be used to generate a signature share.
Definition: tc_internal.h:22
void clear_poly(poly_t *poly)
Definition: poly.c:35
key_share_t ** tc_generate_keys(key_metainfo_t **out, size_t bit_size, uint16_t k, uint16_t l, bytes_t *public_e)
Definition: mathutils.h:11
bytes_t * e
Definition: tc_internal.h:10
poly_t * create_random_poly(mpz_t d, size_t size, mpz_t m)
Definition: poly.c:9
void poly_eval_ui(mpz_t rop, poly_t *poly, unsigned long op)
Definition: poly.c:70
#define TC_MPZ_TO_BYTES(bytes, z)
Definition: tc_internal.h:40
key_share_t ** tc_init_key_shares(key_metainfo_t *info)
Definition: structs_init.c:154