-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFullRepresentInteger.py
More file actions
73 lines (61 loc) · 1.99 KB
/
FullRepresentInteger.py
File metadata and controls
73 lines (61 loc) · 1.99 KB
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
"""
An implementation of FullRepresentInteger from New algorithms for the Deuring correspondence.
https://eprint.iacr.org/2022/234
Provides gamma in O0 (instead of just ZZ[i,j]). This slows down Com and DecVrfy, but the corresponding isogegies are indistinguishable from random.
As an implementation note, we try and append "Heuristic" to function
names when we only know solutions can be derived heuristically. If no
solution is found, `None` is returned.
"""
# Sage imports
from sage.all import (
ZZ,
floor,
sqrt,
randint
)
# Local imports
from ideals import (
quadratic_norm
)
from utilities import Cornacchia
from setup import (
O0,
p,
q,
j,
ω
)
def RepresentIntegerHeuristic(M, parity=False):
"""
Given an integer M, with M > p, attempts to
find a random element γ with norm M.
If no element is found after `bound` tries,
returns none
"""
def RepresentInteger(M, z, t, parity=False):
M_prime = 4 * M - p * quadratic_norm(z, t)
two_squares = Cornacchia(M_prime, -ZZ(ω**2))
if two_squares:
x, y = two_squares
if parity and (x + t) % 2 == 1 and (y + z) % 2 == 1:
return None
return (x + ω * y + j * (z - ω * t)) / 2
# No solution for the given M
return None
if M <= p:
raise ValueError(f"Can only represent integers M > p.")
m = max(floor(sqrt(4 * M / p)), 5)
# TODO: how many times should we try?
for _ in range(m**2):
z = randint(-m, m)
m2 = max(floor(sqrt(4 * M / p - z**2)), 5)
t = randint(-m2, m2)
γ = RepresentInteger(M, z, t, parity=parity)
if γ is not None:
# Found a valid solution, return
assert γ.reduced_norm() == M, "The norm is incorrect"
assert γ in O0, "The element is not contained in O0"
return γ
# No solution found, return None
print(f"DEBUG [RepresentIntegerHeuristic]: No solution found")
return None