-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhelper.py
104 lines (94 loc) · 2.59 KB
/
helper.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#
# Copyright (C) 2023 - This file is part of IPECC project
#
# Authors:
# Karim KHALFALLAH <[email protected]>
# Ryad BENADJILA <[email protected]>
#
# Contributors:
# Adrian THILLARD
# Emmanuel PROUFF
#
# This software is licensed under GPL v2 license.
# See LICENSE file at the root folder of the project.
#
####################
# Helper functions #
####################
# function to REDCify
# requires globals: p (prime)
# R = 2^(nn+2)
def redc(x, y, p, R, ppr):
sr = x * y
tr = (sr * ppr) % R
vr = sr + (tr * p)
wr = vr / R
return wr
# function to display split bit words of a big number
# (no global required)
def disp(x, base):
tmp = Integer(x)
ndx = 0
while (tmp != 0):
print(ndx,(tmp % 2**base).hex())
tmp = tmp // 2**base
ndx+=1
# function to say if a point is on curve
# requires globals: p, a, b (not in Montg. repres.)
# assumes: input (x,y,z) in normal repres. (not Montg.) and in affine form
def is_affine_point_on_curve(x, y, p, a, b):
if ( ((y**2) % p) == (((x**3) + (a*x) + b) % p) ):
return 1
else:
return 0
# function to say if a point is on curve
# requires globals: p (prime)
# a, b (params of the curve)
# redc (routine)
# assumes: input (x,y,z) in Montg. repres. and in Jacobian form
def is_jacobian_point_on_curve(x, y, z, p, R, ppr):
# leave Montgomery domain
xx = redc(1, x, p, R, ppr)
yy = redc(1, y, p, R, ppr)
zz = redc(1, z, p, R, ppr)
# return to affine coordinates
xxx = (xx/(zz**2)) % p
yyy = (yy/(zz**3)) % p
if ( ((yyy**2) % p) == (((xxx**3) + (a*xxx) + b) % p) ):
return 1
else:
return 0
# function to compute affine coordinates of a point given
# requires globals: p (prime)
# a, b (params of the curve)
# redc (routine)
# assumes: input (x,y,z) in Montg. repres. and in Jacobian form
def jacob2affine(x, y, z):
# leave Montgomery domain
xx = redc(1, x, p, R, ppr)
yy = redc(1, y, p, R, ppr)
zz = redc(1, z, p, R, ppr)
# return to affine coordinates
xxx = (xx/(zz**2)) % p
yyy = (yy/(zz**3)) % p
return (xxx, yyy)
# reduction modulo 2p after subtraction
def redsub2p(x, y, p):
z = x - y
if z < 0:
return z + (2*p)
else:
return z
# reduction modulo 2p after addition
def redadd2p(x, y, p):
z = x + y
if z > (2*p):
return z - (2*p)
else:
return z
# reduction modulo p
def reducep(z, p):
if z - p < 0:
return z
else:
return z - p