from sage.modules.free_module_integer import IntegerLattice from random import randint import sys from itertools import starmap from operator import mul
key = p=2141 b= A=Matrix(Zmod(p),key) print(A.solve_right(b)) defclosest_vector(M, G, target): small = target for _ inrange(1): for i inreversed(range(M.nrows())): c = ((small * G[i]) / (G[i] * G[i])).round() small -= M[i] * c return target - small
m = 64 n = 21 q = 2141
A_values =
b_values = A = matrix(ZZ, m + n, m) for i inrange(m): A[i, i] = q for x inrange(m): for y inrange(n): A[m + y, x] = A_values[x][y] lattice = IntegerLattice(A, lll_reduce=True) print("LLL done") gram = lattice.reduced_basis.gram_schmidt()[0] target = vector(ZZ, b_values) res = closest_vector(lattice.reduced_basis, gram, target) print("Closest Vector: {}".format(res))
R = IntegerModRing(q) M = Matrix(R, A_values) ingredients = M.solve_right(res) print("Ingredients: {}".format(ingredients))
d = 1888131080991802984469142252767428996708357845414525789382920257253450734136628648374571804259719424143388465346476293366644738737089607039465127514487405 p = 30611187447617951438057661643828594439836168499045103637492727875883545693704706710809270955729333435251594169116242188128775616562859932991842965711916421973513501170770682029019363173114263880664543027598219327851802812372758784840490412167556903172872555298867886904331797406762790534760341763270179874103596315439602969532398796744300268959212967152977297923303435136058320818336169166426865495487391347983169818270302355278949814298699167819318471127268309841253880404251042151162557234493453591687370817297674493012095163546638810088146738185919186385185115618449986759562697777952684453338987559120695742612663 h = 21312401103364823065661625965265254047976261102113426643161584691333735528853578330462720782012571325209425607725450902072660790420041271498835143983658324491397948972480792470439905794590608772344151134928174674417907495690758864900707486731428987072095395641677518641643685295486795802883828831926635233976602465690044844008674887604376578289424972631509896184649710918327894604786477135136421222185152303050748164371324119467682895596417745201794158939881061666663709040766320031697689020600295103351632352583239176099726804730980876514993290370570966082812844360442005544939574202193639394320702060361753336559136 c = 16940539604003327722979410435578617956215682038759397881493708848549559668095838737615772551889674113155566943460688615187510308352387746131182860772735244685105354949311113745596385193812888514879929848085807780411336350803009420541916609478955298130063431885517727744668841197297817243205861167004168641880226875601283896199831113765890902106375084917385016349416483298441391462272070565319424930392336694536562067484570233782710064495559877940944602837436374378241977922432719824129042239016560841938886647111460591440853620154778487323813228157104022518913321245126759601657046000849672318847957185592562785436558
m = matrix(ZZ, [[1, h], [0, p]]) f, g = m.LLL()[0] print(f, g) a = f*c % p % g m = a * inverse_mod(f, g) % g assert m == d print(long_to_bytes(m))