Bladeren bron

GiantUInt: Comparable, ExpressibleByIntegerLiteral

Nathan Fallet 4 jaren geleden
bovenliggende
commit
fd0d167ef1

+ 4 - 4
CryptoSwift.xcodeproj/project.pbxproj

@@ -16,8 +16,8 @@
 		42012783267A6F1C00F82506 /* ISO10126Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42012782267A6F1C00F82506 /* ISO10126Padding.swift */; };
 		42012783267A6F1C00F82506 /* ISO10126Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42012782267A6F1C00F82506 /* ISO10126Padding.swift */; };
 		674A736F1BF5D85B00866C5B /* RabbitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 674A736E1BF5D85B00866C5B /* RabbitTests.swift */; };
 		674A736F1BF5D85B00866C5B /* RabbitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 674A736E1BF5D85B00866C5B /* RabbitTests.swift */; };
 		6A072FF726CAB3F900F4E94F /* RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A072FF626CAB3F900F4E94F /* RSA.swift */; };
 		6A072FF726CAB3F900F4E94F /* RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A072FF626CAB3F900F4E94F /* RSA.swift */; };
+		6A43B45C26CBC928002AD852 /* GiantUIntTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A43B45B26CBC928002AD852 /* GiantUIntTests.swift */; };
 		6ADC6EEF26CBAC0C00AFBF4E /* GiantUInt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ADC6EEE26CBAC0C00AFBF4E /* GiantUInt.swift */; };
 		6ADC6EEF26CBAC0C00AFBF4E /* GiantUInt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ADC6EEE26CBAC0C00AFBF4E /* GiantUInt.swift */; };
-		6ADC6EF226CBAFC600AFBF4E /* GiantUIntTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ADC6EF026CBAF9F00AFBF4E /* GiantUIntTests.swift */; };
 		750509991F6BEF2A00394A1B /* PKCS7.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750509981F6BEF2A00394A1B /* PKCS7.swift */; };
 		750509991F6BEF2A00394A1B /* PKCS7.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750509981F6BEF2A00394A1B /* PKCS7.swift */; };
 		750CC3EB1DC0CACE0096BE6E /* BlowfishTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750CC3EA1DC0CACE0096BE6E /* BlowfishTests.swift */; };
 		750CC3EB1DC0CACE0096BE6E /* BlowfishTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750CC3EA1DC0CACE0096BE6E /* BlowfishTests.swift */; };
 		75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */; };
 		75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */; };
@@ -211,8 +211,8 @@
 		42012782267A6F1C00F82506 /* ISO10126Padding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ISO10126Padding.swift; sourceTree = "<group>"; };
 		42012782267A6F1C00F82506 /* ISO10126Padding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ISO10126Padding.swift; sourceTree = "<group>"; };
 		674A736E1BF5D85B00866C5B /* RabbitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RabbitTests.swift; sourceTree = "<group>"; };
 		674A736E1BF5D85B00866C5B /* RabbitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RabbitTests.swift; sourceTree = "<group>"; };
 		6A072FF626CAB3F900F4E94F /* RSA.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSA.swift; sourceTree = "<group>"; };
 		6A072FF626CAB3F900F4E94F /* RSA.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSA.swift; sourceTree = "<group>"; };
+		6A43B45B26CBC928002AD852 /* GiantUIntTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiantUIntTests.swift; sourceTree = "<group>"; };
 		6ADC6EEE26CBAC0C00AFBF4E /* GiantUInt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiantUInt.swift; sourceTree = "<group>"; };
 		6ADC6EEE26CBAC0C00AFBF4E /* GiantUInt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiantUInt.swift; sourceTree = "<group>"; };
-		6ADC6EF026CBAF9F00AFBF4E /* GiantUIntTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiantUIntTests.swift; sourceTree = "<group>"; };
 		750509981F6BEF2A00394A1B /* PKCS7.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PKCS7.swift; sourceTree = "<group>"; };
 		750509981F6BEF2A00394A1B /* PKCS7.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PKCS7.swift; sourceTree = "<group>"; };
 		750CC3EA1DC0CACE0096BE6E /* BlowfishTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlowfishTests.swift; sourceTree = "<group>"; };
 		750CC3EA1DC0CACE0096BE6E /* BlowfishTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlowfishTests.swift; sourceTree = "<group>"; };
 		75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Poly1305Tests.swift; sourceTree = "<group>"; };
 		75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Poly1305Tests.swift; sourceTree = "<group>"; };
@@ -441,7 +441,7 @@
 				E3FD2D511D6B813C00A9F35F /* Error+Extension.swift */,
 				E3FD2D511D6B813C00A9F35F /* Error+Extension.swift */,
 				754BE46719693E190098E6F3 /* DigestTests.swift */,
 				754BE46719693E190098E6F3 /* DigestTests.swift */,
 				7576F6EB20726319006688F8 /* DigestTestsPerf.swift */,
 				7576F6EB20726319006688F8 /* DigestTestsPerf.swift */,
-				6ADC6EF026CBAF9F00AFBF4E /* GiantUIntTests.swift */,
+				6A43B45B26CBC928002AD852 /* GiantUIntTests.swift */,
 				75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */,
 				75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */,
 				758A94271A65C59200E46135 /* HMACTests.swift */,
 				758A94271A65C59200E46135 /* HMACTests.swift */,
 				0EE73E72204D599C00110E11 /* CMACTests.swift */,
 				0EE73E72204D599C00110E11 /* CMACTests.swift */,
@@ -935,7 +935,7 @@
 				758A94291A65C67400E46135 /* HMACTests.swift in Sources */,
 				758A94291A65C67400E46135 /* HMACTests.swift in Sources */,
 				75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */,
 				75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */,
 				E6200E171FB9B68C00258382 /* HKDFTests.swift in Sources */,
 				E6200E171FB9B68C00258382 /* HKDFTests.swift in Sources */,
-				6ADC6EF226CBAFC600AFBF4E /* GiantUIntTests.swift in Sources */,
+				6A43B45C26CBC928002AD852 /* GiantUIntTests.swift in Sources */,
 				754BE46819693E190098E6F3 /* DigestTests.swift in Sources */,
 				754BE46819693E190098E6F3 /* DigestTests.swift in Sources */,
 				E3FD2D531D6B81CE00A9F35F /* Error+Extension.swift in Sources */,
 				E3FD2D531D6B81CE00A9F35F /* Error+Extension.swift in Sources */,
 				757DA2591A4ED4D7002BA3EF /* ChaCha20Tests.swift in Sources */,
 				757DA2591A4ED4D7002BA3EF /* ChaCha20Tests.swift in Sources */,

+ 29 - 1
Sources/CryptoSwift/GiantUInt.swift

@@ -16,10 +16,14 @@
 precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence }
 precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence }
 infix operator ^^ : PowerPrecedence
 infix operator ^^ : PowerPrecedence
 
 
-struct GiantUInt: Equatable {
+struct GiantUInt: Equatable, Comparable, ExpressibleByIntegerLiteral {
+  
+  // Properties
   
   
   let bytes: Array<UInt8>
   let bytes: Array<UInt8>
   
   
+  // Initialization
+  
   init(_ raw: Array<UInt8>) {
   init(_ raw: Array<UInt8>) {
     var bytes = raw
     var bytes = raw
     
     
@@ -29,6 +33,14 @@ struct GiantUInt: Equatable {
     
     
     self.bytes = bytes
     self.bytes = bytes
   }
   }
+  
+  // ExpressibleByIntegerLiteral
+  
+  typealias IntegerLiteralType = UInt8
+  
+  init(integerLiteral value: UInt8) {
+    self = GiantUInt([value])
+  }
     
     
   // Equatable
   // Equatable
   
   
@@ -36,6 +48,22 @@ struct GiantUInt: Equatable {
     lhs.bytes == rhs.bytes
     lhs.bytes == rhs.bytes
   }
   }
   
   
+  // Comparable
+  
+  static func < (rhs: GiantUInt, lhs: GiantUInt) -> Bool {
+    for i in (0 ..< max(rhs.bytes.count, lhs.bytes.count)).reversed() {
+      let r = rhs.bytes[safe: i] ?? 0
+      let l = lhs.bytes[safe: i] ?? 0
+      if r < l {
+        return true
+      } else if r > l {
+        return false
+      }
+    }
+    
+    return false
+  }
+  
   // Operations
   // Operations
   
   
   static func + (rhs: GiantUInt, lhs: GiantUInt) -> GiantUInt {
   static func + (rhs: GiantUInt, lhs: GiantUInt) -> GiantUInt {

+ 7 - 0
Tests/CryptoSwiftTests/GiantUIntTests.swift

@@ -19,6 +19,12 @@ import XCTest
 
 
 final class GiantUIntTests: XCTestCase {
 final class GiantUIntTests: XCTestCase {
   
   
+  func testComparable() {
+    XCTAssertTrue(GiantUInt([1, 2, 3]) == GiantUInt([1, 2, 3]), "equality check failed")
+    XCTAssertTrue(GiantUInt([1, 2, 3]) > GiantUInt([1, 3, 2]), "greater than check failed")
+    XCTAssertTrue(GiantUInt([1, 3, 2]) < GiantUInt([1, 2, 3]), "lower than check failed")
+  }
+  
   func testAddition() {
   func testAddition() {
     let a = GiantUInt([1]) + GiantUInt([1])
     let a = GiantUInt([1]) + GiantUInt([1])
     XCTAssertEqual(a, GiantUInt([2]), "simple addition failed")
     XCTAssertEqual(a, GiantUInt([2]), "simple addition failed")
@@ -51,6 +57,7 @@ final class GiantUIntTests: XCTestCase {
 extension GiantUIntTests {
 extension GiantUIntTests {
   static func allTests() -> [(String, (GiantUIntTests) -> () -> Void)] {
   static func allTests() -> [(String, (GiantUIntTests) -> () -> Void)] {
     let tests = [
     let tests = [
+      ("testComparable", testComparable),
       ("testAddition", testAddition),
       ("testAddition", testAddition),
       ("testMultiplication", testMultiplication),
       ("testMultiplication", testMultiplication),
       ("testExponentiation", testExponentiation)
       ("testExponentiation", testExponentiation)