Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
import Data.Monoid
import Control.Monad (guard)
import Test.QuickCheck (quickCheck)

View file

@ -0,0 +1,15 @@
import Data.Monoid
data Elliptic = Elliptic Double Double | Zero
deriving Show
instance Eq Elliptic where
p == q = dist p q < 1e-14
where
dist Zero Zero = 0
dist Zero p = 1/0
dist p Zero = 1/0
dist (Elliptic x1 y1) (Elliptic x2 y2) = (x2-x1)^2 + (y2-y1)^2
inv Zero = Zero
inv (Elliptic x y) = Elliptic x (-y)

View file

@ -0,0 +1,13 @@
instance Monoid Elliptic where
mempty = Zero
mappend Zero p = p
mappend p Zero = p
mappend p@(Elliptic x1 y1) q@(Elliptic x2 y2)
| p == inv q = Zero
| p == q = mkElliptic $ 3*x1^2/(2*y1)
| otherwise = mkElliptic $ (y2 - y1)/(x2 - x1)
where
mkElliptic l = let x = l^2 - x1 - x2
y = l*(x1 - x) - y1
in Elliptic x y

View file

@ -0,0 +1,2 @@
ellipticX b y = Elliptic (qroot (y^2 - b)) y
where qroot x = signum x * abs x ** (1/3)

View file

@ -0,0 +1,2 @@
mult :: Int -> Elliptic -> Elliptic
mult n = mconcat . replicate n

View file

@ -0,0 +1,7 @@
n `mult` p
| n == 0 = Zero
| n == 1 = p
| n == 2 = p <> p
| n < 0 = inv ((-n) `mult` p)
| even n = 2 `mult` ((n `div` 2) `mult` p)
| odd n = p <> (n -1) `mult` p

View file

@ -0,0 +1,19 @@
-- for given a, b and x returns a point on the positive branch of elliptic curve (if point exists)
elliptic a b Nothing = Just Zero
elliptic a b (Just x) =
do let y2 = x**3 + a*x + b
guard (y2 > 0)
return $ Elliptic x (sqrt y2)
addition a b x1 x2 =
let p = elliptic a b
s = p x1 <> p x2
in (s /= Nothing) ==> (s <> (inv <$> s) == Just Zero)
associativity a b x1 x2 x3 =
let p = elliptic a b
in (p x1 <> p x2) <> p x3 == p x1 <> (p x2 <> p x3)
commutativity a b x1 x2 =
let p = elliptic a b
in p x1 <> p x2 == p x2 <> p x1