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

📄 example.bas

📁 CRC计算器
💻 BAS
📖 第 1 页 / 共 3 页
字号:
ShowResult hQuotient, kOPLEN    'Display the results of this operation
ShowResult hRemainder, kOPLEN   'Display the results of this operation


'===============================================================
' Squaring:
'===============================================================

'Now let's try the EPS_Square function. This calculates square of A,
'where A = &HB48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9.
'Result should be: 7F59CE6A 38E5FE54 A64FD082 31E76309 596768A2
'                  B3919B70 CB3596C6 801C78EF 795725F4 A1555191
EPS_SetBufferWord hOp1, 0, EPS_kgFULLWORD, &H721C09A9
EPS_SetBufferWord hOp1, 1, EPS_kgFULLWORD, &H8CDB8A84
EPS_SetBufferWord hOp1, 2, EPS_kgFULLWORD, &H5CD0FC25
EPS_SetBufferWord hOp1, 3, EPS_kgFULLWORD, &H4F2381EF
EPS_SetBufferWord hOp1, 4, EPS_kgFULLWORD, &HB48F48B0

EPS_Square hOp1, hResult, kOPLEN
ShowResult hResult, (2 * kOPLEN)  'Display the results of this operation



'===============================================================
' Shifting:
'===============================================================

'Now let's test the word shift functions. These functions shift a buffer left or right
'by a given number of words (not bits!). First the left shift. We'll shift operand1
'left one word at a time, and after each shift we'll see that a zero word has been
'shifted into the LSW position, and the buffer is 1 word longer. operand1 starts with a
'value of B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9, so the shifted result
'will be: B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000.
EPS_ClearBuffer hResult, kRESULTLEN  'Make room for growing result
EPS_WordShiftLeft hResult, hOp1, 1, kOPLEN
'Shifting again gives us B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000 00000000:
EPS_WordShiftLeft hResult, hResult, 1, kOPLEN + 1
'Once more gives us B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000 00000000 00000000:
EPS_WordShiftLeft hResult, hResult, 1, kOPLEN + 2
ShowResult hResult, (kOPLEN + 3)  'Display the results of this operation

'Now we shift back towards the right. Note that with each shift a zero word is
'shifted into the MSW position, and the LSW is lost. The result of the first
'shift is 00000000 B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000 00000000:
EPS_WordShiftRight hResult, hResult, 1, kOPLEN + 3
'Shifting again gives 00000000 00000000 B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9 00000000:
EPS_WordShiftRight hResult, hResult, 1, kOPLEN + 2
'Once more gives us 00000000 00000000 00000000 B48F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9:
EPS_WordShiftRight hResult, hResult, 1, kOPLEN + 1
ShowResult hResult, (kOPLEN + 3)  'Display the results of this operation



'===============================================================
' Testing Buffers:
'===============================================================

'Now let's try the EPS_TestBuffer() function. The result buffer still holds the result
'of the previous shifting, so it's non-zero. Its MSW is B48F48B0. In CypherMathWin32, a buffer
'is considered to be "less than zero" if the MSB of its MSW is set, which is the
'case here:
status = EPS_TestBuffer(hResult, kOPLEN)    'Should be "less than zero"

'Note that constants for result code returned in "status" are defined
'in CypherMathWin32.h for the various result values.

'We can make the buffer "greater than zero" by clearing the MSB of its MSW:
EPS_SetBufferWord hResult, kOPLEN - 1, EPS_kgFULLWORD, &H348F48B0   'Sign bit cleared
'Now we retest the buffer:
status = EPS_TestBuffer(hResult, kOPLEN)    'Should be "greater than zero" now
'Clear the buffer:
EPS_ClearBuffer hResult, kOPLEN
status = EPS_TestBuffer(hResult, kOPLEN)    'Should be "equal to zero" now


'===============================================================
' Comparing Two Buffers:
'===============================================================

'To test the EPS_Compare() function we'll compare the operand1 buffer to operand2.
'Operand1 still holds 348F48B0 4F2381EF 5CD0FC25 8CDB8A84 721C09A9, and
'operand2 still holds FB9C69FD DCD3CA5C 5F9FF641 DF160E65 F3155D2F from the
'earlier operations. Therefore comparing (operand1 - operand2) should result in
'a "less than" status, since the compare is unsigned. Note that neither operand
'is actually altered:
status = EPS_Compare(hOp1, hOp2, kOPLEN)   'Should be "less than"

'Note that constants for result code returned in "status" are defined
'in CypherMathWin32.h for the various result values.

'If we compare them in the other order, we'll get a "greater than" status:
status = EPS_Compare(hOp2, hOp1, kOPLEN)   'Should be "greater than"
'If we copy operand1 into the result buffer, then compare operand1 to the result
'buffer, we'll get the "equal to" status:
EPS_CopyBuffer hOp1, hResult, kOPLEN
status = EPS_Compare(hOp1, hResult, kOPLEN)     'Should be "equal to"


'===============================================================
' Setting a Buffer to "1":
'===============================================================

'Now we'll test the EPS_UnityBuffer() function, which sets a buffer to 1. We'll
'use the result buffer:
EPS_UnityBuffer hResult, kOPLEN  'result buffer is now "1"
ShowResult hResult, kOPLEN    'Display the results of this operation



'===============================================================
' Modular Math:
'===============================================================

'Now on to some modular calculations:

'We'll need a buffer to hold the modulus for modular calculations:
hModulus = EPS_CreateBuffer(kOPLEN)

'We'll need storage for the Montgomery Representations of operand1 and operand2.
'Call the buffers operand1prime and operand2prime:
hOp1Prime = EPS_CreateBuffer(kOPLEN)
hOp2Prime = EPS_CreateBuffer(kOPLEN)

'First, re-initialize our operands with 160 bit values.
'Use B49E888D 945A4A20 4D4CC654 0396C2BE D081B582 for operand1 and
'use 0B9C69FD DCD3CA5C 5F9FF641 DF160E65 F3155D2F for operand2.

EPS_SetBufferWord hOp1, 0, EPS_kgFULLWORD, &HD081B582  'LSW
EPS_SetBufferWord hOp1, 1, EPS_kgFULLWORD, &H396C2BE
EPS_SetBufferWord hOp1, 2, EPS_kgFULLWORD, &H4D4CC654
EPS_SetBufferWord hOp1, 3, EPS_kgFULLWORD, &H945A4A20
EPS_SetBufferWord hOp1, 4, EPS_kgFULLWORD, &HB49E888D  'MSW


EPS_SetBufferWord hOp2, 0, EPS_kgFULLWORD, &HF3155D2F  'LSW
EPS_SetBufferWord hOp2, 1, EPS_kgFULLWORD, &HDF160E65
EPS_SetBufferWord hOp2, 2, EPS_kgFULLWORD, &H5F9FF641
EPS_SetBufferWord hOp2, 3, EPS_kgFULLWORD, &HDCD3CA5C
EPS_SetBufferWord hOp2, 4, EPS_kgFULLWORD, &HB9C69FD   'MSW


'We'll also need a modulus, so lets initialize the modulus buffer
'to D46DD318 96D2FC6F E9343A13 B1F2FAE0 3E7288AB. Note that
'the modulus is odd and its MSB is set. This is typical in cryptographic
'applications:

EPS_SetBufferWord hModulus, 0, EPS_kgFULLWORD, &H3E7288AB   'LSW (Note: This word is n0)
EPS_SetBufferWord hModulus, 1, EPS_kgFULLWORD, &HB1F2FAE0
EPS_SetBufferWord hModulus, 2, EPS_kgFULLWORD, &HE9343A13
EPS_SetBufferWord hModulus, 3, EPS_kgFULLWORD, &H96D2FC6F
EPS_SetBufferWord hModulus, 4, EPS_kgFULLWORD, &HD46DD318   'MSW


'===============================================================
' Finding n0':
'===============================================================

'For the subsequent modular calculation, we'll need the value of n0', so
'let's calculate it now. We'll need the LSW of the modulus, n0:
n0 = EPS_GetBufferWord(hModulus, 0, EPS_kgFULLWORD)
EPS_FindN0Prime n0, n0prime      'Should be &H101ACDFD for this modulus


'===============================================================
' Computing Montgomery Representations of Numbers:
'===============================================================

'Now let's get the Montgomery Representations of operand1 and operand2. We'll
'store them in operand1prime and operand2prime. First convert operand1 to operand1prime:
EPS_MontRep hOp1, hOp1Prime, hModulus, kOPLEN
'operand1prime is now A3E9C74C F51EBFFE 8C3ACA2A 423CE7BD 85F3DB83, the
'Montgomery Image of operand1 (with respect to our modulus[]).
ShowResult hOp1Prime, kOPLEN    'Display the results of this operation

'Now convert operand2 to operand2prime:
EPS_MontRep hOp2, hOp2Prime, hModulus, kOPLEN
'operand2prime is now 6E1A32F2 458BF7CC 5C924A0C 090B2B40 58C77D2D, the
'Montgomery Image of operand2 (with respect to our modulus[]).
ShowResult hOp2Prime, kOPLEN    'Display the results of this operation

'The inverse of the MontRep function is the NormalRep function. We'll use
'hOp1Prime to test this. It's normal representation should, of course, be
'hOp1:
'  a' = &HA3E9C74C F51EBFFE 8C3ACA2A 423CE7BD 85F3DB83
EPS_NormalRep hOp1Prime, hResult, hModulus, n0prime, kOPLEN
'Result is &HB49E888D 945A4A20 4D4CC654 0396C2BE D081B582, the original operand1
ShowResult hResult, kOPLEN    'Display the results of this operation



'===============================================================
' Computing Montgomery Products:
'===============================================================

'Let's compute the Montgomery Product of operand1prime and operand2prime, with respect
'to our modulus, and the n0prime we calculated above. Store the result in result[]:
EPS_MontPro hOp1Prime, hOp2Prime, hResult, hModulus, n0prime, kOPLEN
'The result is 9F9598E2 313C7C9A 8D2D2C9E 7BF2EAF2 D3CB1D77.
ShowResult hResult, kOPLEN    'Display the results of this operation


'===============================================================
' Montgomery Exponentiation:
'===============================================================

'Now let's try a Montgomery Exponentiation. We'll use operand1prime as the Montgomery
'Image of our base, an exponent of &H1234, and the same modulus from the previous
'calculation. The Montgomery image of the result will be given by the following
'expression: (operand1prime[] ^ &H1234) % modulus[]. We'll store the result in
'the result[] buffer. First, create a buffer for the exponent and set it:
hExponent = EPS_CreateBuffer(kOPLEN)
EPS_SetBufferWord hExponent, 0, EPS_kgFULLWORD, &H1234  'LSW

'Now the exponetiation. Note the use of "1" for the exponent length and kOPLEN
'for the base's length:
EPS_MontExp hOp1Prime, hExponent, hResult, hModulus, n0prime, 1, kOPLEN
'The result is 43C7C091 942B7AFD 98671C13 DC085365 1F223812.
ShowResult hResult, kOPLEN    'Display the results of this operation



'Let's try a longer exponent, 64 bits. We'll use C90260FF 4A27EB5B:
EPS_SetBufferWord hExponent, 0, EPS_kgFULLWORD, &H4A27EB5B  'LSW
EPS_SetBufferWord hExponent, 1, EPS_kgFULLWORD, &HC90260FF  'MSW

'Now the exponetiation. Note the use of "2" for the exponent length this time. The
'base is the same so again its length will be kOPLEN:
EPS_MontExp hOp1Prime, hExponent, hResult, hModulus, n0prime, 2, kOPLEN
'The result is D45DFE57 06728526 81F7FAB8 F6E099FD B9A8D248.
ShowResult hResult, kOPLEN    'Display the results of this operation


'===============================================================
' Montgomery Exponentiation in fixed-time:
'===============================================================
'The EPS_MontExpFT function attempts to control the running time as closely
'as possible, to avoid variations in running time based on the value of the
'exponent.  Of course, this routine will run somewhat slower than EPS_MontExp,
'but if your application requires additional resistance to timing attacks,
'EPS_MontExpFT is the function to use. We'll repeat the previous operation:
EPS_MontExpFT hOp1Prime, hExponent, hResult, hModulus, n0prime, 2, kOPLEN
'The result is still D45DFE57 06728526 81F7FAB8 F6E099FD B9A8D248.
ShowResult hResult, kOPLEN    'Display the results of this operation



'===============================================================
' Bigger Montgomery Exponentiation:
'===============================================================

'Now let's try a bigger Montgomery exponentiation, say a 512 bit (16 word)
'base raised to a 160 bit (5 word) exponent with a 512 bit (16 word) modulus.

'Allocate the storage for the big base and its Montgomery Image:
hBigBase = EPS_CreateBuffer(kBIGOPLEN)
hBigBasePrime = EPS_CreateBuffer(kBIGOPLEN)

'Allocate the storage for the big modulus:
hBigModulus = EPS_CreateBuffer(kBIGOPLEN)

'Allocate storage for the result of the big exponentiation:
hBigResult = EPS_CreateBuffer(kBIGOPLEN)


'Load a 512 bit number for our base:
EPS_SetBufferWord hBigBase, 0, EPS_kgFULLWORD, &H8255C104   'LSW
EPS_SetBufferWord hBigBase, 1, EPS_kgFULLWORD, &HBF782257
EPS_SetBufferWord hBigBase, 2, EPS_kgFULLWORD, &H20DADAB
EPS_SetBufferWord hBigBase, 3, EPS_kgFULLWORD, &H43EC1D3E
EPS_SetBufferWord hBigBase, 4, EPS_kgFULLWORD, &H23BBECC4
EPS_SetBufferWord hBigBase, 5, EPS_kgFULLWORD, &H47BD54B3
EPS_SetBufferWord hBigBase, 6, EPS_kgFULLWORD, &HDF89B717
EPS_SetBufferWord hBigBase, 7, EPS_kgFULLWORD, &H781D21F2
EPS_SetBufferWord hBigBase, 8, EPS_kgFULLWORD, &HA3DC4F71
EPS_SetBufferWord hBigBase, 9, EPS_kgFULLWORD, &H25248B58
EPS_SetBufferWord hBigBase, 10, EPS_kgFULLWORD, &H7E7ADAF
EPS_SetBufferWord hBigBase, 11, EPS_kgFULLWORD, &H58186B50
EPS_SetBufferWord hBigBase, 12, EPS_kgFULLWORD, &H3872CE99
EPS_SetBufferWord hBigBase, 13, EPS_kgFULLWORD, &H49A9E7CD
EPS_SetBufferWord hBigBase, 14, EPS_kgFULLWORD, &H21F9990
EPS_SetBufferWord hBigBase, 15, EPS_kgFULLWORD, &HB3085510  'MSW

'Load a 160 bit number for our exponent:
EPS_SetBufferWord hExponent, 0, EPS_kgFULLWORD, &HC8F8EF67  'LSW
EPS_SetBufferWord hExponent, 1, EPS_kgFULLWORD, &H74C80102
EPS_SetBufferWord hExponent, 2, EPS_kgFULLWORD, &HE2C942B5
EPS_SetBufferWord hExponent, 3, EPS_kgFULLWORD, &HD0192D54
EPS_SetBufferWord hExponent, 4, EPS_kgFULLWORD, &H6B2CD935  'MSW

'Load a 512 bit number for our modulus:
EPS_SetBufferWord hBigModulus, 0, EPS_kgFULLWORD, &H7FC543F7   'LSW
EPS_SetBufferWord hBigModulus, 1, EPS_kgFULLWORD, &H9CC81F42
EPS_SetBufferWord hBigModulus, 2, EPS_kgFULLWORD, &HA9FE5F24
EPS_SetBufferWord hBigModulus, 3, EPS_kgFULLWORD, &H6AF17AC8
EPS_SetBufferWord hBigModulus, 4, EPS_kgFULLWORD, &H263E9023
EPS_SetBufferWord hBigModulus, 5, EPS_kgFULLWORD, &HD88C58D7
EPS_SetBufferWord hBigModulus, 6, EPS_kgFULLWORD, &HD02BBE13
EPS_SetBufferWord hBigModulus, 7, EPS_kgFULLWORD, &H12DB6E7D
EPS_SetBufferWord hBigModulus, 8, EPS_kgFULLWORD, &H71C86224
EPS_SetBufferWord hBigModulus, 9, EPS_kgFULLWORD, &H3FE365CF
EPS_SetBufferWord hBigModulus, 10, EPS_kgFULLWORD, &HCE254454

⌨️ 快捷键说明

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