aboutsummaryrefslogtreecommitdiffstats
path: root/moon-abe/pbc-0.5.14/arith/z.c
blob: ff5a4a97c6d160d0e9c1ddc1f4efd135ee8cd243 (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
// The ring Z.
//
// Wrappers around GMP mpz functions.
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h> // for intptr_t
#include <stdlib.h>
#include <gmp.h>
#include "pbc_utils.h"
#include "pbc_field.h"
#include "pbc_z.h"
#include "pbc_random.h"
#include "pbc_fp.h"
#include "pbc_memory.h"

static void z_init(element_ptr e) {
  e->data = pbc_malloc(sizeof(mpz_t));
  mpz_init(e->data);
}

static void z_clear(element_ptr e) {
  mpz_clear(e->data);
  pbc_free(e->data);
}

static void z_set_si(element_ptr e, signed long int op) {
  mpz_set_si(e->data, op);
}

static void z_set_mpz(element_ptr e, mpz_ptr z) {
  mpz_set(e->data, z);
}

static void z_set0(element_ptr e) {
  mpz_set_ui(e->data, 0);
}

static void z_set1(element_ptr e) {
  mpz_set_ui(e->data, 1);
}

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

static int z_sgn(element_ptr a) {
  mpz_ptr z = a->data;
  return mpz_sgn(z);
}

static void z_add(element_ptr n, element_ptr a, element_ptr b) {
  mpz_add(n->data, a->data, b->data);
}

static void z_sub(element_ptr n, element_ptr a, element_ptr b) {
  mpz_sub(n->data, a->data, b->data);
}

static void z_square(element_ptr c, element_ptr a) {
  mpz_mul(c->data, a->data, a->data);
}

static void z_double(element_ptr n, element_ptr a) {
  mpz_mul_2exp(n->data, a->data, 1);
}

static void z_halve(element_ptr n, element_ptr a) {
  mpz_tdiv_q_2exp(n->data, a->data, -1);
}

static void z_mul(element_ptr n, element_ptr a, element_ptr b) {
  mpz_mul(n->data, a->data, b->data);
}

static void z_mul_mpz(element_ptr n, element_ptr a, mpz_ptr z) {
  mpz_mul(n->data, a->data, z);
}

static void z_mul_si(element_ptr n, element_ptr a, signed long int z) {
  mpz_mul_si(n->data, a->data, z);
}

static void z_pow_mpz(element_ptr n, element_ptr a, mpz_ptr z) {
  mpz_pow_ui(n->data, a->data, mpz_get_ui(z));
}

static void z_set(element_ptr n, element_ptr a) {
  mpz_set(n->data, a->data);
}

static void z_neg(element_ptr n, element_ptr a) {
  mpz_neg(n->data, a->data);
}

static void z_invert(element_ptr n, element_ptr a) {
  if (!mpz_cmpabs_ui(a->data, 1)) {
    mpz_set(n->data, a->data);
  } else mpz_set_ui(n->data, 0);
}

static void z_div(element_ptr c, element_ptr a, element_ptr b) {
  mpz_tdiv_q(c->data, a->data, b->data);
}

//(doesn't make sense if order is infinite)
static void z_random(element_ptr n) {
  mpz_set_ui(n->data, 0);
}

static void z_from_hash(element_ptr n, void *data, int len) {
  mpz_import(n->data, len, -1, 1, -1, 0, data);
}

static int z_is1(element_ptr n) {
  return !mpz_cmp_ui((mpz_ptr) n->data, 1);
}

static int z_is0(element_ptr n) {
  return mpz_is0(n->data);
}

static int z_cmp(element_ptr a, element_ptr b) {
  return mpz_cmp((mpz_ptr) a->data, (mpz_ptr) b->data);
}

static int z_is_sqr(element_ptr a) {
  return mpz_perfect_power_p(a->data);
}

static void z_sqrt(element_ptr c, element_ptr a) {
  mpz_sqrt(c->data, a->data);
}

static void z_field_clear(field_t f) {
  UNUSED_VAR (f);
}

// OpenSSL convention:
//   4 bytes containing length
//   followed by number in big-endian, most-significant bit set if negative
//   (prepending null byte if necessary)
// Positive numbers also the same as mpz_out_raw.
static int z_to_bytes(unsigned char *data, element_t e) {
  mpz_ptr z = e->data;
  size_t msb = mpz_sizeinbase(z, 2);
  size_t n = 4;
  size_t i;

  if (!(msb % 8)) {
    data[4] = 0;
    n++;
  }
  if (mpz_sgn(z) < 0) {
    mpz_export(data + n, NULL, 1, 1, 1, 0, z);
    data[4] |= 128;
  } else {
    mpz_export(data + n, NULL, 1, 1, 1, 0, z);
  }
  n += (msb + 7) / 8 - 4;
  for (i=0; i<4; i++) {
    data[i] = (n >> 8 * (3 - i));
  }
  n += 4;

  return n;
}

static int z_from_bytes(element_t e, unsigned char *data) {
  unsigned char *ptr;
  size_t i, n;
  mpz_ptr z = e->data;
  mpz_t z1;
  int neg = 0;

  mpz_init(z1);
  mpz_set_ui(z, 0);

  ptr = data;
  n = 0;
  for (i=0; i<4; i++) {
    n += ((unsigned int) *ptr) << 8 * (3 - i);
    ptr++;
  }
  if (data[4] & 128) {
    neg = 1;
    data[4] &= 127;
  }
  for (i=0; i<n; i++) {
    mpz_set_ui(z1, *ptr);
    mpz_mul_2exp(z1, z1, 8 * (n - 1 - i));
    ptr++;
    mpz_add(z, z, z1);
  }
  mpz_clear(z1);
  if (neg) mpz_neg(z, z);
  return n;
}

static void z_to_mpz(mpz_ptr z, element_ptr a) {
  mpz_set(z, a->data);
}

static int z_length_in_bytes(element_ptr a) {
  return (mpz_sizeinbase(a->data, 2) + 7) / 8 + 4;
}

static void z_out_info(FILE *out, field_ptr f) {
  UNUSED_VAR(f);
  fprintf(out, "Z: wrapped GMP");
}

static int z_set_str(element_ptr e, const char *s, int base) {
  mpz_t z;
  mpz_init(z);
  int result = pbc_mpz_set_str(z, s, base);
  z_set_mpz(e, z);
  mpz_clear(z);
  return result;
}

void field_init_z(field_ptr f) {
  field_init(f);
  f->init = z_init;
  f->clear = z_clear;
  f->set_si = z_set_si;
  f->set_mpz = z_set_mpz;
  f->set_str = z_set_str;
  f->out_str = z_out_str;
  f->sign = z_sgn;
  f->add = z_add;
  f->sub = z_sub;
  f->set = z_set;
  f->square = z_square;
  f->doub = z_double;
  f->halve = z_halve;
  f->mul = z_mul;
  f->mul_mpz = z_mul_mpz;
  f->mul_si = z_mul_si;
  f->pow_mpz = z_pow_mpz;
  f->neg = z_neg;
  f->cmp = z_cmp;
  f->invert = z_invert;
  f->div = z_div;
  f->random = z_random;
  f->from_hash = z_from_hash;
  f->is1 = z_is1;
  f->is0 = z_is0;
  f->set0 = z_set0;
  f->set1 = z_set1;
  f->is_sqr = z_is_sqr;
  f->sqrt = z_sqrt;
  f->field_clear = z_field_clear;
  f->to_bytes = z_to_bytes;
  f->from_bytes = z_from_bytes;
  f->to_mpz = z_to_mpz;
  f->length_in_bytes = z_length_in_bytes;

  f->out_info = z_out_info;

  mpz_set_ui(f->order, 0);
  f->data = NULL;
  f->fixed_length_in_bytes = -1;
}