Skip to content

Commit

Permalink
Fix nonce generation to url b64
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Arnett committed Apr 12, 2024
1 parent bc76c9a commit c7a4502
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
8 changes: 8 additions & 0 deletions Sources/SuiKit/Extensions/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public extension Data {
}
return UInt16(s % 65535)
}

func base64urlEncodedString() -> String {
var result = self.base64EncodedString()
result = result.replacingOccurrences(of: "+", with: "-")
result = result.replacingOccurrences(of: "/", with: "_")
result = result.replacingOccurrences(of: "=", with: "")
return result
}
}

public extension Data {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SuiKit/Utils/zkLogin/zkLoginNonce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public struct zkLoginNonce {
let ephPublicKey1 = publicKeyBytes % BigInt(2).power(128)
let bigNum = try PoseidonUtilities.poseidonHash(inputs: [ephPublicKey0, ephPublicKey1, BigInt(maxEpoch), BigInt(randomness, radix: 10)!])
let z = zkLoginUtilities.toBigEndianBytes(num: bigNum, width: 20)
let nonce = Data(z).base64EncodedString()
let nonce = Data(z).base64urlEncodedString()
guard nonce.count == Self.nonceLength else { throw SuiError.notImplemented }
return nonce
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public struct zkLoginSignatureInputsClaim: KeyProtocol, Equatable, Codable {
public var value: String
public var indexMod4: UInt8

public init(value: String, indexMod4: UInt8) {
self.value = value
self.indexMod4 = indexMod4
}

public func serialize(_ serializer: Serializer) throws {
try Serializer.str(serializer, self.value)
try Serializer.u8(serializer, self.indexMod4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public struct zkLoginSignatureInputsProofPoints: KeyProtocol, Equatable, Codable
public var b: [[String]]
public var c: [String]

public init(a: [String], b: [[String]], c: [String]) {
self.a = a
self.b = b
self.c = c
}

public func serialize(_ serializer: Serializer) throws {
try serializer.sequence(self.a, Serializer.str)
try serializer.uleb128(UInt(self.b.count))
Expand Down
23 changes: 8 additions & 15 deletions Sources/SuiKit/Utils/zkLogin/zkLoginUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,15 @@ public struct zkLoginUtilities {
}

public static func toPaddedBigEndianBytes(num: BigInt, width: Int) -> [UInt8] {
let hex = String(num, radix: 16, uppercase: false)

// Padding and Slicing
let paddedHex = String(hex).leftPad(toLength: width * 2, withPad: "0")

// Convert Hex String to Bytes
var bytes = [UInt8]()
for i in stride(from: 0, to: paddedHex.count, by: 2) {
let start = paddedHex.index(paddedHex.startIndex, offsetBy: i)
let end = paddedHex.index(paddedHex.startIndex, offsetBy: i + 2)
let byteString = paddedHex[start..<end]
if let byte = UInt8(byteString, radix: 16) {
bytes.append(byte)
}
let hex = String(num, radix: 16)
let paddedHex = String(repeating: "0", count: max(0, width * 2 - hex.count)) + hex
let finalHex = paddedHex.suffix(width * 2)
return stride(from: 0, to: width * 2, by: 2).map {
let startIndex = finalHex.index(finalHex.startIndex, offsetBy: $0)
let endIndex = finalHex.index(startIndex, offsetBy: 2)
let byteString = finalHex[startIndex..<endIndex]
return UInt8(byteString, radix: 16)!
}
return bytes
}

public static func findFirstNonZeroIndex(bytes: [UInt8]) -> Int {
Expand Down
10 changes: 10 additions & 0 deletions Tests/SuiKitTests/Unit/zkLogin/JWTUtilsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ final class JWTUtilsTest: XCTestCase {
)
XCTAssertEqual(extractedValue, "https://accounts.google.com")
}

func testThatGeneratingNonceWorksAsIntended() throws {
let pk = try ED25519PublicKey(value: "dkUcNsSSYV2cFz+L/WAlyxINuXHpah/MJnYZ57/GtKY=")
let nonce = try zkLoginNonce.generateNonce(
publicKey: pk,
maxEpoch: 954,
randomness: "176720613486626510701195520524108477720"
)
XCTAssertEqual("NN9BV-W7MlsscmY042AddYkO1N8", nonce)
}
}

0 comments on commit c7a4502

Please sign in to comment.