-
Notifications
You must be signed in to change notification settings - Fork 1
/
legendre_bit.vy
41 lines (36 loc) · 878 Bytes
/
legendre_bit.vy
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
LOOP_ROUNDS: constant(uint256) = 2**32
@public
@constant
def legendre_bit(input_a: uint256, q: uint256) -> uint256:
a: uint256 = input_a
if a >= q:
a = a % q
if a == 0:
return 0
assert(q > a and a > 0 and q % 2 == 1)
t: uint256 = 1
n: uint256 = q
r: uint256
tmp_a: uint256
for _ in range(LOOP_ROUNDS):
if a == 0:
break
else:
for _1 in range(LOOP_ROUNDS):
if a % 2 != 0:
break
else:
a = a / 2
r = n % 8
if r == 3 or r == 5:
t = -t
tmp_a = a
a = n
n = tmp_a
if a % 4 == 3 and n % 4 == 3:
t = -t
a %= n
if n == 1:
return (t + 1) / 2
else:
return 0