aboutsummaryrefslogtreecommitdiffstats
path: root/moon-abe/pbc-0.5.14/ecc/pairing.c
blob: 48a9c8c66c276398a2a44c103f3b4da139ec0855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h> // for intptr_t
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
#include "pbc_utils.h"
#include "pbc_field.h"
#include "pbc_poly.h"
#include "pbc_curve.h"
#include "pbc_param.h"
#include "pbc_pairing.h"
#include "pbc_memory.h"

static int generic_is_almost_coddh(element_ptr a, element_ptr b,
    element_ptr c, element_ptr d, pairing_t pairing) {
  int res = 0;
  element_t t0, t1;

  element_init(t0, pairing->GT);
  element_init(t1, pairing->GT);
  element_pairing(t0, a, d);
  element_pairing(t1, b, c);
  if (!element_cmp(t0, t1)) {
    res = 1;
  } else {
    element_mul(t0, t0, t1);
    if (element_is1(t0)) res = 1;
  }
  element_clear(t0);
  element_clear(t1);
  return res;
}

static void generic_prod_pairings(element_ptr out, element_t in1[],
    element_t in2[], int n, pairing_t pairing) {
  pairing->map(out, in1[0], in2[0], pairing);
  element_t tmp;
  element_init_same_as(tmp, out);
  int i;
  for(i = 1; i < n; i++) {
    pairing->map(tmp, in1[i], in2[i], pairing);
    element_mul(out, out, tmp);
  }
  element_clear(tmp);
}

static void phi_warning(element_ptr out, element_ptr in, pairing_ptr pairing) {
  UNUSED_VAR(out);
  UNUSED_VAR(in);
  UNUSED_VAR(pairing);
  printf("Phi() not implemented for this pairing type yet!\n");
}

static void default_option_set(struct pairing_s *pairing, char *key, char *value) {
  UNUSED_VAR(pairing);
  UNUSED_VAR(key);
  UNUSED_VAR(value);
}

static void default_pp_init(pairing_pp_t p, element_ptr in1, pairing_t pairing) {
  UNUSED_VAR(pairing);
  p->data = (void *) in1;
}

static void default_pp_apply(element_ptr out, element_ptr in2, pairing_pp_t p) {
  p->pairing->map(out, p->data, in2, p->pairing);
}

static void default_pp_clear(pairing_pp_t p) {
  UNUSED_VAR(p);
}

void pairing_init_pbc_param(pairing_t pairing, pbc_param_ptr p) {
  pairing->option_set = default_option_set;
  pairing->pp_init = default_pp_init;
  pairing->pp_clear = default_pp_clear;
  pairing->pp_apply = default_pp_apply;
  pairing->is_almost_coddh = generic_is_almost_coddh;
  pairing->phi = phi_warning;
  pairing->prod_pairings = generic_prod_pairings;
  p->api->init_pairing(pairing, p->data);
  pairing->G1->pairing = pairing;
  pairing->G2->pairing = pairing;
  pairing->GT->pairing = pairing;
}

int pairing_init_set_buf(pairing_t pairing, const char *input, size_t len) {
  pbc_param_t par;
  int res = pbc_param_init_set_buf(par, input, len);
  if (res) {
    pbc_error("error initializing pairing");
    return 1;
  }
  pairing_init_pbc_param(pairing, par);
  pbc_param_clear(par);
  return 0;
}

int pairing_init_set_str(pairing_t pairing, const char *s) {
  return pairing_init_set_buf(pairing, s, 0);
}

void pairing_clear(pairing_t pairing) {
  pairing->clear_func(pairing);
}

// TODO: it's most likely better to add extra stuff to field_t
// so no new data structures are needed to create mulitplicative subgroups.
// Additionally the same code could be used with curve_t
// Will consider it later, especially if timings turn out bad

static void gt_out_info(FILE *out, field_ptr f) {
  gmp_fprintf(out, "roots of unity, order %Zd, ", f->order);
  field_out_info(out, f->data);
}

static void gt_from_hash(element_ptr e, void *data, int len) {
  pairing_ptr pairing = e->field->pairing;
  element_from_hash(e->data, data, len);
  pairing->finalpow(e);
}

static void gt_random(element_ptr e) {
  pairing_ptr pairing = e->field->pairing;
  element_random(e->data);
  pairing->finalpow(e);
}

// multiplicative subgroup of a field
static void mulg_field_clear(field_t f) {
  UNUSED_VAR(f);
}

static void mulg_init(element_ptr e) {
  e->data = pbc_malloc(sizeof(element_t));
  field_ptr f = e->field->data;
  element_init(e->data, f);
  element_set1(e->data);
}

static void mulg_clear(element_ptr e) {
  element_clear(e->data);
  pbc_free(e->data);
}

static void mulg_set(element_ptr x, element_t a) {
  element_set(x->data, a->data);
}

static int mulg_cmp(element_ptr x, element_t a) {
  return element_cmp(x->data, a->data);
}

static size_t mulg_out_str(FILE *stream, int base, element_ptr e) {
  return element_out_str(stream, base, e->data);
}

static void mulg_set_multiz(element_ptr e, multiz m) {
  return element_set_multiz(e->data, m);
}

static int mulg_set_str(element_ptr e, const char *s, int base) {
  return element_set_str(e->data, s, base);
}

static int mulg_item_count(element_ptr e) {
  return element_item_count(e->data);
}

static element_ptr mulg_item(element_ptr e, int i) {
  return element_item(e->data, i);
}

static int mulg_to_bytes(unsigned char *data, element_ptr e) {
  return element_to_bytes(data, e->data);
}

static int mulg_from_bytes(element_ptr e, unsigned char *data) {
  return element_from_bytes(e->data, data);
}

static int mulg_length_in_bytes(element_ptr e) {
  return element_length_in_bytes(e->data);
}

static int mulg_snprint(char *s, size_t n, element_ptr e) {
  return element_snprint(s, n, e->data);
}

static void mulg_to_mpz(mpz_ptr z, element_ptr e) {
  element_to_mpz(z, e->data);
}

static void mulg_set1(element_t e) {
  element_set1(e->data);
}

static void mulg_mul(element_ptr x, element_t a, element_t b) {
  element_mul(x->data, a->data, b->data);
}

static void mulg_div(element_ptr x, element_t a, element_t b) {
  element_div(x->data, a->data, b->data);
}

static void mulg_invert(element_ptr x, element_t a) {
  element_invert(x->data, a->data);
}

static int mulg_is1(element_ptr x) {
  return element_is1(x->data);
}

static void mulg_pow_mpz(element_t x, element_t a, mpz_t n) {
  element_pow_mpz(x->data, a->data, n);
}

static void mulg_pp_init(element_pp_t p, element_t in) {
  p->data = pbc_malloc(sizeof(element_pp_t));
  element_pp_init(p->data, in->data);
}

static void mulg_pp_clear(element_pp_t p) {
  element_pp_clear(p->data);
  pbc_free(p->data);
}

static void mulg_pp_pow(element_t out, mpz_ptr power, element_pp_t p) {
  element_pp_pow(out->data, power, p->data);
}

void pairing_GT_init(pairing_ptr pairing, field_t f) {
  field_ptr gt = pairing->GT;
  field_init(gt);
  gt->data = f;
  f->pairing = pairing;
  mpz_set(gt->order, pairing->r);
  gt->field_clear = mulg_field_clear;
  gt->out_info = gt_out_info;

  gt->init = mulg_init;
  gt->clear = mulg_clear;
  gt->set = mulg_set;
  gt->cmp = mulg_cmp;

  gt->out_str = mulg_out_str;
  gt->set_multiz = mulg_set_multiz;
  gt->set_str = mulg_set_str;
  gt->to_bytes = mulg_to_bytes;
  gt->from_bytes = mulg_from_bytes;
  gt->length_in_bytes = mulg_length_in_bytes;
  gt->fixed_length_in_bytes = f->fixed_length_in_bytes;
  gt->to_mpz = mulg_to_mpz;
  gt->snprint = mulg_snprint;
  gt->item = mulg_item;
  gt->item_count = mulg_item_count;

  // TODO: set gt->nqr to something?
  // set is_sqr, sqrt to something?

  // additive notation
  gt->set0 = mulg_set1;
  gt->add = mulg_mul;
  gt->sub = mulg_div;
  gt->mul_mpz = mulg_pow_mpz;
  gt->neg = mulg_invert;
  gt->is0 = mulg_is1;

  // multiplicative notation
  gt->set1 = mulg_set1;
  gt->mul = mulg_mul;
  gt->div = mulg_div;
  gt->pow_mpz = mulg_pow_mpz;
  gt->invert = mulg_invert;
  gt->is1 = mulg_is1;
  gt->pp_init = mulg_pp_init;
  gt->pp_clear = mulg_pp_clear;
  gt->pp_pow = mulg_pp_pow;

  gt->random = gt_random;
  gt->from_hash = gt_from_hash;
}