hyugit / EllipticCurve

An elliptic curve library written in Swift 4
MIT License
22 stars 5 forks source link
carthage ecc ecdsa ecdsa-cryptography elliptic-curve-arithmetic elliptic-curves finite-elements finite-fields secp256k1 secp256r1 swift swift-package-manager swift4

EllipticCurve

Build Status Language Platform Carthage Compatible

An elliptic curve library written in Swift 4

Warning: this library started as a learning process. It is not meant for production use. Please use libsecp256k1 instead.

Features

CREATE YOUR OWN ELLIPTIC CURVES! :smiley:

This Library provides the necessary scaffoldings for you to easily create elliptic curves.

The library includes several SEC-2 curves, among which is the secp256k1, the most popular curve at the moment. On top of the curves, a generic ECDSA struct is included for signing and verifying.

Protocol oriented architecture

All the protocols are generic, which means none of them is tied to a certain curve or a specific UInt family member. It is very straight forward to create a specific finite field with a specific prime number as its order, or to create a specific elliptic curve of Double precision or Float80 precision. Please see to the playground for demonstrations.

The top level protocol for creating elliptic curve cryptography is EllipticCurveOverFiniteField. It is constructed from two basic protocols: FiniteFieldInteger and EllipticCurve, the former of which is based on FiniteField. The diagram of protocol inheritance is as follows:


 +--------------------------+                                       
 |                          |                                       
 |        FiniteField       |-----+                                 
 |                          |     |                                 
 +-------------|------------+     |Conformance                      
               |                  |                                 
               |Conformance       |                                 
               |                  |                                 
 +-------------v------------+     |    +---------------------------+
 |                          |     |    |                           |
 |    FiniteFieldInteger    |     |    |        EllipticCurve      |
 |                          |     |    |                           |
 +-------------|------------+     |    +-------------|-------------+
               |                  |                  |              
               |                  |                  |              
 As Coordinates|  +------------------------------+   |Conformance   
               |  |                              |   |              
               +--- EllipticCurveOverFiniteField <---+              
                  |                              |                  
                  +---------------|--------------+                  
                                  |                                 
                                  |As T                             
                                  |                                 
                  +---------------|--------------+                  
                  |                              |                  
                  |           ECDSA<T>           |                  
                  |                              |                  
                  +------------------------------+                                              

FiniteField

FiniteField is the base protocol. It defines several basic properties like Zero, One, Characteristic, and etc. It is not meant to be used directly, but you can use it to create finite fields.

FiniteFieldInteger

FiniteFieldInteger defines the basic scaffolding and provides most of the default implementations for any finite field integer. Example:


let p: UInt8 = 223

struct MyFFInt: FiniteFieldInteger {
    static var Characteristic = p

    var value: UInt8

    init() {
        value = 0
    }
}

let a: MyFFInt = 1
let b: MyFFInt = 500
print(a + b)

This will create a finite field integer of F_223, and then you can use the basic +, -, *, / on it.

EllipticCurve

EllipticCurve is also generic. You can create an elliptic curve on real domain like this:


struct MyECPoint: EllipticCurve {
    static var a: Double = -1
    static var b: Double = 1

    var x: Double
    var y: Double?

    init() {
        x = 0
    }
}

let p: MyECPoint = MyECPoint(x: 1, y: 1)
print(p.description)

EllipticCurveOverFiniteField

This is the top level protocol to use, if you want to create an ECC of your own:


let P: UInt8 = 223

struct FFInt223: FiniteFieldInteger {
    static var Characteristic: UInt8 = P
    var value: UInt8

    init() {
        value = 0
    }
}

struct MyECFF: EllipticCurveOverFiniteField {

    static var Order: UInt8 = 212

    static var a: FFInt223 = 2
    static var b: FFInt223 = 7

    var x: FFInt223
    var y: FFInt223?

    static var Generator: MyECFF = MyECFF(x: 16, y: 11)

    init() {
        x = 0
    }
}

Requirements

Installation

Carthage

Currently, only carthage package installation is tested.

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/hyugit/EllipticCurve.git", from: "0.3.0")
]

Communication

If you have a bug report or a feature request, please open an issue here on GitHub. Any contribution is welcome. :smiley:

License

EllipticCurve is released under MIT license. See LICENSE for details.