Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/security/ec/impl/mpi.c
38918 views
1
/*
2
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
3
* Use is subject to license terms.
4
*
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
9
*
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
14
*
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this library; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* *********************************************************************
25
*
26
* The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
27
*
28
* The Initial Developer of the Original Code is
29
* Michael J. Fromberger.
30
* Portions created by the Initial Developer are Copyright (C) 1998
31
* the Initial Developer. All Rights Reserved.
32
*
33
* Contributor(s):
34
* Netscape Communications Corporation
35
* Douglas Stebila <[email protected]> of Sun Laboratories.
36
*
37
* Last Modified Date from the Original Code: Nov 2019
38
*********************************************************************** */
39
40
/* Arbitrary precision integer arithmetic library */
41
42
#include "mpi-priv.h"
43
#if defined(OSF1)
44
#include <c_asm.h>
45
#endif
46
47
#if MP_LOGTAB
48
/*
49
A table of the logs of 2 for various bases (the 0 and 1 entries of
50
this table are meaningless and should not be referenced).
51
52
This table is used to compute output lengths for the mp_toradix()
53
function. Since a number n in radix r takes up about log_r(n)
54
digits, we estimate the output size by taking the least integer
55
greater than log_r(n), where:
56
57
log_r(n) = log_2(n) * log_r(2)
58
59
This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
60
which are the output bases supported.
61
*/
62
#include "logtab.h"
63
#endif
64
65
/* {{{ Constant strings */
66
67
/* Constant strings returned by mp_strerror() */
68
static const char *mp_err_string[] = {
69
"unknown result code", /* say what? */
70
"boolean true", /* MP_OKAY, MP_YES */
71
"boolean false", /* MP_NO */
72
"out of memory", /* MP_MEM */
73
"argument out of range", /* MP_RANGE */
74
"invalid input parameter", /* MP_BADARG */
75
"result is undefined" /* MP_UNDEF */
76
};
77
78
/* Value to digit maps for radix conversion */
79
80
/* s_dmap_1 - standard digits and letters */
81
static const char *s_dmap_1 =
82
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
83
84
/* }}} */
85
86
unsigned long mp_allocs;
87
unsigned long mp_frees;
88
unsigned long mp_copies;
89
90
/* {{{ Default precision manipulation */
91
92
/* Default precision for newly created mp_int's */
93
static mp_size s_mp_defprec = MP_DEFPREC;
94
95
mp_size mp_get_prec(void)
96
{
97
return s_mp_defprec;
98
99
} /* end mp_get_prec() */
100
101
void mp_set_prec(mp_size prec)
102
{
103
if(prec == 0)
104
s_mp_defprec = MP_DEFPREC;
105
else
106
s_mp_defprec = prec;
107
108
} /* end mp_set_prec() */
109
110
/* }}} */
111
112
/*------------------------------------------------------------------------*/
113
/* {{{ mp_init(mp, kmflag) */
114
115
/*
116
mp_init(mp, kmflag)
117
118
Initialize a new zero-valued mp_int. Returns MP_OKAY if successful,
119
MP_MEM if memory could not be allocated for the structure.
120
*/
121
122
mp_err mp_init(mp_int *mp, int kmflag)
123
{
124
return mp_init_size(mp, s_mp_defprec, kmflag);
125
126
} /* end mp_init() */
127
128
/* }}} */
129
130
/* {{{ mp_init_size(mp, prec, kmflag) */
131
132
/*
133
mp_init_size(mp, prec, kmflag)
134
135
Initialize a new zero-valued mp_int with at least the given
136
precision; returns MP_OKAY if successful, or MP_MEM if memory could
137
not be allocated for the structure.
138
*/
139
140
mp_err mp_init_size(mp_int *mp, mp_size prec, int kmflag)
141
{
142
ARGCHK(mp != NULL && prec > 0, MP_BADARG);
143
144
prec = MP_ROUNDUP(prec, s_mp_defprec);
145
if((DIGITS(mp) = s_mp_alloc(prec, sizeof(mp_digit), kmflag)) == NULL)
146
return MP_MEM;
147
148
SIGN(mp) = ZPOS;
149
USED(mp) = 1;
150
ALLOC(mp) = prec;
151
152
return MP_OKAY;
153
154
} /* end mp_init_size() */
155
156
/* }}} */
157
158
/* {{{ mp_init_copy(mp, from) */
159
160
/*
161
mp_init_copy(mp, from)
162
163
Initialize mp as an exact copy of from. Returns MP_OKAY if
164
successful, MP_MEM if memory could not be allocated for the new
165
structure.
166
*/
167
168
mp_err mp_init_copy(mp_int *mp, const mp_int *from)
169
{
170
ARGCHK(mp != NULL && from != NULL, MP_BADARG);
171
172
if(mp == from)
173
return MP_OKAY;
174
175
if((DIGITS(mp) = s_mp_alloc(ALLOC(from), sizeof(mp_digit), FLAG(from))) == NULL)
176
return MP_MEM;
177
178
s_mp_copy(DIGITS(from), DIGITS(mp), USED(from));
179
USED(mp) = USED(from);
180
ALLOC(mp) = ALLOC(from);
181
SIGN(mp) = SIGN(from);
182
183
#ifndef _WIN32
184
FLAG(mp) = FLAG(from);
185
#endif /* _WIN32 */
186
187
return MP_OKAY;
188
189
} /* end mp_init_copy() */
190
191
/* }}} */
192
193
/* {{{ mp_copy(from, to) */
194
195
/*
196
mp_copy(from, to)
197
198
Copies the mp_int 'from' to the mp_int 'to'. It is presumed that
199
'to' has already been initialized (if not, use mp_init_copy()
200
instead). If 'from' and 'to' are identical, nothing happens.
201
*/
202
203
mp_err mp_copy(const mp_int *from, mp_int *to)
204
{
205
ARGCHK(from != NULL && to != NULL, MP_BADARG);
206
207
if(from == to)
208
return MP_OKAY;
209
210
++mp_copies;
211
{ /* copy */
212
mp_digit *tmp;
213
214
/*
215
If the allocated buffer in 'to' already has enough space to hold
216
all the used digits of 'from', we'll re-use it to avoid hitting
217
the memory allocater more than necessary; otherwise, we'd have
218
to grow anyway, so we just allocate a hunk and make the copy as
219
usual
220
*/
221
if(ALLOC(to) >= USED(from)) {
222
s_mp_setz(DIGITS(to) + USED(from), ALLOC(to) - USED(from));
223
s_mp_copy(DIGITS(from), DIGITS(to), USED(from));
224
225
} else {
226
if((tmp = s_mp_alloc(ALLOC(from), sizeof(mp_digit), FLAG(from))) == NULL)
227
return MP_MEM;
228
229
s_mp_copy(DIGITS(from), tmp, USED(from));
230
231
if(DIGITS(to) != NULL) {
232
#if MP_CRYPTO
233
s_mp_setz(DIGITS(to), ALLOC(to));
234
#endif
235
s_mp_free(DIGITS(to), ALLOC(to));
236
}
237
238
DIGITS(to) = tmp;
239
ALLOC(to) = ALLOC(from);
240
}
241
242
/* Copy the precision and sign from the original */
243
USED(to) = USED(from);
244
SIGN(to) = SIGN(from);
245
} /* end copy */
246
247
return MP_OKAY;
248
249
} /* end mp_copy() */
250
251
/* }}} */
252
253
/* {{{ mp_exch(mp1, mp2) */
254
255
/*
256
mp_exch(mp1, mp2)
257
258
Exchange mp1 and mp2 without allocating any intermediate memory
259
(well, unless you count the stack space needed for this call and the
260
locals it creates...). This cannot fail.
261
*/
262
263
void mp_exch(mp_int *mp1, mp_int *mp2)
264
{
265
#if MP_ARGCHK == 2
266
assert(mp1 != NULL && mp2 != NULL);
267
#else
268
if(mp1 == NULL || mp2 == NULL)
269
return;
270
#endif
271
272
s_mp_exch(mp1, mp2);
273
274
} /* end mp_exch() */
275
276
/* }}} */
277
278
/* {{{ mp_clear(mp) */
279
280
/*
281
mp_clear(mp)
282
283
Release the storage used by an mp_int, and void its fields so that
284
if someone calls mp_clear() again for the same int later, we won't
285
get tollchocked.
286
*/
287
288
void mp_clear(mp_int *mp)
289
{
290
if(mp == NULL)
291
return;
292
293
if(DIGITS(mp) != NULL) {
294
#if MP_CRYPTO
295
s_mp_setz(DIGITS(mp), ALLOC(mp));
296
#endif
297
s_mp_free(DIGITS(mp), ALLOC(mp));
298
DIGITS(mp) = NULL;
299
}
300
301
USED(mp) = 0;
302
ALLOC(mp) = 0;
303
304
} /* end mp_clear() */
305
306
/* }}} */
307
308
/* {{{ mp_zero(mp) */
309
310
/*
311
mp_zero(mp)
312
313
Set mp to zero. Does not change the allocated size of the structure,
314
and therefore cannot fail (except on a bad argument, which we ignore)
315
*/
316
void mp_zero(mp_int *mp)
317
{
318
if(mp == NULL)
319
return;
320
321
s_mp_setz(DIGITS(mp), ALLOC(mp));
322
USED(mp) = 1;
323
SIGN(mp) = ZPOS;
324
325
} /* end mp_zero() */
326
327
/* }}} */
328
329
/* {{{ mp_set(mp, d) */
330
331
void mp_set(mp_int *mp, mp_digit d)
332
{
333
if(mp == NULL)
334
return;
335
336
mp_zero(mp);
337
DIGIT(mp, 0) = d;
338
339
} /* end mp_set() */
340
341
/* }}} */
342
343
/* {{{ mp_set_int(mp, z) */
344
345
mp_err mp_set_int(mp_int *mp, long z)
346
{
347
int ix;
348
unsigned long v = labs(z);
349
mp_err res;
350
351
ARGCHK(mp != NULL, MP_BADARG);
352
353
mp_zero(mp);
354
if(z == 0)
355
return MP_OKAY; /* shortcut for zero */
356
357
if (sizeof v <= sizeof(mp_digit)) {
358
DIGIT(mp,0) = v;
359
} else {
360
for (ix = sizeof(long) - 1; ix >= 0; ix--) {
361
if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
362
return res;
363
364
res = s_mp_add_d(mp, (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
365
if (res != MP_OKAY)
366
return res;
367
}
368
}
369
if(z < 0)
370
SIGN(mp) = NEG;
371
372
return MP_OKAY;
373
374
} /* end mp_set_int() */
375
376
/* }}} */
377
378
/* {{{ mp_set_ulong(mp, z) */
379
380
mp_err mp_set_ulong(mp_int *mp, unsigned long z)
381
{
382
int ix;
383
mp_err res;
384
385
ARGCHK(mp != NULL, MP_BADARG);
386
387
mp_zero(mp);
388
if(z == 0)
389
return MP_OKAY; /* shortcut for zero */
390
391
if (sizeof z <= sizeof(mp_digit)) {
392
DIGIT(mp,0) = z;
393
} else {
394
for (ix = sizeof(long) - 1; ix >= 0; ix--) {
395
if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
396
return res;
397
398
res = s_mp_add_d(mp, (mp_digit)((z >> (ix * CHAR_BIT)) & UCHAR_MAX));
399
if (res != MP_OKAY)
400
return res;
401
}
402
}
403
return MP_OKAY;
404
} /* end mp_set_ulong() */
405
406
/* }}} */
407
408
/*------------------------------------------------------------------------*/
409
/* {{{ Digit arithmetic */
410
411
/* {{{ mp_add_d(a, d, b) */
412
413
/*
414
mp_add_d(a, d, b)
415
416
Compute the sum b = a + d, for a single digit d. Respects the sign of
417
its primary addend (single digits are unsigned anyway).
418
*/
419
420
mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b)
421
{
422
mp_int tmp;
423
mp_err res;
424
425
ARGCHK(a != NULL && b != NULL, MP_BADARG);
426
427
if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
428
return res;
429
430
if(SIGN(&tmp) == ZPOS) {
431
if((res = s_mp_add_d(&tmp, d)) != MP_OKAY)
432
goto CLEANUP;
433
} else if(s_mp_cmp_d(&tmp, d) >= 0) {
434
if((res = s_mp_sub_d(&tmp, d)) != MP_OKAY)
435
goto CLEANUP;
436
} else {
437
mp_neg(&tmp, &tmp);
438
439
DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0);
440
}
441
442
if(s_mp_cmp_d(&tmp, 0) == 0)
443
SIGN(&tmp) = ZPOS;
444
445
s_mp_exch(&tmp, b);
446
447
CLEANUP:
448
mp_clear(&tmp);
449
return res;
450
451
} /* end mp_add_d() */
452
453
/* }}} */
454
455
/* {{{ mp_sub_d(a, d, b) */
456
457
/*
458
mp_sub_d(a, d, b)
459
460
Compute the difference b = a - d, for a single digit d. Respects the
461
sign of its subtrahend (single digits are unsigned anyway).
462
*/
463
464
mp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b)
465
{
466
mp_int tmp;
467
mp_err res;
468
469
ARGCHK(a != NULL && b != NULL, MP_BADARG);
470
471
if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
472
return res;
473
474
if(SIGN(&tmp) == NEG) {
475
if((res = s_mp_add_d(&tmp, d)) != MP_OKAY)
476
goto CLEANUP;
477
} else if(s_mp_cmp_d(&tmp, d) >= 0) {
478
if((res = s_mp_sub_d(&tmp, d)) != MP_OKAY)
479
goto CLEANUP;
480
} else {
481
mp_neg(&tmp, &tmp);
482
483
DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0);
484
SIGN(&tmp) = NEG;
485
}
486
487
if(s_mp_cmp_d(&tmp, 0) == 0)
488
SIGN(&tmp) = ZPOS;
489
490
s_mp_exch(&tmp, b);
491
492
CLEANUP:
493
mp_clear(&tmp);
494
return res;
495
496
} /* end mp_sub_d() */
497
498
/* }}} */
499
500
/* {{{ mp_mul_d(a, d, b) */
501
502
/*
503
mp_mul_d(a, d, b)
504
505
Compute the product b = a * d, for a single digit d. Respects the sign
506
of its multiplicand (single digits are unsigned anyway)
507
*/
508
509
mp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b)
510
{
511
mp_err res;
512
513
ARGCHK(a != NULL && b != NULL, MP_BADARG);
514
515
if(d == 0) {
516
mp_zero(b);
517
return MP_OKAY;
518
}
519
520
if((res = mp_copy(a, b)) != MP_OKAY)
521
return res;
522
523
res = s_mp_mul_d(b, d);
524
525
return res;
526
527
} /* end mp_mul_d() */
528
529
/* }}} */
530
531
/* {{{ mp_mul_2(a, c) */
532
533
mp_err mp_mul_2(const mp_int *a, mp_int *c)
534
{
535
mp_err res;
536
537
ARGCHK(a != NULL && c != NULL, MP_BADARG);
538
539
if((res = mp_copy(a, c)) != MP_OKAY)
540
return res;
541
542
return s_mp_mul_2(c);
543
544
} /* end mp_mul_2() */
545
546
/* }}} */
547
548
/* {{{ mp_div_d(a, d, q, r) */
549
550
/*
551
mp_div_d(a, d, q, r)
552
553
Compute the quotient q = a / d and remainder r = a mod d, for a
554
single digit d. Respects the sign of its divisor (single digits are
555
unsigned anyway).
556
*/
557
558
mp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r)
559
{
560
mp_err res;
561
mp_int qp;
562
mp_digit rem;
563
int pow;
564
565
ARGCHK(a != NULL, MP_BADARG);
566
567
if(d == 0)
568
return MP_RANGE;
569
570
/* Shortcut for powers of two ... */
571
if((pow = s_mp_ispow2d(d)) >= 0) {
572
mp_digit mask;
573
574
mask = ((mp_digit)1 << pow) - 1;
575
rem = DIGIT(a, 0) & mask;
576
577
if(q) {
578
mp_copy(a, q);
579
s_mp_div_2d(q, pow);
580
}
581
582
if(r)
583
*r = rem;
584
585
return MP_OKAY;
586
}
587
588
if((res = mp_init_copy(&qp, a)) != MP_OKAY)
589
return res;
590
591
res = s_mp_div_d(&qp, d, &rem);
592
593
if(s_mp_cmp_d(&qp, 0) == 0)
594
SIGN(q) = ZPOS;
595
596
if(r)
597
*r = rem;
598
599
if(q)
600
s_mp_exch(&qp, q);
601
602
mp_clear(&qp);
603
return res;
604
605
} /* end mp_div_d() */
606
607
/* }}} */
608
609
/* {{{ mp_div_2(a, c) */
610
611
/*
612
mp_div_2(a, c)
613
614
Compute c = a / 2, disregarding the remainder.
615
*/
616
617
mp_err mp_div_2(const mp_int *a, mp_int *c)
618
{
619
mp_err res;
620
621
ARGCHK(a != NULL && c != NULL, MP_BADARG);
622
623
if((res = mp_copy(a, c)) != MP_OKAY)
624
return res;
625
626
s_mp_div_2(c);
627
628
return MP_OKAY;
629
630
} /* end mp_div_2() */
631
632
/* }}} */
633
634
/* {{{ mp_expt_d(a, d, b) */
635
636
mp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c)
637
{
638
mp_int s, x;
639
mp_err res;
640
641
ARGCHK(a != NULL && c != NULL, MP_BADARG);
642
643
if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
644
return res;
645
if((res = mp_init_copy(&x, a)) != MP_OKAY)
646
goto X;
647
648
DIGIT(&s, 0) = 1;
649
650
while(d != 0) {
651
if(d & 1) {
652
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
653
goto CLEANUP;
654
}
655
656
d /= 2;
657
658
if((res = s_mp_sqr(&x)) != MP_OKAY)
659
goto CLEANUP;
660
}
661
662
s_mp_exch(&s, c);
663
664
CLEANUP:
665
mp_clear(&x);
666
X:
667
mp_clear(&s);
668
669
return res;
670
671
} /* end mp_expt_d() */
672
673
/* }}} */
674
675
/* }}} */
676
677
/*------------------------------------------------------------------------*/
678
/* {{{ Full arithmetic */
679
680
/* {{{ mp_abs(a, b) */
681
682
/*
683
mp_abs(a, b)
684
685
Compute b = |a|. 'a' and 'b' may be identical.
686
*/
687
688
mp_err mp_abs(const mp_int *a, mp_int *b)
689
{
690
mp_err res;
691
692
ARGCHK(a != NULL && b != NULL, MP_BADARG);
693
694
if((res = mp_copy(a, b)) != MP_OKAY)
695
return res;
696
697
SIGN(b) = ZPOS;
698
699
return MP_OKAY;
700
701
} /* end mp_abs() */
702
703
/* }}} */
704
705
/* {{{ mp_neg(a, b) */
706
707
/*
708
mp_neg(a, b)
709
710
Compute b = -a. 'a' and 'b' may be identical.
711
*/
712
713
mp_err mp_neg(const mp_int *a, mp_int *b)
714
{
715
mp_err res;
716
717
ARGCHK(a != NULL && b != NULL, MP_BADARG);
718
719
if((res = mp_copy(a, b)) != MP_OKAY)
720
return res;
721
722
if(s_mp_cmp_d(b, 0) == MP_EQ)
723
SIGN(b) = ZPOS;
724
else
725
SIGN(b) = (SIGN(b) == NEG) ? ZPOS : NEG;
726
727
return MP_OKAY;
728
729
} /* end mp_neg() */
730
731
/* }}} */
732
733
/* {{{ mp_add(a, b, c) */
734
735
/*
736
mp_add(a, b, c)
737
738
Compute c = a + b. All parameters may be identical.
739
*/
740
741
mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
742
{
743
mp_err res;
744
745
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
746
747
if(SIGN(a) == SIGN(b)) { /* same sign: add values, keep sign */
748
MP_CHECKOK( s_mp_add_3arg(a, b, c) );
749
} else if(s_mp_cmp(a, b) >= 0) { /* different sign: |a| >= |b| */
750
MP_CHECKOK( s_mp_sub_3arg(a, b, c) );
751
} else { /* different sign: |a| < |b| */
752
MP_CHECKOK( s_mp_sub_3arg(b, a, c) );
753
}
754
755
if (s_mp_cmp_d(c, 0) == MP_EQ)
756
SIGN(c) = ZPOS;
757
758
CLEANUP:
759
return res;
760
761
} /* end mp_add() */
762
763
/* }}} */
764
765
/* {{{ mp_sub(a, b, c) */
766
767
/*
768
mp_sub(a, b, c)
769
770
Compute c = a - b. All parameters may be identical.
771
*/
772
773
mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
774
{
775
mp_err res;
776
int magDiff;
777
778
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
779
780
if (a == b) {
781
mp_zero(c);
782
return MP_OKAY;
783
}
784
785
if (MP_SIGN(a) != MP_SIGN(b)) {
786
MP_CHECKOK( s_mp_add_3arg(a, b, c) );
787
} else if (!(magDiff = s_mp_cmp(a, b))) {
788
mp_zero(c);
789
res = MP_OKAY;
790
} else if (magDiff > 0) {
791
MP_CHECKOK( s_mp_sub_3arg(a, b, c) );
792
} else {
793
MP_CHECKOK( s_mp_sub_3arg(b, a, c) );
794
MP_SIGN(c) = !MP_SIGN(a);
795
}
796
797
if (s_mp_cmp_d(c, 0) == MP_EQ)
798
MP_SIGN(c) = MP_ZPOS;
799
800
CLEANUP:
801
return res;
802
803
} /* end mp_sub() */
804
805
/* }}} */
806
807
/* {{{ mp_mul(a, b, c) */
808
809
/*
810
mp_mul(a, b, c)
811
812
Compute c = a * b. All parameters may be identical.
813
*/
814
mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int * c)
815
{
816
mp_digit *pb;
817
mp_int tmp;
818
mp_err res;
819
mp_size ib;
820
mp_size useda, usedb;
821
822
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
823
824
if (a == c) {
825
if ((res = mp_init_copy(&tmp, a)) != MP_OKAY)
826
return res;
827
if (a == b)
828
b = &tmp;
829
a = &tmp;
830
} else if (b == c) {
831
if ((res = mp_init_copy(&tmp, b)) != MP_OKAY)
832
return res;
833
b = &tmp;
834
} else {
835
MP_DIGITS(&tmp) = 0;
836
}
837
838
if (MP_USED(a) < MP_USED(b)) {
839
const mp_int *xch = b; /* switch a and b, to do fewer outer loops */
840
b = a;
841
a = xch;
842
}
843
844
MP_USED(c) = 1; MP_DIGIT(c, 0) = 0;
845
if((res = s_mp_pad(c, USED(a) + USED(b))) != MP_OKAY)
846
goto CLEANUP;
847
848
#ifdef NSS_USE_COMBA
849
if ((MP_USED(a) == MP_USED(b)) && IS_POWER_OF_2(MP_USED(b))) {
850
if (MP_USED(a) == 4) {
851
s_mp_mul_comba_4(a, b, c);
852
goto CLEANUP;
853
}
854
if (MP_USED(a) == 8) {
855
s_mp_mul_comba_8(a, b, c);
856
goto CLEANUP;
857
}
858
if (MP_USED(a) == 16) {
859
s_mp_mul_comba_16(a, b, c);
860
goto CLEANUP;
861
}
862
if (MP_USED(a) == 32) {
863
s_mp_mul_comba_32(a, b, c);
864
goto CLEANUP;
865
}
866
}
867
#endif
868
869
pb = MP_DIGITS(b);
870
s_mpv_mul_d(MP_DIGITS(a), MP_USED(a), *pb++, MP_DIGITS(c));
871
872
/* Outer loop: Digits of b */
873
useda = MP_USED(a);
874
usedb = MP_USED(b);
875
for (ib = 1; ib < usedb; ib++) {
876
mp_digit b_i = *pb++;
877
878
/* Inner product: Digits of a */
879
if (b_i)
880
s_mpv_mul_d_add(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib);
881
else
882
MP_DIGIT(c, ib + useda) = b_i;
883
}
884
885
s_mp_clamp(c);
886
887
if(SIGN(a) == SIGN(b) || s_mp_cmp_d(c, 0) == MP_EQ)
888
SIGN(c) = ZPOS;
889
else
890
SIGN(c) = NEG;
891
892
CLEANUP:
893
mp_clear(&tmp);
894
return res;
895
} /* end mp_mul() */
896
897
/* }}} */
898
899
/* {{{ mp_sqr(a, sqr) */
900
901
#if MP_SQUARE
902
/*
903
Computes the square of a. This can be done more
904
efficiently than a general multiplication, because many of the
905
computation steps are redundant when squaring. The inner product
906
step is a bit more complicated, but we save a fair number of
907
iterations of the multiplication loop.
908
*/
909
910
/* sqr = a^2; Caller provides both a and tmp; */
911
mp_err mp_sqr(const mp_int *a, mp_int *sqr)
912
{
913
mp_digit *pa;
914
mp_digit d;
915
mp_err res;
916
mp_size ix;
917
mp_int tmp;
918
int count;
919
920
ARGCHK(a != NULL && sqr != NULL, MP_BADARG);
921
922
if (a == sqr) {
923
if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
924
return res;
925
a = &tmp;
926
} else {
927
DIGITS(&tmp) = 0;
928
res = MP_OKAY;
929
}
930
931
ix = 2 * MP_USED(a);
932
if (ix > MP_ALLOC(sqr)) {
933
MP_USED(sqr) = 1;
934
MP_CHECKOK( s_mp_grow(sqr, ix) );
935
}
936
MP_USED(sqr) = ix;
937
MP_DIGIT(sqr, 0) = 0;
938
939
#ifdef NSS_USE_COMBA
940
if (IS_POWER_OF_2(MP_USED(a))) {
941
if (MP_USED(a) == 4) {
942
s_mp_sqr_comba_4(a, sqr);
943
goto CLEANUP;
944
}
945
if (MP_USED(a) == 8) {
946
s_mp_sqr_comba_8(a, sqr);
947
goto CLEANUP;
948
}
949
if (MP_USED(a) == 16) {
950
s_mp_sqr_comba_16(a, sqr);
951
goto CLEANUP;
952
}
953
if (MP_USED(a) == 32) {
954
s_mp_sqr_comba_32(a, sqr);
955
goto CLEANUP;
956
}
957
}
958
#endif
959
960
pa = MP_DIGITS(a);
961
count = MP_USED(a) - 1;
962
if (count > 0) {
963
d = *pa++;
964
s_mpv_mul_d(pa, count, d, MP_DIGITS(sqr) + 1);
965
for (ix = 3; --count > 0; ix += 2) {
966
d = *pa++;
967
s_mpv_mul_d_add(pa, count, d, MP_DIGITS(sqr) + ix);
968
} /* for(ix ...) */
969
MP_DIGIT(sqr, MP_USED(sqr)-1) = 0; /* above loop stopped short of this. */
970
971
/* now sqr *= 2 */
972
s_mp_mul_2(sqr);
973
} else {
974
MP_DIGIT(sqr, 1) = 0;
975
}
976
977
/* now add the squares of the digits of a to sqr. */
978
s_mpv_sqr_add_prop(MP_DIGITS(a), MP_USED(a), MP_DIGITS(sqr));
979
980
SIGN(sqr) = ZPOS;
981
s_mp_clamp(sqr);
982
983
CLEANUP:
984
mp_clear(&tmp);
985
return res;
986
987
} /* end mp_sqr() */
988
#endif
989
990
/* }}} */
991
992
/* {{{ mp_div(a, b, q, r) */
993
994
/*
995
mp_div(a, b, q, r)
996
997
Compute q = a / b and r = a mod b. Input parameters may be re-used
998
as output parameters. If q or r is NULL, that portion of the
999
computation will be discarded (although it will still be computed)
1000
*/
1001
mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r)
1002
{
1003
mp_err res;
1004
mp_int *pQ, *pR;
1005
mp_int qtmp, rtmp, btmp;
1006
int cmp;
1007
mp_sign signA;
1008
mp_sign signB;
1009
1010
ARGCHK(a != NULL && b != NULL, MP_BADARG);
1011
1012
signA = MP_SIGN(a);
1013
signB = MP_SIGN(b);
1014
1015
if(mp_cmp_z(b) == MP_EQ)
1016
return MP_RANGE;
1017
1018
DIGITS(&qtmp) = 0;
1019
DIGITS(&rtmp) = 0;
1020
DIGITS(&btmp) = 0;
1021
1022
/* Set up some temporaries... */
1023
if (!r || r == a || r == b) {
1024
MP_CHECKOK( mp_init_copy(&rtmp, a) );
1025
pR = &rtmp;
1026
} else {
1027
MP_CHECKOK( mp_copy(a, r) );
1028
pR = r;
1029
}
1030
1031
if (!q || q == a || q == b) {
1032
MP_CHECKOK( mp_init_size(&qtmp, MP_USED(a), FLAG(a)) );
1033
pQ = &qtmp;
1034
} else {
1035
MP_CHECKOK( s_mp_pad(q, MP_USED(a)) );
1036
pQ = q;
1037
mp_zero(pQ);
1038
}
1039
1040
/*
1041
If |a| <= |b|, we can compute the solution without division;
1042
otherwise, we actually do the work required.
1043
*/
1044
if ((cmp = s_mp_cmp(a, b)) <= 0) {
1045
if (cmp) {
1046
/* r was set to a above. */
1047
mp_zero(pQ);
1048
} else {
1049
mp_set(pQ, 1);
1050
mp_zero(pR);
1051
}
1052
} else {
1053
MP_CHECKOK( mp_init_copy(&btmp, b) );
1054
MP_CHECKOK( s_mp_div(pR, &btmp, pQ) );
1055
}
1056
1057
/* Compute the signs for the output */
1058
MP_SIGN(pR) = signA; /* Sr = Sa */
1059
/* Sq = ZPOS if Sa == Sb */ /* Sq = NEG if Sa != Sb */
1060
MP_SIGN(pQ) = (signA == signB) ? ZPOS : NEG;
1061
1062
if(s_mp_cmp_d(pQ, 0) == MP_EQ)
1063
SIGN(pQ) = ZPOS;
1064
if(s_mp_cmp_d(pR, 0) == MP_EQ)
1065
SIGN(pR) = ZPOS;
1066
1067
/* Copy output, if it is needed */
1068
if(q && q != pQ)
1069
s_mp_exch(pQ, q);
1070
1071
if(r && r != pR)
1072
s_mp_exch(pR, r);
1073
1074
CLEANUP:
1075
mp_clear(&btmp);
1076
mp_clear(&rtmp);
1077
mp_clear(&qtmp);
1078
1079
return res;
1080
1081
} /* end mp_div() */
1082
1083
/* }}} */
1084
1085
/* {{{ mp_div_2d(a, d, q, r) */
1086
1087
mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r)
1088
{
1089
mp_err res;
1090
1091
ARGCHK(a != NULL, MP_BADARG);
1092
1093
if(q) {
1094
if((res = mp_copy(a, q)) != MP_OKAY)
1095
return res;
1096
}
1097
if(r) {
1098
if((res = mp_copy(a, r)) != MP_OKAY)
1099
return res;
1100
}
1101
if(q) {
1102
s_mp_div_2d(q, d);
1103
}
1104
if(r) {
1105
s_mp_mod_2d(r, d);
1106
}
1107
1108
return MP_OKAY;
1109
1110
} /* end mp_div_2d() */
1111
1112
/* }}} */
1113
1114
/* {{{ mp_expt(a, b, c) */
1115
1116
/*
1117
mp_expt(a, b, c)
1118
1119
Compute c = a ** b, that is, raise a to the b power. Uses a
1120
standard iterative square-and-multiply technique.
1121
*/
1122
1123
mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
1124
{
1125
mp_int s, x;
1126
mp_err res;
1127
mp_digit d;
1128
unsigned int dig, bit;
1129
1130
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1131
1132
if(mp_cmp_z(b) < 0)
1133
return MP_RANGE;
1134
1135
if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
1136
return res;
1137
1138
mp_set(&s, 1);
1139
1140
if((res = mp_init_copy(&x, a)) != MP_OKAY)
1141
goto X;
1142
1143
/* Loop over low-order digits in ascending order */
1144
for(dig = 0; dig < (USED(b) - 1); dig++) {
1145
d = DIGIT(b, dig);
1146
1147
/* Loop over bits of each non-maximal digit */
1148
for(bit = 0; bit < DIGIT_BIT; bit++) {
1149
if(d & 1) {
1150
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1151
goto CLEANUP;
1152
}
1153
1154
d >>= 1;
1155
1156
if((res = s_mp_sqr(&x)) != MP_OKAY)
1157
goto CLEANUP;
1158
}
1159
}
1160
1161
/* Consider now the last digit... */
1162
d = DIGIT(b, dig);
1163
1164
while(d) {
1165
if(d & 1) {
1166
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1167
goto CLEANUP;
1168
}
1169
1170
d >>= 1;
1171
1172
if((res = s_mp_sqr(&x)) != MP_OKAY)
1173
goto CLEANUP;
1174
}
1175
1176
if(mp_iseven(b))
1177
SIGN(&s) = SIGN(a);
1178
1179
res = mp_copy(&s, c);
1180
1181
CLEANUP:
1182
mp_clear(&x);
1183
X:
1184
mp_clear(&s);
1185
1186
return res;
1187
1188
} /* end mp_expt() */
1189
1190
/* }}} */
1191
1192
/* {{{ mp_2expt(a, k) */
1193
1194
/* Compute a = 2^k */
1195
1196
mp_err mp_2expt(mp_int *a, mp_digit k)
1197
{
1198
ARGCHK(a != NULL, MP_BADARG);
1199
1200
return s_mp_2expt(a, k);
1201
1202
} /* end mp_2expt() */
1203
1204
/* }}} */
1205
1206
/* {{{ mp_mod(a, m, c) */
1207
1208
/*
1209
mp_mod(a, m, c)
1210
1211
Compute c = a (mod m). Result will always be 0 <= c < m.
1212
*/
1213
1214
mp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c)
1215
{
1216
mp_err res;
1217
int mag;
1218
1219
ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
1220
1221
if(SIGN(m) == NEG)
1222
return MP_RANGE;
1223
1224
/*
1225
If |a| > m, we need to divide to get the remainder and take the
1226
absolute value.
1227
1228
If |a| < m, we don't need to do any division, just copy and adjust
1229
the sign (if a is negative).
1230
1231
If |a| == m, we can simply set the result to zero.
1232
1233
This order is intended to minimize the average path length of the
1234
comparison chain on common workloads -- the most frequent cases are
1235
that |a| != m, so we do those first.
1236
*/
1237
if((mag = s_mp_cmp(a, m)) > 0) {
1238
if((res = mp_div(a, m, NULL, c)) != MP_OKAY)
1239
return res;
1240
1241
if(SIGN(c) == NEG) {
1242
if((res = mp_add(c, m, c)) != MP_OKAY)
1243
return res;
1244
}
1245
1246
} else if(mag < 0) {
1247
if((res = mp_copy(a, c)) != MP_OKAY)
1248
return res;
1249
1250
if(mp_cmp_z(a) < 0) {
1251
if((res = mp_add(c, m, c)) != MP_OKAY)
1252
return res;
1253
1254
}
1255
1256
} else {
1257
mp_zero(c);
1258
1259
}
1260
1261
return MP_OKAY;
1262
1263
} /* end mp_mod() */
1264
1265
/* }}} */
1266
1267
/* {{{ mp_mod_d(a, d, c) */
1268
1269
/*
1270
mp_mod_d(a, d, c)
1271
1272
Compute c = a (mod d). Result will always be 0 <= c < d
1273
*/
1274
mp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c)
1275
{
1276
mp_err res;
1277
mp_digit rem;
1278
1279
ARGCHK(a != NULL && c != NULL, MP_BADARG);
1280
1281
if(s_mp_cmp_d(a, d) > 0) {
1282
if((res = mp_div_d(a, d, NULL, &rem)) != MP_OKAY)
1283
return res;
1284
1285
} else {
1286
if(SIGN(a) == NEG)
1287
rem = d - DIGIT(a, 0);
1288
else
1289
rem = DIGIT(a, 0);
1290
}
1291
1292
if(c)
1293
*c = rem;
1294
1295
return MP_OKAY;
1296
1297
} /* end mp_mod_d() */
1298
1299
/* }}} */
1300
1301
/* {{{ mp_sqrt(a, b) */
1302
1303
/*
1304
mp_sqrt(a, b)
1305
1306
Compute the integer square root of a, and store the result in b.
1307
Uses an integer-arithmetic version of Newton's iterative linear
1308
approximation technique to determine this value; the result has the
1309
following two properties:
1310
1311
b^2 <= a
1312
(b+1)^2 >= a
1313
1314
It is a range error to pass a negative value.
1315
*/
1316
mp_err mp_sqrt(const mp_int *a, mp_int *b)
1317
{
1318
mp_int x, t;
1319
mp_err res;
1320
mp_size used;
1321
1322
ARGCHK(a != NULL && b != NULL, MP_BADARG);
1323
1324
/* Cannot take square root of a negative value */
1325
if(SIGN(a) == NEG)
1326
return MP_RANGE;
1327
1328
/* Special cases for zero and one, trivial */
1329
if(mp_cmp_d(a, 1) <= 0)
1330
return mp_copy(a, b);
1331
1332
/* Initialize the temporaries we'll use below */
1333
if((res = mp_init_size(&t, USED(a), FLAG(a))) != MP_OKAY)
1334
return res;
1335
1336
/* Compute an initial guess for the iteration as a itself */
1337
if((res = mp_init_copy(&x, a)) != MP_OKAY)
1338
goto X;
1339
1340
used = MP_USED(&x);
1341
if (used > 1) {
1342
s_mp_rshd(&x, used / 2);
1343
}
1344
1345
for(;;) {
1346
/* t = (x * x) - a */
1347
mp_copy(&x, &t); /* can't fail, t is big enough for original x */
1348
if((res = mp_sqr(&t, &t)) != MP_OKAY ||
1349
(res = mp_sub(&t, a, &t)) != MP_OKAY)
1350
goto CLEANUP;
1351
1352
/* t = t / 2x */
1353
s_mp_mul_2(&x);
1354
if((res = mp_div(&t, &x, &t, NULL)) != MP_OKAY)
1355
goto CLEANUP;
1356
s_mp_div_2(&x);
1357
1358
/* Terminate the loop, if the quotient is zero */
1359
if(mp_cmp_z(&t) == MP_EQ)
1360
break;
1361
1362
/* x = x - t */
1363
if((res = mp_sub(&x, &t, &x)) != MP_OKAY)
1364
goto CLEANUP;
1365
1366
}
1367
1368
/* Copy result to output parameter */
1369
mp_sub_d(&x, 1, &x);
1370
s_mp_exch(&x, b);
1371
1372
CLEANUP:
1373
mp_clear(&x);
1374
X:
1375
mp_clear(&t);
1376
1377
return res;
1378
1379
} /* end mp_sqrt() */
1380
1381
/* }}} */
1382
1383
/* }}} */
1384
1385
/*------------------------------------------------------------------------*/
1386
/* {{{ Modular arithmetic */
1387
1388
#if MP_MODARITH
1389
/* {{{ mp_addmod(a, b, m, c) */
1390
1391
/*
1392
mp_addmod(a, b, m, c)
1393
1394
Compute c = (a + b) mod m
1395
*/
1396
1397
mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
1398
{
1399
mp_err res;
1400
1401
ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
1402
1403
if((res = mp_add(a, b, c)) != MP_OKAY)
1404
return res;
1405
if((res = mp_mod(c, m, c)) != MP_OKAY)
1406
return res;
1407
1408
return MP_OKAY;
1409
1410
}
1411
1412
/* }}} */
1413
1414
/* {{{ mp_submod(a, b, m, c) */
1415
1416
/*
1417
mp_submod(a, b, m, c)
1418
1419
Compute c = (a - b) mod m
1420
*/
1421
1422
mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
1423
{
1424
mp_err res;
1425
1426
ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
1427
1428
if((res = mp_sub(a, b, c)) != MP_OKAY)
1429
return res;
1430
if((res = mp_mod(c, m, c)) != MP_OKAY)
1431
return res;
1432
1433
return MP_OKAY;
1434
1435
}
1436
1437
/* }}} */
1438
1439
/* {{{ mp_mulmod(a, b, m, c) */
1440
1441
/*
1442
mp_mulmod(a, b, m, c)
1443
1444
Compute c = (a * b) mod m
1445
*/
1446
1447
mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
1448
{
1449
mp_err res;
1450
1451
ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
1452
1453
if((res = mp_mul(a, b, c)) != MP_OKAY)
1454
return res;
1455
if((res = mp_mod(c, m, c)) != MP_OKAY)
1456
return res;
1457
1458
return MP_OKAY;
1459
1460
}
1461
1462
/* }}} */
1463
1464
/* {{{ mp_sqrmod(a, m, c) */
1465
1466
#if MP_SQUARE
1467
mp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c)
1468
{
1469
mp_err res;
1470
1471
ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
1472
1473
if((res = mp_sqr(a, c)) != MP_OKAY)
1474
return res;
1475
if((res = mp_mod(c, m, c)) != MP_OKAY)
1476
return res;
1477
1478
return MP_OKAY;
1479
1480
} /* end mp_sqrmod() */
1481
#endif
1482
1483
/* }}} */
1484
1485
/* {{{ s_mp_exptmod(a, b, m, c) */
1486
1487
/*
1488
s_mp_exptmod(a, b, m, c)
1489
1490
Compute c = (a ** b) mod m. Uses a standard square-and-multiply
1491
method with modular reductions at each step. (This is basically the
1492
same code as mp_expt(), except for the addition of the reductions)
1493
1494
The modular reductions are done using Barrett's algorithm (see
1495
s_mp_reduce() below for details)
1496
*/
1497
1498
mp_err s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
1499
{
1500
mp_int s, x, mu;
1501
mp_err res;
1502
mp_digit d;
1503
unsigned int dig, bit;
1504
1505
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1506
1507
if(mp_cmp_z(b) < 0 || mp_cmp_z(m) <= 0)
1508
return MP_RANGE;
1509
1510
if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
1511
return res;
1512
if((res = mp_init_copy(&x, a)) != MP_OKAY ||
1513
(res = mp_mod(&x, m, &x)) != MP_OKAY)
1514
goto X;
1515
if((res = mp_init(&mu, FLAG(a))) != MP_OKAY)
1516
goto MU;
1517
1518
mp_set(&s, 1);
1519
1520
/* mu = b^2k / m */
1521
s_mp_add_d(&mu, 1);
1522
s_mp_lshd(&mu, 2 * USED(m));
1523
if((res = mp_div(&mu, m, &mu, NULL)) != MP_OKAY)
1524
goto CLEANUP;
1525
1526
/* Loop over digits of b in ascending order, except highest order */
1527
for(dig = 0; dig < (USED(b) - 1); dig++) {
1528
d = DIGIT(b, dig);
1529
1530
/* Loop over the bits of the lower-order digits */
1531
for(bit = 0; bit < DIGIT_BIT; bit++) {
1532
if(d & 1) {
1533
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1534
goto CLEANUP;
1535
if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
1536
goto CLEANUP;
1537
}
1538
1539
d >>= 1;
1540
1541
if((res = s_mp_sqr(&x)) != MP_OKAY)
1542
goto CLEANUP;
1543
if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
1544
goto CLEANUP;
1545
}
1546
}
1547
1548
/* Now do the last digit... */
1549
d = DIGIT(b, dig);
1550
1551
while(d) {
1552
if(d & 1) {
1553
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1554
goto CLEANUP;
1555
if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
1556
goto CLEANUP;
1557
}
1558
1559
d >>= 1;
1560
1561
if((res = s_mp_sqr(&x)) != MP_OKAY)
1562
goto CLEANUP;
1563
if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
1564
goto CLEANUP;
1565
}
1566
1567
s_mp_exch(&s, c);
1568
1569
CLEANUP:
1570
mp_clear(&mu);
1571
MU:
1572
mp_clear(&x);
1573
X:
1574
mp_clear(&s);
1575
1576
return res;
1577
1578
} /* end s_mp_exptmod() */
1579
1580
/* }}} */
1581
1582
/* {{{ mp_exptmod_d(a, d, m, c) */
1583
1584
mp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c)
1585
{
1586
mp_int s, x;
1587
mp_err res;
1588
1589
ARGCHK(a != NULL && c != NULL, MP_BADARG);
1590
1591
if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
1592
return res;
1593
if((res = mp_init_copy(&x, a)) != MP_OKAY)
1594
goto X;
1595
1596
mp_set(&s, 1);
1597
1598
while(d != 0) {
1599
if(d & 1) {
1600
if((res = s_mp_mul(&s, &x)) != MP_OKAY ||
1601
(res = mp_mod(&s, m, &s)) != MP_OKAY)
1602
goto CLEANUP;
1603
}
1604
1605
d /= 2;
1606
1607
if((res = s_mp_sqr(&x)) != MP_OKAY ||
1608
(res = mp_mod(&x, m, &x)) != MP_OKAY)
1609
goto CLEANUP;
1610
}
1611
1612
s_mp_exch(&s, c);
1613
1614
CLEANUP:
1615
mp_clear(&x);
1616
X:
1617
mp_clear(&s);
1618
1619
return res;
1620
1621
} /* end mp_exptmod_d() */
1622
1623
/* }}} */
1624
#endif /* if MP_MODARITH */
1625
1626
/* }}} */
1627
1628
/*------------------------------------------------------------------------*/
1629
/* {{{ Comparison functions */
1630
1631
/* {{{ mp_cmp_z(a) */
1632
1633
/*
1634
mp_cmp_z(a)
1635
1636
Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0.
1637
*/
1638
1639
int mp_cmp_z(const mp_int *a)
1640
{
1641
if(SIGN(a) == NEG)
1642
return MP_LT;
1643
else if(USED(a) == 1 && DIGIT(a, 0) == 0)
1644
return MP_EQ;
1645
else
1646
return MP_GT;
1647
1648
} /* end mp_cmp_z() */
1649
1650
/* }}} */
1651
1652
/* {{{ mp_cmp_d(a, d) */
1653
1654
/*
1655
mp_cmp_d(a, d)
1656
1657
Compare a <=> d. Returns <0 if a<d, 0 if a=d, >0 if a>d
1658
*/
1659
1660
int mp_cmp_d(const mp_int *a, mp_digit d)
1661
{
1662
ARGCHK(a != NULL, MP_EQ);
1663
1664
if(SIGN(a) == NEG)
1665
return MP_LT;
1666
1667
return s_mp_cmp_d(a, d);
1668
1669
} /* end mp_cmp_d() */
1670
1671
/* }}} */
1672
1673
/* {{{ mp_cmp(a, b) */
1674
1675
int mp_cmp(const mp_int *a, const mp_int *b)
1676
{
1677
ARGCHK(a != NULL && b != NULL, MP_EQ);
1678
1679
if(SIGN(a) == SIGN(b)) {
1680
int mag;
1681
1682
if((mag = s_mp_cmp(a, b)) == MP_EQ)
1683
return MP_EQ;
1684
1685
if(SIGN(a) == ZPOS)
1686
return mag;
1687
else
1688
return -mag;
1689
1690
} else if(SIGN(a) == ZPOS) {
1691
return MP_GT;
1692
} else {
1693
return MP_LT;
1694
}
1695
1696
} /* end mp_cmp() */
1697
1698
/* }}} */
1699
1700
/* {{{ mp_cmp_mag(a, b) */
1701
1702
/*
1703
mp_cmp_mag(a, b)
1704
1705
Compares |a| <=> |b|, and returns an appropriate comparison result
1706
*/
1707
1708
int mp_cmp_mag(mp_int *a, mp_int *b)
1709
{
1710
ARGCHK(a != NULL && b != NULL, MP_EQ);
1711
1712
return s_mp_cmp(a, b);
1713
1714
} /* end mp_cmp_mag() */
1715
1716
/* }}} */
1717
1718
/* {{{ mp_cmp_int(a, z, kmflag) */
1719
1720
/*
1721
This just converts z to an mp_int, and uses the existing comparison
1722
routines. This is sort of inefficient, but it's not clear to me how
1723
frequently this wil get used anyway. For small positive constants,
1724
you can always use mp_cmp_d(), and for zero, there is mp_cmp_z().
1725
*/
1726
int mp_cmp_int(const mp_int *a, long z, int kmflag)
1727
{
1728
mp_int tmp;
1729
int out;
1730
1731
ARGCHK(a != NULL, MP_EQ);
1732
1733
mp_init(&tmp, kmflag); mp_set_int(&tmp, z);
1734
out = mp_cmp(a, &tmp);
1735
mp_clear(&tmp);
1736
1737
return out;
1738
1739
} /* end mp_cmp_int() */
1740
1741
/* }}} */
1742
1743
/* {{{ mp_isodd(a) */
1744
1745
/*
1746
mp_isodd(a)
1747
1748
Returns a true (non-zero) value if a is odd, false (zero) otherwise.
1749
*/
1750
int mp_isodd(const mp_int *a)
1751
{
1752
ARGCHK(a != NULL, 0);
1753
1754
return (int)(DIGIT(a, 0) & 1);
1755
1756
} /* end mp_isodd() */
1757
1758
/* }}} */
1759
1760
/* {{{ mp_iseven(a) */
1761
1762
int mp_iseven(const mp_int *a)
1763
{
1764
return !mp_isodd(a);
1765
1766
} /* end mp_iseven() */
1767
1768
/* }}} */
1769
1770
/* }}} */
1771
1772
/*------------------------------------------------------------------------*/
1773
/* {{{ Number theoretic functions */
1774
1775
#if MP_NUMTH
1776
/* {{{ mp_gcd(a, b, c) */
1777
1778
/*
1779
Like the old mp_gcd() function, except computes the GCD using the
1780
binary algorithm due to Josef Stein in 1961 (via Knuth).
1781
*/
1782
mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
1783
{
1784
mp_err res;
1785
mp_int u, v, t;
1786
mp_size k = 0;
1787
1788
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1789
1790
if(mp_cmp_z(a) == MP_EQ && mp_cmp_z(b) == MP_EQ)
1791
return MP_RANGE;
1792
if(mp_cmp_z(a) == MP_EQ) {
1793
return mp_copy(b, c);
1794
} else if(mp_cmp_z(b) == MP_EQ) {
1795
return mp_copy(a, c);
1796
}
1797
1798
if((res = mp_init(&t, FLAG(a))) != MP_OKAY)
1799
return res;
1800
if((res = mp_init_copy(&u, a)) != MP_OKAY)
1801
goto U;
1802
if((res = mp_init_copy(&v, b)) != MP_OKAY)
1803
goto V;
1804
1805
SIGN(&u) = ZPOS;
1806
SIGN(&v) = ZPOS;
1807
1808
/* Divide out common factors of 2 until at least 1 of a, b is even */
1809
while(mp_iseven(&u) && mp_iseven(&v)) {
1810
s_mp_div_2(&u);
1811
s_mp_div_2(&v);
1812
++k;
1813
}
1814
1815
/* Initialize t */
1816
if(mp_isodd(&u)) {
1817
if((res = mp_copy(&v, &t)) != MP_OKAY)
1818
goto CLEANUP;
1819
1820
/* t = -v */
1821
if(SIGN(&v) == ZPOS)
1822
SIGN(&t) = NEG;
1823
else
1824
SIGN(&t) = ZPOS;
1825
1826
} else {
1827
if((res = mp_copy(&u, &t)) != MP_OKAY)
1828
goto CLEANUP;
1829
1830
}
1831
1832
for(;;) {
1833
while(mp_iseven(&t)) {
1834
s_mp_div_2(&t);
1835
}
1836
1837
if(mp_cmp_z(&t) == MP_GT) {
1838
if((res = mp_copy(&t, &u)) != MP_OKAY)
1839
goto CLEANUP;
1840
1841
} else {
1842
if((res = mp_copy(&t, &v)) != MP_OKAY)
1843
goto CLEANUP;
1844
1845
/* v = -t */
1846
if(SIGN(&t) == ZPOS)
1847
SIGN(&v) = NEG;
1848
else
1849
SIGN(&v) = ZPOS;
1850
}
1851
1852
if((res = mp_sub(&u, &v, &t)) != MP_OKAY)
1853
goto CLEANUP;
1854
1855
if(s_mp_cmp_d(&t, 0) == MP_EQ)
1856
break;
1857
}
1858
1859
s_mp_2expt(&v, k); /* v = 2^k */
1860
res = mp_mul(&u, &v, c); /* c = u * v */
1861
1862
CLEANUP:
1863
mp_clear(&v);
1864
V:
1865
mp_clear(&u);
1866
U:
1867
mp_clear(&t);
1868
1869
return res;
1870
1871
} /* end mp_gcd() */
1872
1873
/* }}} */
1874
1875
/* {{{ mp_lcm(a, b, c) */
1876
1877
/* We compute the least common multiple using the rule:
1878
1879
ab = [a, b](a, b)
1880
1881
... by computing the product, and dividing out the gcd.
1882
*/
1883
1884
mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c)
1885
{
1886
mp_int gcd, prod;
1887
mp_err res;
1888
1889
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1890
1891
/* Set up temporaries */
1892
if((res = mp_init(&gcd, FLAG(a))) != MP_OKAY)
1893
return res;
1894
if((res = mp_init(&prod, FLAG(a))) != MP_OKAY)
1895
goto GCD;
1896
1897
if((res = mp_mul(a, b, &prod)) != MP_OKAY)
1898
goto CLEANUP;
1899
if((res = mp_gcd(a, b, &gcd)) != MP_OKAY)
1900
goto CLEANUP;
1901
1902
res = mp_div(&prod, &gcd, c, NULL);
1903
1904
CLEANUP:
1905
mp_clear(&prod);
1906
GCD:
1907
mp_clear(&gcd);
1908
1909
return res;
1910
1911
} /* end mp_lcm() */
1912
1913
/* }}} */
1914
1915
/* {{{ mp_xgcd(a, b, g, x, y) */
1916
1917
/*
1918
mp_xgcd(a, b, g, x, y)
1919
1920
Compute g = (a, b) and values x and y satisfying Bezout's identity
1921
(that is, ax + by = g). This uses the binary extended GCD algorithm
1922
based on the Stein algorithm used for mp_gcd()
1923
See algorithm 14.61 in Handbook of Applied Cryptogrpahy.
1924
*/
1925
1926
mp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y)
1927
{
1928
mp_int gx, xc, yc, u, v, A, B, C, D;
1929
mp_int *clean[9];
1930
mp_err res;
1931
int last = -1;
1932
1933
if(mp_cmp_z(b) == 0)
1934
return MP_RANGE;
1935
1936
/* Initialize all these variables we need */
1937
MP_CHECKOK( mp_init(&u, FLAG(a)) );
1938
clean[++last] = &u;
1939
MP_CHECKOK( mp_init(&v, FLAG(a)) );
1940
clean[++last] = &v;
1941
MP_CHECKOK( mp_init(&gx, FLAG(a)) );
1942
clean[++last] = &gx;
1943
MP_CHECKOK( mp_init(&A, FLAG(a)) );
1944
clean[++last] = &A;
1945
MP_CHECKOK( mp_init(&B, FLAG(a)) );
1946
clean[++last] = &B;
1947
MP_CHECKOK( mp_init(&C, FLAG(a)) );
1948
clean[++last] = &C;
1949
MP_CHECKOK( mp_init(&D, FLAG(a)) );
1950
clean[++last] = &D;
1951
MP_CHECKOK( mp_init_copy(&xc, a) );
1952
clean[++last] = &xc;
1953
mp_abs(&xc, &xc);
1954
MP_CHECKOK( mp_init_copy(&yc, b) );
1955
clean[++last] = &yc;
1956
mp_abs(&yc, &yc);
1957
1958
mp_set(&gx, 1);
1959
1960
/* Divide by two until at least one of them is odd */
1961
while(mp_iseven(&xc) && mp_iseven(&yc)) {
1962
mp_size nx = mp_trailing_zeros(&xc);
1963
mp_size ny = mp_trailing_zeros(&yc);
1964
mp_size n = MP_MIN(nx, ny);
1965
s_mp_div_2d(&xc,n);
1966
s_mp_div_2d(&yc,n);
1967
MP_CHECKOK( s_mp_mul_2d(&gx,n) );
1968
}
1969
1970
mp_copy(&xc, &u);
1971
mp_copy(&yc, &v);
1972
mp_set(&A, 1); mp_set(&D, 1);
1973
1974
/* Loop through binary GCD algorithm */
1975
do {
1976
while(mp_iseven(&u)) {
1977
s_mp_div_2(&u);
1978
1979
if(mp_iseven(&A) && mp_iseven(&B)) {
1980
s_mp_div_2(&A); s_mp_div_2(&B);
1981
} else {
1982
MP_CHECKOK( mp_add(&A, &yc, &A) );
1983
s_mp_div_2(&A);
1984
MP_CHECKOK( mp_sub(&B, &xc, &B) );
1985
s_mp_div_2(&B);
1986
}
1987
}
1988
1989
while(mp_iseven(&v)) {
1990
s_mp_div_2(&v);
1991
1992
if(mp_iseven(&C) && mp_iseven(&D)) {
1993
s_mp_div_2(&C); s_mp_div_2(&D);
1994
} else {
1995
MP_CHECKOK( mp_add(&C, &yc, &C) );
1996
s_mp_div_2(&C);
1997
MP_CHECKOK( mp_sub(&D, &xc, &D) );
1998
s_mp_div_2(&D);
1999
}
2000
}
2001
2002
if(mp_cmp(&u, &v) >= 0) {
2003
MP_CHECKOK( mp_sub(&u, &v, &u) );
2004
MP_CHECKOK( mp_sub(&A, &C, &A) );
2005
MP_CHECKOK( mp_sub(&B, &D, &B) );
2006
} else {
2007
MP_CHECKOK( mp_sub(&v, &u, &v) );
2008
MP_CHECKOK( mp_sub(&C, &A, &C) );
2009
MP_CHECKOK( mp_sub(&D, &B, &D) );
2010
}
2011
} while (mp_cmp_z(&u) != 0);
2012
2013
/* copy results to output */
2014
if(x)
2015
MP_CHECKOK( mp_copy(&C, x) );
2016
2017
if(y)
2018
MP_CHECKOK( mp_copy(&D, y) );
2019
2020
if(g)
2021
MP_CHECKOK( mp_mul(&gx, &v, g) );
2022
2023
CLEANUP:
2024
while(last >= 0)
2025
mp_clear(clean[last--]);
2026
2027
return res;
2028
2029
} /* end mp_xgcd() */
2030
2031
/* }}} */
2032
2033
mp_size mp_trailing_zeros(const mp_int *mp)
2034
{
2035
mp_digit d;
2036
mp_size n = 0;
2037
unsigned int ix;
2038
2039
if (!mp || !MP_DIGITS(mp) || !mp_cmp_z(mp))
2040
return n;
2041
2042
for (ix = 0; !(d = MP_DIGIT(mp,ix)) && (ix < MP_USED(mp)); ++ix)
2043
n += MP_DIGIT_BIT;
2044
if (!d)
2045
return 0; /* shouldn't happen, but ... */
2046
#if !defined(MP_USE_UINT_DIGIT)
2047
if (!(d & 0xffffffffU)) {
2048
d >>= 32;
2049
n += 32;
2050
}
2051
#endif
2052
if (!(d & 0xffffU)) {
2053
d >>= 16;
2054
n += 16;
2055
}
2056
if (!(d & 0xffU)) {
2057
d >>= 8;
2058
n += 8;
2059
}
2060
if (!(d & 0xfU)) {
2061
d >>= 4;
2062
n += 4;
2063
}
2064
if (!(d & 0x3U)) {
2065
d >>= 2;
2066
n += 2;
2067
}
2068
if (!(d & 0x1U)) {
2069
d >>= 1;
2070
n += 1;
2071
}
2072
#if MP_ARGCHK == 2
2073
assert(0 != (d & 1));
2074
#endif
2075
return n;
2076
}
2077
2078
/* Given a and prime p, computes c and k such that a*c == 2**k (mod p).
2079
** Returns k (positive) or error (negative).
2080
** This technique from the paper "Fast Modular Reciprocals" (unpublished)
2081
** by Richard Schroeppel (a.k.a. Captain Nemo).
2082
*/
2083
mp_err s_mp_almost_inverse(const mp_int *a, const mp_int *p, mp_int *c)
2084
{
2085
mp_err res;
2086
mp_err k = 0;
2087
mp_int d, f, g;
2088
2089
ARGCHK(a && p && c, MP_BADARG);
2090
2091
MP_DIGITS(&d) = 0;
2092
MP_DIGITS(&f) = 0;
2093
MP_DIGITS(&g) = 0;
2094
MP_CHECKOK( mp_init(&d, FLAG(a)) );
2095
MP_CHECKOK( mp_init_copy(&f, a) ); /* f = a */
2096
MP_CHECKOK( mp_init_copy(&g, p) ); /* g = p */
2097
2098
mp_set(c, 1);
2099
mp_zero(&d);
2100
2101
if (mp_cmp_z(&f) == 0) {
2102
res = MP_UNDEF;
2103
} else
2104
for (;;) {
2105
int diff_sign;
2106
while (mp_iseven(&f)) {
2107
mp_size n = mp_trailing_zeros(&f);
2108
if (!n) {
2109
res = MP_UNDEF;
2110
goto CLEANUP;
2111
}
2112
s_mp_div_2d(&f, n);
2113
MP_CHECKOK( s_mp_mul_2d(&d, n) );
2114
k += n;
2115
}
2116
if (mp_cmp_d(&f, 1) == MP_EQ) { /* f == 1 */
2117
res = k;
2118
break;
2119
}
2120
diff_sign = mp_cmp(&f, &g);
2121
if (diff_sign < 0) { /* f < g */
2122
s_mp_exch(&f, &g);
2123
s_mp_exch(c, &d);
2124
} else if (diff_sign == 0) { /* f == g */
2125
res = MP_UNDEF; /* a and p are not relatively prime */
2126
break;
2127
}
2128
if ((MP_DIGIT(&f,0) % 4) == (MP_DIGIT(&g,0) % 4)) {
2129
MP_CHECKOK( mp_sub(&f, &g, &f) ); /* f = f - g */
2130
MP_CHECKOK( mp_sub(c, &d, c) ); /* c = c - d */
2131
} else {
2132
MP_CHECKOK( mp_add(&f, &g, &f) ); /* f = f + g */
2133
MP_CHECKOK( mp_add(c, &d, c) ); /* c = c + d */
2134
}
2135
}
2136
if (res >= 0) {
2137
if (s_mp_cmp(c, p) >= 0) {
2138
MP_CHECKOK( mp_div(c, p, NULL, c));
2139
}
2140
if (MP_SIGN(c) != MP_ZPOS) {
2141
MP_CHECKOK( mp_add(c, p, c) );
2142
}
2143
res = k;
2144
}
2145
2146
CLEANUP:
2147
mp_clear(&d);
2148
mp_clear(&f);
2149
mp_clear(&g);
2150
return res;
2151
}
2152
2153
/* Compute T = (P ** -1) mod MP_RADIX. Also works for 16-bit mp_digits.
2154
** This technique from the paper "Fast Modular Reciprocals" (unpublished)
2155
** by Richard Schroeppel (a.k.a. Captain Nemo).
2156
*/
2157
mp_digit s_mp_invmod_radix(mp_digit P)
2158
{
2159
mp_digit T = P;
2160
T *= 2 - (P * T);
2161
T *= 2 - (P * T);
2162
T *= 2 - (P * T);
2163
T *= 2 - (P * T);
2164
#if !defined(MP_USE_UINT_DIGIT)
2165
T *= 2 - (P * T);
2166
T *= 2 - (P * T);
2167
#endif
2168
return T;
2169
}
2170
2171
/* Given c, k, and prime p, where a*c == 2**k (mod p),
2172
** Compute x = (a ** -1) mod p. This is similar to Montgomery reduction.
2173
** This technique from the paper "Fast Modular Reciprocals" (unpublished)
2174
** by Richard Schroeppel (a.k.a. Captain Nemo).
2175
*/
2176
mp_err s_mp_fixup_reciprocal(const mp_int *c, const mp_int *p, int k, mp_int *x)
2177
{
2178
int k_orig = k;
2179
mp_digit r;
2180
mp_size ix;
2181
mp_err res;
2182
2183
if (mp_cmp_z(c) < 0) { /* c < 0 */
2184
MP_CHECKOK( mp_add(c, p, x) ); /* x = c + p */
2185
} else {
2186
MP_CHECKOK( mp_copy(c, x) ); /* x = c */
2187
}
2188
2189
/* make sure x is large enough */
2190
ix = MP_HOWMANY(k, MP_DIGIT_BIT) + MP_USED(p) + 1;
2191
ix = MP_MAX(ix, MP_USED(x));
2192
MP_CHECKOK( s_mp_pad(x, ix) );
2193
2194
r = 0 - s_mp_invmod_radix(MP_DIGIT(p,0));
2195
2196
for (ix = 0; k > 0; ix++) {
2197
int j = MP_MIN(k, MP_DIGIT_BIT);
2198
mp_digit v = r * MP_DIGIT(x, ix);
2199
if (j < MP_DIGIT_BIT) {
2200
v &= ((mp_digit)1 << j) - 1; /* v = v mod (2 ** j) */
2201
}
2202
s_mp_mul_d_add_offset(p, v, x, ix); /* x += p * v * (RADIX ** ix) */
2203
k -= j;
2204
}
2205
s_mp_clamp(x);
2206
s_mp_div_2d(x, k_orig);
2207
res = MP_OKAY;
2208
2209
CLEANUP:
2210
return res;
2211
}
2212
2213
/* compute mod inverse using Schroeppel's method, only if m is odd */
2214
mp_err s_mp_invmod_odd_m(const mp_int *a, const mp_int *m, mp_int *c)
2215
{
2216
int k;
2217
mp_err res;
2218
mp_int x;
2219
2220
ARGCHK(a && m && c, MP_BADARG);
2221
2222
if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
2223
return MP_RANGE;
2224
if (mp_iseven(m))
2225
return MP_UNDEF;
2226
2227
MP_DIGITS(&x) = 0;
2228
2229
if (a == c) {
2230
if ((res = mp_init_copy(&x, a)) != MP_OKAY)
2231
return res;
2232
if (a == m)
2233
m = &x;
2234
a = &x;
2235
} else if (m == c) {
2236
if ((res = mp_init_copy(&x, m)) != MP_OKAY)
2237
return res;
2238
m = &x;
2239
} else {
2240
MP_DIGITS(&x) = 0;
2241
}
2242
2243
MP_CHECKOK( s_mp_almost_inverse(a, m, c) );
2244
k = res;
2245
MP_CHECKOK( s_mp_fixup_reciprocal(c, m, k, c) );
2246
CLEANUP:
2247
mp_clear(&x);
2248
return res;
2249
}
2250
2251
/* Known good algorithm for computing modular inverse. But slow. */
2252
mp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c)
2253
{
2254
mp_int g, x;
2255
mp_err res;
2256
2257
ARGCHK(a && m && c, MP_BADARG);
2258
2259
if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
2260
return MP_RANGE;
2261
2262
MP_DIGITS(&g) = 0;
2263
MP_DIGITS(&x) = 0;
2264
MP_CHECKOK( mp_init(&x, FLAG(a)) );
2265
MP_CHECKOK( mp_init(&g, FLAG(a)) );
2266
2267
MP_CHECKOK( mp_xgcd(a, m, &g, &x, NULL) );
2268
2269
if (mp_cmp_d(&g, 1) != MP_EQ) {
2270
res = MP_UNDEF;
2271
goto CLEANUP;
2272
}
2273
2274
res = mp_mod(&x, m, c);
2275
SIGN(c) = SIGN(a);
2276
2277
CLEANUP:
2278
mp_clear(&x);
2279
mp_clear(&g);
2280
2281
return res;
2282
}
2283
2284
/* modular inverse where modulus is 2**k. */
2285
/* c = a**-1 mod 2**k */
2286
mp_err s_mp_invmod_2d(const mp_int *a, mp_size k, mp_int *c)
2287
{
2288
mp_err res;
2289
mp_size ix = k + 4;
2290
mp_int t0, t1, val, tmp, two2k;
2291
2292
static const mp_digit d2 = 2;
2293
static const mp_int two = { 0, MP_ZPOS, 1, 1, (mp_digit *)&d2 };
2294
2295
if (mp_iseven(a))
2296
return MP_UNDEF;
2297
if (k <= MP_DIGIT_BIT) {
2298
mp_digit i = s_mp_invmod_radix(MP_DIGIT(a,0));
2299
if (k < MP_DIGIT_BIT)
2300
i &= ((mp_digit)1 << k) - (mp_digit)1;
2301
mp_set(c, i);
2302
return MP_OKAY;
2303
}
2304
MP_DIGITS(&t0) = 0;
2305
MP_DIGITS(&t1) = 0;
2306
MP_DIGITS(&val) = 0;
2307
MP_DIGITS(&tmp) = 0;
2308
MP_DIGITS(&two2k) = 0;
2309
MP_CHECKOK( mp_init_copy(&val, a) );
2310
s_mp_mod_2d(&val, k);
2311
MP_CHECKOK( mp_init_copy(&t0, &val) );
2312
MP_CHECKOK( mp_init_copy(&t1, &t0) );
2313
MP_CHECKOK( mp_init(&tmp, FLAG(a)) );
2314
MP_CHECKOK( mp_init(&two2k, FLAG(a)) );
2315
MP_CHECKOK( s_mp_2expt(&two2k, k) );
2316
do {
2317
MP_CHECKOK( mp_mul(&val, &t1, &tmp) );
2318
MP_CHECKOK( mp_sub(&two, &tmp, &tmp) );
2319
MP_CHECKOK( mp_mul(&t1, &tmp, &t1) );
2320
s_mp_mod_2d(&t1, k);
2321
while (MP_SIGN(&t1) != MP_ZPOS) {
2322
MP_CHECKOK( mp_add(&t1, &two2k, &t1) );
2323
}
2324
if (mp_cmp(&t1, &t0) == MP_EQ)
2325
break;
2326
MP_CHECKOK( mp_copy(&t1, &t0) );
2327
} while (--ix > 0);
2328
if (!ix) {
2329
res = MP_UNDEF;
2330
} else {
2331
mp_exch(c, &t1);
2332
}
2333
2334
CLEANUP:
2335
mp_clear(&t0);
2336
mp_clear(&t1);
2337
mp_clear(&val);
2338
mp_clear(&tmp);
2339
mp_clear(&two2k);
2340
return res;
2341
}
2342
2343
mp_err s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c)
2344
{
2345
mp_err res;
2346
mp_size k;
2347
mp_int oddFactor, evenFactor; /* factors of the modulus */
2348
mp_int oddPart, evenPart; /* parts to combine via CRT. */
2349
mp_int C2, tmp1, tmp2;
2350
2351
/*static const mp_digit d1 = 1; */
2352
/*static const mp_int one = { MP_ZPOS, 1, 1, (mp_digit *)&d1 }; */
2353
2354
if ((res = s_mp_ispow2(m)) >= 0) {
2355
k = res;
2356
return s_mp_invmod_2d(a, k, c);
2357
}
2358
MP_DIGITS(&oddFactor) = 0;
2359
MP_DIGITS(&evenFactor) = 0;
2360
MP_DIGITS(&oddPart) = 0;
2361
MP_DIGITS(&evenPart) = 0;
2362
MP_DIGITS(&C2) = 0;
2363
MP_DIGITS(&tmp1) = 0;
2364
MP_DIGITS(&tmp2) = 0;
2365
2366
MP_CHECKOK( mp_init_copy(&oddFactor, m) ); /* oddFactor = m */
2367
MP_CHECKOK( mp_init(&evenFactor, FLAG(m)) );
2368
MP_CHECKOK( mp_init(&oddPart, FLAG(m)) );
2369
MP_CHECKOK( mp_init(&evenPart, FLAG(m)) );
2370
MP_CHECKOK( mp_init(&C2, FLAG(m)) );
2371
MP_CHECKOK( mp_init(&tmp1, FLAG(m)) );
2372
MP_CHECKOK( mp_init(&tmp2, FLAG(m)) );
2373
2374
k = mp_trailing_zeros(m);
2375
s_mp_div_2d(&oddFactor, k);
2376
MP_CHECKOK( s_mp_2expt(&evenFactor, k) );
2377
2378
/* compute a**-1 mod oddFactor. */
2379
MP_CHECKOK( s_mp_invmod_odd_m(a, &oddFactor, &oddPart) );
2380
/* compute a**-1 mod evenFactor, where evenFactor == 2**k. */
2381
MP_CHECKOK( s_mp_invmod_2d( a, k, &evenPart) );
2382
2383
/* Use Chinese Remainer theorem to compute a**-1 mod m. */
2384
/* let m1 = oddFactor, v1 = oddPart,
2385
* let m2 = evenFactor, v2 = evenPart.
2386
*/
2387
2388
/* Compute C2 = m1**-1 mod m2. */
2389
MP_CHECKOK( s_mp_invmod_2d(&oddFactor, k, &C2) );
2390
2391
/* compute u = (v2 - v1)*C2 mod m2 */
2392
MP_CHECKOK( mp_sub(&evenPart, &oddPart, &tmp1) );
2393
MP_CHECKOK( mp_mul(&tmp1, &C2, &tmp2) );
2394
s_mp_mod_2d(&tmp2, k);
2395
while (MP_SIGN(&tmp2) != MP_ZPOS) {
2396
MP_CHECKOK( mp_add(&tmp2, &evenFactor, &tmp2) );
2397
}
2398
2399
/* compute answer = v1 + u*m1 */
2400
MP_CHECKOK( mp_mul(&tmp2, &oddFactor, c) );
2401
MP_CHECKOK( mp_add(&oddPart, c, c) );
2402
/* not sure this is necessary, but it's low cost if not. */
2403
MP_CHECKOK( mp_mod(c, m, c) );
2404
2405
CLEANUP:
2406
mp_clear(&oddFactor);
2407
mp_clear(&evenFactor);
2408
mp_clear(&oddPart);
2409
mp_clear(&evenPart);
2410
mp_clear(&C2);
2411
mp_clear(&tmp1);
2412
mp_clear(&tmp2);
2413
return res;
2414
}
2415
2416
2417
/* {{{ mp_invmod(a, m, c) */
2418
2419
/*
2420
mp_invmod(a, m, c)
2421
2422
Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
2423
This is equivalent to the question of whether (a, m) = 1. If not,
2424
MP_UNDEF is returned, and there is no inverse.
2425
*/
2426
2427
mp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c)
2428
{
2429
2430
ARGCHK(a && m && c, MP_BADARG);
2431
2432
if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
2433
return MP_RANGE;
2434
2435
if (mp_isodd(m)) {
2436
return s_mp_invmod_odd_m(a, m, c);
2437
}
2438
if (mp_iseven(a))
2439
return MP_UNDEF; /* not invertable */
2440
2441
return s_mp_invmod_even_m(a, m, c);
2442
2443
} /* end mp_invmod() */
2444
2445
/* }}} */
2446
#endif /* if MP_NUMTH */
2447
2448
/* }}} */
2449
2450
/*------------------------------------------------------------------------*/
2451
/* {{{ mp_print(mp, ofp) */
2452
2453
#if MP_IOFUNC
2454
/*
2455
mp_print(mp, ofp)
2456
2457
Print a textual representation of the given mp_int on the output
2458
stream 'ofp'. Output is generated using the internal radix.
2459
*/
2460
2461
void mp_print(mp_int *mp, FILE *ofp)
2462
{
2463
int ix;
2464
2465
if(mp == NULL || ofp == NULL)
2466
return;
2467
2468
fputc((SIGN(mp) == NEG) ? '-' : '+', ofp);
2469
2470
for(ix = USED(mp) - 1; ix >= 0; ix--) {
2471
fprintf(ofp, DIGIT_FMT, DIGIT(mp, ix));
2472
}
2473
2474
} /* end mp_print() */
2475
2476
#endif /* if MP_IOFUNC */
2477
2478
/* }}} */
2479
2480
/*------------------------------------------------------------------------*/
2481
/* {{{ More I/O Functions */
2482
2483
/* {{{ mp_read_raw(mp, str, len) */
2484
2485
/*
2486
mp_read_raw(mp, str, len)
2487
2488
Read in a raw value (base 256) into the given mp_int
2489
*/
2490
2491
mp_err mp_read_raw(mp_int *mp, char *str, int len)
2492
{
2493
int ix;
2494
mp_err res;
2495
unsigned char *ustr = (unsigned char *)str;
2496
2497
ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
2498
2499
mp_zero(mp);
2500
2501
/* Get sign from first byte */
2502
if(ustr[0])
2503
SIGN(mp) = NEG;
2504
else
2505
SIGN(mp) = ZPOS;
2506
2507
/* Read the rest of the digits */
2508
for(ix = 1; ix < len; ix++) {
2509
if((res = mp_mul_d(mp, 256, mp)) != MP_OKAY)
2510
return res;
2511
if((res = mp_add_d(mp, ustr[ix], mp)) != MP_OKAY)
2512
return res;
2513
}
2514
2515
return MP_OKAY;
2516
2517
} /* end mp_read_raw() */
2518
2519
/* }}} */
2520
2521
/* {{{ mp_raw_size(mp) */
2522
2523
int mp_raw_size(mp_int *mp)
2524
{
2525
ARGCHK(mp != NULL, 0);
2526
2527
return (USED(mp) * sizeof(mp_digit)) + 1;
2528
2529
} /* end mp_raw_size() */
2530
2531
/* }}} */
2532
2533
/* {{{ mp_toraw(mp, str) */
2534
2535
mp_err mp_toraw(mp_int *mp, char *str)
2536
{
2537
int ix, jx, pos = 1;
2538
2539
ARGCHK(mp != NULL && str != NULL, MP_BADARG);
2540
2541
str[0] = (char)SIGN(mp);
2542
2543
/* Iterate over each digit... */
2544
for(ix = USED(mp) - 1; ix >= 0; ix--) {
2545
mp_digit d = DIGIT(mp, ix);
2546
2547
/* Unpack digit bytes, high order first */
2548
for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
2549
str[pos++] = (char)(d >> (jx * CHAR_BIT));
2550
}
2551
}
2552
2553
return MP_OKAY;
2554
2555
} /* end mp_toraw() */
2556
2557
/* }}} */
2558
2559
/* {{{ mp_read_radix(mp, str, radix) */
2560
2561
/*
2562
mp_read_radix(mp, str, radix)
2563
2564
Read an integer from the given string, and set mp to the resulting
2565
value. The input is presumed to be in base 10. Leading non-digit
2566
characters are ignored, and the function reads until a non-digit
2567
character or the end of the string.
2568
*/
2569
2570
mp_err mp_read_radix(mp_int *mp, const char *str, int radix)
2571
{
2572
int ix = 0, val = 0;
2573
mp_err res;
2574
mp_sign sig = ZPOS;
2575
2576
ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX,
2577
MP_BADARG);
2578
2579
mp_zero(mp);
2580
2581
/* Skip leading non-digit characters until a digit or '-' or '+' */
2582
while(str[ix] &&
2583
(s_mp_tovalue(str[ix], radix) < 0) &&
2584
str[ix] != '-' &&
2585
str[ix] != '+') {
2586
++ix;
2587
}
2588
2589
if(str[ix] == '-') {
2590
sig = NEG;
2591
++ix;
2592
} else if(str[ix] == '+') {
2593
sig = ZPOS; /* this is the default anyway... */
2594
++ix;
2595
}
2596
2597
while((val = s_mp_tovalue(str[ix], radix)) >= 0) {
2598
if((res = s_mp_mul_d(mp, radix)) != MP_OKAY)
2599
return res;
2600
if((res = s_mp_add_d(mp, val)) != MP_OKAY)
2601
return res;
2602
++ix;
2603
}
2604
2605
if(s_mp_cmp_d(mp, 0) == MP_EQ)
2606
SIGN(mp) = ZPOS;
2607
else
2608
SIGN(mp) = sig;
2609
2610
return MP_OKAY;
2611
2612
} /* end mp_read_radix() */
2613
2614
mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix)
2615
{
2616
int radix = default_radix;
2617
int cx;
2618
mp_sign sig = ZPOS;
2619
mp_err res;
2620
2621
/* Skip leading non-digit characters until a digit or '-' or '+' */
2622
while ((cx = *str) != 0 &&
2623
(s_mp_tovalue(cx, radix) < 0) &&
2624
cx != '-' &&
2625
cx != '+') {
2626
++str;
2627
}
2628
2629
if (cx == '-') {
2630
sig = NEG;
2631
++str;
2632
} else if (cx == '+') {
2633
sig = ZPOS; /* this is the default anyway... */
2634
++str;
2635
}
2636
2637
if (str[0] == '0') {
2638
if ((str[1] | 0x20) == 'x') {
2639
radix = 16;
2640
str += 2;
2641
} else {
2642
radix = 8;
2643
str++;
2644
}
2645
}
2646
res = mp_read_radix(a, str, radix);
2647
if (res == MP_OKAY) {
2648
MP_SIGN(a) = (s_mp_cmp_d(a, 0) == MP_EQ) ? ZPOS : sig;
2649
}
2650
return res;
2651
}
2652
2653
/* }}} */
2654
2655
/* {{{ mp_radix_size(mp, radix) */
2656
2657
int mp_radix_size(mp_int *mp, int radix)
2658
{
2659
int bits;
2660
2661
if(!mp || radix < 2 || radix > MAX_RADIX)
2662
return 0;
2663
2664
bits = USED(mp) * DIGIT_BIT - 1;
2665
2666
return s_mp_outlen(bits, radix);
2667
2668
} /* end mp_radix_size() */
2669
2670
/* }}} */
2671
2672
/* {{{ mp_toradix(mp, str, radix) */
2673
2674
mp_err mp_toradix(mp_int *mp, char *str, int radix)
2675
{
2676
int ix, pos = 0;
2677
2678
ARGCHK(mp != NULL && str != NULL, MP_BADARG);
2679
ARGCHK(radix > 1 && radix <= MAX_RADIX, MP_RANGE);
2680
2681
if(mp_cmp_z(mp) == MP_EQ) {
2682
str[0] = '0';
2683
str[1] = '\0';
2684
} else {
2685
mp_err res;
2686
mp_int tmp;
2687
mp_sign sgn;
2688
mp_digit rem, rdx = (mp_digit)radix;
2689
char ch;
2690
2691
if((res = mp_init_copy(&tmp, mp)) != MP_OKAY)
2692
return res;
2693
2694
/* Save sign for later, and take absolute value */
2695
sgn = SIGN(&tmp); SIGN(&tmp) = ZPOS;
2696
2697
/* Generate output digits in reverse order */
2698
while(mp_cmp_z(&tmp) != 0) {
2699
if((res = mp_div_d(&tmp, rdx, &tmp, &rem)) != MP_OKAY) {
2700
mp_clear(&tmp);
2701
return res;
2702
}
2703
2704
/* Generate digits, use capital letters */
2705
ch = s_mp_todigit(rem, radix, 0);
2706
2707
str[pos++] = ch;
2708
}
2709
2710
/* Add - sign if original value was negative */
2711
if(sgn == NEG)
2712
str[pos++] = '-';
2713
2714
/* Add trailing NUL to end the string */
2715
str[pos--] = '\0';
2716
2717
/* Reverse the digits and sign indicator */
2718
ix = 0;
2719
while(ix < pos) {
2720
char tmp = str[ix];
2721
2722
str[ix] = str[pos];
2723
str[pos] = tmp;
2724
++ix;
2725
--pos;
2726
}
2727
2728
mp_clear(&tmp);
2729
}
2730
2731
return MP_OKAY;
2732
2733
} /* end mp_toradix() */
2734
2735
/* }}} */
2736
2737
/* {{{ mp_tovalue(ch, r) */
2738
2739
int mp_tovalue(char ch, int r)
2740
{
2741
return s_mp_tovalue(ch, r);
2742
2743
} /* end mp_tovalue() */
2744
2745
/* }}} */
2746
2747
/* }}} */
2748
2749
/* {{{ mp_strerror(ec) */
2750
2751
/*
2752
mp_strerror(ec)
2753
2754
Return a string describing the meaning of error code 'ec'. The
2755
string returned is allocated in static memory, so the caller should
2756
not attempt to modify or free the memory associated with this
2757
string.
2758
*/
2759
const char *mp_strerror(mp_err ec)
2760
{
2761
int aec = (ec < 0) ? -ec : ec;
2762
2763
/* Code values are negative, so the senses of these comparisons
2764
are accurate */
2765
if(ec < MP_LAST_CODE || ec > MP_OKAY) {
2766
return mp_err_string[0]; /* unknown error code */
2767
} else {
2768
return mp_err_string[aec + 1];
2769
}
2770
2771
} /* end mp_strerror() */
2772
2773
/* }}} */
2774
2775
/*========================================================================*/
2776
/*------------------------------------------------------------------------*/
2777
/* Static function definitions (internal use only) */
2778
2779
/* {{{ Memory management */
2780
2781
/* {{{ s_mp_grow(mp, min) */
2782
2783
/* Make sure there are at least 'min' digits allocated to mp */
2784
mp_err s_mp_grow(mp_int *mp, mp_size min)
2785
{
2786
if(min > ALLOC(mp)) {
2787
mp_digit *tmp;
2788
2789
/* Set min to next nearest default precision block size */
2790
min = MP_ROUNDUP(min, s_mp_defprec);
2791
2792
if((tmp = s_mp_alloc(min, sizeof(mp_digit), FLAG(mp))) == NULL)
2793
return MP_MEM;
2794
2795
s_mp_copy(DIGITS(mp), tmp, USED(mp));
2796
2797
#if MP_CRYPTO
2798
s_mp_setz(DIGITS(mp), ALLOC(mp));
2799
#endif
2800
s_mp_free(DIGITS(mp), ALLOC(mp));
2801
DIGITS(mp) = tmp;
2802
ALLOC(mp) = min;
2803
}
2804
2805
return MP_OKAY;
2806
2807
} /* end s_mp_grow() */
2808
2809
/* }}} */
2810
2811
/* {{{ s_mp_pad(mp, min) */
2812
2813
/* Make sure the used size of mp is at least 'min', growing if needed */
2814
mp_err s_mp_pad(mp_int *mp, mp_size min)
2815
{
2816
if(min > USED(mp)) {
2817
mp_err res;
2818
2819
/* Make sure there is room to increase precision */
2820
if (min > ALLOC(mp)) {
2821
if ((res = s_mp_grow(mp, min)) != MP_OKAY)
2822
return res;
2823
} else {
2824
s_mp_setz(DIGITS(mp) + USED(mp), min - USED(mp));
2825
}
2826
2827
/* Increase precision; should already be 0-filled */
2828
USED(mp) = min;
2829
}
2830
2831
return MP_OKAY;
2832
2833
} /* end s_mp_pad() */
2834
2835
/* }}} */
2836
2837
/* {{{ s_mp_setz(dp, count) */
2838
2839
#if MP_MACRO == 0
2840
/* Set 'count' digits pointed to by dp to be zeroes */
2841
void s_mp_setz(mp_digit *dp, mp_size count)
2842
{
2843
#if MP_MEMSET == 0
2844
int ix;
2845
2846
for(ix = 0; ix < count; ix++)
2847
dp[ix] = 0;
2848
#else
2849
memset(dp, 0, count * sizeof(mp_digit));
2850
#endif
2851
2852
} /* end s_mp_setz() */
2853
#endif
2854
2855
/* }}} */
2856
2857
/* {{{ s_mp_copy(sp, dp, count) */
2858
2859
#if MP_MACRO == 0
2860
/* Copy 'count' digits from sp to dp */
2861
void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count)
2862
{
2863
#if MP_MEMCPY == 0
2864
int ix;
2865
2866
for(ix = 0; ix < count; ix++)
2867
dp[ix] = sp[ix];
2868
#else
2869
memcpy(dp, sp, count * sizeof(mp_digit));
2870
#endif
2871
2872
} /* end s_mp_copy() */
2873
#endif
2874
2875
/* }}} */
2876
2877
/* {{{ s_mp_alloc(nb, ni, kmflag) */
2878
2879
#if MP_MACRO == 0
2880
/* Allocate ni records of nb bytes each, and return a pointer to that */
2881
void *s_mp_alloc(size_t nb, size_t ni, int kmflag)
2882
{
2883
++mp_allocs;
2884
#ifdef _KERNEL
2885
mp_int *mp;
2886
mp = kmem_zalloc(nb * ni, kmflag);
2887
if (mp != NULL)
2888
FLAG(mp) = kmflag;
2889
return (mp);
2890
#else
2891
return calloc(nb, ni);
2892
#endif
2893
2894
} /* end s_mp_alloc() */
2895
#endif
2896
2897
/* }}} */
2898
2899
/* {{{ s_mp_free(ptr) */
2900
2901
#if MP_MACRO == 0
2902
/* Free the memory pointed to by ptr */
2903
void s_mp_free(void *ptr, mp_size alloc)
2904
{
2905
if(ptr) {
2906
++mp_frees;
2907
#ifdef _KERNEL
2908
kmem_free(ptr, alloc * sizeof (mp_digit));
2909
#else
2910
free(ptr);
2911
#endif
2912
}
2913
} /* end s_mp_free() */
2914
#endif
2915
2916
/* }}} */
2917
2918
/* {{{ s_mp_clamp(mp) */
2919
2920
#if MP_MACRO == 0
2921
/* Remove leading zeroes from the given value */
2922
void s_mp_clamp(mp_int *mp)
2923
{
2924
mp_size used = MP_USED(mp);
2925
while (used > 1 && DIGIT(mp, used - 1) == 0)
2926
--used;
2927
MP_USED(mp) = used;
2928
} /* end s_mp_clamp() */
2929
#endif
2930
2931
/* }}} */
2932
2933
/* {{{ s_mp_exch(a, b) */
2934
2935
/* Exchange the data for a and b; (b, a) = (a, b) */
2936
void s_mp_exch(mp_int *a, mp_int *b)
2937
{
2938
mp_int tmp;
2939
2940
tmp = *a;
2941
*a = *b;
2942
*b = tmp;
2943
2944
} /* end s_mp_exch() */
2945
2946
/* }}} */
2947
2948
/* }}} */
2949
2950
/* {{{ Arithmetic helpers */
2951
2952
/* {{{ s_mp_lshd(mp, p) */
2953
2954
/*
2955
Shift mp leftward by p digits, growing if needed, and zero-filling
2956
the in-shifted digits at the right end. This is a convenient
2957
alternative to multiplication by powers of the radix
2958
The value of USED(mp) must already have been set to the value for
2959
the shifted result.
2960
*/
2961
2962
mp_err s_mp_lshd(mp_int *mp, mp_size p)
2963
{
2964
mp_err res;
2965
mp_size pos;
2966
int ix;
2967
2968
if(p == 0)
2969
return MP_OKAY;
2970
2971
if (MP_USED(mp) == 1 && MP_DIGIT(mp, 0) == 0)
2972
return MP_OKAY;
2973
2974
if((res = s_mp_pad(mp, USED(mp) + p)) != MP_OKAY)
2975
return res;
2976
2977
pos = USED(mp) - 1;
2978
2979
/* Shift all the significant figures over as needed */
2980
for(ix = pos - p; ix >= 0; ix--)
2981
DIGIT(mp, ix + p) = DIGIT(mp, ix);
2982
2983
/* Fill the bottom digits with zeroes */
2984
for(ix = 0; ix < p; ix++)
2985
DIGIT(mp, ix) = 0;
2986
2987
return MP_OKAY;
2988
2989
} /* end s_mp_lshd() */
2990
2991
/* }}} */
2992
2993
/* {{{ s_mp_mul_2d(mp, d) */
2994
2995
/*
2996
Multiply the integer by 2^d, where d is a number of bits. This
2997
amounts to a bitwise shift of the value.
2998
*/
2999
mp_err s_mp_mul_2d(mp_int *mp, mp_digit d)
3000
{
3001
mp_err res;
3002
mp_digit dshift, bshift;
3003
mp_digit mask;
3004
3005
ARGCHK(mp != NULL, MP_BADARG);
3006
3007
dshift = d / MP_DIGIT_BIT;
3008
bshift = d % MP_DIGIT_BIT;
3009
/* bits to be shifted out of the top word */
3010
mask = ((mp_digit)~0 << (MP_DIGIT_BIT - bshift));
3011
mask &= MP_DIGIT(mp, MP_USED(mp) - 1);
3012
3013
if (MP_OKAY != (res = s_mp_pad(mp, MP_USED(mp) + dshift + (mask != 0) )))
3014
return res;
3015
3016
if (dshift && MP_OKAY != (res = s_mp_lshd(mp, dshift)))
3017
return res;
3018
3019
if (bshift) {
3020
mp_digit *pa = MP_DIGITS(mp);
3021
mp_digit *alim = pa + MP_USED(mp);
3022
mp_digit prev = 0;
3023
3024
for (pa += dshift; pa < alim; ) {
3025
mp_digit x = *pa;
3026
*pa++ = (x << bshift) | prev;
3027
prev = x >> (DIGIT_BIT - bshift);
3028
}
3029
}
3030
3031
s_mp_clamp(mp);
3032
return MP_OKAY;
3033
} /* end s_mp_mul_2d() */
3034
3035
/* {{{ s_mp_rshd(mp, p) */
3036
3037
/*
3038
Shift mp rightward by p digits. Maintains the invariant that
3039
digits above the precision are all zero. Digits shifted off the
3040
end are lost. Cannot fail.
3041
*/
3042
3043
void s_mp_rshd(mp_int *mp, mp_size p)
3044
{
3045
mp_size ix;
3046
mp_digit *src, *dst;
3047
3048
if(p == 0)
3049
return;
3050
3051
/* Shortcut when all digits are to be shifted off */
3052
if(p >= USED(mp)) {
3053
s_mp_setz(DIGITS(mp), ALLOC(mp));
3054
USED(mp) = 1;
3055
SIGN(mp) = ZPOS;
3056
return;
3057
}
3058
3059
/* Shift all the significant figures over as needed */
3060
dst = MP_DIGITS(mp);
3061
src = dst + p;
3062
for (ix = USED(mp) - p; ix > 0; ix--)
3063
*dst++ = *src++;
3064
3065
MP_USED(mp) -= p;
3066
/* Fill the top digits with zeroes */
3067
while (p-- > 0)
3068
*dst++ = 0;
3069
3070
#if 0
3071
/* Strip off any leading zeroes */
3072
s_mp_clamp(mp);
3073
#endif
3074
3075
} /* end s_mp_rshd() */
3076
3077
/* }}} */
3078
3079
/* {{{ s_mp_div_2(mp) */
3080
3081
/* Divide by two -- take advantage of radix properties to do it fast */
3082
void s_mp_div_2(mp_int *mp)
3083
{
3084
s_mp_div_2d(mp, 1);
3085
3086
} /* end s_mp_div_2() */
3087
3088
/* }}} */
3089
3090
/* {{{ s_mp_mul_2(mp) */
3091
3092
mp_err s_mp_mul_2(mp_int *mp)
3093
{
3094
mp_digit *pd;
3095
unsigned int ix, used;
3096
mp_digit kin = 0;
3097
3098
/* Shift digits leftward by 1 bit */
3099
used = MP_USED(mp);
3100
pd = MP_DIGITS(mp);
3101
for (ix = 0; ix < used; ix++) {
3102
mp_digit d = *pd;
3103
*pd++ = (d << 1) | kin;
3104
kin = (d >> (DIGIT_BIT - 1));
3105
}
3106
3107
/* Deal with rollover from last digit */
3108
if (kin) {
3109
if (ix >= ALLOC(mp)) {
3110
mp_err res;
3111
if((res = s_mp_grow(mp, ALLOC(mp) + 1)) != MP_OKAY)
3112
return res;
3113
}
3114
3115
DIGIT(mp, ix) = kin;
3116
USED(mp) += 1;
3117
}
3118
3119
return MP_OKAY;
3120
3121
} /* end s_mp_mul_2() */
3122
3123
/* }}} */
3124
3125
/* {{{ s_mp_mod_2d(mp, d) */
3126
3127
/*
3128
Remainder the integer by 2^d, where d is a number of bits. This
3129
amounts to a bitwise AND of the value, and does not require the full
3130
division code
3131
*/
3132
void s_mp_mod_2d(mp_int *mp, mp_digit d)
3133
{
3134
mp_size ndig = (d / DIGIT_BIT), nbit = (d % DIGIT_BIT);
3135
mp_size ix;
3136
mp_digit dmask;
3137
3138
if(ndig >= USED(mp))
3139
return;
3140
3141
/* Flush all the bits above 2^d in its digit */
3142
dmask = ((mp_digit)1 << nbit) - 1;
3143
DIGIT(mp, ndig) &= dmask;
3144
3145
/* Flush all digits above the one with 2^d in it */
3146
for(ix = ndig + 1; ix < USED(mp); ix++)
3147
DIGIT(mp, ix) = 0;
3148
3149
s_mp_clamp(mp);
3150
3151
} /* end s_mp_mod_2d() */
3152
3153
/* }}} */
3154
3155
/* {{{ s_mp_div_2d(mp, d) */
3156
3157
/*
3158
Divide the integer by 2^d, where d is a number of bits. This
3159
amounts to a bitwise shift of the value, and does not require the
3160
full division code (used in Barrett reduction, see below)
3161
*/
3162
void s_mp_div_2d(mp_int *mp, mp_digit d)
3163
{
3164
int ix;
3165
mp_digit save, next, mask;
3166
3167
s_mp_rshd(mp, d / DIGIT_BIT);
3168
d %= DIGIT_BIT;
3169
if (d) {
3170
mask = ((mp_digit)1 << d) - 1;
3171
save = 0;
3172
for(ix = USED(mp) - 1; ix >= 0; ix--) {
3173
next = DIGIT(mp, ix) & mask;
3174
DIGIT(mp, ix) = (DIGIT(mp, ix) >> d) | (save << (DIGIT_BIT - d));
3175
save = next;
3176
}
3177
}
3178
s_mp_clamp(mp);
3179
3180
} /* end s_mp_div_2d() */
3181
3182
/* }}} */
3183
3184
/* {{{ s_mp_norm(a, b, *d) */
3185
3186
/*
3187
s_mp_norm(a, b, *d)
3188
3189
Normalize a and b for division, where b is the divisor. In order
3190
that we might make good guesses for quotient digits, we want the
3191
leading digit of b to be at least half the radix, which we
3192
accomplish by multiplying a and b by a power of 2. The exponent
3193
(shift count) is placed in *pd, so that the remainder can be shifted
3194
back at the end of the division process.
3195
*/
3196
3197
mp_err s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd)
3198
{
3199
mp_digit d;
3200
mp_digit mask;
3201
mp_digit b_msd;
3202
mp_err res = MP_OKAY;
3203
3204
d = 0;
3205
mask = DIGIT_MAX & ~(DIGIT_MAX >> 1); /* mask is msb of digit */
3206
b_msd = DIGIT(b, USED(b) - 1);
3207
while (!(b_msd & mask)) {
3208
b_msd <<= 1;
3209
++d;
3210
}
3211
3212
if (d) {
3213
MP_CHECKOK( s_mp_mul_2d(a, d) );
3214
MP_CHECKOK( s_mp_mul_2d(b, d) );
3215
}
3216
3217
*pd = d;
3218
CLEANUP:
3219
return res;
3220
3221
} /* end s_mp_norm() */
3222
3223
/* }}} */
3224
3225
/* }}} */
3226
3227
/* {{{ Primitive digit arithmetic */
3228
3229
/* {{{ s_mp_add_d(mp, d) */
3230
3231
/* Add d to |mp| in place */
3232
mp_err s_mp_add_d(mp_int *mp, mp_digit d) /* unsigned digit addition */
3233
{
3234
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3235
mp_word w, k = 0;
3236
mp_size ix = 1;
3237
3238
w = (mp_word)DIGIT(mp, 0) + d;
3239
DIGIT(mp, 0) = ACCUM(w);
3240
k = CARRYOUT(w);
3241
3242
while(ix < USED(mp) && k) {
3243
w = (mp_word)DIGIT(mp, ix) + k;
3244
DIGIT(mp, ix) = ACCUM(w);
3245
k = CARRYOUT(w);
3246
++ix;
3247
}
3248
3249
if(k != 0) {
3250
mp_err res;
3251
3252
if((res = s_mp_pad(mp, USED(mp) + 1)) != MP_OKAY)
3253
return res;
3254
3255
DIGIT(mp, ix) = (mp_digit)k;
3256
}
3257
3258
return MP_OKAY;
3259
#else
3260
mp_digit * pmp = MP_DIGITS(mp);
3261
mp_digit sum, mp_i, carry = 0;
3262
mp_err res = MP_OKAY;
3263
int used = (int)MP_USED(mp);
3264
3265
mp_i = *pmp;
3266
*pmp++ = sum = d + mp_i;
3267
carry = (sum < d);
3268
while (carry && --used > 0) {
3269
mp_i = *pmp;
3270
*pmp++ = sum = carry + mp_i;
3271
carry = !sum;
3272
}
3273
if (carry && !used) {
3274
/* mp is growing */
3275
used = MP_USED(mp);
3276
MP_CHECKOK( s_mp_pad(mp, used + 1) );
3277
MP_DIGIT(mp, used) = carry;
3278
}
3279
CLEANUP:
3280
return res;
3281
#endif
3282
} /* end s_mp_add_d() */
3283
3284
/* }}} */
3285
3286
/* {{{ s_mp_sub_d(mp, d) */
3287
3288
/* Subtract d from |mp| in place, assumes |mp| > d */
3289
mp_err s_mp_sub_d(mp_int *mp, mp_digit d) /* unsigned digit subtract */
3290
{
3291
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3292
mp_word w, b = 0;
3293
mp_size ix = 1;
3294
3295
/* Compute initial subtraction */
3296
w = (RADIX + (mp_word)DIGIT(mp, 0)) - d;
3297
b = CARRYOUT(w) ? 0 : 1;
3298
DIGIT(mp, 0) = ACCUM(w);
3299
3300
/* Propagate borrows leftward */
3301
while(b && ix < USED(mp)) {
3302
w = (RADIX + (mp_word)DIGIT(mp, ix)) - b;
3303
b = CARRYOUT(w) ? 0 : 1;
3304
DIGIT(mp, ix) = ACCUM(w);
3305
++ix;
3306
}
3307
3308
/* Remove leading zeroes */
3309
s_mp_clamp(mp);
3310
3311
/* If we have a borrow out, it's a violation of the input invariant */
3312
if(b)
3313
return MP_RANGE;
3314
else
3315
return MP_OKAY;
3316
#else
3317
mp_digit *pmp = MP_DIGITS(mp);
3318
mp_digit mp_i, diff, borrow;
3319
mp_size used = MP_USED(mp);
3320
3321
mp_i = *pmp;
3322
*pmp++ = diff = mp_i - d;
3323
borrow = (diff > mp_i);
3324
while (borrow && --used) {
3325
mp_i = *pmp;
3326
*pmp++ = diff = mp_i - borrow;
3327
borrow = (diff > mp_i);
3328
}
3329
s_mp_clamp(mp);
3330
return (borrow && !used) ? MP_RANGE : MP_OKAY;
3331
#endif
3332
} /* end s_mp_sub_d() */
3333
3334
/* }}} */
3335
3336
/* {{{ s_mp_mul_d(a, d) */
3337
3338
/* Compute a = a * d, single digit multiplication */
3339
mp_err s_mp_mul_d(mp_int *a, mp_digit d)
3340
{
3341
mp_err res;
3342
mp_size used;
3343
int pow;
3344
3345
if (!d) {
3346
mp_zero(a);
3347
return MP_OKAY;
3348
}
3349
if (d == 1)
3350
return MP_OKAY;
3351
if (0 <= (pow = s_mp_ispow2d(d))) {
3352
return s_mp_mul_2d(a, (mp_digit)pow);
3353
}
3354
3355
used = MP_USED(a);
3356
MP_CHECKOK( s_mp_pad(a, used + 1) );
3357
3358
s_mpv_mul_d(MP_DIGITS(a), used, d, MP_DIGITS(a));
3359
3360
s_mp_clamp(a);
3361
3362
CLEANUP:
3363
return res;
3364
3365
} /* end s_mp_mul_d() */
3366
3367
/* }}} */
3368
3369
/* {{{ s_mp_div_d(mp, d, r) */
3370
3371
/*
3372
s_mp_div_d(mp, d, r)
3373
3374
Compute the quotient mp = mp / d and remainder r = mp mod d, for a
3375
single digit d. If r is null, the remainder will be discarded.
3376
*/
3377
3378
mp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r)
3379
{
3380
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
3381
mp_word w = 0, q;
3382
#else
3383
mp_digit w = 0, q;
3384
#endif
3385
int ix;
3386
mp_err res;
3387
mp_int quot;
3388
mp_int rem;
3389
3390
if(d == 0)
3391
return MP_RANGE;
3392
if (d == 1) {
3393
if (r)
3394
*r = 0;
3395
return MP_OKAY;
3396
}
3397
/* could check for power of 2 here, but mp_div_d does that. */
3398
if (MP_USED(mp) == 1) {
3399
mp_digit n = MP_DIGIT(mp,0);
3400
mp_digit rem;
3401
3402
q = n / d;
3403
rem = n % d;
3404
MP_DIGIT(mp,0) = q;
3405
if (r)
3406
*r = rem;
3407
return MP_OKAY;
3408
}
3409
3410
MP_DIGITS(&rem) = 0;
3411
MP_DIGITS(&quot) = 0;
3412
/* Make room for the quotient */
3413
MP_CHECKOK( mp_init_size(&quot, USED(mp), FLAG(mp)) );
3414
3415
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
3416
for(ix = USED(mp) - 1; ix >= 0; ix--) {
3417
w = (w << DIGIT_BIT) | DIGIT(mp, ix);
3418
3419
if(w >= d) {
3420
q = w / d;
3421
w = w % d;
3422
} else {
3423
q = 0;
3424
}
3425
3426
s_mp_lshd(&quot, 1);
3427
DIGIT(&quot, 0) = (mp_digit)q;
3428
}
3429
#else
3430
{
3431
mp_digit p;
3432
#if !defined(MP_ASSEMBLY_DIV_2DX1D)
3433
mp_digit norm;
3434
#endif
3435
3436
MP_CHECKOK( mp_init_copy(&rem, mp) );
3437
3438
#if !defined(MP_ASSEMBLY_DIV_2DX1D)
3439
MP_DIGIT(&quot, 0) = d;
3440
MP_CHECKOK( s_mp_norm(&rem, &quot, &norm) );
3441
if (norm)
3442
d <<= norm;
3443
MP_DIGIT(&quot, 0) = 0;
3444
#endif
3445
3446
p = 0;
3447
for (ix = USED(&rem) - 1; ix >= 0; ix--) {
3448
w = DIGIT(&rem, ix);
3449
3450
if (p) {
3451
MP_CHECKOK( s_mpv_div_2dx1d(p, w, d, &q, &w) );
3452
} else if (w >= d) {
3453
q = w / d;
3454
w = w % d;
3455
} else {
3456
q = 0;
3457
}
3458
3459
MP_CHECKOK( s_mp_lshd(&quot, 1) );
3460
DIGIT(&quot, 0) = q;
3461
p = w;
3462
}
3463
#if !defined(MP_ASSEMBLY_DIV_2DX1D)
3464
if (norm)
3465
w >>= norm;
3466
#endif
3467
}
3468
#endif
3469
3470
/* Deliver the remainder, if desired */
3471
if(r)
3472
*r = (mp_digit)w;
3473
3474
s_mp_clamp(&quot);
3475
mp_exch(&quot, mp);
3476
CLEANUP:
3477
mp_clear(&quot);
3478
mp_clear(&rem);
3479
3480
return res;
3481
} /* end s_mp_div_d() */
3482
3483
/* }}} */
3484
3485
3486
/* }}} */
3487
3488
/* {{{ Primitive full arithmetic */
3489
3490
/* {{{ s_mp_add(a, b) */
3491
3492
/* Compute a = |a| + |b| */
3493
mp_err s_mp_add(mp_int *a, const mp_int *b) /* magnitude addition */
3494
{
3495
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3496
mp_word w = 0;
3497
#else
3498
mp_digit d, sum, carry = 0;
3499
#endif
3500
mp_digit *pa, *pb;
3501
mp_size ix;
3502
mp_size used;
3503
mp_err res;
3504
3505
/* Make sure a has enough precision for the output value */
3506
if((USED(b) > USED(a)) && (res = s_mp_pad(a, USED(b))) != MP_OKAY)
3507
return res;
3508
3509
/*
3510
Add up all digits up to the precision of b. If b had initially
3511
the same precision as a, or greater, we took care of it by the
3512
padding step above, so there is no problem. If b had initially
3513
less precision, we'll have to make sure the carry out is duly
3514
propagated upward among the higher-order digits of the sum.
3515
*/
3516
pa = MP_DIGITS(a);
3517
pb = MP_DIGITS(b);
3518
used = MP_USED(b);
3519
for(ix = 0; ix < used; ix++) {
3520
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3521
w = w + *pa + *pb++;
3522
*pa++ = ACCUM(w);
3523
w = CARRYOUT(w);
3524
#else
3525
d = *pa;
3526
sum = d + *pb++;
3527
d = (sum < d); /* detect overflow */
3528
*pa++ = sum += carry;
3529
carry = d + (sum < carry); /* detect overflow */
3530
#endif
3531
}
3532
3533
/* If we run out of 'b' digits before we're actually done, make
3534
sure the carries get propagated upward...
3535
*/
3536
used = MP_USED(a);
3537
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3538
while (w && ix < used) {
3539
w = w + *pa;
3540
*pa++ = ACCUM(w);
3541
w = CARRYOUT(w);
3542
++ix;
3543
}
3544
#else
3545
while (carry && ix < used) {
3546
sum = carry + *pa;
3547
*pa++ = sum;
3548
carry = !sum;
3549
++ix;
3550
}
3551
#endif
3552
3553
/* If there's an overall carry out, increase precision and include
3554
it. We could have done this initially, but why touch the memory
3555
allocator unless we're sure we have to?
3556
*/
3557
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3558
if (w) {
3559
if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
3560
return res;
3561
3562
DIGIT(a, ix) = (mp_digit)w;
3563
}
3564
#else
3565
if (carry) {
3566
if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
3567
return res;
3568
3569
DIGIT(a, used) = carry;
3570
}
3571
#endif
3572
3573
return MP_OKAY;
3574
} /* end s_mp_add() */
3575
3576
/* }}} */
3577
3578
/* Compute c = |a| + |b| */ /* magnitude addition */
3579
mp_err s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c)
3580
{
3581
mp_digit *pa, *pb, *pc;
3582
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3583
mp_word w = 0;
3584
#else
3585
mp_digit sum, carry = 0, d;
3586
#endif
3587
mp_size ix;
3588
mp_size used;
3589
mp_err res;
3590
3591
MP_SIGN(c) = MP_SIGN(a);
3592
if (MP_USED(a) < MP_USED(b)) {
3593
const mp_int *xch = a;
3594
a = b;
3595
b = xch;
3596
}
3597
3598
/* Make sure a has enough precision for the output value */
3599
if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a))))
3600
return res;
3601
3602
/*
3603
Add up all digits up to the precision of b. If b had initially
3604
the same precision as a, or greater, we took care of it by the
3605
exchange step above, so there is no problem. If b had initially
3606
less precision, we'll have to make sure the carry out is duly
3607
propagated upward among the higher-order digits of the sum.
3608
*/
3609
pa = MP_DIGITS(a);
3610
pb = MP_DIGITS(b);
3611
pc = MP_DIGITS(c);
3612
used = MP_USED(b);
3613
for (ix = 0; ix < used; ix++) {
3614
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3615
w = w + *pa++ + *pb++;
3616
*pc++ = ACCUM(w);
3617
w = CARRYOUT(w);
3618
#else
3619
d = *pa++;
3620
sum = d + *pb++;
3621
d = (sum < d); /* detect overflow */
3622
*pc++ = sum += carry;
3623
carry = d + (sum < carry); /* detect overflow */
3624
#endif
3625
}
3626
3627
/* If we run out of 'b' digits before we're actually done, make
3628
sure the carries get propagated upward...
3629
*/
3630
for (used = MP_USED(a); ix < used; ++ix) {
3631
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3632
w = w + *pa++;
3633
*pc++ = ACCUM(w);
3634
w = CARRYOUT(w);
3635
#else
3636
*pc++ = sum = carry + *pa++;
3637
carry = (sum < carry);
3638
#endif
3639
}
3640
3641
/* If there's an overall carry out, increase precision and include
3642
it. We could have done this initially, but why touch the memory
3643
allocator unless we're sure we have to?
3644
*/
3645
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3646
if (w) {
3647
if((res = s_mp_pad(c, used + 1)) != MP_OKAY)
3648
return res;
3649
3650
DIGIT(c, used) = (mp_digit)w;
3651
++used;
3652
}
3653
#else
3654
if (carry) {
3655
if((res = s_mp_pad(c, used + 1)) != MP_OKAY)
3656
return res;
3657
3658
DIGIT(c, used) = carry;
3659
++used;
3660
}
3661
#endif
3662
MP_USED(c) = used;
3663
return MP_OKAY;
3664
}
3665
/* {{{ s_mp_add_offset(a, b, offset) */
3666
3667
/* Compute a = |a| + ( |b| * (RADIX ** offset) ) */
3668
mp_err s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset)
3669
{
3670
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3671
mp_word w, k = 0;
3672
#else
3673
mp_digit d, sum, carry = 0;
3674
#endif
3675
mp_size ib;
3676
mp_size ia;
3677
mp_size lim;
3678
mp_err res;
3679
3680
/* Make sure a has enough precision for the output value */
3681
lim = MP_USED(b) + offset;
3682
if((lim > USED(a)) && (res = s_mp_pad(a, lim)) != MP_OKAY)
3683
return res;
3684
3685
/*
3686
Add up all digits up to the precision of b. If b had initially
3687
the same precision as a, or greater, we took care of it by the
3688
padding step above, so there is no problem. If b had initially
3689
less precision, we'll have to make sure the carry out is duly
3690
propagated upward among the higher-order digits of the sum.
3691
*/
3692
lim = USED(b);
3693
for(ib = 0, ia = offset; ib < lim; ib++, ia++) {
3694
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3695
w = (mp_word)DIGIT(a, ia) + DIGIT(b, ib) + k;
3696
DIGIT(a, ia) = ACCUM(w);
3697
k = CARRYOUT(w);
3698
#else
3699
d = MP_DIGIT(a, ia);
3700
sum = d + MP_DIGIT(b, ib);
3701
d = (sum < d);
3702
MP_DIGIT(a,ia) = sum += carry;
3703
carry = d + (sum < carry);
3704
#endif
3705
}
3706
3707
/* If we run out of 'b' digits before we're actually done, make
3708
sure the carries get propagated upward...
3709
*/
3710
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3711
for (lim = MP_USED(a); k && (ia < lim); ++ia) {
3712
w = (mp_word)DIGIT(a, ia) + k;
3713
DIGIT(a, ia) = ACCUM(w);
3714
k = CARRYOUT(w);
3715
}
3716
#else
3717
for (lim = MP_USED(a); carry && (ia < lim); ++ia) {
3718
d = MP_DIGIT(a, ia);
3719
MP_DIGIT(a,ia) = sum = d + carry;
3720
carry = (sum < d);
3721
}
3722
#endif
3723
3724
/* If there's an overall carry out, increase precision and include
3725
it. We could have done this initially, but why touch the memory
3726
allocator unless we're sure we have to?
3727
*/
3728
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3729
if(k) {
3730
if((res = s_mp_pad(a, USED(a) + 1)) != MP_OKAY)
3731
return res;
3732
3733
DIGIT(a, ia) = (mp_digit)k;
3734
}
3735
#else
3736
if (carry) {
3737
if((res = s_mp_pad(a, lim + 1)) != MP_OKAY)
3738
return res;
3739
3740
DIGIT(a, lim) = carry;
3741
}
3742
#endif
3743
s_mp_clamp(a);
3744
3745
return MP_OKAY;
3746
3747
} /* end s_mp_add_offset() */
3748
3749
/* }}} */
3750
3751
/* {{{ s_mp_sub(a, b) */
3752
3753
/* Compute a = |a| - |b|, assumes |a| >= |b| */
3754
mp_err s_mp_sub(mp_int *a, const mp_int *b) /* magnitude subtract */
3755
{
3756
mp_digit *pa, *pb, *limit;
3757
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3758
mp_sword w = 0;
3759
#else
3760
mp_digit d, diff, borrow = 0;
3761
#endif
3762
3763
/*
3764
Subtract and propagate borrow. Up to the precision of b, this
3765
accounts for the digits of b; after that, we just make sure the
3766
carries get to the right place. This saves having to pad b out to
3767
the precision of a just to make the loops work right...
3768
*/
3769
pa = MP_DIGITS(a);
3770
pb = MP_DIGITS(b);
3771
limit = pb + MP_USED(b);
3772
while (pb < limit) {
3773
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3774
w = w + *pa - *pb++;
3775
*pa++ = ACCUM(w);
3776
w >>= MP_DIGIT_BIT;
3777
#else
3778
d = *pa;
3779
diff = d - *pb++;
3780
d = (diff > d); /* detect borrow */
3781
if (borrow && --diff == MP_DIGIT_MAX)
3782
++d;
3783
*pa++ = diff;
3784
borrow = d;
3785
#endif
3786
}
3787
limit = MP_DIGITS(a) + MP_USED(a);
3788
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3789
while (w && pa < limit) {
3790
w = w + *pa;
3791
*pa++ = ACCUM(w);
3792
w >>= MP_DIGIT_BIT;
3793
}
3794
#else
3795
while (borrow && pa < limit) {
3796
d = *pa;
3797
*pa++ = diff = d - borrow;
3798
borrow = (diff > d);
3799
}
3800
#endif
3801
3802
/* Clobber any leading zeroes we created */
3803
s_mp_clamp(a);
3804
3805
/*
3806
If there was a borrow out, then |b| > |a| in violation
3807
of our input invariant. We've already done the work,
3808
but we'll at least complain about it...
3809
*/
3810
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3811
return w ? MP_RANGE : MP_OKAY;
3812
#else
3813
return borrow ? MP_RANGE : MP_OKAY;
3814
#endif
3815
} /* end s_mp_sub() */
3816
3817
/* }}} */
3818
3819
/* Compute c = |a| - |b|, assumes |a| >= |b| */ /* magnitude subtract */
3820
mp_err s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c)
3821
{
3822
mp_digit *pa, *pb, *pc;
3823
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3824
mp_sword w = 0;
3825
#else
3826
mp_digit d, diff, borrow = 0;
3827
#endif
3828
int ix, limit;
3829
mp_err res;
3830
3831
MP_SIGN(c) = MP_SIGN(a);
3832
3833
/* Make sure a has enough precision for the output value */
3834
if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a))))
3835
return res;
3836
3837
/*
3838
Subtract and propagate borrow. Up to the precision of b, this
3839
accounts for the digits of b; after that, we just make sure the
3840
carries get to the right place. This saves having to pad b out to
3841
the precision of a just to make the loops work right...
3842
*/
3843
pa = MP_DIGITS(a);
3844
pb = MP_DIGITS(b);
3845
pc = MP_DIGITS(c);
3846
limit = MP_USED(b);
3847
for (ix = 0; ix < limit; ++ix) {
3848
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3849
w = w + *pa++ - *pb++;
3850
*pc++ = ACCUM(w);
3851
w >>= MP_DIGIT_BIT;
3852
#else
3853
d = *pa++;
3854
diff = d - *pb++;
3855
d = (diff > d);
3856
if (borrow && --diff == MP_DIGIT_MAX)
3857
++d;
3858
*pc++ = diff;
3859
borrow = d;
3860
#endif
3861
}
3862
for (limit = MP_USED(a); ix < limit; ++ix) {
3863
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3864
w = w + *pa++;
3865
*pc++ = ACCUM(w);
3866
w >>= MP_DIGIT_BIT;
3867
#else
3868
d = *pa++;
3869
*pc++ = diff = d - borrow;
3870
borrow = (diff > d);
3871
#endif
3872
}
3873
3874
/* Clobber any leading zeroes we created */
3875
MP_USED(c) = ix;
3876
s_mp_clamp(c);
3877
3878
/*
3879
If there was a borrow out, then |b| > |a| in violation
3880
of our input invariant. We've already done the work,
3881
but we'll at least complain about it...
3882
*/
3883
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3884
return w ? MP_RANGE : MP_OKAY;
3885
#else
3886
return borrow ? MP_RANGE : MP_OKAY;
3887
#endif
3888
}
3889
/* {{{ s_mp_mul(a, b) */
3890
3891
/* Compute a = |a| * |b| */
3892
mp_err s_mp_mul(mp_int *a, const mp_int *b)
3893
{
3894
return mp_mul(a, b, a);
3895
} /* end s_mp_mul() */
3896
3897
/* }}} */
3898
3899
#if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
3900
/* This trick works on Sparc V8 CPUs with the Workshop compilers. */
3901
#define MP_MUL_DxD(a, b, Phi, Plo) \
3902
{ unsigned long long product = (unsigned long long)a * b; \
3903
Plo = (mp_digit)product; \
3904
Phi = (mp_digit)(product >> MP_DIGIT_BIT); }
3905
#elif defined(OSF1)
3906
#define MP_MUL_DxD(a, b, Phi, Plo) \
3907
{ Plo = asm ("mulq %a0, %a1, %v0", a, b);\
3908
Phi = asm ("umulh %a0, %a1, %v0", a, b); }
3909
#else
3910
#define MP_MUL_DxD(a, b, Phi, Plo) \
3911
{ mp_digit a0b1, a1b0; \
3912
Plo = (a & MP_HALF_DIGIT_MAX) * (b & MP_HALF_DIGIT_MAX); \
3913
Phi = (a >> MP_HALF_DIGIT_BIT) * (b >> MP_HALF_DIGIT_BIT); \
3914
a0b1 = (a & MP_HALF_DIGIT_MAX) * (b >> MP_HALF_DIGIT_BIT); \
3915
a1b0 = (a >> MP_HALF_DIGIT_BIT) * (b & MP_HALF_DIGIT_MAX); \
3916
a1b0 += a0b1; \
3917
Phi += a1b0 >> MP_HALF_DIGIT_BIT; \
3918
if (a1b0 < a0b1) \
3919
Phi += MP_HALF_RADIX; \
3920
a1b0 <<= MP_HALF_DIGIT_BIT; \
3921
Plo += a1b0; \
3922
if (Plo < a1b0) \
3923
++Phi; \
3924
}
3925
#endif
3926
3927
#if !defined(MP_ASSEMBLY_MULTIPLY)
3928
/* c = a * b */
3929
void s_mpv_mul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
3930
{
3931
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
3932
mp_digit d = 0;
3933
3934
/* Inner product: Digits of a */
3935
while (a_len--) {
3936
mp_word w = ((mp_word)b * *a++) + d;
3937
*c++ = ACCUM(w);
3938
d = CARRYOUT(w);
3939
}
3940
*c = d;
3941
#else
3942
mp_digit carry = 0;
3943
while (a_len--) {
3944
mp_digit a_i = *a++;
3945
mp_digit a0b0, a1b1;
3946
3947
MP_MUL_DxD(a_i, b, a1b1, a0b0);
3948
3949
a0b0 += carry;
3950
if (a0b0 < carry)
3951
++a1b1;
3952
*c++ = a0b0;
3953
carry = a1b1;
3954
}
3955
*c = carry;
3956
#endif
3957
}
3958
3959
/* c += a * b */
3960
void s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b,
3961
mp_digit *c)
3962
{
3963
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
3964
mp_digit d = 0;
3965
3966
/* Inner product: Digits of a */
3967
while (a_len--) {
3968
mp_word w = ((mp_word)b * *a++) + *c + d;
3969
*c++ = ACCUM(w);
3970
d = CARRYOUT(w);
3971
}
3972
*c = d;
3973
#else
3974
mp_digit carry = 0;
3975
while (a_len--) {
3976
mp_digit a_i = *a++;
3977
mp_digit a0b0, a1b1;
3978
3979
MP_MUL_DxD(a_i, b, a1b1, a0b0);
3980
3981
a0b0 += carry;
3982
if (a0b0 < carry)
3983
++a1b1;
3984
a0b0 += a_i = *c;
3985
if (a0b0 < a_i)
3986
++a1b1;
3987
*c++ = a0b0;
3988
carry = a1b1;
3989
}
3990
*c = carry;
3991
#endif
3992
}
3993
3994
/* Presently, this is only used by the Montgomery arithmetic code. */
3995
/* c += a * b */
3996
void s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
3997
{
3998
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
3999
mp_digit d = 0;
4000
4001
/* Inner product: Digits of a */
4002
while (a_len--) {
4003
mp_word w = ((mp_word)b * *a++) + *c + d;
4004
*c++ = ACCUM(w);
4005
d = CARRYOUT(w);
4006
}
4007
4008
while (d) {
4009
mp_word w = (mp_word)*c + d;
4010
*c++ = ACCUM(w);
4011
d = CARRYOUT(w);
4012
}
4013
#else
4014
mp_digit carry = 0;
4015
while (a_len--) {
4016
mp_digit a_i = *a++;
4017
mp_digit a0b0, a1b1;
4018
4019
MP_MUL_DxD(a_i, b, a1b1, a0b0);
4020
4021
a0b0 += carry;
4022
if (a0b0 < carry)
4023
++a1b1;
4024
4025
a0b0 += a_i = *c;
4026
if (a0b0 < a_i)
4027
++a1b1;
4028
4029
*c++ = a0b0;
4030
carry = a1b1;
4031
}
4032
while (carry) {
4033
mp_digit c_i = *c;
4034
carry += c_i;
4035
*c++ = carry;
4036
carry = carry < c_i;
4037
}
4038
#endif
4039
}
4040
#endif
4041
4042
#if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
4043
/* This trick works on Sparc V8 CPUs with the Workshop compilers. */
4044
#define MP_SQR_D(a, Phi, Plo) \
4045
{ unsigned long long square = (unsigned long long)a * a; \
4046
Plo = (mp_digit)square; \
4047
Phi = (mp_digit)(square >> MP_DIGIT_BIT); }
4048
#elif defined(OSF1)
4049
#define MP_SQR_D(a, Phi, Plo) \
4050
{ Plo = asm ("mulq %a0, %a0, %v0", a);\
4051
Phi = asm ("umulh %a0, %a0, %v0", a); }
4052
#else
4053
#define MP_SQR_D(a, Phi, Plo) \
4054
{ mp_digit Pmid; \
4055
Plo = (a & MP_HALF_DIGIT_MAX) * (a & MP_HALF_DIGIT_MAX); \
4056
Phi = (a >> MP_HALF_DIGIT_BIT) * (a >> MP_HALF_DIGIT_BIT); \
4057
Pmid = (a & MP_HALF_DIGIT_MAX) * (a >> MP_HALF_DIGIT_BIT); \
4058
Phi += Pmid >> (MP_HALF_DIGIT_BIT - 1); \
4059
Pmid <<= (MP_HALF_DIGIT_BIT + 1); \
4060
Plo += Pmid; \
4061
if (Plo < Pmid) \
4062
++Phi; \
4063
}
4064
#endif
4065
4066
#if !defined(MP_ASSEMBLY_SQUARE)
4067
/* Add the squares of the digits of a to the digits of b. */
4068
void s_mpv_sqr_add_prop(const mp_digit *pa, mp_size a_len, mp_digit *ps)
4069
{
4070
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
4071
mp_word w;
4072
mp_digit d;
4073
mp_size ix;
4074
4075
w = 0;
4076
#define ADD_SQUARE(n) \
4077
d = pa[n]; \
4078
w += (d * (mp_word)d) + ps[2*n]; \
4079
ps[2*n] = ACCUM(w); \
4080
w = (w >> DIGIT_BIT) + ps[2*n+1]; \
4081
ps[2*n+1] = ACCUM(w); \
4082
w = (w >> DIGIT_BIT)
4083
4084
for (ix = a_len; ix >= 4; ix -= 4) {
4085
ADD_SQUARE(0);
4086
ADD_SQUARE(1);
4087
ADD_SQUARE(2);
4088
ADD_SQUARE(3);
4089
pa += 4;
4090
ps += 8;
4091
}
4092
if (ix) {
4093
ps += 2*ix;
4094
pa += ix;
4095
switch (ix) {
4096
case 3: ADD_SQUARE(-3); /* FALLTHRU */
4097
case 2: ADD_SQUARE(-2); /* FALLTHRU */
4098
case 1: ADD_SQUARE(-1); /* FALLTHRU */
4099
case 0: break;
4100
}
4101
}
4102
while (w) {
4103
w += *ps;
4104
*ps++ = ACCUM(w);
4105
w = (w >> DIGIT_BIT);
4106
}
4107
#else
4108
mp_digit carry = 0;
4109
while (a_len--) {
4110
mp_digit a_i = *pa++;
4111
mp_digit a0a0, a1a1;
4112
4113
MP_SQR_D(a_i, a1a1, a0a0);
4114
4115
/* here a1a1 and a0a0 constitute a_i ** 2 */
4116
a0a0 += carry;
4117
if (a0a0 < carry)
4118
++a1a1;
4119
4120
/* now add to ps */
4121
a0a0 += a_i = *ps;
4122
if (a0a0 < a_i)
4123
++a1a1;
4124
*ps++ = a0a0;
4125
a1a1 += a_i = *ps;
4126
carry = (a1a1 < a_i);
4127
*ps++ = a1a1;
4128
}
4129
while (carry) {
4130
mp_digit s_i = *ps;
4131
carry += s_i;
4132
*ps++ = carry;
4133
carry = carry < s_i;
4134
}
4135
#endif
4136
}
4137
#endif
4138
4139
#if (defined(MP_NO_MP_WORD) || defined(MP_NO_DIV_WORD)) \
4140
&& !defined(MP_ASSEMBLY_DIV_2DX1D)
4141
/*
4142
** Divide 64-bit (Nhi,Nlo) by 32-bit divisor, which must be normalized
4143
** so its high bit is 1. This code is from NSPR.
4144
*/
4145
mp_err s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo, mp_digit divisor,
4146
mp_digit *qp, mp_digit *rp)
4147
{
4148
mp_digit d1, d0, q1, q0;
4149
mp_digit r1, r0, m;
4150
4151
d1 = divisor >> MP_HALF_DIGIT_BIT;
4152
d0 = divisor & MP_HALF_DIGIT_MAX;
4153
r1 = Nhi % d1;
4154
q1 = Nhi / d1;
4155
m = q1 * d0;
4156
r1 = (r1 << MP_HALF_DIGIT_BIT) | (Nlo >> MP_HALF_DIGIT_BIT);
4157
if (r1 < m) {
4158
q1--, r1 += divisor;
4159
if (r1 >= divisor && r1 < m) {
4160
q1--, r1 += divisor;
4161
}
4162
}
4163
r1 -= m;
4164
r0 = r1 % d1;
4165
q0 = r1 / d1;
4166
m = q0 * d0;
4167
r0 = (r0 << MP_HALF_DIGIT_BIT) | (Nlo & MP_HALF_DIGIT_MAX);
4168
if (r0 < m) {
4169
q0--, r0 += divisor;
4170
if (r0 >= divisor && r0 < m) {
4171
q0--, r0 += divisor;
4172
}
4173
}
4174
if (qp)
4175
*qp = (q1 << MP_HALF_DIGIT_BIT) | q0;
4176
if (rp)
4177
*rp = r0 - m;
4178
return MP_OKAY;
4179
}
4180
#endif
4181
4182
#if MP_SQUARE
4183
/* {{{ s_mp_sqr(a) */
4184
4185
mp_err s_mp_sqr(mp_int *a)
4186
{
4187
mp_err res;
4188
mp_int tmp;
4189
4190
if((res = mp_init_size(&tmp, 2 * USED(a), FLAG(a))) != MP_OKAY)
4191
return res;
4192
res = mp_sqr(a, &tmp);
4193
if (res == MP_OKAY) {
4194
s_mp_exch(&tmp, a);
4195
}
4196
mp_clear(&tmp);
4197
return res;
4198
}
4199
4200
/* }}} */
4201
#endif
4202
4203
/* {{{ s_mp_div(a, b) */
4204
4205
/*
4206
s_mp_div(a, b)
4207
4208
Compute a = a / b and b = a mod b. Assumes b > a.
4209
*/
4210
4211
mp_err s_mp_div(mp_int *rem, /* i: dividend, o: remainder */
4212
mp_int *div, /* i: divisor */
4213
mp_int *quot) /* i: 0; o: quotient */
4214
{
4215
mp_int part, t;
4216
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
4217
mp_word q_msd;
4218
#else
4219
mp_digit q_msd;
4220
#endif
4221
mp_err res;
4222
mp_digit d;
4223
mp_digit div_msd;
4224
int ix;
4225
4226
if(mp_cmp_z(div) == 0)
4227
return MP_RANGE;
4228
4229
/* Shortcut if divisor is power of two */
4230
if((ix = s_mp_ispow2(div)) >= 0) {
4231
MP_CHECKOK( mp_copy(rem, quot) );
4232
s_mp_div_2d(quot, (mp_digit)ix);
4233
s_mp_mod_2d(rem, (mp_digit)ix);
4234
4235
return MP_OKAY;
4236
}
4237
4238
DIGITS(&t) = 0;
4239
MP_SIGN(rem) = ZPOS;
4240
MP_SIGN(div) = ZPOS;
4241
4242
/* A working temporary for division */
4243
MP_CHECKOK( mp_init_size(&t, MP_ALLOC(rem), FLAG(rem)));
4244
4245
/* Normalize to optimize guessing */
4246
MP_CHECKOK( s_mp_norm(rem, div, &d) );
4247
4248
part = *rem;
4249
4250
/* Perform the division itself...woo! */
4251
MP_USED(quot) = MP_ALLOC(quot);
4252
4253
/* Find a partial substring of rem which is at least div */
4254
/* If we didn't find one, we're finished dividing */
4255
while (MP_USED(rem) > MP_USED(div) || s_mp_cmp(rem, div) >= 0) {
4256
int i;
4257
int unusedRem;
4258
4259
unusedRem = MP_USED(rem) - MP_USED(div);
4260
MP_DIGITS(&part) = MP_DIGITS(rem) + unusedRem;
4261
MP_ALLOC(&part) = MP_ALLOC(rem) - unusedRem;
4262
MP_USED(&part) = MP_USED(div);
4263
if (s_mp_cmp(&part, div) < 0) {
4264
-- unusedRem;
4265
#if MP_ARGCHK == 2
4266
assert(unusedRem >= 0);
4267
#endif
4268
-- MP_DIGITS(&part);
4269
++ MP_USED(&part);
4270
++ MP_ALLOC(&part);
4271
}
4272
4273
/* Compute a guess for the next quotient digit */
4274
q_msd = MP_DIGIT(&part, MP_USED(&part) - 1);
4275
div_msd = MP_DIGIT(div, MP_USED(div) - 1);
4276
if (q_msd >= div_msd) {
4277
q_msd = 1;
4278
} else if (MP_USED(&part) > 1) {
4279
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
4280
q_msd = (q_msd << MP_DIGIT_BIT) | MP_DIGIT(&part, MP_USED(&part) - 2);
4281
q_msd /= div_msd;
4282
if (q_msd == RADIX)
4283
--q_msd;
4284
#else
4285
mp_digit r;
4286
MP_CHECKOK( s_mpv_div_2dx1d(q_msd, MP_DIGIT(&part, MP_USED(&part) - 2),
4287
div_msd, &q_msd, &r) );
4288
#endif
4289
} else {
4290
q_msd = 0;
4291
}
4292
#if MP_ARGCHK == 2
4293
assert(q_msd > 0); /* This case should never occur any more. */
4294
#endif
4295
if (q_msd <= 0)
4296
break;
4297
4298
/* See what that multiplies out to */
4299
mp_copy(div, &t);
4300
MP_CHECKOK( s_mp_mul_d(&t, (mp_digit)q_msd) );
4301
4302
/*
4303
If it's too big, back it off. We should not have to do this
4304
more than once, or, in rare cases, twice. Knuth describes a
4305
method by which this could be reduced to a maximum of once, but
4306
I didn't implement that here.
4307
* When using s_mpv_div_2dx1d, we may have to do this 3 times.
4308
*/
4309
for (i = 4; s_mp_cmp(&t, &part) > 0 && i > 0; --i) {
4310
--q_msd;
4311
s_mp_sub(&t, div); /* t -= div */
4312
}
4313
if (i < 0) {
4314
res = MP_RANGE;
4315
goto CLEANUP;
4316
}
4317
4318
/* At this point, q_msd should be the right next digit */
4319
MP_CHECKOK( s_mp_sub(&part, &t) ); /* part -= t */
4320
s_mp_clamp(rem);
4321
4322
/*
4323
Include the digit in the quotient. We allocated enough memory
4324
for any quotient we could ever possibly get, so we should not
4325
have to check for failures here
4326
*/
4327
MP_DIGIT(quot, unusedRem) = (mp_digit)q_msd;
4328
}
4329
4330
/* Denormalize remainder */
4331
if (d) {
4332
s_mp_div_2d(rem, d);
4333
}
4334
4335
s_mp_clamp(quot);
4336
4337
CLEANUP:
4338
mp_clear(&t);
4339
4340
return res;
4341
4342
} /* end s_mp_div() */
4343
4344
4345
/* }}} */
4346
4347
/* {{{ s_mp_2expt(a, k) */
4348
4349
mp_err s_mp_2expt(mp_int *a, mp_digit k)
4350
{
4351
mp_err res;
4352
mp_size dig, bit;
4353
4354
dig = k / DIGIT_BIT;
4355
bit = k % DIGIT_BIT;
4356
4357
mp_zero(a);
4358
if((res = s_mp_pad(a, dig + 1)) != MP_OKAY)
4359
return res;
4360
4361
DIGIT(a, dig) |= ((mp_digit)1 << bit);
4362
4363
return MP_OKAY;
4364
4365
} /* end s_mp_2expt() */
4366
4367
/* }}} */
4368
4369
/* {{{ s_mp_reduce(x, m, mu) */
4370
4371
/*
4372
Compute Barrett reduction, x (mod m), given a precomputed value for
4373
mu = b^2k / m, where b = RADIX and k = #digits(m). This should be
4374
faster than straight division, when many reductions by the same
4375
value of m are required (such as in modular exponentiation). This
4376
can nearly halve the time required to do modular exponentiation,
4377
as compared to using the full integer divide to reduce.
4378
4379
This algorithm was derived from the _Handbook of Applied
4380
Cryptography_ by Menezes, Oorschot and VanStone, Ch. 14,
4381
pp. 603-604.
4382
*/
4383
4384
mp_err s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
4385
{
4386
mp_int q;
4387
mp_err res;
4388
4389
if((res = mp_init_copy(&q, x)) != MP_OKAY)
4390
return res;
4391
4392
s_mp_rshd(&q, USED(m) - 1); /* q1 = x / b^(k-1) */
4393
s_mp_mul(&q, mu); /* q2 = q1 * mu */
4394
s_mp_rshd(&q, USED(m) + 1); /* q3 = q2 / b^(k+1) */
4395
4396
/* x = x mod b^(k+1), quick (no division) */
4397
s_mp_mod_2d(x, DIGIT_BIT * (USED(m) + 1));
4398
4399
/* q = q * m mod b^(k+1), quick (no division) */
4400
s_mp_mul(&q, m);
4401
s_mp_mod_2d(&q, DIGIT_BIT * (USED(m) + 1));
4402
4403
/* x = x - q */
4404
if((res = mp_sub(x, &q, x)) != MP_OKAY)
4405
goto CLEANUP;
4406
4407
/* If x < 0, add b^(k+1) to it */
4408
if(mp_cmp_z(x) < 0) {
4409
mp_set(&q, 1);
4410
if((res = s_mp_lshd(&q, USED(m) + 1)) != MP_OKAY)
4411
goto CLEANUP;
4412
if((res = mp_add(x, &q, x)) != MP_OKAY)
4413
goto CLEANUP;
4414
}
4415
4416
/* Back off if it's too big */
4417
while(mp_cmp(x, m) >= 0) {
4418
if((res = s_mp_sub(x, m)) != MP_OKAY)
4419
break;
4420
}
4421
4422
CLEANUP:
4423
mp_clear(&q);
4424
4425
return res;
4426
4427
} /* end s_mp_reduce() */
4428
4429
/* }}} */
4430
4431
/* }}} */
4432
4433
/* {{{ Primitive comparisons */
4434
4435
/* {{{ s_mp_cmp(a, b) */
4436
4437
/* Compare |a| <=> |b|, return 0 if equal, <0 if a<b, >0 if a>b */
4438
int s_mp_cmp(const mp_int *a, const mp_int *b)
4439
{
4440
mp_size used_a = MP_USED(a);
4441
{
4442
mp_size used_b = MP_USED(b);
4443
4444
if (used_a > used_b)
4445
goto IS_GT;
4446
if (used_a < used_b)
4447
goto IS_LT;
4448
}
4449
{
4450
mp_digit *pa, *pb;
4451
mp_digit da = 0, db = 0;
4452
4453
#define CMP_AB(n) if ((da = pa[n]) != (db = pb[n])) goto done
4454
4455
pa = MP_DIGITS(a) + used_a;
4456
pb = MP_DIGITS(b) + used_a;
4457
while (used_a >= 4) {
4458
pa -= 4;
4459
pb -= 4;
4460
used_a -= 4;
4461
CMP_AB(3);
4462
CMP_AB(2);
4463
CMP_AB(1);
4464
CMP_AB(0);
4465
}
4466
while (used_a-- > 0 && ((da = *--pa) == (db = *--pb)))
4467
/* do nothing */;
4468
done:
4469
if (da > db)
4470
goto IS_GT;
4471
if (da < db)
4472
goto IS_LT;
4473
}
4474
return MP_EQ;
4475
IS_LT:
4476
return MP_LT;
4477
IS_GT:
4478
return MP_GT;
4479
} /* end s_mp_cmp() */
4480
4481
/* }}} */
4482
4483
/* {{{ s_mp_cmp_d(a, d) */
4484
4485
/* Compare |a| <=> d, return 0 if equal, <0 if a<d, >0 if a>d */
4486
int s_mp_cmp_d(const mp_int *a, mp_digit d)
4487
{
4488
if(USED(a) > 1)
4489
return MP_GT;
4490
4491
if(DIGIT(a, 0) < d)
4492
return MP_LT;
4493
else if(DIGIT(a, 0) > d)
4494
return MP_GT;
4495
else
4496
return MP_EQ;
4497
4498
} /* end s_mp_cmp_d() */
4499
4500
/* }}} */
4501
4502
/* {{{ s_mp_ispow2(v) */
4503
4504
/*
4505
Returns -1 if the value is not a power of two; otherwise, it returns
4506
k such that v = 2^k, i.e. lg(v).
4507
*/
4508
int s_mp_ispow2(const mp_int *v)
4509
{
4510
mp_digit d;
4511
int extra = 0, ix;
4512
4513
ix = MP_USED(v) - 1;
4514
d = MP_DIGIT(v, ix); /* most significant digit of v */
4515
4516
extra = s_mp_ispow2d(d);
4517
if (extra < 0 || ix == 0)
4518
return extra;
4519
4520
while (--ix >= 0) {
4521
if (DIGIT(v, ix) != 0)
4522
return -1; /* not a power of two */
4523
extra += MP_DIGIT_BIT;
4524
}
4525
4526
return extra;
4527
4528
} /* end s_mp_ispow2() */
4529
4530
/* }}} */
4531
4532
/* {{{ s_mp_ispow2d(d) */
4533
4534
int s_mp_ispow2d(mp_digit d)
4535
{
4536
if ((d != 0) && ((d & (d-1)) == 0)) { /* d is a power of 2 */
4537
int pow = 0;
4538
#if defined (MP_USE_UINT_DIGIT)
4539
if (d & 0xffff0000U)
4540
pow += 16;
4541
if (d & 0xff00ff00U)
4542
pow += 8;
4543
if (d & 0xf0f0f0f0U)
4544
pow += 4;
4545
if (d & 0xccccccccU)
4546
pow += 2;
4547
if (d & 0xaaaaaaaaU)
4548
pow += 1;
4549
#elif defined(MP_USE_LONG_LONG_DIGIT)
4550
if (d & 0xffffffff00000000ULL)
4551
pow += 32;
4552
if (d & 0xffff0000ffff0000ULL)
4553
pow += 16;
4554
if (d & 0xff00ff00ff00ff00ULL)
4555
pow += 8;
4556
if (d & 0xf0f0f0f0f0f0f0f0ULL)
4557
pow += 4;
4558
if (d & 0xccccccccccccccccULL)
4559
pow += 2;
4560
if (d & 0xaaaaaaaaaaaaaaaaULL)
4561
pow += 1;
4562
#elif defined(MP_USE_LONG_DIGIT)
4563
if (d & 0xffffffff00000000UL)
4564
pow += 32;
4565
if (d & 0xffff0000ffff0000UL)
4566
pow += 16;
4567
if (d & 0xff00ff00ff00ff00UL)
4568
pow += 8;
4569
if (d & 0xf0f0f0f0f0f0f0f0UL)
4570
pow += 4;
4571
if (d & 0xccccccccccccccccUL)
4572
pow += 2;
4573
if (d & 0xaaaaaaaaaaaaaaaaUL)
4574
pow += 1;
4575
#else
4576
#error "unknown type for mp_digit"
4577
#endif
4578
return pow;
4579
}
4580
return -1;
4581
4582
} /* end s_mp_ispow2d() */
4583
4584
/* }}} */
4585
4586
/* }}} */
4587
4588
/* {{{ Primitive I/O helpers */
4589
4590
/* {{{ s_mp_tovalue(ch, r) */
4591
4592
/*
4593
Convert the given character to its digit value, in the given radix.
4594
If the given character is not understood in the given radix, -1 is
4595
returned. Otherwise the digit's numeric value is returned.
4596
4597
The results will be odd if you use a radix < 2 or > 62, you are
4598
expected to know what you're up to.
4599
*/
4600
int s_mp_tovalue(char ch, int r)
4601
{
4602
int val, xch;
4603
4604
if(r > 36)
4605
xch = ch;
4606
else
4607
xch = toupper(ch);
4608
4609
if(isdigit(xch))
4610
val = xch - '0';
4611
else if(isupper(xch))
4612
val = xch - 'A' + 10;
4613
else if(islower(xch))
4614
val = xch - 'a' + 36;
4615
else if(xch == '+')
4616
val = 62;
4617
else if(xch == '/')
4618
val = 63;
4619
else
4620
return -1;
4621
4622
if(val < 0 || val >= r)
4623
return -1;
4624
4625
return val;
4626
4627
} /* end s_mp_tovalue() */
4628
4629
/* }}} */
4630
4631
/* {{{ s_mp_todigit(val, r, low) */
4632
4633
/*
4634
Convert val to a radix-r digit, if possible. If val is out of range
4635
for r, returns zero. Otherwise, returns an ASCII character denoting
4636
the value in the given radix.
4637
4638
The results may be odd if you use a radix < 2 or > 64, you are
4639
expected to know what you're doing.
4640
*/
4641
4642
char s_mp_todigit(mp_digit val, int r, int low)
4643
{
4644
char ch;
4645
4646
if(val >= (unsigned int)r)
4647
return 0;
4648
4649
ch = s_dmap_1[val];
4650
4651
if(r <= 36 && low)
4652
ch = tolower(ch);
4653
4654
return ch;
4655
4656
} /* end s_mp_todigit() */
4657
4658
/* }}} */
4659
4660
/* {{{ s_mp_outlen(bits, radix) */
4661
4662
/*
4663
Return an estimate for how long a string is needed to hold a radix
4664
r representation of a number with 'bits' significant bits, plus an
4665
extra for a zero terminator (assuming C style strings here)
4666
*/
4667
int s_mp_outlen(int bits, int r)
4668
{
4669
return (int)((double)bits * LOG_V_2(r) + 1.5) + 1;
4670
4671
} /* end s_mp_outlen() */
4672
4673
/* }}} */
4674
4675
/* }}} */
4676
4677
/* {{{ mp_read_unsigned_octets(mp, str, len) */
4678
/* mp_read_unsigned_octets(mp, str, len)
4679
Read in a raw value (base 256) into the given mp_int
4680
No sign bit, number is positive. Leading zeros ignored.
4681
*/
4682
4683
mp_err
4684
mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len)
4685
{
4686
int count;
4687
mp_err res;
4688
mp_digit d;
4689
4690
ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
4691
4692
mp_zero(mp);
4693
4694
count = len % sizeof(mp_digit);
4695
if (count) {
4696
for (d = 0; count-- > 0; --len) {
4697
d = (d << 8) | *str++;
4698
}
4699
MP_DIGIT(mp, 0) = d;
4700
}
4701
4702
/* Read the rest of the digits */
4703
for(; len > 0; len -= sizeof(mp_digit)) {
4704
for (d = 0, count = sizeof(mp_digit); count > 0; --count) {
4705
d = (d << 8) | *str++;
4706
}
4707
if (MP_EQ == mp_cmp_z(mp)) {
4708
if (!d)
4709
continue;
4710
} else {
4711
if((res = s_mp_lshd(mp, 1)) != MP_OKAY)
4712
return res;
4713
}
4714
MP_DIGIT(mp, 0) = d;
4715
}
4716
return MP_OKAY;
4717
} /* end mp_read_unsigned_octets() */
4718
/* }}} */
4719
4720
/* {{{ mp_unsigned_octet_size(mp) */
4721
int
4722
mp_unsigned_octet_size(const mp_int *mp)
4723
{
4724
int bytes;
4725
int ix;
4726
mp_digit d = 0;
4727
4728
ARGCHK(mp != NULL, MP_BADARG);
4729
ARGCHK(MP_ZPOS == SIGN(mp), MP_BADARG);
4730
4731
bytes = (USED(mp) * sizeof(mp_digit));
4732
4733
/* subtract leading zeros. */
4734
/* Iterate over each digit... */
4735
for(ix = USED(mp) - 1; ix >= 0; ix--) {
4736
d = DIGIT(mp, ix);
4737
if (d)
4738
break;
4739
bytes -= sizeof(d);
4740
}
4741
if (!bytes)
4742
return 1;
4743
4744
/* Have MSD, check digit bytes, high order first */
4745
for(ix = sizeof(mp_digit) - 1; ix >= 0; ix--) {
4746
unsigned char x = (unsigned char)(d >> (ix * CHAR_BIT));
4747
if (x)
4748
break;
4749
--bytes;
4750
}
4751
return bytes;
4752
} /* end mp_unsigned_octet_size() */
4753
/* }}} */
4754
4755
/* {{{ mp_to_unsigned_octets(mp, str) */
4756
/* output a buffer of big endian octets no longer than specified. */
4757
mp_err
4758
mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen)
4759
{
4760
int ix, pos = 0;
4761
unsigned int bytes;
4762
4763
ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
4764
4765
bytes = mp_unsigned_octet_size(mp);
4766
ARGCHK(bytes <= maxlen, MP_BADARG);
4767
4768
/* Iterate over each digit... */
4769
for(ix = USED(mp) - 1; ix >= 0; ix--) {
4770
mp_digit d = DIGIT(mp, ix);
4771
int jx;
4772
4773
/* Unpack digit bytes, high order first */
4774
for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
4775
unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
4776
if (!pos && !x) /* suppress leading zeros */
4777
continue;
4778
str[pos++] = x;
4779
}
4780
}
4781
if (!pos)
4782
str[pos++] = 0;
4783
return pos;
4784
} /* end mp_to_unsigned_octets() */
4785
/* }}} */
4786
4787
/* {{{ mp_to_signed_octets(mp, str) */
4788
/* output a buffer of big endian octets no longer than specified. */
4789
mp_err
4790
mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen)
4791
{
4792
int ix, pos = 0;
4793
unsigned int bytes;
4794
4795
ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
4796
4797
bytes = mp_unsigned_octet_size(mp);
4798
ARGCHK(bytes <= maxlen, MP_BADARG);
4799
4800
/* Iterate over each digit... */
4801
for(ix = USED(mp) - 1; ix >= 0; ix--) {
4802
mp_digit d = DIGIT(mp, ix);
4803
int jx;
4804
4805
/* Unpack digit bytes, high order first */
4806
for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
4807
unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
4808
if (!pos) {
4809
if (!x) /* suppress leading zeros */
4810
continue;
4811
if (x & 0x80) { /* add one leading zero to make output positive. */
4812
ARGCHK(bytes + 1 <= maxlen, MP_BADARG);
4813
if (bytes + 1 > maxlen)
4814
return MP_BADARG;
4815
str[pos++] = 0;
4816
}
4817
}
4818
str[pos++] = x;
4819
}
4820
}
4821
if (!pos)
4822
str[pos++] = 0;
4823
return pos;
4824
} /* end mp_to_signed_octets() */
4825
/* }}} */
4826
4827
/* {{{ mp_to_fixlen_octets(mp, str) */
4828
/* output a buffer of big endian octets exactly as long as requested. */
4829
mp_err
4830
mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size length)
4831
{
4832
int ix, pos = 0;
4833
unsigned int bytes;
4834
4835
ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
4836
4837
bytes = mp_unsigned_octet_size(mp);
4838
ARGCHK(bytes <= length, MP_BADARG);
4839
4840
/* place any needed leading zeros */
4841
for (;length > bytes; --length) {
4842
*str++ = 0;
4843
}
4844
4845
/* Iterate over each digit... */
4846
for(ix = USED(mp) - 1; ix >= 0; ix--) {
4847
mp_digit d = DIGIT(mp, ix);
4848
int jx;
4849
4850
/* Unpack digit bytes, high order first */
4851
for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
4852
unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
4853
if (!pos && !x) /* suppress leading zeros */
4854
continue;
4855
str[pos++] = x;
4856
}
4857
}
4858
if (!pos)
4859
str[pos++] = 0;
4860
return MP_OKAY;
4861
} /* end mp_to_fixlen_octets() */
4862
/* }}} */
4863
4864
4865
/*------------------------------------------------------------------------*/
4866
/* HERE THERE BE DRAGONS */
4867
4868