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?

  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

  2. #2
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

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

    From the title of your post, and the equations you're looking at, it seems like you're looking at how to compute the modular inverse of a number in a given base.
    note that this also allows you to solve Ax + By = k when GCD(x, y) is a factor of k, because you then divide by GCD(x, y) and you can morph into Ax' + By' = 1, where GCD(x', y') = 1.
    Taking this mod x', you get By' = 1, so B is the modular inverse of y' mod A.
    Similarly, A is the modular inverse of x' mod B.

    What you've posted is roughly the general accepted way of doing it - basically the Euclidean Algorithm in reverse. A nice explanation can be found here:
    http://www.antilles.k12.vi.us/math/c..._algorithm.htm
    sql_lall

  3. #3

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

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

    Thanks!
    I'd rep you, but I can't just yet.

    Anyways, from that site, I've developed the following code, a little neater than what I had before:

    VB Code:
    1. Public Sub CALC_MOD_INVERSE( _
    2.                                 ByVal MY_PRIME As Integer, _
    3.                                 ByVal P_0 As Integer, _
    4.                                 ByVal P_1 As Integer, _
    5.                                 ByRef INV_P_1 As Integer, _
    6.                                 Optional ByVal X_0 As Integer = 0, _
    7.                                 Optional ByVal X_1 As Integer = 1, _
    8.                                 Optional ByVal mLev As Integer = 0)
    9.         Dim P_2 As Integer
    10.         Dim Q_0 As Integer
    11.         Dim X_2 As Integer
    12.  
    13.         If mLev = 0 Then
    14.             If P_1 = 0 Then
    15.                 INV_P_1 = -1
    16.                 Exit Sub
    17.             End If
    18.             If P_1 = 1 Then
    19.                 INV_P_1 = 1
    20.                 Exit Sub
    21.             End If
    22.         End If
    23.  
    24.         P_2 = P_0 Mod P_1
    25.         Q_0 = P_0 \ P_1
    26.         If P_2 <> 0 Then
    27.             INV_P_1 = (X_0 - X_1 * Q_0) Mod MY_PRIME
    28.             X_2 = INV_P_1
    29.             Call CALC_MOD_INVERSE(MY_PRIME, P_1, P_2, INV_P_1, X_1, X_2, 1)
    30.         End If
    31.         If mLev = 0 Then
    32.             INV_P_1 = (INV_P_1 + MY_PRIME) Mod MY_PRIME
    33.         End If
    34.         'when mLev = 0,
    35.         'If you are trying to find B, where B is the "inverse" of A,
    36.         'Such That (A*B) Mod SomeNum = 1,
    37.         'Then the initial values are:
    38.         'MY_PRIME = SomeNum
    39.         'P_0 = SomeNum
    40.         'P_1 = A
    41.         'INV_P_1 will be the returned inverse of A
    42.  
    43.         'I don't really need X_2, since it is being passed ByVal to the next level.
    44.         'I could just pass INV_P_1 a second time,
    45.  
    46.         'But I think it keeps things clearer, in relation to the formula:
    47.         'x[sub]i[/sub] = (x[sub]i-2[/sub] - [x[sub]i[/sub]-1*q[sub]i-2[/sub]]) (mod b)  
    48.     End Sub

    Nice and tidy way to build conversion matrices to change multiplands of variables to 1 when solving modular simultaneous equations.


  4. #4
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

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

    FWIW, Here's some code I saw used by someone else in a recent contest (excuse the use of C++ rather than VB)
    Code:
    //returns x, such that a*x = b mod c
    int axbmodc(int a,int b,int c){
      if(a==0)return 0;
      return (axbmodc(c % a, (a - b % a) % a, a) * c + b) / a;
    }
    Now, don't ask me how this works :S - but it seems similar to what you're doing.
    sql_lall

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