1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
*----------------------------------------------------------------------
*
* tclTomMathInterface.c --
*
* This file contains procedures that are used as a 'glue'
* layer between Tcl and libtommath.
*
* Copyright (c) 2005 by Kevin B. Kenny. All rights reserved.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclTomMathInterface.c,v 1.3 2005/10/08 14:42:45 dgp Exp $
*/
#include "tclInt.h"
#include "tommath.h"
#include <limits.h>
/*
*----------------------------------------------------------------------
*
* TclBNAlloc --
*
* Allocate memory for libtommath.
*
* Results:
* Returns a pointer to the allocated block.
*
* This procedure is a wrapper around Tcl_Alloc, needed because of
* a mismatched type signature between Tcl_Alloc and malloc.
*
*----------------------------------------------------------------------
*/
extern void *
TclBNAlloc( size_t x )
{
return (void*) Tcl_Alloc( (unsigned int) x );
}
/*
*----------------------------------------------------------------------
*
* TclBNAlloc --
*
* Change the size of an allocated block of memory in libtommath
*
* Results:
* Returns a pointer to the allocated block.
*
* This procedure is a wrapper around Tcl_Realloc, needed because of
* a mismatched type signature between Tcl_Realloc and realloc.
*
*----------------------------------------------------------------------
*/
extern void *
TclBNRealloc( void* p, size_t s )
{
return (void*) Tcl_Realloc( (char*) p, (unsigned int) s );
}
/*
*----------------------------------------------------------------------
*
* TclBNFree --
*
* Free allocated memory in libtommath.
*
* Results:
* None.
*
* Side effects:
* Memory is freed.
*
* This function is simply a wrapper around Tcl_Free, needed in
* libtommath because of a type mismatch between free and Tcl_Free.
*
*----------------------------------------------------------------------
*/
extern void
TclBNFree( void* p )
{
Tcl_Free( (char*) p);
}
/*
*----------------------------------------------------------------------
*
* TclBNInitBignumFromLong --
*
* Allocate and initialize a 'bignum' from a native 'long'.
*
* Results:
* None.
*
* Side effects:
* The 'bignum' is constructed.
*
*----------------------------------------------------------------------
*/
extern void
TclBNInitBignumFromLong( mp_int* a, long initVal )
{
int status;
unsigned long v;
mp_digit* p;
/*
* Allocate enough memory to hold the largest possible long
*/
status = mp_init_size( a, ( ( CHAR_BIT * sizeof( long ) + DIGIT_BIT - 1 )
/ DIGIT_BIT ) );
if ( status != MP_OKAY ) {
Tcl_Panic( "initialization failure in TclBNInitBignumFromLong" );
}
/* Convert arg to sign and magnitude */
if ( initVal < 0 ) {
a->sign = MP_NEG;
v = -initVal;
} else {
a->sign = MP_ZPOS;
v = initVal;
}
/* Store the magnitude in the bignum. */
p = a->dp;
while ( v ) {
*p++ = (mp_digit) ( v & MP_MASK );
v >>= MP_DIGIT_BIT;
}
a->used = p - a->dp;
}
/*
*----------------------------------------------------------------------
*
* TclBNInitBignumFromWideInt --
*
* Allocate and initialize a 'bignum' from a Tcl_WideInt
*
* Results:
* None.
*
* Side effects:
* The 'bignum' is constructed.
*
*----------------------------------------------------------------------
*/
extern void
TclBNInitBignumFromWideInt(mp_int* a,
/* Bignum to initialize */
Tcl_WideInt v)
/* Initial value */
{
if (v < (Tcl_WideInt)0) {
TclBNInitBignumFromWideUInt(a, (Tcl_WideUInt)(-v));
mp_neg(a, a);
} else {
TclBNInitBignumFromWideUInt(a, (Tcl_WideUInt)v);
}
}
/*
*----------------------------------------------------------------------
*
* TclBNInitBignumFromWideUInt --
*
* Allocate and initialize a 'bignum' from a Tcl_WideUInt
*
* Results:
* None.
*
* Side effects:
* The 'bignum' is constructed.
*
*----------------------------------------------------------------------
*/
extern void
TclBNInitBignumFromWideUInt(mp_int* a,
/* Bignum to initialize */
Tcl_WideUInt v)
/* Initial value */
{
int status;
mp_digit* p;
/*
* Allocate enough memory to hold the largest possible Tcl_WideUInt
*/
status = mp_init_size(a, ((CHAR_BIT * sizeof( Tcl_WideUInt )
+ DIGIT_BIT - 1)
/ DIGIT_BIT));
if (status != MP_OKAY) {
Tcl_Panic( "initialization failure in TclBNInitBignumFromWideUInt" );
}
a->sign = MP_ZPOS;
/* Store the magnitude in the bignum. */
p = a->dp;
while ( v ) {
*p++ = (mp_digit) ( v & MP_MASK );
v >>= MP_DIGIT_BIT;
}
a->used = p - a->dp;
}
|