2020-11-07 18:33:34 +01:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
#
|
|
|
|
# Marcin Woźniak
|
|
|
|
# s434812
|
|
|
|
#
|
2020-11-09 19:51:28 +01:00
|
|
|
# Last edit: 09-11-2020
|
2020-11-07 18:33:34 +01:00
|
|
|
#
|
|
|
|
#####################################
|
|
|
|
|
|
|
|
require 'openssl'
|
|
|
|
require 'securerandom'
|
|
|
|
require 'prime'
|
|
|
|
|
|
|
|
def extended_euklides(a, b)
|
|
|
|
return 1, 0 if b == 0
|
|
|
|
|
|
|
|
q, r = a.divmod b
|
|
|
|
s, t = extended_euklides(b, r)
|
|
|
|
|
|
|
|
return t, s - q * t
|
|
|
|
end
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
# Zad. 1.1
|
2020-11-07 18:33:34 +01:00
|
|
|
def random_gen_Zn(n,k)
|
|
|
|
if 2**(k-1) < n && k > 0 then
|
|
|
|
if k == 1 then
|
|
|
|
min = 0
|
|
|
|
max = 1
|
|
|
|
else
|
|
|
|
min = 2**(k-1)
|
|
|
|
max = (2**k)-1
|
|
|
|
end
|
|
|
|
end
|
2020-11-08 23:33:06 +01:00
|
|
|
|
|
|
|
while true do
|
2020-11-09 19:51:28 +01:00
|
|
|
r = SecureRandom.random_number(max)
|
2020-11-08 23:33:06 +01:00
|
|
|
if r < n then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2020-11-07 18:33:34 +01:00
|
|
|
|
2020-11-09 19:51:28 +01:00
|
|
|
return r
|
2020-11-07 18:33:34 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
# Zad. 1.2
|
2020-11-08 23:33:06 +01:00
|
|
|
def reciprocal_Phi_p(n,p)
|
|
|
|
u = extended_euklides(n,p)[0]
|
|
|
|
v = extended_euklides(n,p)[1]
|
2020-11-07 18:33:34 +01:00
|
|
|
|
2020-11-08 23:33:06 +01:00
|
|
|
if u * n % p == 1
|
2020-11-09 19:51:28 +01:00
|
|
|
return u
|
2020-11-07 18:33:34 +01:00
|
|
|
else
|
2020-11-09 19:51:28 +01:00
|
|
|
return v
|
2020-11-07 18:33:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
# Zad. 1.3
|
2020-11-07 18:33:34 +01:00
|
|
|
def betterExponentiation(x,k,n)
|
2020-11-09 23:11:40 +01:00
|
|
|
if n == 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-11-08 23:33:06 +01:00
|
|
|
b = k.to_s(2).reverse
|
2020-11-07 18:33:34 +01:00
|
|
|
l = b.count "[0-1]"
|
|
|
|
y = 1
|
|
|
|
i = l - 1
|
|
|
|
|
2020-11-08 23:33:06 +01:00
|
|
|
while i >= 0
|
2020-11-07 18:33:34 +01:00
|
|
|
y = y**2 % n
|
2020-11-08 23:33:06 +01:00
|
|
|
if b[i]=="1" then
|
|
|
|
y = y * x % n
|
2020-11-07 18:33:34 +01:00
|
|
|
end
|
2020-11-08 23:33:06 +01:00
|
|
|
i = i - 1
|
2020-11-07 18:33:34 +01:00
|
|
|
end
|
|
|
|
return y
|
|
|
|
end
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
# Zad. 1.4
|
2020-11-07 18:33:34 +01:00
|
|
|
def remSqEuler(a,p)
|
|
|
|
ans = betterExponentiation(a,(p-1)/2,p)
|
|
|
|
|
2020-11-08 23:33:06 +01:00
|
|
|
if ans == 1 && Prime.prime?(p) then
|
2020-11-07 18:33:34 +01:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
# Zad. 1.5
|
2020-11-08 23:33:06 +01:00
|
|
|
def squareRootFp(p,b)
|
|
|
|
if p % 4 == 3 && remSqEuler(p,b) == true then
|
2020-11-09 23:11:40 +01:00
|
|
|
a = betterExponentiation(b, (p+1)/4, p)
|
|
|
|
return a
|
2020-11-08 23:33:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
# Zad. 1.6
|
2020-11-08 23:33:06 +01:00
|
|
|
def primalityTest(n)
|
|
|
|
if n == 1 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if n == 2 || n == 3 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-11-09 14:05:01 +01:00
|
|
|
counter = rand(3..n)
|
2020-11-08 23:33:06 +01:00
|
|
|
while (counter != 0) do
|
2020-11-09 14:05:01 +01:00
|
|
|
b = rand(2..n-2) # Tez dziala n-1
|
2020-11-08 23:33:06 +01:00
|
|
|
if betterExponentiation(b,n-1,n) != 1 then
|
|
|
|
return false
|
|
|
|
end
|
2020-11-09 14:05:01 +01:00
|
|
|
counter = counter - 1
|
2020-11-07 18:33:34 +01:00
|
|
|
end
|
2020-11-08 23:33:06 +01:00
|
|
|
return true
|
2020-11-07 18:33:34 +01:00
|
|
|
end
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
###################################################################################
|
|
|
|
|
|
|
|
# Zadanie.1 Losowy element z zbioru Z_n
|
2020-11-09 19:51:28 +01:00
|
|
|
#
|
2020-11-09 23:11:40 +01:00
|
|
|
# Uzycie funkcji:
|
|
|
|
# random_gen_Zn(n,k)
|
|
|
|
#
|
|
|
|
# Gdzie n - grupa mod
|
|
|
|
# k - ilosc bitow
|
|
|
|
|
2020-11-09 19:51:28 +01:00
|
|
|
#puts "Zadanie 1 " + random_gen_Zn(2817960879631397637428637785383222308241674912977296371078328827783823173932945686560271709717223685413005001487628305095704070793958971415087255523073935114518202433817141315589046697665767753020703330561718642810157214393228915238808369985314803605127547366769525321238034444131545038866079220135011303234308583293055538438855440051375449844277218217712896675378963068720345515473434111699867542985885331741594726201346190830240692715577811297545617592986841862266976883958728343753502720654957572591625318922387371348775299617016576604164586635463962864505536324347369114225605202600072081219902252264258236028218613372215876496394341867072862629022156295534655310094481486891656868251869749451857161662890356600101860594118885136794555465318782343870373413707643678756555809662335374658562880045543430140169913540153961566294704894163494226738977413711366121112146042996377423578488354012607979075727403258666348434920910981957617905109649522084109820427893425361930683246183514216962034556016051184044970184676662827978420213281480510069379347007543973139554178175690773315927469361215139519543395792659825942864347234234285907203635728345512501417615714291999748745375110198782386769755218587158953598215023147871490893052124457996091330393194346889440433647348345868290410045628941650640315817691074244257070087501412656465257388504574920756736653658826614297081956072241319983753399595743495837950519045870810249068878008938965513649172515852388457433200750100902751828486806189811961720524504976040165319005498382580099718522287255514024665802127411452771343820848551192190973237603226514162458911052832006635099614443220118029822801995173084006448848714642581145597450095993844936566471047767974285413266855493150984091512278787539020882065620089396507786047199321023827465489494495513119543424285384803025749486259267450477917340669954258887755716092814959537213397128776047910200001947756249766479670986137076981224642743069725401099384031028670810757683522821674118926373517718330706064178699035286815124318059529948663730575377063300582239742709428517028624628630255640464009691179606104444672232047869709734836046571505598681429286372976726889778746623725623485244272247797117303046464791441413956650048631706362550020890628552166104678993837549091779514727408814458173452235187339390489070470959043964826221641981413639067723526075949807992319555177364587831440901887979371647118775640122112691496917328672989185787786333743943571201682455849788768447941052589112093217146238196393058840427167875379895033862665036580869700901671149307822867600084775797450043528335040523181300939047560092173656437964805986878846909755616898664300908149121103065456346895895553346803762232995496756924185713587688989499627282573083392504068537865838813511676470822568313458216527495376958238195629276140142912559084213140117194512209752564237149304569698487234268004667685663277598956964116311211220970915272172182795014191615102024324085452311963348372375839824417067170842114940446271152850552864783993722225100122068507428533164772669151799555291529447719732682260868021763685052338407930046892500473287361324180925326385131283404240275852188687583358717018916949107259963059221294732376067807306008309012964676038322426438467806570485299377626514669181680963962016304313021681669696580753784295259640169007074506637844332833804045109981978476343942218065708022217566222881348496310403404164467438352649885637702780969376650799749541038736604262724814821757258372470336571211357167468680200965241152364670892328226187787108777323221988154289834826302745446444518424440700033948649807665278475079826664974213162552336459719975478725157313669922561440432360473265250572606571269284353534676478375363478936179040075297456773814344907213125333476476655366455733030460908712160208004620689954788191597043073132610377893018543064926955781952650214504633097216561549411315119718460509357878230720821699669794678584993658379092808863297233433557739623968183417003317301945898487820593437670555891465841469699644209269444311251317668652989906375182749637967733151694
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
###################################################################################
|
|
|
|
|
|
|
|
# Zadanie.2 Odwrotnosc w grupie Phi(n)
|
|
|
|
#
|
|
|
|
# Uzycie funkcji:
|
|
|
|
# reciprocal_Phi_p(n,p)
|
2020-11-09 19:51:28 +01:00
|
|
|
#
|
2020-11-09 23:11:40 +01:00
|
|
|
# Gdzie p - element w grupie phi
|
|
|
|
# n - liczba nalezaca do N
|
|
|
|
|
|
|
|
#puts "Zadanie 2 " + reciprocal_Phi_p(10,13).inspect
|
2020-11-09 19:51:28 +01:00
|
|
|
#puts "Zadanie 2 " + reciprocal_Phi_p(814124120905749248299480415803476335639365723498296123469468592347073656928224527500310685973387246534780850320750411701278256916776754378412188178575147137091744859172404523278057868961064832876066462511095131108735875202132121424592704723, 9526498155551074622733236474497582527180243659164477550500224943978308908366162791700317474349020449651725045825391711253868497973855214826394160675701816160034393010008158121969738694286765731179652981990154532407906402992580840627870456203877142535098973).inspect
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
###################################################################################
|
|
|
|
|
|
|
|
# Zadanie.3 Efektywne potegowanie.
|
|
|
|
#
|
|
|
|
# Uzycie funkcji:
|
|
|
|
# betterExponentiation(x,k,n)
|
|
|
|
#
|
|
|
|
# Gdzie obliczna jest wartosc x^k mod n
|
2020-11-09 19:51:28 +01:00
|
|
|
|
|
|
|
#puts "Zadanie 3 " + betterExponentiation(8,2,30).inspect
|
|
|
|
#puts "Zadanie 3 " + betterExponentiation(814124120905749248299480415803476335639365723498296123469468592347073656928224527500310685973387246534780850320750411701278256916776754378412188178575147137091744859172404523278057868961064832876066462511095131108735875202132121424592704723, 9526498155551074622733236474497582527180243659164477550500224943978308908366162791700317474349020449651725045825391711253868497973855214826394160675701816160034393010008158121969738694286765731179652981990154532407906402992580840627870456203877142535098973, 814124120905749248299480415803476335639365723498296123469468592347073656928224527500310685973387246534780850320750411701278256916776754378412188178575147137091744859172404523278057868961064832876066462511095131108735875202132121424592704723321321312312312312321312312321).inspect
|
|
|
|
|
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
###################################################################################
|
|
|
|
|
|
|
|
# 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
|
2020-11-09 19:51:28 +01:00
|
|
|
|
2020-11-09 23:11:40 +01:00
|
|
|
###################################################################################
|