summaryrefslogtreecommitdiffstats
path: root/mpi-patches/add-bitops
blob: 19b9fee9d03861c24ca9de0666ca675b99e465e6 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
Index: mpi-1.8.6/mpi.c
===================================================================
--- mpi-1.8.6.orig/mpi.c	2015-02-07 19:33:17.280186661 -0800
+++ mpi-1.8.6/mpi.c	2015-02-07 19:33:19.412149446 -0800
@@ -16,6 +16,9 @@
 #include <ctype.h>
 #include <math.h>
 
+#define MAX(A, B) ((A) > (B) ? (A) : (B))
+#define MIN(A, B) ((A) < (B) ? (A) : (B))
+
 typedef unsigned char mem_t;
 extern mem_t *chk_calloc(size_t n, size_t size);
 
@@ -157,6 +160,7 @@
 mp_err   s_mp_grow(mp_int *mp, mp_size min);   /* increase allocated size */
 mp_err   s_mp_pad(mp_int *mp, mp_size min);    /* left pad with zeroes    */
 
+static int s_highest_bit(mp_digit n);
 int      s_highest_bit_mp(mp_int *a);
 mp_err   s_mp_set_bit(mp_int *a, int bit);
 
@@ -2334,6 +2338,430 @@
 
 /* }}} */
 
+/*
+ * Convert a's bit vector to its two's complement, up to the
+ * number of words that it contains, storing result in b. The numeric value of
+ * this result depends on the size of mpi_digit. This is a building block for
+ * handling negative operands in the bit operations.
+ */
+mp_err mp_2comp(mp_int *a, mp_int *b, mp_size dig)
+{
+  mp_err res;
+  mp_size ix, adig = USED(a);
+  mp_digit *pa, *pb;
+  mp_digit padding = ISNEG(a) ? MP_DIGIT_MAX : 0;
+  mp_word w;
+
+  ARGCHK(a != NULL && b != NULL, MP_BADARG);
+
+  if (a != b) {
+    if ((res = mp_init_size(b, dig)) != MP_OKAY)
+	return res;
+    SIGN(b) = SIGN(a);
+  } else {
+    if((res = s_mp_pad(b, dig)) != MP_OKAY)
+      return res;
+  }
+
+  for (pa = DIGITS(a), pb = DIGITS(b), w = 0, ix = 0; ix < dig; ix++) {
+    w += (ix == 0);
+    w += (ix < adig) ? ~pa[ix] : padding;
+    pb[ix] = ACCUM(w);
+    w = CARRYOUT(w);
+  }
+
+  USED(b) = dig;
+
+  return MP_OKAY;
+}
+
+mp_err mp_and(mp_int *a, mp_int *b, mp_int *c)
+{
+  mp_err res = MP_OKAY;
+  mp_size ix, extent = 0;
+  mp_digit *pa, *pb, *pc;
+  mp_int tmp_a, tmp_b;
+
+  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
+
+  if (a == b)
+    return mp_copy(a, c);
+
+  if (ISNEG(a)) {
+    extent = USED(b);
+    mp_init(&tmp_a);
+    if ((res = mp_2comp(a, &tmp_a, extent)) != MP_OKAY)
+	goto out;
+    a = &tmp_a;
+  }
+
+  if (ISNEG(b)) {
+    extent = USED(a);
+    mp_init(&tmp_b);
+    if ((res = mp_2comp(b, &tmp_b, extent)) != MP_OKAY)
+	goto out;
+    b = &tmp_b;
+  }
+
+  if (!extent)
+    extent = MIN(USED(a), USED(b));
+
+  if (c != a && c != b) {
+    if ((res = mp_init_size(c, extent)) != MP_OKAY)
+	goto out;
+  }
+
+  for (pa = DIGITS(a), pb = DIGITS(b), pc = DIGITS(c), ix = 0;
+	 ix < extent; ix++)
+  {
+    pc[ix] = pa[ix] & pb[ix];
+  }
+
+  USED(c) = extent;
+
+  if (ISNEG(a) && ISNEG(b)) {
+    mp_2comp(c, c, extent);
+    SIGN(c) = MP_NEG;
+  }
+
+  s_mp_clamp(c);
+
+out:
+  if (ISNEG(a))
+    mp_clear(&tmp_a);
+
+  if (ISNEG(b))
+    mp_clear(&tmp_b);
+
+  return res;
+}
+
+mp_err mp_or(mp_int *a, mp_int *b, mp_int *c)
+{
+  mp_err res;
+  mp_size ix, extent = 0;
+  mp_digit *pa, *pb, *pc;
+  mp_int tmp_a, tmp_b;
+
+  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
+
+  extent = MAX(USED(a), USED(b));
+
+  if (a == b)
+    return mp_copy(a, c);
+
+  if (ISNEG(a)) {
+    mp_init(&tmp_a);
+    if ((res = mp_2comp(a, &tmp_a, extent)) != MP_OKAY)
+	goto out;
+    a = &tmp_a;
+  }
+
+  if (ISNEG(b)) {
+    mp_init(&tmp_b);
+    if ((res = mp_2comp(b, &tmp_b, extent)) != MP_OKAY)
+	goto out;
+    b = &tmp_b;
+  }
+
+
+  if (c != a && c != b)
+    res = mp_init_size(c, extent);
+  else
+    res = s_mp_pad(c, extent);
+
+  if (res != MP_OKAY)
+    goto out;
+
+  for (pa = DIGITS(a), pb = DIGITS(b), pc = DIGITS(c), ix = 0;
+	 ix < extent; ix++)
+  {
+    pc[ix] = pa[ix] | pb[ix];
+  }
+
+  USED(c) = extent;
+
+  if (ISNEG(a) || ISNEG(b)) {
+    mp_2comp(c, c, extent);
+    SIGN(c) = MP_NEG;
+  }
+
+  s_mp_clamp(c);
+
+out:
+  if (ISNEG(a))
+    mp_clear(&tmp_a);
+
+  if (ISNEG(b))
+    mp_clear(&tmp_b);
+
+  return res;
+}
+
+mp_err mp_xor(mp_int *a, mp_int *b, mp_int *c)
+{
+  mp_err res;
+  mp_size ix, extent = 0;
+  mp_digit *pa, *pb, *pc;
+  mp_int tmp_a, tmp_b;
+
+  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
+
+  if (a == b) {
+    mp_zero(c);
+    return MP_OKAY;
+  }
+
+  extent = MAX(USED(a), USED(b));
+
+  if (ISNEG(a)) {
+    mp_init(&tmp_a);
+    if ((res = mp_2comp(a, &tmp_a, extent)) != MP_OKAY)
+	goto out;
+    a = &tmp_a;
+  }
+
+  if (ISNEG(b)) {
+    mp_init(&tmp_b);
+    if ((res = mp_2comp(b, &tmp_b, extent)) != MP_OKAY)
+	goto out;
+    b = &tmp_b;
+  }
+
+
+  if (c != a && c != b)
+    res = mp_init_size(c, extent);
+  else
+    res = s_mp_pad(c, extent);
+
+  if (res != MP_OKAY)
+    goto out;
+
+  for (pa = DIGITS(a), pb = DIGITS(b), pc = DIGITS(c), ix = 0;
+	 ix < extent; ix++)
+  {
+    pc[ix] = pa[ix] ^ pb[ix];
+  }
+
+  USED(c) = extent;
+
+  if (ISNEG(a) ^ ISNEG(b)) {
+    mp_2comp(c, c, extent);
+    SIGN(c) = MP_NEG;
+  }
+
+  s_mp_clamp(c);
+
+out:
+  if (ISNEG(a))
+    mp_clear(&tmp_a);
+
+  if (ISNEG(b))
+    mp_clear(&tmp_b);
+
+  return res;
+}
+
+mp_err mp_comp(mp_int *a, mp_int *b)
+{
+  mp_err res;
+  mp_size ix, dig = USED(a);
+  mp_digit *pa, *pb;
+  mp_int tmp;
+
+  ARGCHK(a != NULL && b != NULL, MP_BADARG);
+
+  if (a != b)
+    res = mp_init_size(b, dig);
+  else
+    res = s_mp_pad(b, dig);
+
+  if (res != MP_OKAY)
+    return res;
+
+  if (ISNEG(a)) {
+    mp_init(&tmp);
+    if ((res = mp_2comp(a, &tmp, dig)) != MP_OKAY)
+	return res;
+    a = &tmp;
+  }
+
+  for (pa = DIGITS(a), pb = DIGITS(b), ix = 0; ix < dig; ix++)
+    pb[ix] = ~pa[ix];
+
+  USED(b) = dig;
+
+  if (ISNEG(a)) {
+    mp_clear(&tmp);
+  } else {
+    if ((res = mp_2comp(b, b, dig)) != MP_OKAY)
+	return res;
+    SIGN(b) = MP_NEG;
+  }
+
+  s_mp_clamp(b);
+  return MP_OKAY;
+}
+
+mp_err mp_trunc_comp(mp_int *a, mp_int *b, mp_digit bits)
+{
+  mp_err res;
+  mp_size ix, dig = bits / DIGIT_BIT, rembits = bits % DIGIT_BIT;
+  mp_size adig = USED(a);
+  mp_digit padding = ISNEG(a) ? MP_DIGIT_MAX : 0;
+  int extra = (rembits != 0);
+  mp_digit *pa, *pb;
+  mp_int tmp;
+
+  ARGCHK(a != NULL && b != NULL, MP_BADARG);
+
+  if (a != b)
+    res = mp_init_size(b, dig + extra);
+  else
+    res = s_mp_pad(b, dig + extra);
+
+  if (res != MP_OKAY)
+    return res;
+
+  if (ISNEG(a)) {
+    mp_init(&tmp);
+    if ((res = mp_2comp(a, &tmp, dig + extra)) != MP_OKAY)
+	return res;
+    a = &tmp;
+  }
+
+  for (pa = DIGITS(a), pb = DIGITS(b), ix = 0; ix < dig; ix++)
+    pb[ix] = (ix < adig) ? ~pa[ix] : ~padding;
+
+  if (rembits) {
+    mp_digit mask = (MP_DIGIT_MAX >> (DIGIT_BIT - rembits));
+    pb[ix] = (((ix < adig) ? pa[ix] : padding) & mask) ^ mask;
+  }
+
+  USED(b) = dig + extra;
+
+  if (ISNEG(a))
+    mp_clear(&tmp);
+
+  s_mp_clamp(b);
+  return MP_OKAY;
+}
+
+mp_err mp_trunc(mp_int *a, mp_int *b, mp_digit bits)
+{
+  mp_err res;
+  mp_size ix, dig = bits / DIGIT_BIT, rembits = bits % DIGIT_BIT;
+  mp_size adig = USED(a);
+  mp_digit padding = ISNEG(a) ? MP_DIGIT_MAX : 0;
+  int extra = (rembits != 0);
+  mp_digit *pa, *pb;
+  mp_int tmp;
+
+  ARGCHK(a != NULL && b != NULL, MP_BADARG);
+
+  if (a != b)
+    res = mp_init_size(b, dig + extra);
+  else
+    res = s_mp_pad(b, dig + extra);
+
+  if (res != MP_OKAY)
+    return res;
+
+  if (ISNEG(a)) {
+    mp_init(&tmp);
+    if ((res = mp_2comp(a, &tmp, dig + extra)) != MP_OKAY)
+	return res;
+    a = &tmp;
+  }
+
+  for (pa = DIGITS(a), pb = DIGITS(b), ix = 0; ix < dig; ix++)
+    pb[ix] = (ix < adig) ? pa[ix] : padding;
+
+  if (rembits) {
+    mp_digit mask = (MP_DIGIT_MAX >> (DIGIT_BIT - rembits));
+    pb[ix] = ((ix < adig) ? pa[ix] : padding) & mask;
+  }
+
+  USED(b) = dig + extra;
+
+  if (ISNEG(a))
+    mp_clear(&tmp);
+
+  s_mp_clamp(b);
+  return MP_OKAY;
+}
+
+mp_err mp_shift(mp_int *a, mp_int *b, int bits)
+{
+  mp_int tmp;
+  mp_err res;
+  int a_neg = ISNEG(a);
+
+  if (bits == 0)
+    return mp_copy(a, b);
+
+  if (a_neg) {
+    mp_size ua = USED(a);
+    mp_init(&tmp);
+    if ((res = mp_2comp(a, &tmp, ua)) != MP_OKAY)
+	return res;
+    SIGN(&tmp) = MP_ZPOS;
+    a = &tmp;
+  }
+
+  if (bits > 0)
+    res = mp_mul_2d(a, bits, b);
+  else
+    res = mp_div_2d(a, -bits, b, NULL);
+
+  if (res != MP_OKAY) {
+    if (a_neg)
+	mp_clear(&tmp);
+    return res;
+  }
+
+  if (a_neg) {
+    int hb, msd;
+    mp_digit *db;
+
+    mp_clear(&tmp);
+
+    msd = USED(b)-1;
+    db = DIGITS(b);
+    hb = s_highest_bit(db[msd]);
+
+    if (hb < DIGIT_BIT)
+	db[msd] |= MP_DIGIT_MAX << hb;
+
+    if ((res = mp_2comp(b, b, USED(b))) != MP_OKAY)
+	return res;
+
+    SIGN(b) = MP_NEG;
+    s_mp_clamp(b);
+  }
+
+  return MP_OKAY;
+}
+
+mp_err mp_bit(mp_int *a, mp_digit bit)
+{
+  mp_int tmp;
+  mp_err res;
+  int a_neg = ISNEG(a);
+  int digit = bit / MP_DIGIT_BIT;
+  mp_digit mask = ((mp_digit) 1 << (bit % MP_DIGIT_BIT));
+
+  if (a_neg) {
+    mp_init(&tmp);
+    if ((res = mp_2comp(a, &tmp, bit + 1)) != MP_OKAY)
+	return res;
+    SIGN(&tmp) = MP_ZPOS;
+    a = &tmp;
+  }
+
+  return (DIGITS(a)[digit] & mask) != 0 ? MP_YES : MP_NO;
+}
+
 mp_err mp_to_double(mp_int *mp, double *d)
 {
   int ix;
Index: mpi-1.8.6/mpi.h
===================================================================
--- mpi-1.8.6.orig/mpi.h	2015-02-07 19:33:14.612233293 -0800
+++ mpi-1.8.6/mpi.h	2015-02-07 19:33:19.412149446 -0800
@@ -54,6 +54,7 @@
 
 /* Macros for accessing the mp_int internals           */
 #define  SIGN(MP)     ((MP)->sign)
+#define  ISNEG(MP)    ((MP)->sign == MP_NEG)
 #define  USED(MP)     ((MP)->used)
 #define  ALLOC(MP)    ((MP)->alloc)
 #define  DIGITS(MP)   ((MP)->dp)
@@ -187,6 +188,18 @@
 #endif /* end MP_NUMTH */
 
 /*------------------------------------------------------------------------*/
+/* Bit ops                                                                */
+mp_err mp_2comp(mp_int *a, mp_int *b, mp_size dig); /* peculiar semantics */
+mp_err mp_and(mp_int *a, mp_int *b, mp_int *c);
+mp_err mp_or(mp_int *a, mp_int *b, mp_int *c);
+mp_err mp_xor(mp_int *a, mp_int *b, mp_int *c);
+mp_err mp_comp(mp_int *a, mp_int *b);
+mp_err mp_trunc_comp(mp_int *a, mp_int *b, mp_digit bits);
+mp_err mp_trunc(mp_int *a, mp_int *b, mp_digit bits);
+mp_err mp_shift(mp_int *a, mp_int *b, int bits); /* + left, - right */
+mp_err mp_bit(mp_int *a, mp_digit bit);
+
+/*------------------------------------------------------------------------*/
 /* Conversions                                                            */
 
 mp_err mp_to_double(mp_int *mp, double *d);