241 lines
11 KiB
Ruby
241 lines
11 KiB
Ruby
|
#!/usr/bin/ruby
|
||
|
|
||
|
#####################################
|
||
|
#
|
||
|
# Marcin Woźniak
|
||
|
# s434812
|
||
|
#
|
||
|
#####################################
|
||
|
|
||
|
require 'openssl'
|
||
|
require 'securerandom'
|
||
|
require 'prime'
|
||
|
require 'thread'
|
||
|
|
||
|
def nwd(a, b)
|
||
|
b == 0 ? a : nwd(b, a.modulo(b))
|
||
|
end
|
||
|
|
||
|
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
|
||
|
|
||
|
# Zad. 1.1
|
||
|
def random_gen_Zn(n,k)
|
||
|
if 2**(k-1) < n && k > 0
|
||
|
if k == 1
|
||
|
min = 0
|
||
|
max = 1
|
||
|
else
|
||
|
min = 2**(k-1)
|
||
|
max = (2**k)-1
|
||
|
end
|
||
|
end
|
||
|
|
||
|
while true do
|
||
|
r = SecureRandom.random_number(min..max)
|
||
|
if r < n
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
|
||
|
return r
|
||
|
|
||
|
end
|
||
|
|
||
|
# Zad. 1.2
|
||
|
def reciprocal_Phi_p(n,p)
|
||
|
u = extended_euklides(n,p)[0]
|
||
|
v = extended_euklides(n,p)[1]
|
||
|
|
||
|
if u * n % p == 1
|
||
|
return u
|
||
|
else
|
||
|
return v
|
||
|
end
|
||
|
end
|
||
|
|
||
|
# Zad. 1.3
|
||
|
def betterExponentiation(x,k,n)
|
||
|
if n == 0
|
||
|
return false
|
||
|
end
|
||
|
|
||
|
b = k.to_s(2).reverse
|
||
|
l = b.count "[0-1]"
|
||
|
y = 1
|
||
|
i = l - 1
|
||
|
|
||
|
while i >= 0
|
||
|
y = y**2 % n
|
||
|
if b[i]=="1"
|
||
|
y = y * x % n
|
||
|
end
|
||
|
i = i - 1
|
||
|
end
|
||
|
return y
|
||
|
end
|
||
|
|
||
|
# Zad. 1.4
|
||
|
def remSqEuler(a,p)
|
||
|
ans = betterExponentiation(a,(p-1)/2,p)
|
||
|
|
||
|
if ans == 1 && Prime.prime?(p)
|
||
|
return true
|
||
|
else
|
||
|
return false
|
||
|
end
|
||
|
end
|
||
|
|
||
|
# Zad. 1.5
|
||
|
def squareRootFp(p,b)
|
||
|
if p % 4 == 3 && remSqEuler(p,b) == true
|
||
|
a = betterExponentiation(b, (p+1)/4, p)
|
||
|
return a
|
||
|
end
|
||
|
end
|
||
|
|
||
|
# Zad. 1.6
|
||
|
def primalityTest(n)
|
||
|
if n == 1
|
||
|
return false
|
||
|
end
|
||
|
|
||
|
if n == 2 || n == 3
|
||
|
return true
|
||
|
end
|
||
|
|
||
|
counter = 10
|
||
|
while (counter != 0) do
|
||
|
b = SecureRandom.random_number(2..n-2) # Tez dziala n-1
|
||
|
if betterExponentiation(b,n-1,n) != 1
|
||
|
return false
|
||
|
end
|
||
|
counter = counter - 1
|
||
|
end
|
||
|
return true
|
||
|
end
|
||
|
|
||
|
|
||
|
def randomNumber(k)
|
||
|
randomNumberArray=[]
|
||
|
randomNumberArray << 1
|
||
|
k= k - 1
|
||
|
|
||
|
while (k !=0 ) do
|
||
|
j = SecureRandom.random_number(2)
|
||
|
randomNumberArray << j
|
||
|
k = k - 1
|
||
|
end
|
||
|
return randomNumberArray.join.to_i(2)
|
||
|
end
|
||
|
|
||
|
def specyficPrimaryNumber
|
||
|
while true do
|
||
|
q = SecureRandom.random_number(2 ** 256)
|
||
|
p = 2 * q + 1
|
||
|
if primalityTest(q) && primalityTest(p)
|
||
|
return p,q
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def generator(p,q)
|
||
|
while true
|
||
|
g = SecureRandom.random_number(2..p-2)
|
||
|
if betterExponentiation(g,q,p) == 1
|
||
|
next
|
||
|
else
|
||
|
return g
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def generatePrime(n)
|
||
|
return `openssl prime -generate -bits '#{n}'`.gsub(/\n$/, '').to_i
|
||
|
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(281796087963139763742863778538322230824167491297729637107832882778382317393294568656027170971722368541300500148762830509570407079395897141508725552307393511451820243381714131558904669766576775302070333056171864281015721439322891523880836998531480360512754736676952532123803444413154503886607922013501130323430858329305553843885544005137544984427721821771289667537896306872034551547343411169986754298588533174159472620134619083024069271557781129754561759298684186226697688395872834375350272065495757259162531892238737134877529961701657660416458663546396286450553632434736911422560520260007208121990225226425823602821861337221587649639434186707286262902215629553465531009448148689165686825186974945185716166289035660010186059411888513679455546531878234387037341370764367875655580966233537465856288004554343014016991354015396156629470489416349422673897741371136612111214604299637742357848835401260797907572740325866634843492091098195761790510964952208410982042789342536193068324618351421696203455601605118404497018467666282797842021328148051006937934700754397313955417817569077331592746936121513951954339579265982594286434723423428590720363572834551250141761571429199974874537511019878238676975521858715895359821502314787149089305212445799609133039319434688944043364734834586829041004562894165064031581769107424425707008750141265646525738850457492075673665365882661429708195607224131998375339959574349583795051904587081024906887800893896551364917251585238845743320075010090275182848680618981196172052450497604016531900549838258009971852228725551402466580212741145277134382084855119219097323760322651416245891105283200663509961444322011802982280199517308400644884871464258114559745009599384493656647104776797428541326685549315098409151227878753902088206562008939650778604719932102382746548949449551311954342428538480302574948625926745047791734066995425888775571609281495953721339712877604791020000194775624976647967098613707698122464274306972540109938403102867081075768352282167411892637351771833070606417869903528681512431805952994866373057537706330058223974270942851702862462863025564046400969117960610444467223204786970973483604657150559868142928637297672688977874662372562348524427224779711730304646479144141395665004863170636255002089062855216610467899383754909177951472740881445817345223518733939048907047095904396482622164198141363906772352607594980799231955517736458783144090188797937164711877564012211269149691732867298918578778633374394357120168245584978876844794105258911209321714623819639305884042716787537989503386266503658086970090167114930782286760008477579745004352833504052318130093904756009217365643796480598687884690975561689866430090814912110306545634689589555334680376223299549675692418571358768898949962728257308339250406853786583881351167647082256831345821652749537695823819562927614014291255908421314011719451220975256423714930456969848723426800466768566327759895696411631121122097091527217218279501419161510202432408545231196334837237583982441706717084211494044627115285055286478399372222510012206850742853316477266915179955529152944771973268226086802176368505233840793004689250047328736132418092532638513128340424027585218868758335871701891694910725996305922129473237606780730600830901296467603832242643846780657048529937762651466918168096396201630431302168166969658075378429525964016900707450663784433283380404510998197847634394221806570802221756622288134849631040340416446743835264988563770278096937665079974954103873660426272481482175725837247033657121135716746868020096524115236467089232822618778710877732322198815428983482630274544644451842444070003394864980766527847507982666497421316255233645971997547872515731366992256144043236047326525057260657126928435353467647837536347893617904007529745677381434490721312533347647665536645573303046090871216020800462068995478819159704307313261037789301854306492695578195265021450463309721656154941131511971846050935787823072082169966979467858499365837909280886329723343355773962396818341700331730194589848782059343767055589146584146969964420926944431125131766865298990637518274963796773315169
|
||
|
|
||
|
###################################################################################
|
||
|
|
||
|
# 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(76638723687263876287368268368726378623873687326872634868374687236487623874687648634863847623846834687643,100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961 ).inspect
|
||
|
|
||
|
###################################################################################
|
||
|
|
||
|
# Zadanie.3 Efektywne potegowanie.
|
||
|
#
|
||
|
# Uzycie funkcji:
|
||
|
# betterExponentiation(x,k,n)
|
||
|
#
|
||
|
# Gdzie obliczna jest wartosc x^k mod n
|
||
|
|
||
|
#puts "Zadanie 3: " + betterExponentiation(823789137891789217389173981378913789137289,565490994747691690475378499398697660773449981085993539792067,1399661509700116309409184866497198118594638278433610469383879).inspect
|
||
|
#puts "Zadanie 3: " + betterExponentiation(8,2,30).inspect
|
||
|
#puts "Zadanie 3: " + betterExponentiation(76638723687263876287368268368726378623873687326872634868374687236487623874687648634863847623846834687643, 76382637812836812638612836812638612376182263812623861283618723681263861238612386, 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961).inspect
|
||
|
|
||
|
|
||
|
###################################################################################
|
||
|
|
||
|
# 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
|
||
|
|
||
|
###################################################################################
|
||
|
|
||
|
# 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
|
||
|
#puts "Zadanie 6: " + primalityTest(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961).inspect
|
||
|
|
||
|
###################################################################################
|