Skip to content

Commit

Permalink
Init a BInt from a [UInt8] (#56)
Browse files Browse the repository at this point in the history
* Updating project settings

* Init a `BInt` from a `[UInt8]`

* Get bytes `[UInt8]` from `BInt`

* Adding `Byte` and `Bytes` type aliases
  • Loading branch information
Nathan Fallet authored Aug 25, 2021
1 parent 367471a commit 0ec7214
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
32 changes: 32 additions & 0 deletions Sources/Swift-Big-Number-Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public typealias Limb = UInt64
public typealias Digits = [UInt64]
public typealias Digit = UInt64

// Bytes allow to initialize and export BInt for operations like network related ones.

public typealias Bytes = [UInt8]
public typealias Byte = UInt8

// MARK: - Imports
// ————————————————————————————————————————————————————————————————————————————————————————————
// |||||||| Operators |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Expand Down Expand Up @@ -432,6 +437,19 @@ public struct BInt:
{
self.init(source)
}

/// Creates a new instance from a `[UInt8]` array
public init(bytes: Bytes)
{
var num = BInt()

for byte in bytes
{
num = num << 8 | BInt(byte)
}

self.init(sign: num.sign, limbs: num.limbs)
}

//
//
Expand Down Expand Up @@ -551,6 +569,20 @@ public struct BInt:
i += 1
}
}

/// Bytes of the number
public func getBytes() -> Bytes
{
var bytes = Bytes()
var copy = self

while copy != 0 {
bytes.append(Byte(copy & 0xff))
copy >>= 8
}

return bytes.reversed()
}

//
//
Expand Down
12 changes: 7 additions & 5 deletions Swift-BigNumber.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 0940;
LastUpgradeCheck = 1250;
ORGANIZATIONNAME = "Marcel Kröker";
TargetAttributes = {
AE8388A5203F1CAF00FAC88F = {
Expand Down Expand Up @@ -546,7 +546,7 @@
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "BigNumber-iOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 11.3;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOS";
PRODUCT_NAME = BigNumber;
Expand Down Expand Up @@ -577,7 +577,7 @@
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "BigNumber-iOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 11.3;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOS";
PRODUCT_NAME = BigNumber;
Expand All @@ -604,7 +604,7 @@
DEVELOPMENT_TEAM = "";
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "BigNumber-iOSTests/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOSTests";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -628,7 +628,7 @@
DEVELOPMENT_TEAM = "";
GCC_C_LANGUAGE_STANDARD = gnu11;
INFOPLIST_FILE = "BigNumber-iOSTests/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.yuzushioh.BigNumber-iOSTests";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -663,6 +663,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -720,6 +721,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down
16 changes: 16 additions & 0 deletions Tests/Test_Initialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ class Test_Initialization: XCTestCase {
XCTAssert(b.rawValue.limbs == [0, 0, 0, 0, 1])
}

func testBytes()
{
// Bytes and expected number
// 0x0102030405 is 4328719365 in decimal
let array: Bytes = [0x01, 0x02, 0x03, 0x04, 0x05]
let expected: Int = 4328719365

// Init from bytes (array)
let b = BInt(bytes: array)
XCTAssertEqual(b.description, expected.description)

// Convert back to bytes
let bytes = b.getBytes()
XCTAssertEqual(bytes, array)
}

func testCodable() {
for i in 0..<50 {
let one = BInt(i)
Expand Down

0 comments on commit 0ec7214

Please sign in to comment.