Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Vlasov committed Aug 7, 2018
0 parents commit c9e4ae5
Show file tree
Hide file tree
Showing 139 changed files with 11,703 additions and 0 deletions.
26 changes: 26 additions & 0 deletions EllipticSwift.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Pod::Spec.new do |s|
s.name = "EllipticSwift"
s.version = "1.0"
s.summary = "Elliptic curve arithmetics in vanilla Swift for iOS ans macOS"

s.description = <<-DESC
Elliptic curve arithmetics and modular multiprecision arithmetics in vanilla Swift. Uses Apple's Accelerate framework for with numeric types for now.
DESC

s.homepage = "https://github.com/shamatar/EllipticSwift"
s.license = 'Apache License 2.0'
s.author = { "Alex Vlasov" => "[email protected]" }
s.source = { :git => 'https://github.com/shamatar/EllipticSwift.git', :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/shamatar'

s.swift_version = '4.1'
s.module_name = 'EllipticSwift'
s.ios.deployment_target = "9.0"
s.osx.deployment_target = "10.11"
s.source_files = "EllipticSwift/**/*.{swift}, EllipticSwift/FixedWidthTypes/**/*.{swift}",
s.public_header_files = "EllipticSwift/**/*.{h}"
#s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }

s.frameworks = 'Accelerate'
s.dependency 'BigInt', '~> 3.1'
end
1,032 changes: 1,032 additions & 0 deletions EllipticSwift.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
10 changes: 10 additions & 0 deletions EllipticSwift.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
31 changes: 31 additions & 0 deletions EllipticSwift/Curve/AffineCoordinates.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// AffineCoordinates.swift
// EllipticSwift
//
// Created by Alexander Vlasov on 10.07.2018.
// Copyright © 2018 Alexander Vlasov. All rights reserved.
//

import Foundation
import BigInt

public struct AffineCoordinates: CustomStringConvertible {
public var description: String {
if self.isInfinity {
return "Point of O"
} else {
return "Point " + "(0x" + String(self.X, radix: 16) + ", 0x" + String(self.Y, radix: 16) + ")"
}
}

public var isInfinity: Bool = false
public var X: BigUInt
public var Y: BigUInt
public init(_ x: BigUInt, _ y: BigUInt) {
self.X = x
self.Y = y
}
internal mutating func setInfinity() {
self.isInfinity = true
}
}
75 changes: 75 additions & 0 deletions EllipticSwift/Curve/CurveProtocols.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// CurveProtocol.swift
// EllipticSwift
//
// Created by Alexander Vlasov on 02.08.2018.
// Copyright © 2018 Alexander Vlasov. All rights reserved.
//

import Foundation
import BigInt

public protocol CurveProtocol {
associatedtype Field
associatedtype FieldElement: PrimeFieldElementProtocol where FieldElement.Field == Field
// associatedtype ScalarElement: FieldBound

associatedtype AffineType: AffinePointProtocol
associatedtype ProjectiveType: ProjectivePointProtocol

var field: Field {get}
var order: Field.UnderlyingRawType {get}
var curveOrderField: Field {get}
// var generator: AffineType? {get}

func checkOnCurve(_ p: AffineType) -> Bool
func add(_ p: ProjectiveType, _ q: ProjectiveType) -> ProjectiveType
func sub(_ p: ProjectiveType, _ q: ProjectiveType) -> ProjectiveType
func mixedAdd(_ p: ProjectiveType, _ q: AffineType) -> ProjectiveType
// func mul(_ scalar: BigNumber, _ p: AffineType) -> ProjectiveType
// func mul(_ scalar: BigUInt, _ p: AffineType) -> ProjectiveType
// func mul<U>(_ scalar: GeneralizedPrimeFieldElement<U>, _ p: AffineType) -> ProjectiveType
// func mul(_ scalar: BytesRepresentable, _ p: AffineType) -> ProjectiveType
func mul(_ scalar: Field.UnderlyingRawType, _ p: AffineType) -> ProjectiveType
func neg(_ p: ProjectiveType) -> ProjectiveType
func hashInto(_ data: Data) -> AffineType
func testGenerator(_ p: AffineCoordinates) -> Bool
}

public protocol AffinePointProtocol {
associatedtype Curve: CurveProtocol
associatedtype ProjectiveType: ProjectivePointProtocol where ProjectiveType.Curve == Curve
var curve: Curve {get}
var isInfinity: Bool {get}
var rawX: Curve.FieldElement {get}
var rawY: Curve.FieldElement {get}
var X: Curve.Field.UnderlyingRawType {get}
var Y: Curve.Field.UnderlyingRawType {get}

var coordinates: AffineCoordinates {get}

func isEqualTo(_ other: Self) -> Bool

init(_ rawX: Curve.FieldElement, _ rawY: Curve.FieldElement, _ curve: Curve)

func toProjective() -> ProjectiveType
}

public protocol ProjectivePointProtocol {
associatedtype Curve
associatedtype AffineType: AffinePointProtocol where AffineType.Curve == Curve
var curve: Curve {get}

var isInfinity: Bool {get}
var rawX: Curve.FieldElement {get}
var rawY: Curve.FieldElement {get}
var rawZ: Curve.FieldElement {get}

static func infinityPoint(_ curve: Curve) -> Self

func isEqualTo(_ other: Self) -> Bool

init(_ rawX: Curve.FieldElement, _ rawY: Curve.FieldElement, _ rawZ: Curve.FieldElement, _ curve: Curve)

func toAffine() -> AffineType
}
149 changes: 149 additions & 0 deletions EllipticSwift/Curve/GeneralizedPoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//
// GeneralizedPoint.swift
// EllipticSwift
//
// Created by Alexander Vlasov on 03.08.2018.
// Copyright © 2018 Alexander Vlasov. All rights reserved.
//

import Foundation
import BigInt

public struct GeneralizedAffinePoint<T>: AffinePointProtocol where T: CurveProtocol {
public typealias ProjectiveType = GeneralizedProjectivePoint<T>
public typealias Curve = T
public typealias FE = T.FieldElement
public typealias UnderlyingRawType = T.Field.UnderlyingRawType

public var description: String {
return self.coordinates.description
}

public var curve: Curve
public var isInfinity: Bool = true
public var rawX: FE
public var rawY: FE
public var X: UnderlyingRawType {
return self.rawX.nativeValue
}
public var Y: UnderlyingRawType {
return self.rawY.nativeValue
}

public var coordinates: AffineCoordinates {
if !self.isInfinity {
return AffineCoordinates(BigUInt(self.X.bytes), BigUInt(self.Y.bytes))
} else {
var p = AffineCoordinates(0, 0)
p.setInfinity()
return p
}
}

public init(_ rawX: FE, _ rawY: FE, _ curve: Curve) {
self.rawX = rawX
self.rawY = rawY
self.curve = curve
self.isInfinity = false
}

public func toProjective() -> ProjectiveType {
if self.isInfinity {
return ProjectiveType.infinityPoint(self.curve)
}
let field = self.curve.field
let one = FE.identityElement(field)
let p = ProjectiveType(self.rawX, self.rawY, one, curve)
return p
}

public func isEqualTo(_ other: GeneralizedAffinePoint<T>) -> Bool {
return self.rawX == other.rawX && self.rawY == other.rawY
}

public static func == (lhs: GeneralizedAffinePoint<T>, rhs: GeneralizedAffinePoint<T>) -> Bool {
return lhs.isEqualTo(rhs)
}

// public static func *<U> (lhs: U, rhs: GeneralizedAffinePoint<T>) -> ProjectiveType where U: FiniteFieldCompatible {
// return rhs.curve.mul(lhs, rhs)
// }
//
// public static func + (lhs: T.AffineType, rhs: T.AffineType) -> ProjectiveType {
// return lhs.curve.mixedAdd(lhs.toProjective(), rhs)
// }
}

public struct GeneralizedProjectivePoint<T>: ProjectivePointProtocol where T: CurveProtocol {
// also refered as Jacobian Point
public typealias AffineType = GeneralizedAffinePoint<T>
public typealias Curve = T
public typealias FE = T.FieldElement
public typealias UnderlyingRawType = T.Field.UnderlyingRawType

public var curve: Curve

public var isInfinity: Bool {
return self.rawZ.isZero
}
public var rawX: FE
public var rawY: FE
public var rawZ: FE

public static func infinityPoint<U>(_ curve: U) -> GeneralizedProjectivePoint<U> where U: CurveProtocol {
let field = curve.field
let zero = U.FieldElement.zeroElement(field)
let one = U.FieldElement.identityElement(field)
return GeneralizedProjectivePoint<U>(zero, one, zero, curve)
}

public func isEqualTo(_ other: GeneralizedProjectivePoint<T>) -> Bool {
return self.toAffine().isEqualTo(other.toAffine())
}

public init(_ rawX: FE, _ rawY: FE, _ rawZ: FE, _ curve: Curve) {
self.rawX = rawX
self.rawY = rawY
self.rawZ = rawZ
self.curve = curve
}

public func toAffine() -> AffineType {
if self.isInfinity {
let field = curve.field
let zero = FE.zeroElement(field)
var p = AffineType(zero, zero, self.curve)
p.isInfinity = true
return p
}
let zInv = self.rawZ.inv()
let zInv2 = zInv * zInv
let zInv3 = zInv2 * zInv
let affineX = self.rawX * zInv2
let affineY = self.rawY * zInv3
return AffineType(affineX, affineY, self.curve)
}

public static func == (lhs: GeneralizedProjectivePoint<T>, rhs: GeneralizedProjectivePoint<T>) -> Bool {
return lhs.isEqualTo(rhs)
}

// public static func + (lhs: GeneralizedProjectivePoint<T>, rhs: GeneralizedProjectivePoint<T>) -> GeneralizedProjectivePoint<T> {
// return lhs.curve.add(lhs, rhs)
// }
//
// public static func - (lhs: GeneralizedProjectivePoint<T>, rhs: GeneralizedProjectivePoint<T>) -> GeneralizedProjectivePoint<T> {
// return lhs.curve.sub(lhs, rhs)
// }
//
// public static func * (lhs: FiniteFieldCompatible, rhs: GeneralizedProjectivePoint<T>) -> GeneralizedProjectivePoint<T> {
// if rhs.isInfinity {
// return rhs
// }
// return rhs.curve.mul(lhs, rhs.toAffine())
// }
//
// public static func + (lhs: GeneralizedProjectivePoint<T>, rhs: GeneralizedAffinePoint<T>) -> GeneralizedProjectivePoint<T> {
// return lhs.curve.mixedAdd(lhs, rhs)
// }
}
Loading

0 comments on commit c9e4ae5

Please sign in to comment.