⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bnintern.doc

📁 包含哈希,对称以及非对称的经典算法 包含经典事例
💻 DOC
📖 第 1 页 / 共 2 页
字号:
* The organization of the BigNum LibraryAs mentioned in bn.doc, the library should compile on anything with anANSI C compiler and 16 and 32-bit data types.  (Non-power-of-2 wordlengths probably wouldn't be *too* hard, but the matter is likely toremain academic.)  However, assembly subroutines can be added in agreat variety of ways to speed up computations.It's even possible to vary the word length dynamically at run time.Currently, 80x86 and 680x0 assembly primitives have been written in 16and 32-bit forms, as not all members of these families support 32x32->64bit multiply.  In future, 32/64 bit routines may be nice for the MIPSand PowerPC processors.  (The SPARC has a 64-bit extension, but it stillonly produces a maximum 64-bit multiply result.  The MIPS, PowerPC andAlpha give access to 128 bits of product.)The way that this works is that the file bn.c declares a big pile offunction pointers, and the first bnInit() call figures out which setof functions to point these to.  The functions are named so thatit is possible to link several sets into the same executable withoutcollisions.The library can store numbers in big-endian or little-endian word order,although the order of bytes within a word is always the platform nativeorder.  As long as you're using the pure C version, you can compileindependent of the native byte ordering, but the flexibility is availablein case assembly primitives are easier to write one way or the other.(In the absence of other considerations, little-endian is somewhat moreefficient, and is the default.  This is controlled by BN_XXX_ENDIAN.)In fact, it would be possible to change the word order at run time,except that there is no naming convention to support linking infunctions that differ only in endianness.  (Which is because thepoint of doing so is unclear.)The core of the library is in the files lbn??.c and bn??.c, where "??"is 16, 32, or 64.  The 32 and 64-bit files are generated from the 16-bitversion by a simple textual substitution.  The 16-bit files are generallyconsidered the master source, and the others generated from it with sed.Usually, only one set of these files is used on any given platform,but if you want multiple word sizes, you include one for each supportedword size.  The files bninit??.c define a bnInit function for a givenword size, which calls bnInit_??() internally.  Only one of these maybe included at a time, and multiple word sizes are handled by a morecomplex bnInit function such as the ones in bn8086.c and bn68000.c,which determine the word size of the processor they're running on andcall the appropriate bnInit_??() function.The file lbn.h uses <limits.h> to find the platform's available datatypes.  The types are defined both as macros (BNWORD32) and as typedefs(bnword32) which aren't used anywhere but can come in very handy whenusing a debugger (which doesn't know about macros).  Any of these maybe overridden either on the compiler command line (cc -DBN_BIG_ENDIAN-DBNWORD32="unsigned long"), or from an extra include file BNINCLUDEdefined on the command line.  (cc -DBNINCLUDE=lbnmagic.h)  This is thepreferred way to specify assembly primitives.So, for example, to build a 68020 version of the library, compile the32-bit library with -DBNINCLUDE=lbn68020.h, and compile and link inlbn68020.c (which is actually an assembly source file, if you look).Both 16- and 32-bit 80x86 code is included in lbn8086.h and .asm.  Thatcode uses 16-bit large-model addressing.  lbn80386.h and .asm use 32-bitflat-model addressing.Three particularly heavily used macros defined by lbn.h are BIG(x),LITTLE(y) and BIGLITTLE(x,y).  These expand to x (or nothing) ona big-endian system, and y (or nothing) on a little-endian system.These are used to conditionalize the rest of the code without takingup entire lines to say "#ifdef BN_BIG_ENDIAN", "#else" and "#endif".* The lbn??.c filesThe lbn?? file contains the low-level bignum functions.  These universallyexpect their numbers to be passed to them in (buffer, length) form anddo not attempt to extend the buffers.  (In some cases, they do allocatetemporary buffers.)  The buffer pointer points to the least-significantend of the buffer.  If the machine uses big-endian word ordering, thatis a pointer to the end of the buffer.  This is motivated by consideringpointers to point to the boundaries between words (or bytes).  If youconsider a pointer to point to a word rather than between words, thepointer in the big-endian case points to the first word past the end of thebuffer.All of the primitives have names of the form  lbnAddN_16, where the_16 is the word size.  All are surrounded by "#ifndef lbnAddN_16".If you #define lbnAddN_16 previously (either on the command like orin the BNINCLUDE file), the C code will neither define *nor declare* thecorresponding function.  The declaration must be suppressed in case youdeclare it in a magic way with special calling attributes or define it asa macro.If you wish to write an assembly primitive, lbnMulAdd1_??, whichmultiplies N words by 1 word and adds the result to N words, returningthe carry word, is by FAR the most important function - almost all ofthe time spent performing a modular exponentiation is spent in thisfunction.  lbnMulSub1_??, which does the same but subtracts the productand returns a word of borrow, is used heavily in the division routineand thus by GCD and modular inverse computation.These two functions are the only functions which *require* some sortof double-word data type, so if you define them in assembly language,the ?? may be the widest word your C compiler supports; otherwise, youmust limit your implementation to half of the maximum word size.  Otherfunctions will, however, use a double-word data type if available.Actually, there are some even simpler primitives which you can provideto allow double-width multiplication: mul??_ppmm, mul??_ppmma andmul??_ppmmaa These are expected to be defined as macros (all argumentsare always side-effect-free lvalues), and must return two words of resultof the computation m1*m2 + a1 + a2.  It is best to define all three,although any that are not defined will be generated from the others inthe obvious way.  GCC's inline assembler can be used to define these.(The names are borrowed from the GNU MP package.)There is also lbnMulN1_??, which stores the result rather than adding orsubtracting it, but it is less critical.  If it is not provided, butlbnMulAdd1_?? is, it will be implemented in terms of lbnMulAdd1_?? in theobvious way.lbnDiv21_??, which divides two words by one word and returns a quotientand remainder, is greatly sped up by a double-word data type, macrodefinition, or assembly implementation, but has a version which will runwithout one.  If your platform has a double/single divide with remainder,it would help to define this, and it's quite simple.lbnModQ_?? (return a multi-precision number reduced modulo a "quick"(< 65536) modulus is used heavily by prime generation for trial division,but is otherwise little used.Other primitives may be implemented depending on the expected usage mix.It is generally not worth implementing lbnAddN_?? and lbnSubN_?? unlessyou want to start learning to write assembly primitives on somethingsimple; they just aren't used very much.  (Of course, if you do, you'llprobably get some improvements, in both speed and object code size, soit's worth keeping them in, once written.)* The bn??.c filesWhile the lbn??.c files deal in words, the bn??.c files provide thepublic interface to the library and deal in bignum structures.  Thesecontain a buffer pointer, an allocated length, and a used length.The lengths are specified in words, but as long as the user doesn't goprying into such innards, all of the different word-size librariesprovide the same interface; they may be exchanged at link time, or evenat run time.The bn.c file defines a large collection of function pointers and onefunction, bnInit.  bnInit is responsible for setting the function pointersto point to the appropriate bn??.c functions.  Each bn??.c fileprovides a bnInit_?? function which sets itself up; it is the jobof bnInit to figure out which word size to use and call the appropriate

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -