1
0
Fork 0
This commit is contained in:
Mike 2024-01-31 19:36:43 +02:00
parent 433563fe7f
commit bbdcaeb7a1

View file

@ -1,15 +1,59 @@
#{
def expmod(b,e,m):
if e == 0: return 1
t = expmod(b,e/2,m)**2 % m
if e & 1: t = (t*b) % m
return t
r = expmod(b,e/2,m)**2 % m
if e & 1: r = (r*b) % m
return r
B 121666
E 57896044618658097711785492504343953926634992332820282019728792003956564819947
M 57896044618658097711785492504343953926634992332820282019728792003956564819949
t 37095705934669439343138083508754565189542113879843219016388785533085940283556
R 37095705934669439343138083508754565189542113879843219016388785533085940283556
}#
(de steps (E)
(flip
(make
(while
(and
(link (swap 'E (/ E 2)))
(gt0 E) ) ) ) ) )
(de expmod-OLD (B E M)
(let R 1
(for I (steps E)
(and
(setq R (modulo (* R R) M))
(bit? 1 I)
(setq R (modulo (* R B) M)) ) )
R ) )
(de expmod-NEW (B E M)
(if (=0 E)
1
(let R (% (** (expmod-NEW B (/ E 2) M) 2) M)
(when (bit? 1 E) (setq R (% (* R B) M)))
R ) ) )
(println 'expmod-NEW)
(bench
(do 100000
(test
37095705934669439343138083508754565189542113879843219016388785533085940283556
(expmod-NEW
121666
57896044618658097711785492504343953926634992332820282019728792003956564819947
57896044618658097711785492504343953926634992332820282019728792003956564819949 ) ) ) )
(println 'expmod-OLD)
(bench
(do 100000
(test
37095705934669439343138083508754565189542113879843219016388785533085940283556
(expmod-NEW
121666
57896044618658097711785492504343953926634992332820282019728792003956564819947
57896044618658097711785492504343953926634992332820282019728792003956564819949 ) ) ) )
(msg 'ok)
(bye)