libtc  20160415
Threshold Cryptography functions library
algorithms_join_signatures.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <assert.h>
3 #include <gmp.h>
4 #include <stdlib.h>
5 
6 #include "tc.h"
7 #include "tc_internal.h"
8 
9 void lagrange_interpolation(mpz_t out, int j, int k,
10  const signature_share_t ** S, const mpz_t delta);
11 
12 /* All the signatures are valid before getting them here.
13  * k is the number of signatures in the array
14  * TODO: verify if the array has less than info->l signatures.
15  */
16 
26  const bytes_t * document, const key_metainfo_t * info) {
27  assert(signatures != NULL);
28 #ifndef NDEBUG
29  for (int i = 0; i < info->k; i++) {
30  assert(signatures[i] != NULL);
31  }
32 #endif
33  assert(document != NULL && document->data != NULL);
34  assert(info != NULL);
35 
36  bytes_t * out = tc_init_bytes(NULL, 0);
37 
38  mpz_t x, n, e, u, delta, e_prime, w, s_i, lambda_k_2, aux, a, b, wa, xb, y;
39  mpz_inits(x, n, e, u, delta, e_prime, w, s_i, lambda_k_2, aux, a, b, wa, xb, y,
40  NULL);
41 
42  TC_BYTES_TO_MPZ(x, document);
43  TC_BYTES_TO_MPZ(n, info->public_key->n);
44  TC_BYTES_TO_MPZ(e, info->public_key->e);
45  TC_BYTES_TO_MPZ(u, info->vk_u);
46 
47  // x = doc if (doc | n) == 1 else doc * u^e
48  int jacobied = 0;
49  if(mpz_jacobi(x, n) == -1) {
50  mpz_t ue;
51  mpz_init(ue);
52  mpz_powm(ue, u, e, n);
53  mpz_mul(x, x, ue);
54  mpz_mod(x, x, n);
55  mpz_clear(ue);
56  jacobied = 1;
57  }
58 
59  mpz_fac_ui(delta, info->l);
60  mpz_set_ui(e_prime, 4);
61 
62  /* Calculate w */
63  mpz_set_si(w, 1);
64 
65  int k = info->k;
66  for (int i = 0; i < k; i++) {
67  int id = signatures[i]->id;
68  TC_BYTES_TO_MPZ(s_i, signatures[i]->x_i);
69  lagrange_interpolation(lambda_k_2, id, k, signatures, delta);
70  mpz_mul_ui(lambda_k_2, lambda_k_2, 2);
71 
72  mpz_powm(aux, s_i, lambda_k_2, n);
73  mpz_mul(w, w, aux);
74  }
75  mpz_mod(w, w, n);
76 
77  mpz_gcdext(aux, a, b, e_prime, e);
78 
79  mpz_powm(wa, w, a, n);
80  mpz_powm(xb, x, b, n);
81 
82  mpz_mul(y, wa, xb);
83 
84  if (jacobied) {
85  mpz_t inv_u;
86  mpz_init(inv_u);
87  mpz_invert(inv_u, u, n);
88  mpz_mul(y, y, inv_u);
89  mpz_clear(inv_u);
90  }
91 
92  mpz_mod(y, y, n);
93 
94  TC_MPZ_TO_BYTES(out, y);
95 
96  mpz_clears(x, n, e, u, delta, e_prime, w, s_i, lambda_k_2, aux, a, b, wa, xb,
97  y, NULL);
98 
99  assert(out != NULL && out->data != NULL);
100  return out;
101 }
102 
103 void lagrange_interpolation(mpz_t out, int j, int k,
104  const signature_share_t ** S, const mpz_t delta) {
105  mpz_set(out, delta);
106  mpz_t num, den;
107  mpz_init_set_si(num, 1);
108  mpz_init_set_si(den, 1);
109 
110  for (int i = 0; i < k; i++) {
111  int id = S[i]->id;
112  if (id != j) {
113  mpz_mul_si(num, num, id); // num <-- num*j_
114  mpz_mul_si(den, den, id - j); // den <-- den*(j_-j)
115  }
116  }
117 
118  mpz_mul(out, out, num);
119  mpz_fdiv_q(out, out, den);
120 
121  mpz_clears(num, den, NULL);
122 }
123 
public_key_t * public_key
Definition: tc_internal.h:14
void lagrange_interpolation(mpz_t out, int j, int k, const signature_share_t **S, const mpz_t delta)
Structure that&#39;s stores a pointer that points to data_len bytes.
Definition: tc.h:14
Structure that represents a signature share, to be joined generating a standard RSA signatures...
Definition: tc_internal.h:28
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
void * data
Definition: tc.h:15
uint16_t k
Definition: tc_internal.h:15
uint16_t l
Definition: tc_internal.h:16
#define TC_BYTES_TO_MPZ(z, bytes)
Definition: tc_internal.h:42
bytes_t * vk_u
Definition: tc_internal.h:18
bytes_t * e
Definition: tc_internal.h:10
bytes_t * tc_join_signatures(const signature_share_t **signatures, const bytes_t *document, const key_metainfo_t *info)
bytes_t * tc_init_bytes(void *bs, size_t len)
Definition: structs_init.c:18
#define TC_MPZ_TO_BYTES(bytes, z)
Definition: tc_internal.h:40