-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivf.m
60 lines (57 loc) · 1.04 KB
/
divf.m
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
function [x1] = divf( x,p)
% 实现了有限域求逆的计算
% x是有限域中的数,p是一个素数
% 输出x1=x^(-1)
% made by koala
% 2016,7,14
u=p;
v=x;
r=0;
s=1;
while 1
if bitget(u,1)==0
if bitget(r,1)==0
r=bitshift(r,-1);
else
r=bitshift(r+p,-1);
end
r=mod(r,p);
if bitget(u,1)==0
u=bitshift(u,-1);
else
u=bitshift(u+p,-1);
end
end
v=uint32(v);
if bitget(v,1)==0
if bitget(s,1)==0
s=bitshift(s,-1);
else
s=bitshift(s+p,-1);
end
s=mod(s,p);
if bitget(v,1)==0
v=bitshift(v,-1);
else
v=bitshift(v+p,-1);
end
end
if bitget(v,1)==1 && bitget(u,1)==1
if u > v
r=mod((r-s),p);
u=u-v;
else
s=mod((s-r),p);
v=v-u;
end
end
if u==1 || v==1
break;
end
end
if u==1
x1=r;
else
x1=s;
end
end