]> gitweb.factorcode.org Git - factor.git/blob - libs/crypto/montgomery.factor
1151c0e524e815ddefdebca7c335d9f87dd9938c
[factor.git] / libs / crypto / montgomery.factor
1 USING: kernel math errors math-contrib ;
2 IN: crypto
3
4 ! As per http://www.cyphercalc.com/index.htm
5 :  montgomery-image ( a n -- a' )
6     #! a' = a * nextpowerof2(a) mod n
7     >r dup next-power-of-2 * r> mod ;
8
9 ! : montgomery* ( a b -- a*b )
10     ! "todo" throw ;
11
12 : montgomery-r^2 ( n -- a )
13     #! ans = r^2 mod n, where r = nextpowerof2(n)
14     [ next-power-of-2 sq ] keep mod ;
15
16 : montgomery-n0' ( n0 size -- n0' )
17     #! size should be a multiple of 2, n0 is odd and n0 < 2^size
18     #! n0 * n0' = -1 mod 2^w
19     2 swap ^ swap neg mod-inv ;
20