Skip to content

Commit 898f6b0

Browse files
Merge pull request #5 from CanoeFZH/master
adds Euler Criterion method
2 parents 5540447 + 32b76ea commit 898f6b0

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The Number-Theory-Python package currently includes:
1111
* ECDSA.py: Elliptic curve signatures
1212

1313
Functions implemented in numbthy.py are:
14+
* euler_criterion(a, p) - Check if a is QR mod p
1415
* gcd(a,b) - Compute the greatest common divisor of a and b.
1516
* xgcd(a,b) - Find [g,x,y] such that g=gcd(a,b) and g = ax + by.
1617
* power_mod(b,e,n) - Compute b^e mod n efficiently.

numbthy.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
import math # Use sqrt, floor
4444
import functools # Use reduce (Python 2.5+ and 3.x)
4545

46+
def euler_criterion(a, p):
47+
"""p is odd prime, a is positive integer. Euler's Criterion will check if a is QR mod p. If yes, returns True. If a is NR mod p, then False"""
48+
return a ** ((p - 1) / 2) % p == 1
49+
4650
def gcd(a,b):
4751
"""gcd(a,b) returns the greatest common divisor of the integers a and b."""
4852
a = abs(a); b = abs(b)
@@ -185,7 +189,7 @@ def TSRsqrtmod(a,grpord,p):
185189
# Now a*(g**powg) is in cyclic group of odd order non2 - can sqrt directly
186190
d = invmod(2,non2)
187191
tmp = pow(a*pow(g,gpow,p),d,p) # sqrt(a*(g**gpow))
188-
return (tmp*inverse_mod(pow(g,gpow//2,p),p)) % p # sqrt(a*(g**gpow))//g**(gpow//2)
192+
return (tmp*inverse_mod(pow(g,gpow//2,p),p)) % p # sqrt(a*(g**gpow))//g**(gpow//2)
189193

190194
################ Internally used functions #########################################
191195

@@ -256,8 +260,8 @@ def invmod(b,n):
256260
return inverse_mod(b,n)
257261

258262
def eulerphi(n):
259-
"""eulerphi(n) - Compute Euler's Phi function of n - the number of integers strictly less than n which are coprime to n.
260-
(Renamed euler_phi(n) in ver 0.7)"""
263+
"""eulerphi(n) - Compute Euler's Phi function of n - the number of integers strictly less than n which are coprime to n.
264+
(Renamed euler_phi(n) in ver 0.7)"""
261265
return euler_phi(n)
262266

263267
def carmichaellambda(n):

0 commit comments

Comments
 (0)