Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-11-09 23:11:40 +01:00
parent bd41b78bf8
commit f442f4c319
Signed by: y0rune
GPG Key ID: F204C385F57EB348
1 changed files with 77 additions and 24 deletions

View File

@ -9,7 +9,6 @@
#
#####################################
require 'prime'
require 'openssl'
require 'securerandom'
require 'prime'
@ -23,7 +22,7 @@ def extended_euklides(a, b)
return t, s - q * t
end
## Zad. 1.1
# Zad. 1.1
def random_gen_Zn(n,k)
if 2**(k-1) < n && k > 0 then
if k == 1 then
@ -46,9 +45,8 @@ def random_gen_Zn(n,k)
end
## Zad. 1.2
# Zad. 1.2
def reciprocal_Phi_p(n,p)
# n mod p
u = extended_euklides(n,p)[0]
v = extended_euklides(n,p)[1]
@ -59,8 +57,12 @@ def reciprocal_Phi_p(n,p)
end
end
## Zad. 1.3
# Zad. 1.3
def betterExponentiation(x,k,n)
if n == 0 then
return false
end
b = k.to_s(2).reverse
l = b.count "[0-1]"
y = 1
@ -76,7 +78,7 @@ def betterExponentiation(x,k,n)
return y
end
## Zad. 1.4
# Zad. 1.4
def remSqEuler(a,p)
ans = betterExponentiation(a,(p-1)/2,p)
@ -87,21 +89,15 @@ def remSqEuler(a,p)
end
end
## Zad. 1.5
# Zad. 1.5
def squareRootFp(p,b)
if p % 4 == 3 && remSqEuler(p,b) == true then
for i in 1..p
a = betterExponentiation(i, (p+1)/4, p)
#puts i.inspect + " " + a.inspect + "=" + b.inspect
if a == b then
puts i
break
end
end
a = betterExponentiation(b, (p+1)/4, p)
return a
end
end
## Zad. 1.6
# Zad. 1.6
def primalityTest(n)
if n == 1 then
return false
@ -122,23 +118,80 @@ def primalityTest(n)
return true
end
###################################################################################
# Zadanie.1 Losowy element z zbioru Z_n
#
# Uzycie funkcji:
# random_gen_Zn(n,k)
#
# Gdzie n - grupa mod
# k - ilosc bitow
#puts "Zadanie 1 " + random_gen_Zn(2817960879631397637428637785383222308241674912977296371078328827783823173932945686560271709717223685413005001487628305095704070793958971415087255523073935114518202433817141315589046697665767753020703330561718642810157214393228915238808369985314803605127547366769525321238034444131545038866079220135011303234308583293055538438855440051375449844277218217712896675378963068720345515473434111699867542985885331741594726201346190830240692715577811297545617592986841862266976883958728343753502720654957572591625318922387371348775299617016576604164586635463962864505536324347369114225605202600072081219902252264258236028218613372215876496394341867072862629022156295534655310094481486891656868251869749451857161662890356600101860594118885136794555465318782343870373413707643678756555809662335374658562880045543430140169913540153961566294704894163494226738977413711366121112146042996377423578488354012607979075727403258666348434920910981957617905109649522084109820427893425361930683246183514216962034556016051184044970184676662827978420213281480510069379347007543973139554178175690773315927469361215139519543395792659825942864347234234285907203635728345512501417615714291999748745375110198782386769755218587158953598215023147871490893052124457996091330393194346889440433647348345868290410045628941650640315817691074244257070087501412656465257388504574920756736653658826614297081956072241319983753399595743495837950519045870810249068878008938965513649172515852388457433200750100902751828486806189811961720524504976040165319005498382580099718522287255514024665802127411452771343820848551192190973237603226514162458911052832006635099614443220118029822801995173084006448848714642581145597450095993844936566471047767974285413266855493150984091512278787539020882065620089396507786047199321023827465489494495513119543424285384803025749486259267450477917340669954258887755716092814959537213397128776047910200001947756249766479670986137076981224642743069725401099384031028670810757683522821674118926373517718330706064178699035286815124318059529948663730575377063300582239742709428517028624628630255640464009691179606104444672232047869709734836046571505598681429286372976726889778746623725623485244272247797117303046464791441413956650048631706362550020890628552166104678993837549091779514727408814458173452235187339390489070470959043964826221641981413639067723526075949807992319555177364587831440901887979371647118775640122112691496917328672989185787786333743943571201682455849788768447941052589112093217146238196393058840427167875379895033862665036580869700901671149307822867600084775797450043528335040523181300939047560092173656437964805986878846909755616898664300908149121103065456346895895553346803762232995496756924185713587688989499627282573083392504068537865838813511676470822568313458216527495376958238195629276140142912559084213140117194512209752564237149304569698487234268004667685663277598956964116311211220970915272172182795014191615102024324085452311963348372375839824417067170842114940446271152850552864783993722225100122068507428533164772669151799555291529447719732682260868021763685052338407930046892500473287361324180925326385131283404240275852188687583358717018916949107259963059221294732376067807306008309012964676038322426438467806570485299377626514669181680963962016304313021681669696580753784295259640169007074506637844332833804045109981978476343942218065708022217566222881348496310403404164467438352649885637702780969376650799749541038736604262724814821757258372470336571211357167468680200965241152364670892328226187787108777323221988154289834826302745446444518424440700033948649807665278475079826664974213162552336459719975478725157313669922561440432360473265250572606571269284353534676478375363478936179040075297456773814344907213125333476476655366455733030460908712160208004620689954788191597043073132610377893018543064926955781952650214504633097216561549411315119718460509357878230720821699669794678584993658379092808863297233433557739623968183417003317301945898487820593437670555891465841469699644209269444311251317668652989906375182749637967733151694853436985266479700749763565024494627271013468757521359567838089899510388135209838269214753262972848939363295323913595985611694315406247934779335196977854974497594863447427077079600386381593134665326520126544264832044936966738225526176229232138173609626231926255343974851041449136902209592667970872917688005523210617825501923851244940494708102661449185546240415803793231334092660117374732910436571816210806786100000371385219128407888245069151381708001509375,15000).inspect
###################################################################################
# Zadanie.2 Odwrotnosc w grupie Phi(n)
#
# Uzycie funkcji:
# reciprocal_Phi_p(n,p)
#
# Gdzie p - element w grupie phi
# n - liczba nalezaca do N
#puts "Zadanie 2 " + reciprocal_Phi_p(10,13).inspect
#puts "Zadanie 2 " + reciprocal_Phi_p(814124120905749248299480415803476335639365723498296123469468592347073656928224527500310685973387246534780850320750411701278256916776754378412188178575147137091744859172404523278057868961064832876066462511095131108735875202132121424592704723, 9526498155551074622733236474497582527180243659164477550500224943978308908366162791700317474349020449651725045825391711253868497973855214826394160675701816160034393010008158121969738694286765731179652981990154532407906402992580840627870456203877142535098973).inspect
###################################################################################
# Zadanie.3 Efektywne potegowanie.
#
# Uzycie funkcji:
# betterExponentiation(x,k,n)
#
# Gdzie obliczna jest wartosc x^k mod n
# Example of usage: betterExponentiation(a,x,n) == a^x mod n
#puts "Zadanie 3 " + betterExponentiation(8,2,30).inspect
#puts "Zadanie 3 " + betterExponentiation(814124120905749248299480415803476335639365723498296123469468592347073656928224527500310685973387246534780850320750411701278256916776754378412188178575147137091744859172404523278057868961064832876066462511095131108735875202132121424592704723, 9526498155551074622733236474497582527180243659164477550500224943978308908366162791700317474349020449651725045825391711253868497973855214826394160675701816160034393010008158121969738694286765731179652981990154532407906402992580840627870456203877142535098973, 814124120905749248299480415803476335639365723498296123469468592347073656928224527500310685973387246534780850320750411701278256916776754378412188178575147137091744859172404523278057868961064832876066462511095131108735875202132121424592704723321321312312312312321312312321).inspect
# Example of usage: remSqEuler(64,13)
#puts "Zadanie 4 " + remSqEuler(5477183851370352794483563784128563497130907056727482167786963097750078781018279771969331294744464367184772268010020054134138848983974910921696137050139568263269632429620583642373162979289657018424493982802635787634837286977661054870863166216065362776490713, 9526498155551074622733236474497582527180243659164477550500224943978308908366162791700317474349020449651725045825391711253868497973855214826394160675701816160034393010008158121969738694286765731179652981990154532407906402992580840627870456203877142535098973).inspect
#puts "Zadanie 4 " + remSqEuler(5,13).inspect
# Example
puts squareRootFp(7,2).inspect
###################################################################################
# Example
#puts "Zadanie 6 " + primalityTest(13)
# Zadanie.4 Sprawdzenie czy element a jest reszta kwadratowa w Z_p
#
# Uzycie funkcji:
# remSqEuler(a,p)
#
# Gdzie a - element
# p - liczba pierwsza
#puts "Zadanie 4: " + remSqEuler(4,15485863).inspect
#puts "Zadanie 4: " + remSqEuler(3,13).inspect
#puts "Zadanie 4: " + remSqEuler(5,13).inspect
#puts "Zadanie 4: " + remSqEuler(5477183851370352794483563784128563497130907056727482167786963097750078781018279771969331294744464367184772268010020054134138848983974910921696137050139568263269632429620583642373162979289657018424493982802635787634837286977661054870863166216065362776490713, 9526498155551074622733236474497582527180243659164477550500224943978308908366162791700317474349020449651725045825391711253868497973855214826394160675701816160034393010008158121969738694286765731179652981990154532407906402992580840627870456203877142535098973).inspect
###################################################################################
# Zadanie.5 Obliczanie pierwiastka kwadratowego w ciele F_p*.
#
# Uzycie funkcji
# squareRootFp(p,b)
#
# Gdzie p - liczba pierwsza (modulo)
# b - reszta kwadratowa
puts "Zadanie 5: " + squareRootFp(15485863,2).inspect
###################################################################################
# Zadanie 6. Test pierwszości.
#
# Uzycie funkcji:
# primalityTest(n)
#
# Gdzie n - liczba wejsciowa
#puts "Zadanie 6: " + primalityTest(13).inspect
###################################################################################