Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Minor bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
JarryShaw committed Jul 12, 2017
1 parent 03f08fe commit 9ce44e8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion jsntlib/NTLArchive/NTLChineseRemainderTheorem.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def CHNRemainderTheorem(*args):
def solve(variable, modulo):
polyCgc = '%d*x - 1' % variable # 將係數與指數數組生成多項式

r = lambda x: eval(polyCgc) # 用於計算多項式的取值
r = lambda x: eval(polyCgc) # 用於計算多項式的取值
for x in jsrange(modulo): # 逐一驗算,如模為0則加入結果數組
if r(x) % modulo == 0:
return x
Expand Down
31 changes: 20 additions & 11 deletions jsntlib/NTLArchive/NTLCongruence.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _eval(self, *vars):

# 求取self在x=_num時(不取模)的值
def _calc(self, *vars):
_ret = super().eval(*vars)
_ret = super(Congruence, self).eval(*vars)
return _ret

# 素數模同餘式的簡化
Expand Down Expand Up @@ -216,11 +216,11 @@ def _primePro(self, rem, exp):

for tmpRem in rem:
# 尋找滿足(f'(x1),p)=1的x1
if greatestCommonDivisor(drv._calc(tmpRem), mod) == 1:
if greatestCommonDivisor(drv.mod(tmpRem), mod) == 1:
x = tmpRem

# 計算導式的值f'(x1) (mod p),取其負值
drvMod = crv._eval(x) - mod
drvMod = drv.mod(x, mod=mod) - mod
drvRcp = 1 // drvMod
break

Expand All @@ -231,20 +231,29 @@ def _primePro(self, rem, exp):
# x_i ≡ x_(i-1) + t_(i-1) * p^(i-1) (mod p^i)
x += (t * (mod**ctr)) % (mod**(ctr+1))

return x # remainder = x
return [x] # remainder = x

# 中國剩餘定理
def _CTR(self, rem, mod):
modulo = self._modulo # M(original modulo) = ∏m_i

# 求解M_i^-1 (mod m_i)
def solve(variable, modulo):
polyCgc = '%d*x - 1' % variable # 將係數與指數數組生成多項式

r = lambda x: eval(polyCgc) # 用於計算多項式的取值
for x in jsrange(modulo): # 逐一驗算,如模為0則加入結果數組
if r(x) % modulo == 0:
return x

modulo = self._modulo # M(original modulo) = ∏m_i

bList = []
for tmpMod2 in mod:
M = modulo // tmpMod2 # M_i = M / m_i
c = Congruence(((1, M), (0, -1)), mod=tmpMod2)
t = c._prime()[0] # t_i * M_i ≡ 1 (mod m_i)
bList.append(t * M) # b_i = t_i * M_i
for tmpMod in mod:
M = modulo // tmpMod # M_i = M / m_i
t = solve(M, tmpMod) # t_i * M_i ≡ 1 (mod m_i)
bList.append(t * M) # b_i = t_i * M_i

_rem = self._iterCalc(rem, bList) # x_j = Σ(b_i * r_i) (mod M)
_rem = self._iterCalc(rem, bList) # x_j = Σ(b_i * r_i) (mod M)
return _rem

# 對rto多維數組(層,號)中的數進行全排列並計算結果
Expand Down
2 changes: 1 addition & 1 deletion jsntlib/NTLArchive/NTLPolynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def _der(self):
_coe = self._vec[var][exp] * exp
else:
_exp = 0; _coe = 0
_ec[_exp] = [_coe]
_ec[_exp] = _coe
vec[var] = _ec
_der = Polynomial(vec)
return _der
Expand Down
3 changes: 2 additions & 1 deletion jsntlib/NTLArchive/NTLPolynomialCongruence.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def cpsMCS(cgcExp, cgcCoe, modulo):
tmpExpVar = q[ptr]
tmpMod.append(tmpModVar ** tmpExpVar)
tmpRmd.append(prmMCSLite(cgcExp, cgcCoe, tmpModVar, tmpExpVar))

# 用中國剩餘定理處理上述結果,得到最終結果
remainder = CHNRemainderTheorem(*zip(tmpMod, tmpRmd))

Expand Down Expand Up @@ -160,7 +161,7 @@ def prmMCSPro(cgcExp, cgcCoe, rmd, mod, exp):
# x_i ≡ x_(i-1) + t_(i-1) * p^(i-1) (mod p^i)
x += (t * (mod**ctr)) % (mod**(ctr+1))

return x # remainder = x
return [x] # remainder = x


# 求取多項式的導式
Expand Down
4 changes: 4 additions & 0 deletions jsntlib/NTLArchive/NTLTrivialDivision.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def trivialDivision(N):
raise DefinitionError('The argument must be a natural number greater than 1.')

try:
# 若輸入的常數過大,則採用強偽素數判斷
if math.log(N, 10) > 4.0:
raise MemoryError

# 得出小於等於N的所有素數
table = eratosthenesSieve(N+1)

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

name = 'jsntlib',

version = '1.1.0.1.3',
version = '1.1.1',

description =
'A number theory library adapted from mathematic fundamentals of information security homework codes.',
Expand All @@ -32,7 +32,7 @@

url = 'https://github.com/JarryShaw/jsntlib/tree/release',

download_url = 'https://github.com/JarryShaw/jsntlib/archive/1.1.0.1.3.tar.gz',
download_url = 'https://github.com/JarryShaw/jsntlib/archive/1.1.1.tar.gz',

packages = [
'jsntlib',
Expand Down

0 comments on commit 9ce44e8

Please sign in to comment.