Point_addition_affine_Mont.txt 2.02 KB
Newer Older
Aaisha Ghodekar's avatar
Aaisha Ghodekar committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def Pointaddition_affine_Mont(x1,y1,x2,y2,a,PRECISION):
r=(2^PRECISION).__xor__(n); rpoly = str_to_poly(r)
	r2poly = rpoly * rpoly; r2 = poly_to_str(r2poly)
	rinvpoly = rpoly^-1; rinv = poly_to_str(rinvpoly)

x1_poly= str_to_poly(x1)
y1_poly=str_to_poly(y1)
x2_poly= str_to_poly(x2)
y2_poly=str_to_poly(y2)

x1_poly_MD = x1_poly * r2poly * rinvpoly
y1_poly_MD = y1_poly * r2poly * rinvpoly
x2_poly_MD = x2_poly * r2poly * rinvpoly
y2_poly_MD = y2_poly * r2poly * rinvpoly

#print 'x1_poly=',x1_poly
#print 'y1_poly=',y1_poly
#print 'x2_poly=',x2_poly
#print 'y2_poly=',y2_poly

x1poly_MD = (1 * r2poly * rinvpoly)
	exp = 2^PRECISION-2
	for i in reversed(xrange(PRECISION)):
		x1poly_MD = x1poly_MD * x1poly_MD * rinvpoly
		if(exp.digits(base=2,padto=PRECISION)[i] == 1):
			x1poly_MD = x1_poly_MD * x1poly_MD * rinvpoly
	inv_x1_poly_MD = x1poly_MD

x2poly_MD = (1 * r2poly * rinvpoly)
	exp = 2^PRECISION-2
	for i in reversed(xrange(PRECISION)):
		x2poly_MD = x2poly_MD * x2poly_MD * rinvpoly
		if(exp.digits(base=2,padto=PRECISION)[i] == 1):
			x2poly_MD = x2_poly_MD * x2poly_MD * rinvpoly
	inv_x2_poly_MD = x2poly_MD

a_MD = (a * r2poly * rinvpoly)
1_MD = (1 * r2poly * rinvpoly)
	c1_MD = y1_poly_MD + y1_poly_MD * rinvpoly
	c2_MD = inv_x1_poly_MD + inv_x2_poly_MD * rinvpoly
	c3_MD = c1_MD * c2_MD * rinvpoly
	c4_MD = c3_MD * c3_MD * rinvpoly
	c5_MD = c4_MD + c3_MD * rinvpoly
	c6_MD = c5_MD +x1_poly_MD * rinvpoly
	c7_MD = c6_MD +x2_poly_MD * rinvpoly
	x3_poly_MD = c7_MD +a_MD * rinvpoly

	c1_MD = y1_poly_MD + y1_poly_MD * rinvpoly
	c2_MD = inv_x1_poly_MD + inv_x2_poly_MD * rinvpoly
	c3_MD = c1_MD * c2_MD * rinvpoly
	c4_MD = c3_MD * c3_MD * rinvpoly
	c5_MD = c4_MD * c3_MD * rinvpoly
	c6_MD = c5_MD + 1_MD * rinvpoly
	c7_MD = c6_MD + x2_poly_MD * rinvpoly
	c8_MD = c6_MD + a_MD * rinvpoly
	c9_MD = c8_MD + c3_MD * rinvpoly
	c10_MD = c9_MD + x1_poly_MD * rinvpoly
	c11_MD = c10_MD + x2_poly_MD * rinvpoly
	c12_MD = c11_MD + y1_poly_MD * rinvpoly
	y3_poly_MD = c12_MD +a_MD * rinvpoly



print 'x3_MD=',poly_to_str(x3_poly_MD)
print 'y3_MD=',poly_to_str(y3_poly_MD)