Skip to content

A polymorphic interface for elliptic curve operations

License

Notifications You must be signed in to change notification settings

sdiehl/elliptic-curve

Repository files navigation

Adjoint Logo

Stack CI Cabal CI Hackage

Elliptic Curve

An extensible library of elliptic curves used in cryptography research.

Curve representations

An elliptic curve E(K) over a field K is a smooth projective plane algebraic cubic curve with a specified base point O, and the points on E(K) form an algebraic group with identity point O. By the Riemann-Roch theorem, any elliptic curve is isomorphic to a cubic curve of the form

where is the point at infinity, and are K-rational coefficients that satisfy a non-zero discriminant condition. For cryptographic computational purposes, elliptic curves are represented in several different forms.

Weierstrass curves

A (short) Weierstrass curve is an elliptic curve over for some prime , and is of the form

where A and B are K-rational coefficients such that is non-zero. Weierstrass curves are the most common representations of elliptic curves, as any elliptic curve over a field of characteristic greater than 3 is isomorphic to a Weierstrass curve.

Binary curves

A (short Weierstrass) binary curve is an elliptic curve over for some positive , and is of the form

where A and B are K-rational coefficients such that B is non-zero. Binary curves have field elements represented by binary integers for efficient arithmetic, and are special cases of long Weierstrass curves over a field of characteristic 2.

Montgomery curves

A Montgomery curve is an elliptic curve over for some prime , and is of the form

where A and B are K-rational coefficients such that is non-zero. Montgomery curves only use the first affine coordinate for computations, and can utilise the Montgomery ladder for efficient multiplication.

Edwards curves

A (twisted) Edwards curve is an elliptic curve over for some prime , and is of the form

where A and D are K-rational coefficients such that is non-zero. Edwards curves have no point at infinity, and their addition and doubling formulae converge.

Curve usage

This library is open for new curve representations and curve implementations through pull requests. These should ideally be executed by replicating and modifying existing curve files, for ease, quickcheck testing, and formatting consistency, but a short description of the file organisation is provided here for clarity. Note that it also has a dependency on the Galois field library and its required language extensions.

The library exposes four promoted data kinds which are used to define a type-safe interface for working with curves.

Forms

Coordinates

These are then specialised down into type classes for the different forms.

And then by coordinate system.

A curve class is constructed out of four type parameters which are instantiated in the associated data type Point on the Curve typeclass.

class Curve (f :: Form) (c :: Coordinates) e q r
                                           | | |
                              Curve Type o-+ | |
                         Field of Points o---+ |
                   Field of Coefficients o-----+

data Point f c e q r :: *

For example:

data Anomalous

type Fq = Prime Q
type Q = 0xb0000000000000000000000953000000000000000000001f9d7

type Fr = Prime R
type R = 0xb0000000000000000000000953000000000000000000001f9d7

instance Curve 'Weierstrass c Anomalous Fq Fr => WCurve c Anomalous Fq Fr where
-- data instance Point 'Weierstrass c Anomalous Fq Fr

Arithmetic

-- Point addition
add :: Point f c e q r -> Point f c e q r -> Point f c e q r

-- Point doubling
dbl :: Point f c e q r -> Point f c e q r

-- Point multiplication by field element
mul :: Curve f c e q r => Point f c e q r -> r -> Point f c e q r

-- Point multiplication by Integral
mul' :: (Curve f c e q r, Integral n) => Point f c e q r -> n -> Point f c e q r

-- Point identity
id :: Point f c e q r

-- Point inversion
inv :: Point f c e q r -> Point f c e q r

-- Frobenius endomorphism
frob :: Point f c e q r -> Point f c e q r

-- Random point
rnd :: MonadRandom m => m (Point f c e q r)

Other Functions

-- Curve characteristic 
char :: Point f c e q r -> Natural

-- Curve cofactor
cof :: Point f c e q r -> Natural

-- Check if a point is well-defined
def :: Point f c e q r -> Bool

-- Discriminant
disc :: Point f c e q r -> q

-- Curve order
order :: Point f c e q r -> Natural

-- Curve generator point
gen :: Point f c e q r

Point Arithmetic

See Example.hs.

Elliptic Curve Diffie-Hellman (ECDH)

See DiffieHellman.hs.

Representing a new curve using the curve class

See Weierstrass.

Implementing a new curve using a curve representation

See Anomalous.

Using an implemented curve

Import a curve implementation.

import qualified Data.Curve.Weierstrass.Anomalous as Anomalous

The data types and constants can then be accessed readily as Anomalous.PA and Anomalous._g.

We'll test that the Hasse Theorem is successful with an implemented curve as a usage example:

import Protolude
import GHC.Natural
import qualified Data.Field.Galois as F

main :: IO ()
main = do
    putText $ "Hasse Theorem succeeds: " <> show (hasseTheorem Anomalous._h Anomalous._r (F.order (witness :: Anomalous.Fq)))
  where
    hasseTheorem h r q = join (*) (naturalToInteger (h * r) - naturalToInteger q - 1) <= 4 * naturalToInteger q

Curve implementations

The following curves have already been implemented.

Binary curves

Edwards curves

Montgomery curves

Weierstrass curves

Disclaimer

The data structures in this library are meant for use in research-grade projects and not in interactive protocols. The elliptic curve operations in this library are not constant time and thus may be vulernable to timing attacks if used improperly. If you are unsure of the implications of this, do not use this library.

License

Copyright (c) 2019-2020 Adjoint Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.