K1 - Secp256k1 Elliptic Curve in Swift

Overview

K1 πŸ”

Safer than K2

K1 is Swift wrapper around libsecp256k1 (bitcoin-core/secp256k1), offering ECDSA, Schnorr (BIP340) and ECDH features.

Features

ECDSA Signatures

let alice = try K1.PrivateKey.generateNew()
let message = "Send Bob 3 BTC".data(using: .utf8)!
let signature = try alice.ecdsaSign(unhashed: message)
let isSignatureValid = try alice.publicKey.isValidECDSASignature(signature, unhashed: message)
assert(isSignatureValid, "Signature should be valid.")

Schnorr Signatures

let alice = try K1.PrivateKey.generateNew()
let message = "Send Bob 3 BTC".data(using: .utf8)!
let signature = try alice.schnorrSign(unhashed: message)
let isSignatureValid = try alice.publicKey.isValidSchnorrSignature(signature, unhashed: message)
assert(isSignatureValid, "Signature should be valid.")

Schnorr Scheme

The Schnorr signature implementation is BIP340, since we use libsecp256k1 which only provides the BIP340 Schnorr scheme.

It is worth noting that some Schnorr implementations are incompatible with BIP340 and thus this library, e.g. Zilliqa's (kudelski report, libsecp256k1 proposal, Twitter thread).

ECDH

let alice = try K1.PrivateKey.generateNew()
let bob = try K1.PrivateKey.generateNew()

let ab = try alice.sharedSecret(with: bob.publicKey)
let ba = try bob.sharedSecret(with: alice.publicKey)
assert(ab == ba, "Alice and Bob should be able to agree on the same secret")

Alternatives

Non-Swift but SPM support

greymass/secp256k1 (Fork of libsecp256k1)

You might also like...
Simple and secure hashing in Swift with the SipHash algorithm

SipHash ⚠️ WARNING This package has been obsoleted by the Hasher type and the Hashable.hash(into:) requirement introduced in Swift 4.2. Using this pac

Simple Swift wrapper for Keychain that works on iOS, watchOS, tvOS and macOS.
Simple Swift wrapper for Keychain that works on iOS, watchOS, tvOS and macOS.

KeychainAccess KeychainAccess is a simple Swift wrapper for Keychain that works on iOS and OS X. Makes using Keychain APIs extremely easy and much mor

A simple Swift Keychain Wrapper for iOS, watchOS, and OS X.

Latch A simple Swift 2.0 Keychain Wrapper for iOS, watchOS 2, and OS X. Usage A proper example of how to use Latch can be seen in the tests. import La

A simple wrapper for the iOS Keychain to allow you to use it in a similar fashion to User Defaults. Written in Swift.

SwiftKeychainWrapper A simple wrapper for the iOS / tvOS Keychain to allow you to use it in a similar fashion to User Defaults. Written in Swift. Prov

Swift cross-platform crypto library using CommonCrypto/libcrypto

BlueCryptor Swift cross-platform crypto library derived from IDZSwiftCommonCrypto. IMPORTANT NOTE: This release is NOT entirely source code compatible

A powerful, protocol-oriented library for working with the keychain in Swift.

Locksmith A powerful, protocol-oriented library for working with the keychain in Swift. πŸ“± iOS 8.0+ πŸ’» Mac OS X 10.10+ ⌚️ watchOS 2 πŸ“Ί tvOS πŸš€ I make

πŸ”’ Swift Obfuscator that protects iOS apps against reverse engineering attacks.
πŸ”’ Swift Obfuscator that protects iOS apps against reverse engineering attacks.

struct fjiovh4894bvic: XbuinvcxoDHFh3fjid { let VNfhnfn3219d: Vnahfi5n34djga func cxncjnx8fh83FDJSDd() - Lghbna2gf0gmh3d { return vPAOSNdcbif

Swift OpenSSL for OS X and Linux

OpenSSL OpenSSL for Swift 3.0. Installation import PackageDescription let package = Package( dependencies: [ .Package(url: "https://github.com/Zew

Elegant Swift interface to access the CommonCrypto routines

SCrypto [Overview β€’ Requirements β€’ Installation β€’ Usage β€’ Alternatives β€’ Licence] Overview SCrypto provides neat Swift interface to access the CommonC

Comments
  • Rationale for ecdh_skip_hash_extract_x_and_y

    Rationale for ecdh_skip_hash_extract_x_and_y

    This libarary is great, I really like the minimalist API but I'm having interoperability issues using it for ECDH.

    I believe it is due to PrivateKey.sharedSecret using ecdh_skip_hash_extract_x_and_y

    https://github.com/Sajjon/K1/blob/89cac3e2f7765f4a782ae71d2de231c08dcfaa53/Sources/K1/K1/Keys/PrivateKey/PrivateKey/PrivateKey%2BBridge%2BTo%2BC.swift#L144

    The result is Data that when put through SHA256.hash produces a inconsistent result from libraries like this:

    https://github.com/bitcoin-core/secp256k1/blob/185a6af22792531a629959834fff9257e396abb5/src/modules/ecdh/main_impl.h#L13-L24

    I was wondering if this was an intentional design decision and what the rationale was?

    opened by mikekelly 2
Owner
Alexander Cyon
Crypto & DLT dev πŸ’Έ OSS author πŸ“– Donations: BTC: 3GarsdAzLpEYbhkryYz1WiZxhtTLLaNJwo ETH: 0xAB8F0137295BFE37f50b581F76418518a91ab8DB
Alexander Cyon
RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

BlueRSA Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux

Kitura 122 Dec 16, 2022
Swift-problem-solving - Swift μ•Œκ³ λ¦¬μ¦˜ 맛보기 πŸ˜‹

swift-problem-solving Swift 둜 μ•Œκ³ λ¦¬μ¦˜ 읡히기 ?? Programmers λ‚œμ΄λ„ 풀이 문제 λ°”λ‘œκ°€κΈ° Lv.2 μ˜€ν”ˆμ±„νŒ…λ°© 링크 Lv.3 닀단계 칫솔 판맀 링크 Lv.3 ν•©μŠΉ νƒμ‹œ μš”κΈˆ 링크 Leetcode λ‚œμ΄λ„ 풀이 문제 λ°”λ‘œκ°€κΈ° Medium 1

jegyun 3 Dec 27, 2022
Swift-cuckoo-collections - Cross-platform Swift dictionaries & sets that use a cuckoo hashing algorithm

CuckooCollections A Swift package for open-addressed sets and dictionaries that

Christopher Richez 0 Aug 2, 2022
Helps you define secure storages for your properties using Swift property wrappers.

?? Secure Property Storage Helps you define secure storages for your properties using Swift property wrappers. ?? Features All keys are hashed using S

Alex RupΓ©rez 443 Jan 4, 2023
Swift cross-platform crypto library using CommonCrypto/libcrypto

BlueCryptor Swift cross-platform crypto library derived from IDZSwiftCommonCrypto. IMPORTANT NOTE: This release is NOT entirely source code compatible

Kitura 183 Oct 15, 2022
CryptoSwift is a growing collection of standard and secure cryptographic algorithms implemented in Swift

CryptoSwift Crypto related functions and helpers for Swift implemented in Swift. (#PureSwift) Note: The master branch follows the latest currently rel

Marcin Krzyzanowski 9.4k Jan 5, 2023
A wrapper for Apple's Common Crypto library written in Swift.

IDZSwiftCommonCrypto A Swift wrapper for Apple's CommonCrypto library. IDZSwiftCommonCrypto works with both CocoaPods and Cathage. For more details on

idz 472 Dec 12, 2022
A framework for the JOSE standards JWS, JWE, and JWK written in Swift.

JOSESwift is a modular and extensible framework for the JOSE standards JWS, JWE, and JWK written in Swift. ?? Please note that this implementation of

Airside Mobile, Inc. 162 Dec 15, 2022
CCCryptor (AES encryption) wrappers for iOS and Mac in Swift. -- For ObjC, see RNCryptor/RNCryptor-objc

RNCryptor Cross-language AES Encryptor/Decryptor data format. The primary targets are Swift and Objective-C, but implementations are available in C, C

null 3.3k Dec 30, 2022
Elegant Swift interface to access the CommonCrypto routines

SCrypto [Overview β€’ Requirements β€’ Installation β€’ Usage β€’ Alternatives β€’ Licence] Overview SCrypto provides neat Swift interface to access the CommonC

Max 39 Mar 31, 2022