This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

ECurve.Curve

Contains curve definitions, utility functions for retrieving said definitions, and a function for determining if point is on the curve

type CurveType = Secp128r1 | Secp160k1 | Secp160r1 | Secp192k1 | Secp192r1 | Secp256k1 | Secp256r1

Standard definitions of elliptic curves

getCurve : CurveType -> Models.Curve

Gets a curve for a given curve type

new : BigInt -> BigInt -> BigInt -> BigInt -> BigInt -> BigInt -> BigInt -> Curve

Returns a new elliptic curve from points

isOnCurve : Curve -> Point -> Bool

Checks if point is on a curve

module ECurve.Curve exposing (..)

{-| Contains curve definitions,
utility functions for retrieving said definitions,
and a function for determining if point is on the curve

@docs CurveType, getCurve, new, isOnCurve

-}

import ECurve.Point as Point exposing (affineX, affineY)
import BigInt exposing (BigInt, add, mul, mod, fromString, pow)
import ECurve.Util exposing (zero, bitLength, str, signum, square, three)
import ECurve.Models as Models exposing (Point, Curve)

{-| Standard definitions of elliptic curves
-}
type CurveType
    = Secp128r1
    | Secp160k1
    | Secp160r1
    | Secp192k1
    | Secp192r1
    | Secp256k1
    | Secp256r1

{-| Gets a curve for a given curve type
-}
getCurve : CurveType -> Models.Curve
getCurve curve =
    case curve of
        Secp128r1 ->
            new (str "340282366762482138434845932244680310783") (str "340282366762482138434845932244680310780") (str "308990863222245658030922601041482374867") (str "340282366762482138443322565580356624661") (str "1") (str "29408993404948928992877151431649155974") (str "275621562871047521857442314737465260675")

        Secp160k1 ->
            new (str "1461501637330902918203684832716283019651637554291") (str "0") (str "7") (str "1461501637330902918203686915170869725397159163571") (str "1") (str "338530205676502674729549372677647997389429898939") (str "842365456698940303598009444920994870805149798382")

        Secp160r1 ->
            new (str "1461501637330902918203684832716283019653785059327") (str "1461501637330902918203684832716283019653785059324") (str "163235791306168110546604919403271579530548345413") (str "1461501637330902918203687197606826779884643492439") (str "1") (str "425826231723888350446541592701409065913635568770") (str "203520114162904107873991457957346892027982641970")

        Secp192k1 ->
            new (str "6277101735386680763835789423207666416102355444459739541047") (str "0") (str "3") (str "6277101735386680763835789423061264271957123915200845512077") (str "1") (str "5377521262291226325198505011805525673063229037935769709693") (str "3805108391982600717572440947423858335415441070543209377693")

        Secp192r1 ->
            new (str "6277101735386680763835789423207666416083908700390324961279") (str "6277101735386680763835789423207666416083908700390324961276") (str "2455155546008943817740293915197451784769108058161191238065") (str "6277101735386680763835789423176059013767194773182842284081") (str "1") (str "602046282375688656758213480587526111916698976636884684818") (str "174050332293622031404857552280219410364023488927386650641")

        Secp256k1 ->
            new (str "115792089237316195423570985008687907853269984665640564039457584007908834671663") (str "0") (str "7") (str "115792089237316195423570985008687907852837564279074904382605163141518161494337") (str "1") (str "55066263022277343669578718895168534326250603453777594175500187360389116729240") (str "32670510020758816978083085130507043184471273380659243275938904335757337482424")

        Secp256r1 ->
            new (str "115792089210356248762697446949407573530086143415290314195533631308867097853951") (str "115792089210356248762697446949407573530086143415290314195533631308867097853948") (str "41058363725152142129326129780047268409114441015993725554835256314039467401291") (str "115792089210356248762697446949407573529996955224135760342422259061068512044369") (str "1") (str "48439561293906451759052585252797914202762949526041747995844080717082404635286") (str "36134250956749795798585127919587881956611106672985015071877198253568414405109")

{-| Returns a new elliptic curve from points
-}
new : BigInt -> BigInt -> BigInt -> BigInt -> BigInt -> BigInt -> BigInt -> Curve
new p a b n h gx gy =
    let
        c =
            { p = p
            , a = a
            , b = b
            , g = Point.new p a zero zero zero
            , n = n
            , h = h
            , pLength = 0
            }

        g =
            Point.fromAffine p a gx gy

        pLength =
            ((bitLength p) + 7) // 8

        cc =
            { c | g = g, pLength = pLength }
    in
        cc

{-| Checks if point is on a curve
-}
isOnCurve : Curve -> Point -> Bool
isOnCurve curve q =
    let
        x =
            affineX q

        y =
            affineY q

        a =
            curve.a

        b =
            curve.b

        p =
            curve.p

        result =
            if signum x == LT || BigInt.compare x p == GT || BigInt.compare x p == EQ then
                False
            else if signum y == LT || BigInt.compare y p == GT || BigInt.compare y p == EQ then
                False
            else
                -- check that y^2 = x^3 + ax + b (mod p)
                let
                    lhs =
                        mod (square y) p

                    rhs =
                        mod (add (add (pow x three) (mul a x)) b) p
                in
                    BigInt.compare lhs rhs == EQ
    in
        result