annotate libtommath/bn_mp_exptmod_fast.c @ 348:cd14c94fe89c debug-unrandom

add some more copious debugging
author Matt Johnston <matt@ucc.asn.au>
date Sun, 06 Aug 2006 15:29:41 +0000
parents eed26cff980b
children e66eec4dcba7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
348
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
1 #include "../dbutil.h"
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #include <tommath.h>
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 #ifdef BN_MP_EXPTMOD_FAST_C
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 /* LibTomMath, multiple-precision integer library -- Tom St Denis
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * LibTomMath is a library that provides multiple-precision
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * integer arithmetic as well as number theoretic functionality.
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * The library was designed directly after the MPI library by
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * Michael Fromberger but has been written from scratch with
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * additional optimizations in place.
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 *
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 * The library is free for all purposes without any express
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * guarantee it works.
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 *
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 * Tom St Denis, [email protected], http://math.libtomcrypt.org
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 *
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * The value of k changes based on the size of the exponent.
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 *
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #ifdef MP_LOW_MEM
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #define TAB_SIZE 32
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #else
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #define TAB_SIZE 256
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 mp_int M[TAB_SIZE], res;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 mp_digit buf, mp;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 /* use a pointer to the reduction algorithm. This allows us to use
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 * one of many reduction algorithms without modding the guts of
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 * the code with if statements everywhere.
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 int (*redux)(mp_int*,mp_int*,mp_digit);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 /* find window size */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 x = mp_count_bits (X);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 if (x <= 7) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 winsize = 2;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 } else if (x <= 36) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 winsize = 3;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 } else if (x <= 140) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 winsize = 4;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 } else if (x <= 450) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 winsize = 5;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 } else if (x <= 1303) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 winsize = 6;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 } else if (x <= 3529) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 winsize = 7;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 } else {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 winsize = 8;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 #ifdef MP_LOW_MEM
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if (winsize > 5) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 winsize = 5;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68
348
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
69 dropbear_trace("mp_exptmod_fast x bits %d redmode %d", x, redmode);
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
70
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 /* init M array */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 /* init first cell */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 if ((err = mp_init(&M[1])) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 return err;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 /* now init the second half of the array */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 if ((err = mp_init(&M[x])) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 for (y = 1<<(winsize-1); y < x; y++) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 mp_clear (&M[y]);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 mp_clear(&M[1]);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 return err;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 /* determine and setup reduction code */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 if (redmode == 0) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 #ifdef BN_MP_MONTGOMERY_SETUP_C
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 /* now setup montgomery */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 goto LBL_M;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 #else
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 err = MP_VAL;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 goto LBL_M;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 /* automatically pick the comba one if available (saves quite a few calls/ifs) */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 if (((P->used * 2 + 1) < MP_WARRAY) &&
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 redux = fast_mp_montgomery_reduce;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 } else
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 #ifdef BN_MP_MONTGOMERY_REDUCE_C
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 /* use slower baseline Montgomery method */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 redux = mp_montgomery_reduce;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 #else
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 err = MP_VAL;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 goto LBL_M;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 } else if (redmode == 1) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 /* setup DR reduction for moduli of the form B**k - b */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 mp_dr_setup(P, &mp);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 redux = mp_dr_reduce;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 #else
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 err = MP_VAL;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 goto LBL_M;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 } else {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 #if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 /* setup DR reduction for moduli of the form 2**k - b */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 goto LBL_M;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 redux = mp_reduce_2k;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 #else
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 err = MP_VAL;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 goto LBL_M;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 /* setup result */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 if ((err = mp_init (&res)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 goto LBL_M;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 /* create M table
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 *
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 *
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 * The first half of the table is not computed though accept for M[0] and M[1]
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 if (redmode == 0) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 /* now we need R mod m */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 #else
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 err = MP_VAL;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 /* now set M[1] to G * R mod m */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 } else {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 mp_set(&res, 1);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 for (x = 0; x < (winsize - 1); x++) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 /* create upper table */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 /* set initial mode and bit cnt */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 mode = 0;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 bitcnt = 1;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 buf = 0;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 digidx = X->used - 1;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 bitcpy = 0;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 bitbuf = 0;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 for (;;) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 /* grab next digit as required */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 if (--bitcnt == 0) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 /* if digidx == -1 we are out of digits so break */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 if (digidx == -1) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 break;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 /* read next digit and reset bitcnt */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 buf = X->dp[digidx--];
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 bitcnt = (int)DIGIT_BIT;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
348
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
216 dropbear_trace("mp_exptmod_fast digidx %d buf %u", digidx+1, buf);
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
217
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 /* grab the next msb from the exponent */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 buf <<= (mp_digit)1;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 /* if the bit is zero and mode == 0 then we ignore it
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 * These represent the leading zero bits before the first 1 bit
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224 * in the exponent. Technically this opt is not required but it
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 * does lower the # of trivial squaring/reductions used
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 if (mode == 0 && y == 0) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 continue;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 /* if the bit is zero and mode == 1 then we square */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232 if (mode == 1 && y == 0) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 if ((err = redux (&res, P, mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 }
348
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
239 print_mp_int("mp_exptmod_fast bit=0 mode=1 sq", &res);
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 continue;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 /* else we add it to the window */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 bitbuf |= (y << (winsize - ++bitcpy));
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 mode = 2;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 if (bitcpy == winsize) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 /* ok window is filled so square as required and multiply */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 /* square first */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 for (x = 0; x < winsize; x++) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 if ((err = redux (&res, P, mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 /* then multiply */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 if ((err = redux (&res, P, mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 /* empty window and reset */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 bitcpy = 0;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 bitbuf = 0;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 mode = 1;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 }
348
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
272 print_mp_int("mp_exptmod_fast other way", &res);
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 /* if bits remain then square/multiply */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 if (mode == 2 && bitcpy > 0) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 /* square then multiply if the bit is set */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 for (x = 0; x < bitcpy; x++) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 if ((err = redux (&res, P, mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 /* get next bit of the window */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 bitbuf <<= 1;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 if ((bitbuf & (1 << winsize)) != 0) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 /* then multiply */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 if ((err = redux (&res, P, mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 }
348
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
299 print_mp_int("mp_exptmod_fast remaining bits", &res);
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 if (redmode == 0) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 /* fixup result if Montgomery reduction is used
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 * recall that any value in a Montgomery system is
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 * actually multiplied by R mod n. So we have
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 * to reduce one more time to cancel out the factor
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 * of R.
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 if ((err = redux(&res, P, mp)) != MP_OKAY) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 goto LBL_RES;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 }
348
cd14c94fe89c add some more copious debugging
Matt Johnston <matt@ucc.asn.au>
parents: 284
diff changeset
311 print_mp_int("mp_exptmod_fast fixup", &res);
284
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 /* swap res with Y */
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 mp_exch (&res, Y);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 err = MP_OKAY;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317 LBL_RES:mp_clear (&res);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318 LBL_M:
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 mp_clear(&M[1]);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321 mp_clear (&M[x]);
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 return err;
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 }
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 #endif
eed26cff980b propagate from branch 'au.asn.ucc.matt.ltm.dropbear' (head 6c790cad5a7fa866ad062cb3a0c279f7ba788583)
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326