模逆stein算法(sram)


input:two integers u,m
output:u inv mod m
s1:if u and m are both even, then no inv,over;
else if m is even, swap u and m,f <=1;
s2:n1<=u, n2 <= m, a1 <=1, b1 <=0 ,a2 <= 0,b2 <=1;
s3:if n1 is even, then "if a1 is even, then s4; else s5"; else s6;
s4:n1 <= n1 /2, a1<=a1/2, b1 <= b1/2, then s3;
s5:if a1<0, then "a1 <= a1 + m, b1 <= b1 -u, then s4";
else "a1 <= a1 - m, b1 <= b1 +u,then s4";
s6:compare n1 and n2,if n1 and n2,then swap n1 and n2,then s7; else if n1 == n2 ,then flag_zero <= 1, then s7;
s7:if n2 != 1 and flag_zero == 1, then no inv,over;
else if n2 == 1, then s9;
else s8;
s8:n1 <= n1 - n2,a1 <= a1 - a2, b1 <= b1 - b2, then s3;
s9:if f == 1, then a2 <= b2, then s10;
s10: if a2 <0, then "if f ==1, return a2+u ; else return a2+m"
else return a2;

posted on 2012-06-26 08:35  好记性不如烂笔头  阅读(540)  评论(0编辑  收藏  举报

导航