I'm trying to implement ECDSA using python as part of my homework, I have a function named multiplication which takes two arguments Point P and t calculates B= tP.I have implemented this algorith based on iterative double and add algorithm on this wikipedia page the problem is when p coordination is small(one or two digits), the algorithm works fine but when coordination is large (about 70 digits) the result is different than what it supposed to be. here is the part of my code which calculates multiplication:
def addition(self, p, q):
if p.is_infinite:
return q
elif q.is_infinite:
return p
else :
if (q.x - p.x) == 0:
point = Point.Point(0, 0)
point.is_infinite = True
return point
s = int(((q.y - p.y) * Utils.Utils.mode_inverse(q.x - p.x, self.prime)) % self.prime)
xr = int((math.pow(s, 2) - p.x - q.x) % self.prime)
yr = int(((s * (p.x - xr)) - p.y) % self.prime)
r = Point.Point(xr, yr)
return r
def double(self, p):
if p.is_infinite:
return p
if p.y == 0:
point = Point.Point(0, 0)
point.is_infinite = True
return point
s = int((((3 * math.pow(p.x, 2)) + self.a) * Utils.Utils.mode_inverse(2 * p.y, self.prime)) % self.prime)
xr = int((math.pow(s, 2) - p.x - p.x) % self.prime)
yr = int(((s * (p.x - xr)) - p.y) % self.prime)
r = Point.Point(xr, yr)
return r
def multiplication(self, p, t):
bin_t = bin(t)[2:]
Q = Point.Point(p.x, p.y)
Q.is_infinite = True
for i, digit in enumerate(bin_t):
Q = self.double(Q)
if digit == '1':
Q = self.addition(Q, p)
return Q
Here is my Util class:
class Utils(object):
#staticmethod
def mode_inverse(a, m):
return pow(a, m - 2, m)
Here is my Point class:
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.is_infinite = False
I use Curve P-224 parameters which are:
p = 26959946667150639794667015087019630673557916260026308143510066298881
a = -3
b = 18958286285566608000408668544493926415504680968679321075787234672564
Gx = 19277929113566293071110308034699488026831934219452440156649784352033
Gy = 19926808758034470970197974370888749184205991990603949537637343198772
according to calculator http://www.christelbach.com/eccalculator.aspx I should get this result for calculating 2G:
Px = 11838696407187388799350957250141035264678915751356546206913969278886
Py = 2966624012289393637077209076615926844583158638456025172915528198331
but what I actually get is:
Px = 15364035107168693070617763393106849380516103015030577748254379737088
Py = 7033137909116168824469040716130881489351924269422358605872723100109
Is there any way to fix this?
This was just a guess:
math.pow returns a floating point number (which has finite precision). I'd suggest using e.g. s.x * s.x instead of math.pow(s.x,2) in case the issue is hitting precision issues with bigger numbers.
Related
I’m trying to solve the following problem for around 2 days, but have not had success.
I want to calculate the derivative using the sparse matrix, but the result doesn’t true. I think there is a mistake in the solution function, but I cannot find it.
class Cahn_Hillard(object):
def __init__(self, n, X, T, dt, skip):
self.n = n
self.X = X
self.dx = 1 / (n - 1)
self.T = T
self.dt = dt
self.N = int(self.T / self.dt)
def __call__(self):
central = self.forward_diff_sparse()
itr = int(self.T / self.dt)
for i in range(0, itr + 1):
if i == 0:
c = np.random.uniform(0, 1, (self.n, 1))
else:
c_next = self.solution(c, central)
c = c_next
print(i)
return c
def forward_diff_sparse(self):
sparse_mat = np.eye(self.n) - np.eye(self.n, k=-1)
return sparse_mat
def solution(self, c, central):
# calculate derivative of concentration
deriv = central.dot(c) / self.dx
# calculate difusion coeffcient
D_eff = (1 - 2 * self.X * c * (1 - c)) * deriv
Diff = central.dot(D_eff) / self.dx
# Calculate the next step concentration
next_c = c + self.dt * Diff
return next_c
It would be great if you help me.
So far, finding the hypotenuse, "side c" works fine without problems, the problem is when I try to calculate the angle, I am pretty sure it's returning a wrong value for the angle.
import math
from math import sqrt
class Triangle:
def __init__(self, side_a, side_b, ):
self.side_a = side_a
self.side_b = side_b
def SidesCalculate(self, ):
side_c = sqrt(self.side_a ** 2 + self.side_b ** 2)
return side_c
def AnglesCalculate(self, side_c):
x = math.sin(self.side_a / side_c)
math.asin(x)
x = round(x * 180 / math.pi)
return x
g = Triangle(side_a=int(input("Enter side a: ")), side_b=int(input("Enter side b: ")))
print("side c =", + g.SidesCalculate())
print("angle is =", + g.AnglesCalculate(side_c=True), '°')
Here is a way to do what you want.
First when you compute side_c, save it as an attribute.
def SidesCalculate(self, ):
self.side_c = sqrt(self.side_a ** 2 + self.side_b ** 2)
return self.side_c
Secondly, give the parameter side_c a default value None. (You also have another math error in the following function pointed by CFLS)
def AnglesCalculate(self, side_c = None):
if side_c == None:
side_c = self.side_c
x = math.asin(self.side_a / side_c)
x = round(x * 180 / math.pi)
return x
Now, if you want the class to pick previously computed side_c, you can do this:
g = Triangle(side_a=int(input("Enter side a: ")), side_b=int(input("Enter side b: ")))
print("side c =", + g.SidesCalculate())
print("angle is =", + g.AnglesCalculate(), '°')
def AnglesCalculate(self, side_c):
x = math.asin(self.side_a / side_c)
x = round(x * 180 / math.pi)
return x
Using python 3: I have three functions: montePi, isInCircle and main.
I need to have the isInCircle called by montePi. The function will work, it just says that isInCircle is not defined. How can I define it?
import random
import math
def montePi(numDarts):
inCircle = 0
def isInCircle(x, y, r):
r = 1
d = math.sqrt(x**2 + y**2)
if d <= r:
return True
else:
return False
for i in range(numDarts):
x = random.random()
y = random.random()
d = math.sqrt(x**2 + y**2)
if d <= 1:
inCircle = inCircle +1
pi = inCircle / numDarts * 4
return pi
def main():
print(montePi(100))
print(montePi(1000))
print(montePi(10000))
print(montePi(100000))
main()
Because function isInCircle is defined in montePi, it can be called within montePi but not other functions as it is local. If you define isInCircle outside of montePi then you'll be able to call it from main.
Not sure what you're trying to program here, but there seems to be a chance that this question, regarding functions within functions can help you decide what you want here. Here is a question that covers how scopes work.
Should you need to call isInCircle from main or outside main, then this is how it should be formatted;
import random
import math
def isInCircle(x, y, r):
r = 1
d = math.sqrt(x**2 + y**2)
if d <= r:
return True
else:
return False
def montePi(numDarts):
inCircle = 0
for i in range(numDarts):
x = random.random()
y = random.random()
d = math.sqrt(x**2 + y**2)
if d <= 1:
inCircle = inCircle +1
pi = inCircle / numDarts * 4
return pi
def main():
print(montePi(100))
print(montePi(1000))
print(montePi(10000))
print(montePi(100000))
main()
I'm currently writing a code to perform Gaussian elimination in MATLAB and then write out the code needed to generate a LaTex file showing all the steps. A lot of the times when I do Gaussian elimination the answers start turning into Fractions. So I thought as a nice learning exercise for classes in Matlab that I would write a Fraction class. But I have no clue how to overload operators and frankly Mathwork's documentation wasn't helpful.
classdef Fraction
properties
numer
denom
end
methods
function a = Fraction(numer,denom)
a.denom = denom;
a.numer = numer;
end
function r = mtimes(a,b)
r = Fraction(a.numer*b.numer,a.denom*b.demon);
end
function r = plus(a,b)
c = a.numer*b.denom+a.denom*b.numer;
d = a.denom*b.denom;
r = Fraction(c,d);
function r = minus(a,b)
c = a.numer*b.denom-a.denom*b.numer;
d = a.denom*b.denom;
r = Fraction(c,d);
end
function r = mrdivide(a,b)
r = Fraction(a.numer*b.denom,a.denom*b.numer);
end
function b = reduceFrac(a)
x = a.numer;
y = b.denom;
while y ~= 0
x = y;
y = mod(x,y);
end
b =Fraction(a.numer/x, a.denom/x)
end
end
end
The plus operator works but the other three do not. Does any one have any ideas? Also how do I call my method reduceFrac?
Fraction.reduceFrac(Fraction(2.4))
I thought that the code above would work, but it didn't. Below is the python version of what I am trying to acheive.
Fraction.py
class Fraction(object):
"""Fraction class
Attributes:
numer: the numerator of the fraction.
denom: the denominator of the fraction.
"""
def __init__(self, numer, denom):
"""Initializes the Fraction class
Sets the inital numer and denom for the
fraction class.
Args:
numer: Top number of the Fraction
denom: Bottom number of the Fraction
Returns:
None
Raises:
None
"""
self.numer = numer
self.denom = denom
def __str__(self):
"""function call along with the print command
Args:
None
Returns:
String: numer / denom.
Raises:
None
"""
return str(self.numer) + '/' + str(self.denom)
def get_numer(self):
return self.numer
def set_numer(self, numer):
self.numer = numer
def get_denom(self):
return self.denom
def set_denom(self, denom):
self.denom = denom
def __add__(self, other):
numer = self.numer*other.denom+other.numer*self.denom
denom = self.denom*other.denom
return Fraction.reduceFrac(Fraction(numer,denom))
def __div__(self, other):
numer = self.numer*other.denom
denom = self.denom*other.numer
return Fraction.reduceFrac(Fraction(numer,denom))
def __sub__(self, other):
numer = self.numer*other.denom-other.numer*self.denom
denom = self.denom*other.denom
return Fraction.reduceFrac(Fraction(numer,denom))
def __mul__(self, other):
numer = self.numer*other.numer
denom = self.denom*other.denom
return Fraction.reduceFrac(Fraction(numer,denom))
def reduceFrac(self):
x = self.numer
y = self.denom
while y != 0:
(x, y) = (y, x % y)
return Fraction(self.numer/x, self.denom/x)
if __name__ == "__main__":
v = Fraction(4,3)
g = Fraction(7,8)
r = Fraction(4,8)
a = v + g
print a
s = v - g
print s
d = v / g
print d
m = v * g
print m
f = Fraction.reduceFrac(r)
print f
Your plus function misses an end
So I have this program:
def gcd(x, y):
while y % x != 0:
y = y % x
x = x % y
return y
def lcm(x, y):
(x * y) / gcd(x, y)
a = lcm(1, 2)
b = lcm(3, a)
c = lcm(b, 4)
d = lcm(5, c)
e = lcm(6, d)
f = lcm(e, 7)
g = lcm(f, 8)
h = lcm(9, g)
i = lcm(h, 10)
j = lcm(11, i)
k = lcm(12, j)
l = lcm(k, 13)
m = lcm(l, 14)
n = lcm(15, m)
o = lcm(n, 16)
p = lcm(17, o)
q = lcm(18, p)
r = lcm(q, 19)
print(lcm(r, 20))
Ok, so I've worked that one out now thanks to answers on this post, but why does this:
def gcd(x, y):
while y % x != 0:
y = y % x
x = x % y
return y
def lcm(x, y):
return (x * y) / gcd(x, y)
a = lcm(1, 2)
b = lcm(3, a)
c = lcm(b, 4)
d = lcm(5, c)
e = lcm(6, d)
f = lcm(e, 7)
g = lcm(f, 8)
h = lcm(9, g)
i = lcm(h, 10)
j = lcm(11, i)
k = lcm(12, j)
l = lcm(k, 13)
m = lcm(l, 14)
n = lcm(15, m)
o = lcm(n, 16)
p = lcm(17, o)
q = lcm(18, p)
r = lcm(q, 19)
print(lcm(r, 20))
return this:
ZeroDivisonError: float modulo
Complaining about the second line, I gather I might need some sort of if statement to put the two numbers in order.
Anyone got any information about why this is happening?
Thanks.
You forgot to return something in the lcm function.
def lcm(x, y):
return (x * y) / gcd(x, y)
When you don't explicitly return anything in a function, Python automatically returns None. When you try lcm(r, 20), that's the same as lcm(None, 20), which doesn't work since you multiply both parameters in the lcm function - you can't do None * 20.
However this will yield another error - there's a problem with your gcd function as well!
The error comes when y % x is 0, and then you do x % y straight after. This would be a working solution:
def gcd(x, y):
while y != 0:
x, y = y, x % y
return x
This is taken straight from Euclid's algorithm from the Wikipedia article (Section 2.2).
There is a misunderstanding here.when you use gcd() inside lcm(), you are creating nested environments. gcd()'s environment doesn't have access to the global environment.anything that it returns, will be accessible by lcm()'s environment.you don't return anything to the global environment in lcm(), so python by default returns None.to fix this try:
def lcm(x, y):
return (x * y) / gcd(x, y)
also you have to add this block to the lcm() to avoid throwing ZeroDivision exception:
def lcm(x, y):
try:
return (x * y) / gcd(x, y)
except ZeroDivisionError:
do whatever you want and return a value