项目作者: adjoint-io

项目描述 :
A polymorphic interface for elliptic curve operations
高级语言: Haskell
项目地址: git://github.com/adjoint-io/elliptic-curve.git
创建时间: 2019-05-29T14:24:32Z
项目社区:https://github.com/adjoint-io/elliptic-curve

开源协议:MIT License

下载




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.

  1. class Curve (f :: Form) (c :: Coordinates) e q r
  2. | | |
  3. Curve Type o-+ | |
  4. Field of Points o---+ |
  5. Field of Coefficients o-----+
  6. data Point f c e q r :: *

For example:

```haskell ignore
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

  1. **Arithmetic**
  2. ```haskell ignore
  3. -- Point addition
  4. add :: Point f c e q r -> Point f c e q r -> Point f c e q r
  5. -- Point doubling
  6. dbl :: Point f c e q r -> Point f c e q r
  7. -- Point multiplication by field element
  8. mul :: Curve f c e q r => Point f c e q r -> r -> Point f c e q r
  9. -- Point multiplication by Integral
  10. mul' :: (Curve f c e q r, Integral n) => Point f c e q r -> n -> Point f c e q r
  11. -- Point identity
  12. id :: Point f c e q r
  13. -- Point inversion
  14. inv :: Point f c e q r -> Point f c e q r
  15. -- Frobenius endomorphism
  16. frob :: Point f c e q r -> Point f c e q r
  17. -- Random point
  18. rnd :: MonadRandom m => m (Point f c e q r)

Other Functions

```haskell ignore
— 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

  1. ### Point Arithmetic
  2. See [**Example.hs**](examples/Example.hs).
  3. ### Elliptic Curve Diffie-Hellman (ECDH)
  4. See [**DiffieHellman.hs**](examples/DiffieHellman.hs).
  5. ### Representing a new curve using the curve class
  6. See [**Weierstrass**](src/Data/Curve/Weierstrass.hs).
  7. ### Implementing a new curve using a curve representation
  8. See [**Anomalous**](src/Data/Curve/Weierstrass/Anomalous.hs).
  9. ### Using an implemented curve
  10. Import a curve implementation.
  11. ```haskell
  12. 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:

  1. import Protolude
  2. import GHC.Natural
  3. import qualified Data.Field.Galois as F
  4. main :: IO ()
  5. main = do
  6. putText $ "Hasse Theorem succeeds: " <> show (hasseTheorem Anomalous._h Anomalous._r (F.order (witness :: Anomalous.Fq)))
  7. where
  8. 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

  1. Copyright (c) 2019-2020 Adjoint Inc.
  2. Permission is hereby granted, free of charge, to any person obtaining a copy
  3. of this software and associated documentation files (the "Software"), to deal
  4. in the Software without restriction, including without limitation the rights
  5. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all
  9. copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  14. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  16. OR OTHER DEALINGS IN THE SOFTWARE.