📄 example.bas
字号:
Attribute VB_Name = "Example"
Option Explicit
'Most of our operands will be 160 (five 32 bit words) bits long.
'Lets define a constant for this operand length:
Public Const kOPLEN = 5
'Results should be twice as long as the operands (320 bits or
'ten 32 bit words) to accomodate multiplies:
Public Const kRESULTLEN = 10
'We'll also be trying a "big" exponentiation (512 base and exponent), which is 16 words.
Public Const kBIGOPLEN = 16
Public Sub ShowResult(ByVal hOp As Long, buflen As Integer)
'Takes the operand pointed to by hOp and displays it as a string representation.
Dim OpWord As Integer
Dim Pad As String
Dim S As String
Dim WordValMSBs As Long, WordValLSBs As Long
Dim MSBsInHex As String, LSBsInHex As String
'This sub pulls each 32-bit word from the CypherMathWin32 buffer
'whose handle is passed in hOp. Since VB has no unsigned 32-bit
'data type ("Long" is 32 bits, but signed) we'll need to extract
'the upper 16 bits, then the lower 16 bits. Once each 16-bit half
'has been converted to a Hex string, we can re-combine them into
'a 32-bit word. We'll build up all the 32-bit words in the string S:
S = "" 'Clear the result string
For OpWord = (buflen - 1) To 0 Step -1
'For each 32-word in the operand buffers:
'Get the 16 MSBs:
WordValMSBs = EPS_GetBufferWord(hOp, OpWord, EPS_kgHIWORD)
'Get the 16 LSB:
WordValLSBs = EPS_GetBufferWord(hOp, OpWord, EPS_kgLOWORD)
'Format MSBs as hex string:
MSBsInHex = Hex$(WordValMSBs)
Pad = String$(4 - Len(MSBsInHex), "0") 'Add any leading zeroes needed
MSBsInHex = Pad + MSBsInHex
'Format LSBs as string:
LSBsInHex = Hex$(WordValLSBs)
Pad = String$(4 - Len(LSBsInHex), "0") 'Add any leading zeroes needed
LSBsInHex = Pad + LSBsInHex
'Append all 32 bits to string S amd add a trailing
'space to separate each 32-bit word:
S = S & MSBsInHex & LSBsInHex & " "
Next OpWord
'Show final string:
frmResult.txtResult.Text = S
End Sub
Public Sub Main()
'===============================================================================
' DESCRIPTION:
' -----------
' This sub contains example code for the CypherMathWin32
' multiprecision unsigned integer math functions.
'
' IMPORTANT: This is a rather long sub, because it shows examples of
' every CypherMath function. It is expected that you will single step through
' this file (using your VB IDE), and verify the results of each calculation
' by examining the displayed memory buffers. The expected result for each
' calculation is given in a comment near the call to the CypherMath
' function.
'
' Some basic buffer managment examples are show first, followed by basic math
' operations, then modular math operations, then bit-wise logical operations.
'
'
' REVISION HISTORY:
' ----------------
' 24 November, 1999 - Initial release.
'
' COPYRIGHT NOTICE:
' ----------------
' Copyright (c) 1999, EPS/Solutions. You may use and/or redistribute
' this code as you see fit.
'
' CONTACT INFORMATION:
' -------------------
' EPS/Solutions
' PO Box 862
' Broomall, PA. 19008-0862 (USA)
' Web site: www.cyphercalc.com/math
' Email: support@cyphercalc.com
'
'===============================================================================
'============================================================================
' Creating Handles to CypherMath Buffers:
'============================================================================
'Let's start by creating the handles to all of our
'operands, and buffers:
'Create some operands to feed the calculations.
Dim hOp1 As Long 'Handle for operand1 buffer
Dim hOp2 As Long 'Handle for operand2 buffer
'Now create a buffer to hold calculation results.
Dim hResult As Long 'Handle for "calculation results" buffer
'For divisions, we'll need a buffer to hold the quotient and
'another to hold the remainder:
Dim hQuotient As Long
Dim hRemainder As Long
'We'll need a buffer to hold the modulus for modular calculations:
Dim hModulus As Long
'For Montgomery Math calculations, we'll also need the values of
'n0 and n0', so let's make some storage for it now:
Dim n0 As Long
Dim n0prime As Long
'We'll need storage for the Montgomery Representations of operand1 and operand2.
'Call the buffers operand1prime and operand2prime:
Dim hOp1Prime As Long
Dim hOp2Prime As Long
'For exponentiations we'll need an exponent:
Dim hExponent As Long
'Later we'll try a "big" exponentiation (512 base and exponent, which is 16 words.
'Create handles for the big base and its Montgomery Image:
Dim hBigBase As Long
Dim hBigBasePrime As Long
'Allocate the storage for the big modulus:
Dim hBigModulus As Long
'Allocate storage for the result of the big exponentiation:
Dim hBigResult As Long
'We'll need storage for the operands for a GCD later:
Dim hR As Long
Dim hRinverse As Long
Dim hN As Long
Dim hNPrime As Long
Dim hD As Long
'Create a variable to hold overflow indications, and one
'to hold underflow (borrow) indications:
Dim overflow As Long
Dim borrow As Long
'Create a variable to hold the status results of buffer tests
'and comparisons:
Dim status As Long
'================================================================
' Start of calls to CypherMath functions:
'================================================================
frmResult.Show 'This form will be used to display results
'Put some initialization messages in the "Results" window:
frmResult.txtResult = "CypherMathWin32 DLL Test. Math Engine version number " & EPS_GetMathEngineVersion() & ". "
frmResult.txtResult = frmResult.txtResult & "Starting Test..."
'Now that the buffer handles have all been created, let's
'create the buffers themselves for operand 1 and
'operand 2:
hOp1 = EPS_CreateBuffer(kOPLEN)
hOp2 = EPS_CreateBuffer(kOPLEN)
'Now create a buffer to hold calculation results.
hResult = EPS_CreateBuffer(kRESULTLEN)
'Now let's try some calculations:
'===============================================================
' Addition:
'===============================================================
'Set 1st operand to 12345678 (hex):
EPS_SetBufferWord hOp1, 0, EPS_kgFULLWORD, &H12345678 'LSW
'Now copy operand1 to operand2:
EPS_CopyBuffer hOp1, hOp2, kOPLEN
'Now overwite operand2 with some other data:
EPS_SetBufferWord hOp2, 0, EPS_kgFULLWORD, &H2FD7C5A0
'Now we add the operands. The operation is "result = operand1 + operand2",
'which is 12345678 + 2FD7C5A0. The result should be 420C1C18.
EPS_Add hOp1, hOp2, hResult, kOPLEN
ShowResult hResult, kOPLEN 'Display the results of this operation
'Note that since we know the operands are only 32 bits long in this example,
'we can speed things up by specifying a shorter buflen in the call to the
'EPS_Add routine and still get the same result. Of course it would make more
'sense to simply size the operand1 and operand2 buffers smaller, but this
'is just for illustration:
EPS_Add hOp1, hOp2, hResult, 1 'buflen now 1, same result.
ShowResult hResult, kOPLEN 'Display the results of this operation
'Here's a 160 bit addition. First load the operands with 160 bit values.
'Use B49E888D 945A4A20 4D4CC654 0396C2BE D081B582 for operand1 and
'use 0B9C69FD DCD3CA5C 5F9FF641 DF160E65 F3155D2F for operand2. The
'result should be C03AF28B 712E147C ACECBC95 E2ACD124 C39712B1.
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
'This calculation should NOT produce an overflow, but we'll check for one anyway.
'The value of overflow should be zero after the addition:
overflow = EPS_Add(hOp1, hOp2, hResult, kOPLEN) 'overflow should be 0
ShowResult hResult, kOPLEN 'Display the results of this operation
'Note that when the addition completes the result buffer has one extra word, where
'the overflow goes (zero in this example). Lets change the MSW of operand2 to
'make the addition overflow:
EPS_SetBufferWord hOp2, 4, EPS_kgFULLWORD, &HFB9C69FD 'MSW (was &H0B9C69FD)
'This addition WILL overflow, and the value of overflow will be "1" after the
'addition. The sum should be 00000001 B03AF28B 712E147C ACECBC95 E2ACD124 C39712B1.
'Note the "exta" MSW of 00000001. This is the overflow being propagated into the MSW:
overflow = EPS_Add(hOp1, hOp2, hResult, kOPLEN) 'overflow should be 1
ShowResult hResult, (kOPLEN + 1) 'Display the results of this operation
'===============================================================
' Subtraction:
'===============================================================
'Now lets subtract the operands. Since operand2 is currently greater than operand1,
'the operation (operand2 - operand1) will NOT produce a borrow. The result should
'be 46FDE170 4879803C 12532FED DB7F4BA7 2293A7AD.
EPS_ClearBuffer hResult, kRESULTLEN
borrow = EPS_Subtract(hOp2, hOp1, hResult, kOPLEN) 'borrow should be 0
ShowResult hResult, kOPLEN 'Display the results of this operation
'Now lets subtract the operands in the other order. Since operand2 is currently
'greater than operand1, the operation (operand1 - operand2) WILL produce a borrow.
'The result should be B9021E8F B7867FC3 EDACD012 2480B458 DD6C5853.
EPS_ClearBuffer hResult, kRESULTLEN
borrow = EPS_Subtract(hOp1, hOp2, hResult, kOPLEN) 'borrow should be 1
ShowResult hResult, kOPLEN 'Display the results of this operation
'===============================================================
' Multiplication:
'===============================================================
'Now lets try a multiply. We'll multiply operand1 by operand2.
'The product (operand1 * operand2) will be twice as long as the operands. The result should be:
'B185CB46 29379E41 F97B3BCF B1C84F75 49D0FB2E 27ADC023 DA4D49F4 45B183E6 B94504FF B06A8CDE.
'Note that the length paramater we give the multiply function is the length of the operands, not the
'length of the expected result:
EPS_Multiply hOp1, hOp2, hResult, kOPLEN
ShowResult hResult, (2 * kOPLEN) 'Display the results of this operation
'===============================================================
' Division:
'===============================================================
'We can also divide the operands. For divisions, we'll need an additional buffers to
'hold the quotient and the remainder:
hQuotient = EPS_CreateBuffer(kOPLEN)
hRemainder = EPS_CreateBuffer(kOPLEN)
'First, clear the quotient buffer in preparation for division. This is
'required by the EPS_Divide function:
EPS_ClearBuffer hQuotient, kOPLEN
'Now the division. We'll use operand2 as the dividend (numerator) and operand1 as the
'divisor (denominator):
status = EPS_Divide(hOp2, hOp1, hQuotient, hRemainder, kOPLEN)
'You can use the returned status to check for divide-by-zero errors, or bad-dividend
'errors. The resulting quotient should be 00000001.
'The remainder should be 46FDE170 4879803C 12532FED DB7F4BA7 2293A7AD.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -