-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollard-rho-graph.lisp
55 lines (45 loc) · 2.06 KB
/
pollard-rho-graph.lisp
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
(defun sethash (key value table)
(setf (gethash key table) value))
;; using member and list is dumb, rewrite with hashes
(defun pollard-walk (polynomial modulus &key (initial (random modulus)))
(let ((observed (make-hash-table))
(count 0))
(while (not (gethash (setf initial (mod (polyeval polynomial initial) modulus))
observed))
(incf count)
(sethash initial count observed))
(values
(hash-table-count observed)
observed
initial
(- count (gethash initial observed)))))
(defun pollard-walk-steps (polynomial modulus steps &key (initial (random
modulus)))
(loop for i upto steps
collect (setf initial (mod (polyeval polynomial initial) modulus)))))
(defun random-polynomial (modulus &key (degree (random modulus)))
(loop for i from 1 to degree collect (random modulus)))
(let ((comp (random-composite)))
(pollard-walk (random-polynomial comp :degree 3) comp))
(defun print-dot-for-walk (polynomial modulus &key (initial (random modulus)))
(multiple-value-bind (count table last length)
(pollard-walk polynomial modulus :initial initial)
(declare (ignore length count))
(format t "digraph{~%")
(loop for k being the hash-keys of table
for i = 1 then (mod (1+ i) 2)
do (if (oddp i) (format t "\"~a\"->" k) (format t "\"~a\";~%\"~a\"->" k k)))
(format t "\"~a\"" (mod (polyeval '(1 0 3) last) modulus))
(format t ";~%}~%~%")))
(defun print-dot-for-walk (polynomial modulus &key (initial (random modulus)))
(format t "digraph{~%")
(let ((observed (make-hash-table))
last)
(until (gethash
(progn (setf last initial) (setf initial (mod (polyeval polynomial initial)
modulus))) observed)
(sethash initial initial observed)
(format t "\"~a\"->\"~a\";~%" last initial))
(format t "\"~a\"->\"~a\";~%" last (mod (polyeval polynomial last) modulus))
(format t "}~%")))
(log (fermat 7) 10)