def inverse_function(): print("Testing -- inverse_mod function--") print("Since this function will raise ValueError easily from random\n" "generating numbers, we will use three differen pair of numbers\n" "to test its functionality.") a = 57 b = 61 tf1 = nmtf.inverse_mod(tf.constant(a), tf.constant(b)) nm1 = nm.inverse_mod(a, b) print("nmtf.inverse_mod({0}, {1}) = ({2}, {3})".format( a, b, tf1[0].eval(), tf1[1].eval())) print(" nm.inverse_mod({0}, {1}) = {2}".format(a, b, nm1)) a = 23 b = 58 tf1 = nmtf.inverse_mod(tf.constant(a), tf.constant(b)) nm1 = nm.inverse_mod(a, b) print("nmtf.inverse_mod({0}, {1}) = ({2}, {3})".format( a, b, tf1[0].eval(), tf1[1].eval())) print(" nm.inverse_mod({0}, {1}) = {2}".format(a, b, nm1)) print("") print( "The last pair of numbers will make the normal inverse_mod function\n" "raise an ValueError. For the tensorflow version, the second parameter\n" "is 0") a = 10 b = 34 tf1 = nmtf.inverse_mod(tf.constant(a), tf.constant(b)) #nm1 = nm.inverse_mod(a, b) print("nmtf.inverse_mod({0}, {1}) = ({2}, {3})".format( a, b, tf1[0].eval(), tf1[1].eval()))
def reducemat(submat): pivotrow=0 for rj in range(thedegree): # Find the pivot element in this column for ri in range(pivotrow,len(submat)): if (submat[ri][rj] != 0): # Swap rows temp = submat[pivotrow]; submat[pivotrow] = submat[ri]; submat[ri] = temp break else: continue # No pivot in this column - move to next column (for rj) # Subtract multiples of pivot row from lower rows (zeroing out column elts) invpivot = numbthy.inverse_mod(submat[pivotrow][rj],themod) for ri in range(pivotrow+1,len(submat)): rowmult = mod(invpivot*submat[ri][rj],themod) for rjj in range(rj,numcols): submat[ri][rjj] = mod(submat[ri][rjj]-rowmult*submat[pivotrow][rjj],themod) #if not any(themat[i][:thedegree]): return themat[i][thedegree:] pivotrow += 1 # Move down to next row
def reducemat(submat): pivotrow = 0 for rj in range(thedegree): # Find the pivot element in this column for ri in range(pivotrow, len(submat)): if (submat[ri][rj] != 0): # Swap rows temp = submat[pivotrow] submat[pivotrow] = submat[ri] submat[ri] = temp break else: continue # No pivot in this column - move to next column (for rj) # Subtract multiples of pivot row from lower rows (zeroing out column elts) invpivot = numbthy.inverse_mod(submat[pivotrow][rj], themod) for ri in range(pivotrow + 1, len(submat)): rowmult = mod(invpivot * submat[ri][rj], themod) for rjj in range(rj, numcols): submat[ri][rjj] = mod( submat[ri][rjj] - rowmult * submat[pivotrow][rjj], themod) #if not any(themat[i][:thedegree]): return themat[i][thedegree:] pivotrow += 1 # Move down to next row
#5 base 2 in Z13 = 9 #x0 = 2 #x1 = 1 #p=1073676287 #g=1010343267 #h=857348958 #B=pow(2, 10) #1026831 (*this is the solution*) #x0 = 1002; #x1 = 783; print datetime.datetime.now() gi = numbthy.inverse_mod(g, p) gPowB = numbthy.power_mod(g, B, p) print "p", p print "g", g print "h", h print "B", B print "inverse g", gi print "gPowB", gPowB left = {} powX1 = h % p left[powX1] = 0 x1 = 1
def f2(p, g, h, x1): denom = power_mod(g, x1, p) return (h * inverse_mod(denom, p)) % p
def DecryptCipher(CipherVal, EncExp, p1, q1): DecExp = inverse_mod(EncExp, (p1 - 1) * (q1 - 1)) MsgVal = power_mod(CipherVal, DecExp, p1 * q1) return MsgVal
from numbthy import inverse_mod, power_mod p = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171 g = 11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568 h = 3239475104050450443565264378728065788649097520952449527834792452971981976143292558073856937958553180532878928001494706097394108577585732452307673444020333 table = [] B = 2**20 for x1 in range(B + 1): denominator = inverse_mod(power_mod(g, x1, p), p) table.append((h * denominator) % p) for x0 in range(B + 1): power = B * x0 trial = power_mod(g, power, p) print 'Try ' + str(x0) try: x1 = table.index(trial) print x0, x1 print x0 * B + x1 break except ValueError: continue