readme
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· 代码 · 共 796 行 · 第 1/3 页
TXT
796 行
The contents of this file are subject to the Mozilla PublicLicense Version 1.1 (the "License"); you may not use this fileexcept in compliance with the License. You may obtain a copy ofthe License at http://www.mozilla.org/MPL/Software distributed under the License is distributed on an "ASIS" basis, WITHOUT WARRANTY OF ANY KIND, either express orimplied. See the License for the specific language governingrights and limitations under the License.The Original Code is the MPI Arbitrary Precision Integer Arithmeticlibrary.The Initial Developer of the Original Code is Michael J. Fromberger <sting@linguist.dartmouth.edu>Portions created by Michael J. Fromberger are Copyright (C) 1997, 1998, 1999, 2000 Michael J. Fromberger. All Rights Reserved.Contributor(s):Alternatively, the contents of this file may be used under theterms of the GNU General Public License Version 2 or later (the"GPL"), in which case the provisions of the GPL are applicableinstead of those above. If you wish to allow use of yourversion of this file only under the terms of the GPL and not toallow others to use your version of this file under the MPL,indicate your decision by deleting the provisions above andreplace them with the notice and other provisions required bythe GPL. If you do not delete the provisions above, a recipientmay use your version of this file under either the MPL or the GPL.About the MPI Library---------------------The files 'mpi.h' and 'mpi.c' define a simple, arbitrary precisionsigned integer arithmetic package. The implementation is not the mostefficient possible, but the code is small and should be fairly easilyportable to just about any machine that supports an ANSI C compiler,as long as it is capable of at least 16-bit arithmetic (but also seebelow for more on this).This library was written with an eye to cryptographic applications;thus, some care is taken to make sure that temporary values are notleft lying around in memory when they are no longer in use. This addssome overhead for zeroing buffers before they are released back intothe free pool; however, it gives you the assurance that there is onlyone copy of your important values residing in your process's addressspace at a time. Obviously, it is difficult to guarantee anything, ina pre-emptive multitasking environment, but this at least helps youkeep a lid on the more obvious ways your data can get spread around inmemory.Using the Library-----------------To use the MPI library in your program, you must include the header:#include "mpi.h"This header provides all the type and function declarations you'llneed to use the library. Almost all the names defined by the librarybegin with the prefix 'mp_', so it should be easy to keep them fromclashing with your program's namespace (he says, glibly, knowing fullwell there are always pathological cases).There are a few things you may want to configure about the library.By default, the MPI library uses an unsigned short for its digit type,and an unsigned int for its word type. The word type must be bigenough to contain at least two digits, for the primitive arithmetic towork out. On my machine, a short is 2 bytes and an int is 4 bytes --but if you have 64-bit ints, you might want to use a 4-byte digit andan 8-byte word. I have tested the library using 1-byte digits and2-byte words, as well. Whatever you choose to do, the things you needto change are:(1) The type definitions for mp_digit and mp_word.(2) The macro DIGIT_FMT which tells mp_print() how to display a single digit. This is just a printf() format string, so you can adjust it appropriately.(3) The macros DIGIT_MAX and MP_WORD_MAX, which specify the largest value expressible in an mp_digit and an mp_word, respectively.Both the mp_digit and mp_word should be UNSIGNED integer types. Thecode relies on having the full positive precision of the type used fordigits and words.The remaining type definitions should be left alone, for the mostpart. The code in the library does not make any significantassumptions about the sizes of things, but there is little if anyreason to change the other parameters, so I would recommend you leavethem as you found them.The library comes with a Perl script, 'types.pl', which will scan yourcurrent Makefile settings, and attempt to find good definitions forthese types. It relies on a Unix sort of build environment, so itprobably won't work under MacOS or Windows, but it can be convenientif you're porting to a new flavour of Unix. Just run 'types.pl' atthe command line, and it will spit out its results to the standardoutput.Conventions-----------Most functions in the library return a value of type mp_err. Thispermits the library to communicate success or various kinds of failureto the calling program. The return values currently defined are: MP_OKAY - okay, operation succeeded, all's well MP_YES - okay, the answer is yes (same as MP_OKAY) MP_NO - okay, but answer is no (not MP_OKAY) MP_MEM - operation ran out of memory MP_RANGE - input parameter was out of range MP_BADARG - an invalid input parameter was provided MP_UNDEF - no output value is defined for this inputThe only function which currently uses MP_UNDEF is mp_invmod().Division by zero is undefined, but the division functions will returnMP_RANGE for a zero divisor. MP_BADARG usually means you passed abogus mp_int structure to the function. MP_YES and MP_NO are not usedby the library itself; they're defined so you can use them in your ownextensions.If you need a readable interpretation of these error codes in yourprogram, you may also use the mp_strerror() function. This functiontakes an mp_err as input, and returns a pointer to a human-readablestring describing the meaning of the error. These strings are storedas constants within the library, so the caller should not attempt tomodify or free the memory associated with these strings.The library represents values in signed-magnitude format. Valuesstrictly less than zero are negative, all others are consideredpositive (zero is positive by fiat). You can access the 'sign' memberof the mp_int structure directly, but better is to use the mp_cmp_z()function, to find out which side of zero the value lies on.Most arithmetic functions have a single-digit variant, as well as thefull arbitrary-precision. An mp_digit is an unsigned value between 0and DIGIT_MAX inclusive. The radix is available as RADIX. The numberof bits in a given digit is given as DIGIT_BIT.Generally, input parameters are given before output parameters.Unless otherwise specified, any input parameter can be re-used as anoutput parameter, without confusing anything.The basic numeric type defined by the library is an mp_int. Virtuallyall the functions in the library take a pointer to an mp_int as one oftheir parameters. An explanation of how to create and use these<HR><A NAME="p23"><H3>Problem 23:</H3>structures follows. And so, without further ado...Initialization and Cleanup--------------------------The basic numeric type defined by the library is an 'mp_int'.However, it is not sufficient to simply declare a variable of typemp_int in your program. These variables also need to be initializedbefore they can be used, to allocate the internal storage they requirefor computation.This is done using one of the following functions: mp_init(mp_int *mp); mp_init_copy(mp_int *mp, mp_int *from); mp_init_size(mp_int *mp, mp_size p);Each of these requires a pointer to a structure of type mp_int. Thebasic mp_init() simply initializes the mp_int to a default size, andsets its value to zero. If you would like to initialize a copy of anexisting mp_int, use mp_init_copy(), where the 'from' parameter is themp_int you'd like to make a copy of. The third function,mp_init_size(), permits you to specify how many digits of precisionshould be preallocated for your mp_int. This can help the libraryavoid unnecessary re-allocations later on.The default precision used by mp_init() can be retrieved using: precision = mp_get_prec();This returns the number of digits that will be allocated. You canchange this value by using: mp_set_prec(unsigned int prec);Any positive value is acceptable -- if you pass zero, the defaultprecision will be re-set to the compiled-in library default (this isspecified in the header file 'mpi-config.h', and typically defaults to8 or 16).Just as you must allocate an mp_int before you can use it, you mustclean up the structure when you are done with it. This is performedusing the mp_clear() function. Remember that any mp_int that youcreate as a local variable in a function must be mp_clear()'d beforethat function exits, or else the memory allocated to that mp_int willbe orphaned and unrecoverable.To set an mp_int to a given value, the following functions are given: mp_set(mp_int *mp, mp_digit d); mp_set_int(mp_int *mp, long z);The mp_set() function sets the mp_int to a single digit value, whilemp_set_int() sets the mp_int to a signed long integer value.To set an mp_int to zero, use: mp_zero(mp_int *mp);Copying and Moving------------------If you have two initialized mp_int's, and you want to copy the valueof one into the other, use: mp_copy(from, to)This takes care of clearing the old value of 'to', and copies the newvalue into it. If 'to' is not yet initialized, use mp_init_copy()instead (see above).Note: The library tries, whenever possible, to avoid allocating---- new memory. Thus, mp_copy() tries first to satisfy the needs of the copy by re-using the memory already allocated to 'to'. Only if this proves insufficient will mp_copy() actually allocate new memory. For this reason, if you know a priori that 'to' has enough available space to hold 'from', you don't need to check the return value of mp_copy() for memory failure. The USED() macro tells you how many digits are used by an mp_int, and the ALLOC() macro tells you how many are allocated.If you have two initialized mp_int's, and you want to exchange theirvalues, use: mp_exch(a, b)This is better than using mp_copy() with a temporary, since it willnot (ever) touch the memory allocator -- it just swaps the exactcontents of the two structures. The mp_exch() function cannot fail;if you pass it an invalid structure, it just ignores it, and doesnothing.Basic Arithmetic----------------Once you have initialized your integers, you can operate on them. Thebasic arithmetic functions on full mp_int values are:mp_add(a, b, c) - computes c = a + bmp_sub(a, b, c) - computes c = a - bmp_mul(a, b, c) - computes c = a * bmp_sqr(a, b) - computes b = a * amp_div(a, b, q, r) - computes q, r such that a = bq + r
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?