Results 1 to 4 of 4

Thread: If P is prime, and A is known, what is B such that A*B = 1 mod P?

Threaded View

  1. #1

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    If P is prime, and A is known, what is B such that A*B = 1 mod P?

    I wanted a function that would return such a result.
    After playing around a bit, I dredged up some old contemplations, and was able to code it to work as I expected.

    Lets say you wanted to find some number B, such that 7B = 31A+1

    That equation can be zeroed to the following:

    7B0-31A0-1=0

    Using modular arithmatic, you can create the following chain:

    Code:
    7B0-31A0-1=0      'Mod this from the 7multiplier, except for 7 itself
    7B1-3A0-1=0        'Mod this from the 3multiplier, except for 3 itself
    1B1-3A1-1=0        'Since one of the |mults| is now 1, we can now solve for the unknowns
    Since the last equation now ends up, unzeroed as:
    3A1 = B1-1
    A1 can be "assigned" as zero, and B1 becomes 1

    Knowing B1=1, then the middle equation becomes:
    7*1-3A0-1=0
    or...
    3A0 = 6

    which results in A0 = 2

    From this, the initial equation becomes:
    7B0-31*2-1=0
    or...
    B0 = (63/7) = 9

    which, when all is said and done,
    9 is the uniary compliment of 7 modular 31
    or:
    7*9 mod 31 = 1

    Now, the following is the VBNet function that achieves this chaining down and back up.

    But, I wonder, is there a better way?

    VB Code:
    1. Public Function GET_MULT_TO_ONE(ByVal mA As Integer, ByVal mPrime As Integer) As Integer
    2.         Dim mD As Integer
    3.         If Iterate_Mod_To_One(mA, -1 * mPrime, -1, mD, 0) = True Then
    4.             mD = (mD + mPrime) Mod mPrime
    5.             Return mD
    6.         End If
    7.         Return -1
    8.     End Function
    9.     Public Function Iterate_Mod_To_One(ByVal mA As Integer, ByVal mB As Integer, ByVal mC As Integer, ByRef mD As Integer, ByVal mLev As Integer) As Boolean
    10.         mLev = mLev Mod 2
    11.         If mA = 0 Or mB = 0 Or mC = 0 Then
    12.             mD = -1
    13.             Return False
    14.         End If
    15.         'assume user knows what they are doing, ie..
    16.         'initial |mB| is prime,
    17.         'initial |mC| is 1
    18.         'ma will always be positive, mb and mc will always be negative
    19.         'such that X*ma + Y*mb + mc = 0
    20.         If mA = 1 Then
    21.             mD = -1 * mC
    22.             Return True
    23.         End If
    24.         If mB = -1 Then
    25.             mD = mC
    26.             Return True
    27.         End If
    28.  
    29.  
    30.         Dim mA2 As Integer
    31.         Dim mB2 As Integer
    32.         Dim mD2 As Integer
    33.  
    34.         mA2 = mA
    35.         mB2 = mB
    36.  
    37.         Select Case mLev
    38.             Case 0
    39.                 mB2 = mB Mod mA
    40.             Case 1
    41.                 mA2 = mA Mod mB
    42.         End Select
    43.         If Iterate_Mod_To_One(mA2, mB2, mC, mD, mLev + 1) = True Then
    44.             Select Case mLev
    45.                 Case 0
    46.                     mD = -1 * (mB * mD + mC) / mA
    47.                 Case 1
    48.                     mD = -1 * (mA * mD + mC) / mB
    49.             End Select
    50.             Return True
    51.         End If
    52.     End Function

    And the following is some code illustrating how its used, and that it seems to actually work:

    VB Code:
    1. Private Sub MenuItem28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem28.Click
    2.         'lets just see if it works for the first 19 primes
    3.  
    4.         Dim MyPrimes() As Integer
    5.         Dim mOutArr() As Integer
    6.         Dim MyI As Integer
    7.         ReDim MyPrimes(19)
    8.         MyPrimes(0) = 2
    9.         MyPrimes(1) = 3
    10.         MyPrimes(2) = 5
    11.         MyPrimes(3) = 7
    12.         MyPrimes(4) = 11
    13.         MyPrimes(5) = 13
    14.         MyPrimes(6) = 17
    15.         MyPrimes(7) = 19
    16.         MyPrimes(8) = 23
    17.         MyPrimes(9) = 29
    18.         MyPrimes(10) = 31
    19.         MyPrimes(11) = 37
    20.         MyPrimes(12) = 41
    21.         MyPrimes(13) = 43
    22.         MyPrimes(14) = 47
    23.         MyPrimes(15) = 53
    24.         MyPrimes(16) = 59
    25.         MyPrimes(17) = 61
    26.         MyPrimes(18) = 67
    27.         MyPrimes(19) = 71
    28.         For MyI = 0 To 19
    29.             Call BUILD_MULT_TO_ONE_FROM_PRIME2(MyPrimes(MyI), mOutArr)
    30.         Next
    31.     End Sub
    32.     Public Sub BUILD_MULT_TO_ONE_FROM_PRIME2(ByVal mPrime As Integer, ByRef mOutArr() As Integer)
    33.         ReDim mOutArr(mPrime - 1)
    34.         Dim MyOutStr As String
    35.         Dim MySeed As Integer
    36.         For MySeed = 0 To mPrime - 1
    37.             mOutArr(MySeed) = -1
    38.         Next
    39.         'so, if good, then
    40.         'MySeed * mOutArr(MySeed) = K => K mod mP = 1
    41.         For MySeed = 0 To mPrime - 1
    42.             mOutArr(MySeed) = GET_MULT_TO_ONE(MySeed, mPrime)
    43.         Next
    44.  
    45.         'now, lets illustrate the results
    46.         MyOutStr = "Prime " & mPrime & ":" & vbTab & "A*B = 1 (mod " & mPrime & " )" & vbCrLf
    47.         For MySeed = 0 To mPrime - 1
    48.             MyOutStr += vbTab & MySeed & "*" & mOutArr(MySeed) & "=" & ((MySeed * mOutArr(MySeed)) Mod mPrime) & vbCrLf
    49.         Next
    50.         MessageBox.Show(MyOutStr)
    51.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width