Elliptic curve arithmetic: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Added Haskell solution) |
|||
Line 431: | Line 431: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
First, some |
First, some useful imports: |
||
<lang haskell>import Data.Monoid |
<lang haskell>import Data.Monoid |
||
import Control.Monad (guard) |
import Control.Monad (guard) |
||
import Test.QuickCheck (quickCheck)</lang> |
import Test.QuickCheck (quickCheck)</lang> |
||
The datatype for a point on an elliptic curve |
The datatype for a point on an elliptic curve: |
||
<lang haskell>import Data.Monoid |
<lang haskell>import Data.Monoid |
||
Line 511: | Line 511: | ||
We use QuickCheck to test general properties of points on arbitrary elliptic curve. |
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 elliptic curve ( |
<lang haskell>-- 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 |
do let y2 = x**3 + a*x + b |
||
guard (y2 > 0) |
guard (y2 > 0) |
||
return $ Elliptic x (sqrt y2) |
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 = |
associativity a b x1 x2 x3 = |
||
let p = |
let p = elliptic a b |
||
in (p x1 <> p x2) <> p x3 == p x1 <> (p x2 <> p x3) |
in (p x1 <> p x2) <> p x3 == p x1 <> (p x2 <> p x3) |
||
commutativity a b x1 x2 = |
commutativity a b x1 x2 = |
||
let p = |
let p = elliptic a b |
||
in p x1 <> p x2 == p x2 <> p x1</lang> |
in p x1 <> p x2 == p x2 <> p x1</lang> |
||
<pre>λ> quickCheck |
<pre>λ> quickCheck addition |
||
+++ OK, passed 100 tests. |
|||
λ> quickCheck associativity |
|||
+++ OK, passed 100 tests. |
+++ OK, passed 100 tests. |
||
λ> quickCheck commutativity |
λ> quickCheck commutativity |