Elliptic curve arithmetic: Difference between revisions

m
(→‎{{header|Haskell}}: Added Haskell solution)
Line 431:
 
=={{header|Haskell}}==
First, some sefuluseful imports:
<lang haskell>import Data.Monoid
import Control.Monad (guard)
import Test.QuickCheck (quickCheck)</lang>
 
The datatype for a point on an elliptic curve with exact zero:
 
<lang haskell>import Data.Monoid
Line 511:
We use QuickCheck to test general properties of points on arbitrary elliptic curve.
 
<lang haskell>-- for given a, b and x returns a point on the positive branch of elliptic curve (it theif point exists)
ellipticYelliptic a b Nothing = Just Zero
ellipticYelliptic 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 = ellipticYelliptic a b
in (p x1 <> p x2) <> p x3 == p x1 <> (p x2 <> p x3)
 
commutativity a b x1 x2 =
let p = ellipticYelliptic a b
in p x1 <> p x2 == p x2 <> p x1</lang>
 
<pre>λ> quickCheck associativityaddition
+++ OK, passed 100 tests.
λ> quickCheck associativity
+++ OK, passed 100 tests.
λ> quickCheck commutativity
Anonymous user