Giter VIP home page Giter VIP logo

elliptic-curve's Introduction

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.

elliptic-curve's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elliptic-curve's Issues

Upload 0.3.1 to Hackage

Hello, the latest version of this package is 0.3.1, but it's not available on Hackage which makes it harder to use this library. Could you please update the version on Hackage?

Test curves against well-known outputs

Hi, I came across this nice library, took a look at the tests, and I wondered: Wouldn't it be a good idea to test each curve against some well-known outputs in a “golden test” kind of way? Mostly to ensure that the curves are defined using the correct parameters, and that it stays that way over time. Thanks!

Adding point to itself results in `O`

Hi,

thanks a lot for this library!

I followed RFC6090 for the implementation of affine point addition and found that elliptic-curve returns O in the case of adding a point p to itself (at least for Weierstrass curves). I am aware that I can use dbl for this, but I wondered why this was chosen that way.

Best regards
Matthias

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.