Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions Sources/CryptoSwift/UInt128.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
//

#if canImport(FoundationEssentials)
import FoundationEssentials
import FoundationEssentials
#else
import Foundation
import Foundation
#endif

struct UInt128: Equatable, ExpressibleByIntegerLiteral {
Expand All @@ -31,9 +31,9 @@ struct UInt128: Equatable, ExpressibleByIntegerLiteral {
init(_ raw: Array<UInt8>) {
precondition(raw.count >= 16, "UInt128 requires at least 16 bytes")
let a = UInt64(raw[0]) << 56 | UInt64(raw[1]) << 48 | UInt64(raw[2]) << 40 | UInt64(raw[3]) << 32 |
UInt64(raw[4]) << 24 | UInt64(raw[5]) << 16 | UInt64(raw[6]) << 8 | UInt64(raw[7])
UInt64(raw[4]) << 24 | UInt64(raw[5]) << 16 | UInt64(raw[6]) << 8 | UInt64(raw[7])
let b = UInt64(raw[8]) << 56 | UInt64(raw[9]) << 48 | UInt64(raw[10]) << 40 | UInt64(raw[11]) << 32 |
UInt64(raw[12]) << 24 | UInt64(raw[13]) << 16 | UInt64(raw[14]) << 8 | UInt64(raw[15])
UInt64(raw[12]) << 24 | UInt64(raw[13]) << 16 | UInt64(raw[14]) << 8 | UInt64(raw[15])
Comment on lines 17 to +36

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modified by swiftformat

self.init((a, b))
}

Expand Down Expand Up @@ -76,13 +76,12 @@ struct UInt128: Equatable, ExpressibleByIntegerLiteral {
}

static func >> (value: UInt128, by: Int) -> UInt128 {
var result = value
for _ in 0..<by {
let a = result.i.a >> 1
let b = result.i.b >> 1 + ((result.i.a & 1) << 63)
result = UInt128((a, b))
}
return result
guard by > 0 else { return value }
guard by < 128 else { return UInt128(a: 0, b: 0) }
guard by < 64 else { return UInt128(a: 0, b: value.i.a >> (by - 64)) }
let a = value.i.a >> by
let b = (value.i.b >> by) | (value.i.a << (64 - by))
return UInt128(a: a, b: b)
}

// Equatable.
Expand Down