Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
leif-ibsen committed Jun 23, 2020
1 parent 4f64406 commit 8a50f1d
Show file tree
Hide file tree
Showing 43 changed files with 82 additions and 2,074 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/leif-ibsen/ASN1", from: "1.2.0"),
.package(url: "https://github.com/leif-ibsen/BigInt", from: "1.1.0"),
.package(url: "https://github.com/leif-ibsen/ASN1", from: "1.2.1"),
.package(url: "https://github.com/leif-ibsen/BigInt", from: "1.1.2"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This encompasses:
In your project Package.swift file add a dependency like<br/>

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

<h2><b>Basics</b></h2>
Expand Down Expand Up @@ -337,8 +337,8 @@ SwiftECC requires Swift 5.0.
The SwiftECC package depends on the ASN1 and BigInt packages

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

<h2><b>References</b></h2>
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftECC/Domain2/Domain2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Domain2 {
}
var q = pt
let npt = negate(pt)
let m = n << 1 + n
let m = n * 3
for i in (1 ... m.bitWidth - 2).reversed() {
q = double(q)
let mi = m.testBit(i)
Expand Down
23 changes: 18 additions & 5 deletions Sources/SwiftECC/DomainP/DomainP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class DomainP {
let order: BInt
let cofactor: Int

// Stuff related to Barrett reduction modulo p

let u: BInt
let shifts: Int

// Stuff related to Montgomery inversion

let Rsize: Int
Expand All @@ -44,6 +49,8 @@ class DomainP {
self.g = Point(gx, gy)
self.order = order
self.cofactor = cofactor
self.shifts = self.p.magnitude.count * 128
self.u = (BInt.ONE << self.shifts) / self.p
self.modulus = Vector(self.p)
self.Rsize = self.modulus.count
self.Rsize64 = self.Rsize * 64
Expand Down Expand Up @@ -132,7 +139,7 @@ class DomainP {
}
var q = pt
let npt = negate(pt)
let m = n << 1 + n
let m = n * 3
for i in (1 ... m.bitWidth - 2).reversed() {
q = double(q)
let mi = m.testBit(i)
Expand Down Expand Up @@ -208,8 +215,14 @@ class DomainP {
return self.oid!
}

// Barrett reduction
func reduceModP(_ x: BInt) -> BInt {
return x.mod(self.p)
let x1 = x.isNegative ? -x : x
var t = x1 - ((x1 * self.u) >> self.shifts) * self.p
if t >= self.p {
t -= self.p
}
return t.isZero ? BInt.ZERO : (x.isNegative ? self.p - t : t)
}

func addModP(_ x: BInt, _ y: BInt) -> BInt {
Expand All @@ -227,12 +240,12 @@ class DomainP {
}

func mul2ModP(_ x: BInt) -> BInt {
let z = x << 1
let z = x * 2
return z >= self.p ? z - self.p : z
}

func mul3ModP(_ x: BInt) -> BInt {
var z = (x << 1) + x
var z = x * 3
while z >= self.p {
z -= self.p
}
Expand Down Expand Up @@ -473,7 +486,7 @@ struct Vector {
self.v.append(1)
}
}

mutating func subtract(_ x: inout Vector) {
var borrow = false
for i in 0 ..< x.count {
Expand Down
25 changes: 0 additions & 25 deletions Sources/SwiftECC/DomainP/EC521.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,4 @@ class EC521r1: DomainP {
super.init(EC521r1.name, EC521r1.p, EC521r1.a, EC521r1.b, EC521r1.gx, EC521r1.gy, EC521r1.order, EC521r1.cofactor, EC521r1.oid)
}

// Guide to Elliptic Curve Cryptography - algorithm 2.31
// Efficient modP implementation
override func reduceModP(_ x: BInt) -> BInt {
precondition(x.abs() < EC521r1.p ** 2)
var l = Limbs(repeating: 0, count: 9)
let n = Swift.min(x.magnitude.count, l.count)
for i in 0 ..< n {
l[i] = x.magnitude[i]
}
l[8] &= 0x1ff
if x.isNegative {
var s = BInt(l) - (x >> 521)
while s > EC521r1.p {
s -= EC521r1.p
}
return EC521r1.p - s
} else {
var s = BInt(l) + (x >> 521)
while s >= EC521r1.p {
s -= EC521r1.p
}
return s
}
}

}
20 changes: 20 additions & 0 deletions Tests/SwiftECCTests/DomainTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ class DomainTest: XCTestCase {
XCTAssertEqual(domain.multiplyG(domain.order), Point.INFINITY)
}

func reduceModPTest(_ domain: Domain) {
guard let d = domain.domainP else {
return
}
XCTAssertEqual(d.reduceModP(BInt.ZERO), BInt.ZERO)
XCTAssertEqual(d.reduceModP(BInt.ONE), BInt.ONE)
XCTAssertEqual(d.reduceModP(-BInt.ONE), (-BInt.ONE).mod(domain.p))
XCTAssertEqual(d.reduceModP(domain.order), domain.order.mod(domain.p))
XCTAssertEqual(d.reduceModP(-domain.order), (-domain.order).mod(domain.p))
XCTAssertEqual(d.reduceModP(domain.order ** 2), (domain.order ** 2).mod(domain.p))
XCTAssertEqual(d.reduceModP(-(domain.order ** 2)), (-(domain.order ** 2)).mod(domain.p))
XCTAssertEqual(d.reduceModP((domain.p - 1) ** 2), ((domain.p - 1) ** 2).mod(domain.p))
XCTAssertEqual(d.reduceModP(-((domain.p - 1) ** 2)), (-((domain.p - 1) ** 2)).mod(domain.p))
XCTAssertEqual(d.reduceModP(domain.p), BInt.ZERO)
XCTAssertEqual(d.reduceModP(-domain.p), BInt.ZERO)
XCTAssertEqual(d.reduceModP(domain.p + 1), BInt.ONE)
XCTAssertEqual(d.reduceModP(-domain.p + 1), BInt.ONE)
}

func doTest(_ c: ECCurve) {
let domain = Domain.instance(curve: c)
domainTest(domain, domain.multiply(domain.g, BInt(0)))
Expand All @@ -49,6 +68,7 @@ class DomainTest: XCTestCase {
multiplyGTest(domain, BInt(1))
multiplyGTest(domain, BInt(2))
multiplyGTest(domain, BInt(bitWidth: domain.g.x.bitWidth / 2))
reduceModPTest(domain)
}

func test() {
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/Domain.html
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ <h4>Return Value</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/ECPrivateKey.html
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ <h4>Return Value</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/ECPublicKey.html
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ <h4>Return Value</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Classes/ECSignature.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums.html
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums/AESCipher.html
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums/BlockMode.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
2 changes: 1 addition & 1 deletion docs/Enums/ECCurve.html
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-19)</p>
<p>&copy; 2020 <a class="link" href="" target="_blank" rel="external"></a>. All rights reserved. (Last updated: 2020-02-24)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.11.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
Loading

0 comments on commit 8a50f1d

Please sign in to comment.