summaryrefslogtreecommitdiff
path: root/main.rkt
blob: 845bd189f13fbe3c35c2e05025fcfbd938a6ceb8 (plain)
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
#lang racket

(require "modulo-arithmetic.rkt")

;; 4a^3 + 27b^2 != 0
(define a 2)
(define b 3)

(define (point-in-curve lst)
  (define x (car lst))
  (define y (cadr lst))
  (= (* y y) (+ (* x x x) (* x a) b)))

(define (add p q)
  (cond ((not (point-in-curve p))
         q)
        ((not (point-in-curve q))
         p)
        (else
         (define xp (first p))
         (define xq (first q))
         (define yp (second p))
         (define yq (second q))
         (let* ((m (if (equal? p q)
                       (/ (+ (* 3 xp xp) a)
                          (* 2 yp))
                       (/ (- yp yq)
                          (- xp xq))))
                (xr (- (* m m) xp xq))
                (yr (+ yp (* m (- xr xp)))))
           (list xr (- yr))))))

(define (mul n p)
  (cond ((= n 0)
         (list 0 0))
        ((= n 1)
         p)
        (else
         (add p (mul (sub1 n) p)))))

(define curve-points (filter point-in-curve
                             (cartesian-product (range cardinality)
                                                (range cardinality))))