基础知识:
先理清他和别的概念的包含知识,包含整数环,整数环包含域,如果一个环是Z素数环,那他就是域
基础判别条件:
1.满足加法封闭,交换律,结合律,存在加法单位元,每一个元素都有加法逆元
2.满足乘法封闭,结合律
3.加法,乘法之间满足结合律(a(b+c)=ab+ac)
4.单位元:
运算之后还是被运算者本身,比如加法单位元0,乘法单位元1
5.单位:
单位x有xu=1,u是x逆元,比如Z中11=1,Z7中2*4=1
6.零因子:
比如Z8的零因子2*4=0,一旦有了零因子,这个环的消元可能失效,所以素数环就是域
7.子环:
如果一个环RR 里面有个子集SS,这个子集自己也能成为环,那SS 就叫RR 的子环
8.理想:
除了内部加减封闭外,还要求
被整个环乘上去还留在里面,元素的理想就是他的倍数集合
9.等价类:
a关于I的等价类,相当于在模I下为a的集合
10.多项式环:
元素是多项式的环,记为Fp
11.商环:
写作Z/nZ,就是n的商环,就是在R环下等价类的集合,也可以看作模R下的所有数+k*R
12.零元,幺元:
0+I,1+I(乘法单位元),后者作为逆元的判断
性质利用
1.PELL方程
对于PELL方程x2-Dy2=1,先化简得到(x-isqrt(D)y)(x+isqrt(D)y)=1,这一步看起来就是环的逆元,x-isqrt(D)y就是单位,另一个就是逆元,
这时得到在Z(isqrt(D))环上(这个叫二次扩张环
),元素都是a0+a1*isqrt(D),这个方程的解是环的单位,
单位的次方也是单位,这时就可以得到解的式子,un=un,即(xn-isqrt(D)y)=(x1-isqrt(D)y)n,通过连分数解出x1以及其具体后续不继续在此展开,详细见RSA进阶题型
性质利用
2.RLWE
先定义RLWE是什么,就是把传统LWE转化到环Z[x]/xn-t上,数字全变成多项式,flag转化成逐字节的ord系数的多项式,对于b=a*s+e modp,给出b,a,p和环,求s
题干:
from Crypto.Util.number import *
from random import *
from secret import flag
p = getPrime(128)
n = 64
assert len(flag) < n
PRp.<x> = PolynomialRing(Zmod(p))
f = x^n+2024
PR = PRp.quo(f)
assert f.is_irreducible()
A = [randint(0, p) for i in range(n)]
E = [randint(-4, 4) for i in range(n)]
S = [ord(flag[i]) for i in range(len(flag))]
B = PR(A)*PR(S)+PR(E)
print(A)
print(B.list())
print(p)
#[203211966212120647994404162543901058385, 113998159211311856163959557092243561040, 300945935078651395200612763362025786190, 289539358962072307485802285781112184506, 266860644401078099268835882620812059236, 109469988130078797414202659254940150397, 142193430695353860553837582452159920561, 163603443131512016589524436620766900399, 125570052004030843134707673404921516235, 289517595415670285587801685152176297797, 260336477592153246815046507549532230446, 89128330175853905837766718032373473126, 187717315035064338496744033397006646838, 47398231438108384695170959715078016751, 320133498675264306580441374518511607677, 289224545427213583554005441616103706530, 48124450498844786580485017131751952117, 139700877104641187048620578708689412417, 71574579434689200165233820726593943746, 138252394765364749010226318178055019086, 261521192680609185598298661488532314373, 226827408720691280529315243806063440819, 312302166514031202715929873278517388968, 41543713499646037109947736777306720313, 265649922622063773566846630839697709881, 243184423040946982395304999253912592156, 206198258022435934039803402480943364253, 26031203447491686152181680360833340987, 267200487804167757054737887061948559784, 301825292179107181769802726785989059100, 245678367957552191949253880949910260990, 171086126608355874606827637031412493750, 238890683861901290139689496391030872360, 22845910272546674819720769022817968767, 28421978727921425902205492605476665131, 171865962533483544755013698922220144571, 133361830829435140268114465077140337526, 86265129464226210912927536339928098681, 68669331909862762270788724935161403154, 201866646039120111937128925580825280486, 13363589230429215865126757180274178298, 17909430443917365585897023486940736171, 91757667623716596753047172893731203404, 46825614193892927425213536328954419018, 131552079476235333764480742173849445520, 235812708672214623030453261917981487844, 128419026868422646823814506658958018620, 165071194644940049087362229894395125949, 119007848732714973887391773573107966930, 142175857554093924103634649494868118501, 324116975329566500720942384202511507819, 145202961579339260969095793970625895397, 302068925092915352636494174062039213344, 204865812301701145536309778960425467197, 73204183646529021271666811846606597696, 305266620775559116657495389805588046145, 38573243820497560644612322159835262762, 204132117511623685134894865125985283485, 152661573492853637425833316355962196973, 234147606390346795925961511757446390699, 101214795511329059676976012743816028858, 45259554101612024332508390665659000501, 69808091348685800959727855379331581323, 96236770545429224235813571394198156104]
#[122276244512812151700561410687880152355, 288902265068638112029210922418017307765, 227157280930411828282688954386270375417, 51392889392439935890454046852827718770, 107818090106926819686128970258855341987, 39797693303511873260100821082668360148, 165165311283389310714977999739406590465, 145742713856100761940986808065029032102, 85681122792038178595571398323505588215, 161750232371529874705027252096297284499, 65492947650590100527727115268504178500, 158980937836523416847183159463389719119, 217622237154991741032187619647917235486, 214403448117831129512734472087177528755, 7203970244866082501778704003244517036, 209327947737263058848347484690330837593, 261471938052725575109694161428073566879, 290548445194039386389333379248698464710, 786777047021934678707520080022646500, 181116047323074255804762916647191259040, 321394759425138484862188864303222149962, 172828377940330639355360387538637264455, 66514389687485973164456706712404934406, 123351805496712587796702291879739918777, 52527849016391552904401277415521541344, 269082045250550515743652451127140175208, 46611178931314336386280465499464556662, 306738233577541073584072095728624359658, 27700046031756446145225137030746217892, 90225963569273234686624144350335934112, 67692467833088163585206113599832005433, 323500338166210082579512376483956531150, 268393735497552347113220159171930201434, 303137349721700995847836323480358301717, 37409490969034515063902941681541879806, 89987553043378360059649171549894177100, 177195833588614025644477178454034861283, 186453862621405866128018280976135392700, 134539827243365382868881776050914249090, 174423939567752183215031097600196039560, 141295420547653230540490506885038874517, 71077155156144579112947495766255667953, 219294250229364145810766028169964010349, 32511078113518753980651829431342417337, 303329577727497287329206502612155346064, 90557253357868638265309134765551619996, 274174517174776162629592599514881337711, 49932861529770838835799830915660452043, 12677314962488846037929508134986771433, 234965385179859810854399287906068885976, 11962701379314564572722140202812617562, 315105728460289255044517314167781474357, 92397698128764550243762952707724608359, 79161472796010129344236399474925315198, 8230717415162461432383488082982662910, 187550346095617606538292501947907284159, 111838535125019998906719357578490542024, 127632981408215865540832181515313916183, 77787230784265032836219137545199240126, 209675082880646980235572000733260128373, 318088800052782828672155510824979199450, 197936332876846440605381880211398578620, 163607618093826671398256369217224613243, 325974106851521509536970685901401508674]
#330464659390823375471800230370495966901
解题示例:
就是在环背景下的LWE,先是环的处理,把A看作算子,展开得到矩阵,这里的具体处理也一样,不过因为t不同,具体的折射也不同,下图举个例子:会在下三角变化
现在没了环的约束,现在是小误差模矩阵运算,移项得到a*s-b-kp=e,一眼CVP,适用embedding构造矩阵
k(pI 0 0
s (a I 0
-1 (B 0 1)=(e,s,-1)
这个基相对较小,LLL之后取abs就是s
脚本:
from Crypto.Util.number import *
n = 64
A = [203211966212120647994404162543901058385, 113998159211311856163959557092243561040, 300945935078651395200612763362025786190, 289539358962072307485802285781112184506, 266860644401078099268835882620812059236, 109469988130078797414202659254940150397, 142193430695353860553837582452159920561, 163603443131512016589524436620766900399, 125570052004030843134707673404921516235, 289517595415670285587801685152176297797, 260336477592153246815046507549532230446, 89128330175853905837766718032373473126, 187717315035064338496744033397006646838, 47398231438108384695170959715078016751, 320133498675264306580441374518511607677, 289224545427213583554005441616103706530, 48124450498844786580485017131751952117, 139700877104641187048620578708689412417, 71574579434689200165233820726593943746, 138252394765364749010226318178055019086, 261521192680609185598298661488532314373, 226827408720691280529315243806063440819, 312302166514031202715929873278517388968, 41543713499646037109947736777306720313, 265649922622063773566846630839697709881, 243184423040946982395304999253912592156, 206198258022435934039803402480943364253, 26031203447491686152181680360833340987, 267200487804167757054737887061948559784, 301825292179107181769802726785989059100, 245678367957552191949253880949910260990, 171086126608355874606827637031412493750, 238890683861901290139689496391030872360, 22845910272546674819720769022817968767, 28421978727921425902205492605476665131, 171865962533483544755013698922220144571, 133361830829435140268114465077140337526, 86265129464226210912927536339928098681, 68669331909862762270788724935161403154, 201866646039120111937128925580825280486, 13363589230429215865126757180274178298, 17909430443917365585897023486940736171, 91757667623716596753047172893731203404, 46825614193892927425213536328954419018, 131552079476235333764480742173849445520, 235812708672214623030453261917981487844, 128419026868422646823814506658958018620, 165071194644940049087362229894395125949, 119007848732714973887391773573107966930, 142175857554093924103634649494868118501, 324116975329566500720942384202511507819, 145202961579339260969095793970625895397, 302068925092915352636494174062039213344, 204865812301701145536309778960425467197, 73204183646529021271666811846606597696, 305266620775559116657495389805588046145, 38573243820497560644612322159835262762, 204132117511623685134894865125985283485, 152661573492853637425833316355962196973, 234147606390346795925961511757446390699, 101214795511329059676976012743816028858, 45259554101612024332508390665659000501, 69808091348685800959727855379331581323, 96236770545429224235813571394198156104]
B = [122276244512812151700561410687880152355, 288902265068638112029210922418017307765, 227157280930411828282688954386270375417, 51392889392439935890454046852827718770, 107818090106926819686128970258855341987, 39797693303511873260100821082668360148, 165165311283389310714977999739406590465, 145742713856100761940986808065029032102, 85681122792038178595571398323505588215, 161750232371529874705027252096297284499, 65492947650590100527727115268504178500, 158980937836523416847183159463389719119, 217622237154991741032187619647917235486, 214403448117831129512734472087177528755, 7203970244866082501778704003244517036, 209327947737263058848347484690330837593, 261471938052725575109694161428073566879, 290548445194039386389333379248698464710, 786777047021934678707520080022646500, 181116047323074255804762916647191259040, 321394759425138484862188864303222149962, 172828377940330639355360387538637264455, 66514389687485973164456706712404934406, 123351805496712587796702291879739918777, 52527849016391552904401277415521541344, 269082045250550515743652451127140175208, 46611178931314336386280465499464556662, 306738233577541073584072095728624359658, 27700046031756446145225137030746217892, 90225963569273234686624144350335934112, 67692467833088163585206113599832005433, 323500338166210082579512376483956531150, 268393735497552347113220159171930201434, 303137349721700995847836323480358301717, 37409490969034515063902941681541879806, 89987553043378360059649171549894177100, 177195833588614025644477178454034861283, 186453862621405866128018280976135392700, 134539827243365382868881776050914249090, 174423939567752183215031097600196039560, 141295420547653230540490506885038874517, 71077155156144579112947495766255667953, 219294250229364145810766028169964010349, 32511078113518753980651829431342417337, 303329577727497287329206502612155346064, 90557253357868638265309134765551619996, 274174517174776162629592599514881337711, 49932861529770838835799830915660452043, 12677314962488846037929508134986771433, 234965385179859810854399287906068885976, 11962701379314564572722140202812617562, 315105728460289255044517314167781474357, 92397698128764550243762952707724608359, 79161472796010129344236399474925315198, 8230717415162461432383488082982662910, 187550346095617606538292501947907284159, 111838535125019998906719357578490542024, 127632981408215865540832181515313916183, 77787230784265032836219137545199240126, 209675082880646980235572000733260128373, 318088800052782828672155510824979199450, 197936332876846440605381880211398578620, 163607618093826671398256369217224613243, 325974106851521509536970685901401508674]
p = 330464659390823375471800230370495966901
#part1 construct matrix of poly_mul
poly_mul_mat = Matrix(ZZ,n,n)
for i in range(n):
for j in range(i+1):
poly_mul_mat[j,i] = A[i-j]
for j in range(n-i-1):
poly_mul_mat[j+i+1,i] = A[-(j+1)]*(-2024)
#part2 construct Lattice of RLWE
I = identity_matrix(n)
B_mat = Matrix(ZZ,B)
O = diagonal_matrix([0]*n)
O_vec = Matrix(ZZ,1,n)
O_vec_T = Matrix(ZZ,n,1)
L = block_matrix(ZZ,[[p*I,O,0],[poly_mul_mat,I,O_vec_T],[B_mat,O_vec,1]])
#part3 LLL to get s and get flag
res = L.LLL()[0]
s = res[n:2*n]
for i in s:
print(chr(abs(i)),end = "")
#NSSCTF{U_c4n_5olv3_th1s_pr0b!em_0nce_u_kn0W_M4trix_0f_Polymu1}
性质利用3:NTRU
实际上这里没有多少运用环的性质,只起到一个限定范围的作用利用进阶:4.更广义环下的RLWE
题目来源:NSSCTF2024
题目1
题干:
from Crypto.Util.number import *
from random import *
from secret import flag
p = getPrime(128)
n = 64
assert len(flag) < n
PRp.<x> = PolynomialRing(Zmod(p))
f = x^n+2*x^3+0*x^2+2*x+4 #welcome to 2024!
PR = PRp.quo(f)
assert f.is_irreducible()
A = [randint(0, p) for i in range(n)]
E = [randint(-4, 4) for i in range(n)]
S = [ord(flag[i]) for i in range(len(flag))]
B = PR(A)*PR(S)+PR(E)
print(A)
print(B.list())
print(p)
#[25628433222756085994492259068849402115, 108192689210265022288173745176216677895, 85974352459523213168503152436203993854, 41828313114844948099739205193013146660, 15961445864161535368425735570577871568, 59860624089211511801244965491114405123, 72238114736535717286419734164194491146, 1731419769773538054984020741260144321, 110926179088722293171658261432626666958, 4729059639835826882559436203284470206, 168495809056757598731123948067040373565, 3338360642630078653320363127167526308, 19389936220034072874232261675901806153, 10438117635879553768611012406691341169, 153667038046207304584441437937384897775, 72166665774655568880543097047667804863, 95319528381704092095798376951073542165, 62898039808274250589921375492358418705, 38256076367009076141369526506747299307, 116682168147974451260364221046574659055, 26917522774848180141649752130328445540, 155767907175579627310170473659502971153, 77475859845928815191730640478115081925, 70014064310214050035280946814599808820, 85993451838284022745989979544017241858, 1559588202521630621698182776746048258, 52424771201426327411234245307354332594, 92380990205273211168316314938871853614, 55868954214861940284812204603983552046, 86766496415006406161245997036503548760, 65631024912623216715877274133399692328, 165552413066518454864366169079315182837, 27090806019996818214310558040178209015, 24884235560487406082425405304334927881, 167131388381252997695719535111304864318, 84309157064049564271714180786273613498, 53226032826639984552629376026393806036, 10043888404273006035387494091731007691, 12579929759225042382751229048796566699, 166363316914893642035801926729741157934, 37839073681950914796852925854557791313, 59823734019834883911363191453005985507, 72713054697995652053446065501322532087, 7355549923971285393506940087861413681, 21734747677920938504945342916533076569, 93933129166049277428941778125233010374, 58955842096425290408052247755008181893, 35959399375270454336325579486716264076, 134512300913738209016228764504703580317, 156877088942396466398635185398252103142, 104513294126348280186621088034948750337, 58139022317938952038809444482141783106, 98565612260156356115401166878515345986, 114416081175175578305854221578018095237, 22901750234260702478992290068664968479, 156611313609023033314989985233189973832, 125083132555321363078288077242345281728, 41404163164349704408020008746598776378, 148785934103979787297708145558354509586, 60576000474984112015781658795707443603, 157378782433123699827402744836263199181, 115808625117495689666174307128964343007, 55532885470248050887252206269085843644, 167226824329071601899518640284153209079]
#[125436276837023032466255967451858640606, 144448119937248956307846718982491347769, 162306129951573761575068204811168998078, 83181011846487027653033036479190112408, 40957533970438134530667298492545012058, 39781894692503876914564462045404705807, 164051006333092368306831348749408921012, 111012742481005666423330275146981180733, 7715720858029882578293974044643808669, 157677217515339873901600902409190036650, 47743593975916243882559558507891093023, 157986164305142738632123943371346719550, 127635891927296917312348404019956617317, 29559276398124624593609203169860820474, 65960871111768788316941346555260753893, 168042316135611110131429808009369471733, 1212659292467585393525194729053590464, 131142075720852427381782478157244639696, 102842995705164719704337121568124710266, 34358829636338044181518280022135929086, 46061675284276849395800943167734760351, 135977825342856793359560567810384022667, 79194609568617048147045548433187673199, 3676419995870098657902754244090307337, 129102404404523795672123957629514020212, 132931883751740337774987369503377603250, 157631873978881249131112881234813720862, 25064634967243118171242164160769943822, 90201135758213533081777343764941590513, 28708219563163506588485189820706388938, 56568253458138625936076514562071708932, 6347413009696777418004642228340087455, 142708918733729617152197380466325161544, 90675635242552386205397936839615479057, 92501808867676816791538575898905495372, 119574898086174634261072854178847743198, 109965735435324316785759245560872940757, 88027969746581556885954470651939385855, 134321591115155640446804238393795645472, 138737181251155600814378949277591209336, 99644145229459444132485749993357552167, 72102411291892187794699247448258325603, 83762370748394317206974170435605895442, 50088402451174667055894866569295371337, 151985189458457003469569692195947019009, 94074842589490698882361618773816024181, 73064286019059695311967281427732071049, 15926695040044471692620032927734856548, 92040182094543438421700664648795356800, 72067816134410856572981520568082148623, 51543145349488111975169803932104264498, 133476358234399233123899184886685568681, 138871089323661273034769987507240180967, 135182088478518211398715618356312799892, 142132493504501637417871042380487888261, 152821195684002682180600788766787854997, 108605984719600234041614912160255546443, 107009285527858918462712915168386920973, 142315287684855833566016316758770217595, 120564701831156855156008640956601967150, 76342952544707200039421682949094541165, 1616883292120605398623492917180224681, 114803483209077151333130433872010751344, 59602734024539592667147877839382863851]
#p = 171384865635734387982308861436753436427
WP:
这道题的处理难点在于环的扩展:f(x)=x64+2x3+2x+4,这让转化到矩阵需要格外处理,其他部分一样
这里详细讲解矩阵是怎么填的,方便理解
首先看在环下,是怎么映射的,先不看环,顶多达到x126,现在看一下怎么映射到低次:x64+i=-(2x3+i+2xi+1+4xi),
但是有特殊情况,由于i+3,i+1的存在,有时会需要二次展开,分界线就在i=61,列出式子:x64+61=-(2x64+2x62+4x61) =-2x62-4x61+4x3+4x+8,后面的i=62,63乘上一个x,x2就行
之后是矩阵映射:首先明确,是按列来填的,我们先举第0列为例,第0列就是x0的系数,记作c0,这里要往回看,把d记作无环时x系数,哪些能有x0,一个dk流向ck,ck+1,ck+33个c,那首先是d0对应c0,-64d64对应c0,
因为二次对应,还有8d125对应c0(不理解可以详细展开),c0=d0+4d64+8d125,接下来是d与x的对应,x实质是a*s
把c0用d表示再用as表示
第一列就是c0对于每一个s的展开,再扩展到其他x
最后的LWE环节就不讲解了
from Crypto.Util.number import *
from random import *
n = 64
A = [25628433222756085994492259068849402115, 108192689210265022288173745176216677895, 85974352459523213168503152436203993854, 41828313114844948099739205193013146660, 15961445864161535368425735570577871568, 59860624089211511801244965491114405123, 72238114736535717286419734164194491146, 1731419769773538054984020741260144321, 110926179088722293171658261432626666958, 4729059639835826882559436203284470206, 168495809056757598731123948067040373565, 3338360642630078653320363127167526308, 19389936220034072874232261675901806153, 10438117635879553768611012406691341169, 153667038046207304584441437937384897775, 72166665774655568880543097047667804863, 95319528381704092095798376951073542165, 62898039808274250589921375492358418705, 38256076367009076141369526506747299307, 116682168147974451260364221046574659055, 26917522774848180141649752130328445540, 155767907175579627310170473659502971153, 77475859845928815191730640478115081925, 70014064310214050035280946814599808820, 85993451838284022745989979544017241858, 1559588202521630621698182776746048258, 52424771201426327411234245307354332594, 92380990205273211168316314938871853614, 55868954214861940284812204603983552046, 86766496415006406161245997036503548760, 65631024912623216715877274133399692328, 165552413066518454864366169079315182837, 27090806019996818214310558040178209015, 24884235560487406082425405304334927881, 167131388381252997695719535111304864318, 84309157064049564271714180786273613498, 53226032826639984552629376026393806036, 10043888404273006035387494091731007691, 12579929759225042382751229048796566699, 166363316914893642035801926729741157934, 37839073681950914796852925854557791313, 59823734019834883911363191453005985507, 72713054697995652053446065501322532087, 7355549923971285393506940087861413681, 21734747677920938504945342916533076569, 93933129166049277428941778125233010374, 58955842096425290408052247755008181893, 35959399375270454336325579486716264076, 134512300913738209016228764504703580317, 156877088942396466398635185398252103142, 104513294126348280186621088034948750337, 58139022317938952038809444482141783106, 98565612260156356115401166878515345986, 114416081175175578305854221578018095237, 22901750234260702478992290068664968479, 156611313609023033314989985233189973832, 125083132555321363078288077242345281728, 41404163164349704408020008746598776378, 148785934103979787297708145558354509586, 60576000474984112015781658795707443603, 157378782433123699827402744836263199181, 115808625117495689666174307128964343007, 55532885470248050887252206269085843644, 167226824329071601899518640284153209079]
B = [125436276837023032466255967451858640606, 144448119937248956307846718982491347769, 162306129951573761575068204811168998078, 83181011846487027653033036479190112408, 40957533970438134530667298492545012058, 39781894692503876914564462045404705807, 164051006333092368306831348749408921012, 111012742481005666423330275146981180733, 7715720858029882578293974044643808669, 157677217515339873901600902409190036650, 47743593975916243882559558507891093023, 157986164305142738632123943371346719550, 127635891927296917312348404019956617317, 29559276398124624593609203169860820474, 65960871111768788316941346555260753893, 168042316135611110131429808009369471733, 1212659292467585393525194729053590464, 131142075720852427381782478157244639696, 102842995705164719704337121568124710266, 34358829636338044181518280022135929086, 46061675284276849395800943167734760351, 135977825342856793359560567810384022667, 79194609568617048147045548433187673199, 3676419995870098657902754244090307337, 129102404404523795672123957629514020212, 132931883751740337774987369503377603250, 157631873978881249131112881234813720862, 25064634967243118171242164160769943822, 90201135758213533081777343764941590513, 28708219563163506588485189820706388938, 56568253458138625936076514562071708932, 6347413009696777418004642228340087455, 142708918733729617152197380466325161544, 90675635242552386205397936839615479057, 92501808867676816791538575898905495372, 119574898086174634261072854178847743198, 109965735435324316785759245560872940757, 88027969746581556885954470651939385855, 134321591115155640446804238393795645472, 138737181251155600814378949277591209336, 99644145229459444132485749993357552167, 72102411291892187794699247448258325603, 83762370748394317206974170435605895442, 50088402451174667055894866569295371337, 151985189458457003469569692195947019009, 94074842589490698882361618773816024181, 73064286019059695311967281427732071049, 15926695040044471692620032927734856548, 92040182094543438421700664648795356800, 72067816134410856572981520568082148623, 51543145349488111975169803932104264498, 133476358234399233123899184886685568681, 138871089323661273034769987507240180967, 135182088478518211398715618356312799892, 142132493504501637417871042380487888261, 152821195684002682180600788766787854997, 108605984719600234041614912160255546443, 107009285527858918462712915168386920973, 142315287684855833566016316758770217595, 120564701831156855156008640956601967150, 76342952544707200039421682949094541165, 1616883292120605398623492917180224681, 114803483209077151333130433872010751344, 59602734024539592667147877839382863851]
p = 171384865635734387982308861436753436427
#part1 construct matrix of poly_mul
poly_mul_mat = Matrix(ZZ,n,n)
#first column
poly_mul_mat[0,0] = A[0]
for i in range(1,n):
poly_mul_mat[i,0] = -4*A[n-i]
#handle special twice mod
poly_mul_mat[-1,0] += 8*A[-2]
poly_mul_mat[-2,0] += 8*A[-1]
#2-3 column
for col in [1,2]:
poly_mul_mat[col,col] = A[0] - 2*A[n-1]
for i in range(col):
poly_mul_mat[i,col] = A[col-i]
for i in range(n-1-col):
poly_mul_mat[col+i+1,col] = -4*A[n-1-i]-2*A[n-1-i-1]
#handle special twice mod
poly_mul_mat[-1,1] += 4*A[-2] + 8*A[-1]
poly_mul_mat[-2,1] += 4*A[-1]
poly_mul_mat[-1,2] += 4*A[-1]
#rest column
for col in range(3,n):
poly_mul_mat[col-2,col] = A[2] - 2*A[n-1]
poly_mul_mat[col-1,col] = A[1] - 2*A[n-2]
poly_mul_mat[col,col] = A[0] - 2*A[n-1] - 2*A[n-3]
for i in range(col-2):
poly_mul_mat[i,col] = A[col-i]
for i in range(n-1-col):
poly_mul_mat[col+i+1,col] = -4*A[n-1-i]-2*A[n-1-i-1]-2*A[n-1-i-3]
#handle special twice mod
poly_mul_mat[-1,3] += 4*A[-2]
poly_mul_mat[-2,3] += 4*A[-1]
poly_mul_mat[-1,4] += 4*A[-1]
#part2 construct Lattice of RLWE
I = identity_matrix(n)
B_mat = Matrix(ZZ,B)
O = diagonal_matrix([0]*n)
O_vec = Matrix(ZZ,1,n)
O_vec_T = Matrix(ZZ,n,1)
L = block_matrix(ZZ,[[p*I,O,0],[poly_mul_mat,I,O_vec_T],[B_mat,O_vec,1]])
#part3 LLL to get s and get flag
res = L.LLL()[0]
s = res[n:2*n]
for i in s:
print(chr(abs(i)),end = "")
#NSSCTF{M4st3r_0F_RLWE_h3r3_15_ur_flag_4nD_HHHHappy_niu_Ye4r!!}
题目2
题干:
from Crypto.Util.number import *
from random import *
from secret import flag
p = getPrime(128)
n = 64
assert len(flag) < n
PRp.<x> = PolynomialRing(Zmod(p))
newyear = [2,0,2,4]
f = x^n
for i in range(n):
f += newyear[i%4]*x^(n-1-i) #welcome to 2024!
PR = PRp.quo(f)
assert f.is_irreducible()
A = [randint(0, p) for i in range(n)]
E = [randint(-4, 4) for i in range(n)]
S = [ord(flag[i]) for i in range(len(flag))]
B = PR(A)*PR(S)+PR(E)
print(A)
print(B.list())
print(p)
#[143895536039223361852265879930678271849, 49941095619845369403633015749131244252, 192366807276439803341529420280624695341, 41863179329950350668896955448215016801, 161730900763954941459384930538350726304, 69486722690005239063204459423772119076, 150044605995739190031133614780160002164, 53617544566841857383903597080623109339, 74625820568619243297350181792710531670, 16380616502578067837931320551193635969, 161905270710745874415457791187229895491, 60615195134615631027916010677640865675, 160490605829184543601552626825699212302, 67566984475203250552779752218068594160, 213285653211278131505736720165333128065, 64852378214458505102852766762259296220, 127863063181868054387649888847647635563, 173388212610482051840613449798996257379, 107853129453974811439304180468226801619, 47026991726695830686352132714620786077, 54537407094343620242102574881749270054, 213117990243770129927508589374671252215, 188551573436848023902527678127834115095, 184904756222053551643771035280489901913, 130154717708566694574065576508622379407, 169935928816833755787895658897579592244, 25127997681478110567876718932464424139, 66241086523494222064942298262635441275, 128384348198631966247204855175670185827, 186534789734452323940160584274797242101, 44113757786219743282268996106377142498, 161464089943212437590741012855856579087, 152978274982120638112396394811148002893, 127858672640521604574047701048286112335, 24811959746896810879635351294335540800, 139884017165706571446169150175115903276, 162069295856137595644855638161429085830, 103852979898094376585707063387003994190, 209853862047034349714977775246731397993, 83875540721706520814802129116239183141, 229717680288888669127660062951596981300, 142905448436402318840598200380283379791, 97701946983539136207969604175005004177, 132882918810599573425074347748636321411, 30946979906897798248175975370883897061, 227445609344085479411697428271056340360, 226861499760726945354933277553812121082, 34610454530275079356327169999644151033, 86543487884993671802260662621113434518, 193748181739943346975874601639150010426, 34955296746796322689176100212054938608, 78982586205793555689802056285642735691, 3417188406740628320649576312400181913, 165141388407797054166867506278447838336, 12695265941766745754277957144871301850, 89458008041873757788350494973818918496, 17544328835809254417440184162473311528, 17809092337865928270441021151091911120, 190539046594231331045902135957952644092, 54013202950149354717829653296038417693, 182140149560821210676566057705914354798, 182590162902622112992977612842205026523, 226186981126958513892204825722664037031, 96763448317215723992226901770790799160]
#[146131953632371057251386640770029761962, 155126848145561802955686124994407020596, 215548579819339372855889610814620506576, 215378077144333849353671692326217769974, 211651696346814349480371028078398365850, 56211588439881618182356345926007014351, 35792916285189253601060073066755768657, 193834084101163540069238791365615525212, 64185605303590514314207911465024315433, 74011476974910970618385371699236712496, 144080173424901828501825948809304329878, 50478515840382641218068722055014699097, 61424373096897526822902957616641049161, 228135094283701120116040034916450645966, 173676278129085855942014338366292660640, 4958486587405211188374567586180119556, 107653280004443417845206647750419894242, 227948848364624440330554299487592496262, 103344313582462128597769184556262118266, 179090340541635393147453223953633783366, 91157025540035569434535505051126504071, 218301646561306199078249711983197566905, 207984369886452772406666980534904709935, 84848453635637121750244379243550315275, 70455676155616420940430093468129934421, 112082679346753821742720835769195197209, 100096437088582331947465760446007491622, 23148318752965414660522976895342715316, 175024877143863308838121164427112023613, 65455761812920253083377790817920776573, 80719540338474829645398437070034720831, 30265965699771257065456865006701716765, 65059939935789446486144438246062876092, 73412866905686210879034022846184532326, 15496701729614207294798217895713334887, 187927627267834618400725936475600101114, 51596647781094949272750319452641626273, 34936140628305933632043160171328035222, 227636764157434931002269545814362460674, 40310006070105509297521946935690917605, 158199753843142436703851148792841271128, 62042757128547429572711130709028563495, 4994577444829057554913018889328955170, 212120189443587994433256482180523602261, 217895194863190305322702282626368171628, 14347476896110839167629995111874858945, 92969919157086783529099464146480743366, 98421412347381684974090776331896675353, 7670925013164447091797082877392123056, 92948133844692458289793766569490521009, 37843417064278243200690200946062028300, 73246898716604229152337274167003969610, 48576361100747286180365305580004800549, 226777738621945748117749363525255463543, 202653365491161565340111352416610858743, 81343653066789166205981715378171248137, 43128791749740623981661146675626160300, 51978531727836859802588862475571492852, 158673579466427370117557321982403789903, 154526278040551924441198621574328917110, 23520842615720508371775640194790884675, 76597771060168857457987619277375824543, 229138699410621489832761240581614631878, 62849396773221525801893336014307910648]
#229934842599910967421870245339955481121
WP:
这一次的环更扩展了,新的环:
也可以像之前一样处理,但是会及其繁琐
这时我们使用更直接的处理,as相乘得到d直接模得到c,
d*c就是矩阵,d可以直接出,问题出在怎么模
把模v看作矩阵运算:我们这里要尝试解出这个格外的r
首先从0~n-1,不需要处理,直接记作单位矩阵,下面记作r0,r1,…,接下来5句解决,就是一个降次等效处理
from Crypto.Util.number import *
A = [143895536039223361852265879930678271849, 49941095619845369403633015749131244252, 192366807276439803341529420280624695341, 41863179329950350668896955448215016801, 161730900763954941459384930538350726304, 69486722690005239063204459423772119076, 150044605995739190031133614780160002164, 53617544566841857383903597080623109339, 74625820568619243297350181792710531670, 16380616502578067837931320551193635969, 161905270710745874415457791187229895491, 60615195134615631027916010677640865675, 160490605829184543601552626825699212302, 67566984475203250552779752218068594160, 213285653211278131505736720165333128065, 64852378214458505102852766762259296220, 127863063181868054387649888847647635563, 173388212610482051840613449798996257379, 107853129453974811439304180468226801619, 47026991726695830686352132714620786077, 54537407094343620242102574881749270054, 213117990243770129927508589374671252215, 188551573436848023902527678127834115095, 184904756222053551643771035280489901913, 130154717708566694574065576508622379407, 169935928816833755787895658897579592244, 25127997681478110567876718932464424139, 66241086523494222064942298262635441275, 128384348198631966247204855175670185827, 186534789734452323940160584274797242101, 44113757786219743282268996106377142498, 161464089943212437590741012855856579087, 152978274982120638112396394811148002893, 127858672640521604574047701048286112335, 24811959746896810879635351294335540800, 139884017165706571446169150175115903276, 162069295856137595644855638161429085830, 103852979898094376585707063387003994190, 209853862047034349714977775246731397993, 83875540721706520814802129116239183141, 229717680288888669127660062951596981300, 142905448436402318840598200380283379791, 97701946983539136207969604175005004177, 132882918810599573425074347748636321411, 30946979906897798248175975370883897061, 227445609344085479411697428271056340360, 226861499760726945354933277553812121082, 34610454530275079356327169999644151033, 86543487884993671802260662621113434518, 193748181739943346975874601639150010426, 34955296746796322689176100212054938608, 78982586205793555689802056285642735691, 3417188406740628320649576312400181913, 165141388407797054166867506278447838336, 12695265941766745754277957144871301850, 89458008041873757788350494973818918496, 17544328835809254417440184162473311528, 17809092337865928270441021151091911120, 190539046594231331045902135957952644092, 54013202950149354717829653296038417693, 182140149560821210676566057705914354798, 182590162902622112992977612842205026523, 226186981126958513892204825722664037031, 96763448317215723992226901770790799160]
B = [146131953632371057251386640770029761962, 155126848145561802955686124994407020596, 215548579819339372855889610814620506576, 215378077144333849353671692326217769974, 211651696346814349480371028078398365850, 56211588439881618182356345926007014351, 35792916285189253601060073066755768657, 193834084101163540069238791365615525212, 64185605303590514314207911465024315433, 74011476974910970618385371699236712496, 144080173424901828501825948809304329878, 50478515840382641218068722055014699097, 61424373096897526822902957616641049161, 228135094283701120116040034916450645966, 173676278129085855942014338366292660640, 4958486587405211188374567586180119556, 107653280004443417845206647750419894242, 227948848364624440330554299487592496262, 103344313582462128597769184556262118266, 179090340541635393147453223953633783366, 91157025540035569434535505051126504071, 218301646561306199078249711983197566905, 207984369886452772406666980534904709935, 84848453635637121750244379243550315275, 70455676155616420940430093468129934421, 112082679346753821742720835769195197209, 100096437088582331947465760446007491622, 23148318752965414660522976895342715316, 175024877143863308838121164427112023613, 65455761812920253083377790817920776573, 80719540338474829645398437070034720831, 30265965699771257065456865006701716765, 65059939935789446486144438246062876092, 73412866905686210879034022846184532326, 15496701729614207294798217895713334887, 187927627267834618400725936475600101114, 51596647781094949272750319452641626273, 34936140628305933632043160171328035222, 227636764157434931002269545814362460674, 40310006070105509297521946935690917605, 158199753843142436703851148792841271128, 62042757128547429572711130709028563495, 4994577444829057554913018889328955170, 212120189443587994433256482180523602261, 217895194863190305322702282626368171628, 14347476896110839167629995111874858945, 92969919157086783529099464146480743366, 98421412347381684974090776331896675353, 7670925013164447091797082877392123056, 92948133844692458289793766569490521009, 37843417064278243200690200946062028300, 73246898716604229152337274167003969610, 48576361100747286180365305580004800549, 226777738621945748117749363525255463543, 202653365491161565340111352416610858743, 81343653066789166205981715378171248137, 43128791749740623981661146675626160300, 51978531727836859802588862475571492852, 158673579466427370117557321982403789903, 154526278040551924441198621574328917110, 23520842615720508371775640194790884675, 76597771060168857457987619277375824543, 229138699410621489832761240581614631878, 62849396773221525801893336014307910648]
p = 229934842599910967421870245339955481121
n = 64
#part1 construct matrix of poly_mul
#n:The highest degree of a modular polynomial
#v:vector of modular polynomial
#a:vector of a polynomial multiplier
#aim to construct a matrix of c=a*b%v
def construct_poly_mul_mat(n,v,b):
assert v[-1] == 1 #use this after monic
#step1 construct a matrix of d=a*b
mat1 = Matrix(ZZ,n,2*n-1)
for i in range(n):
for j in range(n):
mat1[i,j+i] = b[j]
#step2 construct a matrix of c=d%v
mat2 = Matrix(ZZ,2*n-1,n)
for i in range(n):
mat2[i,i] = 1
for i in range(n,2*n-1):
for j in range(i-n,n):
mat2[i,j] = -v[j-(i-n)]
init_row = vector(ZZ,n*[0])
for j in range(i-n):
temp = -v[n-1-j]*vector(ZZ,mat2[i-j-1])
init_row += temp
for j in range(n):
mat2[i,j] += init_row[j]
#step3 multiply them and return
return(mat1*mat2)
PRp.<x> = PolynomialRing(Zmod(p))
newyear = [2,0,2,4]
f = x^n
for i in range(n):
f += newyear[i%4]*x^(n-1-i)
v = f.list()
poly_mul_mat = construct_poly_mul_mat(n,v,A)
#part2 construct Lattice of RLWE
I = identity_matrix(n)
B_mat = Matrix(ZZ,B)
O = diagonal_matrix([0]*n)
O_vec = Matrix(ZZ,1,n)
O_vec_T = Matrix(ZZ,n,1)
L = block_matrix(ZZ,[[p*I,O,0],[poly_mul_mat,I,O_vec_T],[B_mat,O_vec,1]])
#part3 LLL to get s and get flag
res = L.LLL()[0]
s = res[n:2*n]
for i in s:
print(chr(abs(i)),end = "")
#NSSCTF{!!!Hah4H@,H0p3_Y0U_h@ve_fang_In_y0ur_po1Ynomial_5tudY!!}