monotone

monotone Mtn Source Tree

Root/botan/mp_misc.cpp

1/*************************************************
2* MP Misc Functions Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/mp_core.h>
7#include <botan/mp_madd.h>
8
9namespace Botan {
10
11/*************************************************
12* Core Division Operation *
13*************************************************/
14u32bit bigint_divcore(word q, word y1, word y2,
15 word x1, word x2, word x3)
16 {
17 word y0 = 0;
18 bigint_madd(q, y2, 0, 0, &y2, &y0);
19 bigint_madd(q, y1, y0, 0, &y1, &y0);
20
21 if(y0 > x1) return 1;
22 if(y0 < x1) return 0;
23 if(y1 > x2) return 1;
24 if(y1 < x2) return 0;
25 if(y2 > x3) return 1;
26 if(y2 < x3) return 0;
27 return 0;
28 }
29
30/*************************************************
31* Compare two MP integers *
32*************************************************/
33s32bit bigint_cmp(const word x[], u32bit x_size,
34 const word y[], u32bit y_size)
35 {
36 if(x_size < y_size) { return (-bigint_cmp(y, y_size, x, x_size)); }
37
38 while(x_size > y_size)
39 {
40 if(x[x_size-1])
41 return 1;
42 x_size--;
43 }
44 for(u32bit j = x_size; j > 0; j--)
45 {
46 if(x[j-1] > y[j-1]) return 1;
47 if(x[j-1] < y[j-1]) return -1;
48 }
49 return 0;
50 }
51
52/*************************************************
53* Do a 2-word/1-word Division *
54*************************************************/
55word bigint_divop(word n1, word n0, word d)
56 {
57 word high = n1 % d;
58 word quotient = 0;
59 for(u32bit j = 0; j != MP_WORD_BITS; j++)
60 {
61 const word mask = (word)1 << (MP_WORD_BITS-1-j);
62 const bool high_top_bit = (high & MP_WORD_TOP_BIT) ? true : false;
63
64 high = (high << 1) | ((n0 & mask) >> (MP_WORD_BITS-1-j));
65
66 if(high_top_bit || high >= d)
67 {
68 high -= d;
69 quotient |= mask;
70 }
71 }
72 return quotient;
73 }
74
75/*************************************************
76* Do a 2-word/1-word Modulo *
77*************************************************/
78word bigint_modop(word n1, word n0, word d)
79 {
80 word z0 = n1 / d, z1 = bigint_divop(n1, n0, d);
81 word carry = 0;
82 bigint_madd(z1, d, 0, 0, &z1, &carry);
83 bigint_madd(z0, d, carry, 0, &z0, &carry);
84 return (n0-z1);
85 }
86
87/*************************************************
88* Do a word*word->2-word Multiply *
89*************************************************/
90void bigint_wordmul(word a, word b, word* out_low, word* out_high)
91 {
92 const u32bit MP_HWORD_BITS = MP_WORD_BITS / 2;
93 const word MP_HWORD_MASK = ((word)1 << MP_HWORD_BITS) - 1;
94
95 const word a_hi = (a >> MP_HWORD_BITS);
96 const word a_lo = (a & MP_HWORD_MASK);
97 const word b_hi = (b >> MP_HWORD_BITS);
98 const word b_lo = (b & MP_HWORD_MASK);
99
100 word x0 = a_hi * b_hi;
101 word x1 = a_lo * b_hi;
102 word x2 = a_hi * b_lo;
103 word x3 = a_lo * b_lo;
104
105 x2 += x3 >> (MP_HWORD_BITS);
106 x2 += x1;
107 if(x2 < x1)
108 x0 += ((word)1 << MP_HWORD_BITS);
109
110 *out_high = x0 + (x2 >> MP_HWORD_BITS);
111 *out_low = ((x2 & MP_HWORD_MASK) << MP_HWORD_BITS) + (x3 & MP_HWORD_MASK);
112 }
113
114}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status