Swiftpack.co - Package - leif-ibsen/AEC

Description

The AEC package implements Arithmetic of Elliptic Curves in Swift. Its intended usage is in elliptic curve cryptography (like ECIES) and elliptic curve signatures (like ECDSA).

AEC provides the following functionality:

  • Ten predefined NIST characteristic 2 curves
  • Eight predefined NIST prime characteristic curves
  • Seven predefined Brainpool curves
  • Seven predefined Brainpool twisted curves
  • Characteristic 2 or prime characteristic curves that you define yourself
  • Curve point arithmetic: double, add, subtract, negate, multiply
  • ASN1 encoding of curve domains and points

Example 1

Example 1 shows how to generate a private/public elliptic curve keypair.

import AEC
import BigInt

// Create the domain - for example NIST sect233k1
let domain = Domain.instance(.EC233k1)

// The private key is a random number less than the curve order
let privateKey: BInt = domain.order.randomLessThan()

// The public key is the curve generator point multiplied by the private key
let publicKey = domain.multiply(domain.g, privateKey)

print("private key =", privateKey)
print("public key =", publicKey)

giving (for example):
private key = 1254640759319210091999152754890569067241767785912691726576652194048512
public key = Point(8151580714977954750664452597174335212433953705122126408819729482061262,
9555310969367793743982149466248073263570763240467442493511865575877894)

Example 2

Example 2 shows how to make your own prime characteristic domain. This is example 3.5 from 'Guide to Elliptic Curve Cryptography'.

import AEC
import BigInt

// Create the domain
let domain = Domain.instance("EC29", BInt(29), BInt(4), BInt(20), BInt(1), BInt(5), BInt(37), 1)

let p1 = Point(BInt(5), BInt(22))
let p2 = Point(BInt(16), BInt(27))

print("p1 + p2 =", domain.add(p1, p2))
print("p1 * 2  =", domain.multiply(p1, BInt(2)))

giving
p1 + p2 = Point(13, 6)
p1 * 2 = Point(14, 6)

Example 3

Example 3 shows how to make your own characteristic 2 domain. This is example 3.6 from 'Guide to Elliptic Curve Cryptography'.

import AEC
import BigInt

// Reduction polynomial for x^4 + x + 1    
let rp = RP(4, 1)
// Create the domain
let domain = Domain.instance("EC4", rp, BInt(8), BInt(9), BInt(1), BInt(1), BInt(22), 2)

let p1 = Point(BInt(5), BInt(22))
let p2 = Point(BInt(16), BInt(27))

print("p1 + p2 =", domain.add(p1, p2))
print("p1 * 2  =", domain.multiply(p1, BInt(2)))

giving
p1 + p2 = Point(1, 1)
p1 * 2 = Point(11, 2)

Dependencies

AEC requires Swift 5.0.

The AEC package depends on the ASN1 and BigInt packages

dependencies: [
    .package(url: "https://github.com/leif-ibsen/ASN1", from: "1.0.0"),
    .package(url: "https://github.com/leif-ibsen/BigInt", from: "1.0.0"),
],

References

Algorithms from the following books have been used in the implementation. There are references in the source code where appropriate.

  • Crandall and Pomerance: Prime Numbers - A Computational Perspective. Second Edition, Springer 2005
  • Hankerson, Menezes, Vanstone: Guide to Elliptic Curve Cryptography. Springer 2004
  • Standards for Efficient Cryptography 1 (SEC 1), Certicom Corp. 2009
  • Standards for Efficient Cryptography 2 (SEC 2), Certicom Corp. 2010

Github

link
Stars: 0
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

1.0.0 - Oct 31, 2019