Skip to content

Conversation

@Jevaites
Copy link

Describe your changes

Adds a small, self-contained key generation utility that mirrors the key grinding logic used by other StarkNet SDKs, plus a convenience for generating a random private key.

Implementation

  • StarkKeygen.grindKey(seed:maxIters:)
    Deterministically grinds an arbitrary seed into a valid Stark curve private key:
    • For i = 0..N: compute SHA256(seed || i)
    • Accept x only if x < limit, where limit = 2^256 - (2^256 mod n)
    • Return x % n (in [0, n)), where n is the Stark curve subgroup order
  • StarkKeygen.randomPrivateKeyHex()
    Pulls 32 bytes from a CSPRNG and returns a 0x-prefixed, 64-hex-chars private key, produced by grindKey.

Both functions are pure and do not touch global state.

Public API

public enum StarkKeygen {
    public static func grindKey(seed: Data, maxIters: Int = 100_000) throws -> BigUInt
    public static func randomPrivateKeyHex() throws -> String
}

Errors

  • StarkKeygenError.rngFailure – CSPRNG failure (only for randomPrivateKeyHex)
  • StarkKeygenError.grindExceeded – exceeded maxIters without finding a value < limit

Breaking changes

  • This issue contains breaking changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant