Secp256k1.Net 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Secp256k1.Net --version 1.0.0                
NuGet\Install-Package Secp256k1.Net -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Secp256k1.Net" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Secp256k1.Net --version 1.0.0                
#r "nuget: Secp256k1.Net, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Secp256k1.Net as a Cake Addin
#addin nuget:?package=Secp256k1.Net&version=1.0.0

// Install Secp256k1.Net as a Cake Tool
#tool nuget:?package=Secp256k1.Net&version=1.0.0                

Secp256k1.Net

NuGet NuGet

Cross platform C# wrapper for the native secp256k1 library.

The nuget package supports win-x64, win-x86, win-arm64, macOS-x64, macOS-arm64 (Apple Silcon), linux-x64, linux-x86, and linux-arm64 out of the box. The native libraries are bundled from the Secp256k1.Native package. This wrapper should work on any other platform that supports netstandard2.0 (.NET Core 2.0+, Mono 5.4+, etc) but requires that the native secp256k1 library be compiled from source.


Example Usage

Generate key pair
using var secp256k1 = new Secp256k1();

// Generate a private key
var privateKey = new byte[Secp256k1.PRIVKEY_LENGTH];
var rnd = System.Security.Cryptography.RandomNumberGenerator.Create();
do { rnd.GetBytes(privateKey); }
while (!secp256k1.SecretKeyVerify(privateKey));

// Derive public key bytes
var publicKey = new byte[Secp256k1.PUBKEY_LENGTH];
Assert.True(secp256k1.PublicKeyCreate(publicKey, privateKey), "Public key creation failed");

// Serialize the public key to compressed format
var serializedCompressedPublicKey = new byte[Secp256k1.SERIALIZED_COMPRESSED_PUBKEY_LENGTH];
Assert.True(secp256k1.PublicKeySerialize(serializedCompressedPublicKey, publicKey, Flags.SECP256K1_EC_COMPRESSED));

// Serialize the public key to uncompressed format
var serializedUncompressedPublicKey = new byte[Secp256k1.SERIALIZED_UNCOMPRESSED_PUBKEY_LENGTH];
Assert.True(secp256k1.PublicKeySerialize(serializedUncompressedPublicKey, publicKey, Flags.SECP256K1_EC_UNCOMPRESSED));
Sign and verify message
using var secp256k1 = new Secp256k1();
var keypair = new
{
    PrivateKey = Convert.FromHexString("7ef7543476bf146020cb59f9968a25ec67c3c73dbebad8a0b53a3256170dcdfe"),
    PublicKey = Convert.FromHexString("2208d5dc41d4f3ed555aff761e9bb0b99fbe6d1503b98711944be6a362242ebfa1c788c7a4e13f6aaa4099f9d2175fc031e5aa3ba08eb280e87dfb43bdae207f")
};

// Create message hash
var msgBytes = System.Text.Encoding.UTF8.GetBytes("Hello!!");
var msgHash = System.Security.Cryptography.SHA256.HashData(msgBytes);
Assert.Equal(Secp256k1.HASH_LENGTH, msgHash.Length);

// Sign then verify message hash
var signature = new byte[Secp256k1.SIGNATURE_LENGTH];
Assert.True(secp256k1.Sign(signature, msgHash, keypair.PrivateKey));
Assert.True(secp256k1.Verify(signature, msgHash, keypair.PublicKey));
Compute an ECDH (EC Diffie-Hellman) secret
using var secp256k1 = new Secp256k1();
            
var aliceKeyPair = new
{
  PrivateKey = Convert.FromHexString("7ef7543476bf146020cb59f9968a25ec67c3c73dbebad8a0b53a3256170dcdfe"),
  PublicKey = Convert.FromHexString("2208d5dc41d4f3ed555aff761e9bb0b99fbe6d1503b98711944be6a362242ebfa1c788c7a4e13f6aaa4099f9d2175fc031e5aa3ba08eb280e87dfb43bdae207f")
};
var bobKeyPair = new
{
  PrivateKey = Convert.FromHexString("d8bdb07407bb011137ef7ba6a7f07c6a55c1e3600a6aa138e34ab5c16439ceda"),
  PublicKey = Convert.FromHexString("62127c4563f711169b1d3e56a34f218302a2587c3725bd418b9388933373e095d45ec4d74ca734599598c89d7719bda5fb799afeec89c6940d569e05bd5a1bba")
};

// Create secret using Alice's public key and Bob's private key
var secret1 = new byte[Secp256k1.SECRET_LENGTH];
Assert.True(secp256k1.Ecdh(secret1, aliceKeyPair.PublicKey, bobKeyPair.PrivateKey));

// Create secret using Bob's public key and Alice's private key
var secret2 = new byte[Secp256k1.SECRET_LENGTH];
Assert.True(secp256k1.Ecdh(secret2, bobKeyPair.PublicKey, aliceKeyPair.PrivateKey));

// Validate secrets match
Assert.Equal(Convert.ToHexString(secret1), Convert.ToHexString(secret2));
Parsing and serializing DER signatures
using var secp256k1 = new Secp256k1();

// Parse DER signature
var signatureOutput = new byte[Secp256k1.SIGNATURE_LENGTH];
var derSignature = Convert.FromHexString("30440220484ECE2B365D2B2C2EAD34B518328BBFEF0F4409349EEEC9CB19837B5795A5F5022040C4F6901FE489F923C49D4104554FD08595EAF864137F87DADDD0E3619B0605");                
Assert.True(secp256k1.SignatureParseDer(signatureOutput, derSignature));

// Serialize DER signature
Span<byte> derSignatureOutput = new byte[Secp256k1.SERIALIZED_DER_SIGNATURE_MAX_SIZE];
Assert.True(secp256k1.SignatureSerializeDer(derSignatureOutput, signatureOutput, out int signatureOutputLength));
derSignatureOutput = derSignatureOutput.Slice(0, signatureOutputLength);

// Validate signature is the same after round trip parse and serialize
Assert.Equal(Convert.ToHexString(derSignature), Convert.ToHexString(derSignatureOutput));

See the tests project for more examples.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on Secp256k1.Net:

Package Downloads
AElf.Cryptography

Cryptographic primitives used in AElf.

Meadow.Core

Package Description

Meadow.EVM

Package Description

Libplanet.Crypto.Secp256k1

Package Description

Cosm.Net.Core

Package Description

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on Secp256k1.Net:

Repository Stars
AElfProject/AElf
An AI-enhanced cloud-native layer-1 blockchain network. 
planetarium/libplanet
Blockchain in C#/.NET for on-chain, decentralized gaming
hcmlab/nova
NOVA is a tool for annotating and analyzing behaviours in social interactions. It supports Annotators using Machine Learning already during the coding process. Further it features both, discrete labels and continuous scores and a visuzalization of streams recorded with the SSI Framework.
MeadowSuite/Meadow
Integrated Ethereum implementation and tool suite focused on Solidity testing and development.
Version Downloads Last updated
1.2.0 82,579 6/17/2023
1.1.0 10,062 2/12/2023
1.0.1 342 2/4/2023
1.0.0 411 2/4/2023
1.0.0-alpha.3 127 2/4/2023
1.0.0-alpha.2 129 2/4/2023
1.0.0-alpha 187,151 2/9/2022
0.1.55 200,338 1/28/2020
0.1.54 190,790 4/1/2019
0.1.52 15,045 10/25/2018
0.1.51 749 10/25/2018
0.1.48 108,641 9/6/2018
0.1.47 788 9/6/2018
0.1.46 827 9/6/2018
0.1.45 942 8/8/2018
0.1.44 836 7/30/2018
0.1.43 855 7/28/2018
0.1.42 835 7/27/2018
0.1.41 831 7/27/2018
0.1.40 830 7/27/2018
0.1.39 823 7/27/2018
0.1.38 838 7/27/2018
0.1.37 827 7/27/2018
0.1.36 1,032 7/13/2018
0.1.35 850 7/11/2018
0.1.34 923 7/8/2018
0.1.33 936 7/5/2018
0.1.31 860 7/3/2018
0.1.30 910 7/3/2018
0.1.29 857 7/2/2018
0.1.28 994 6/20/2018
0.1.23 560 1/28/2020
0.1.22 569 1/28/2020
0.1.16 864 6/20/2018
0.1.15 840 6/20/2018
0.1.14 852 6/20/2018
0.1.13 932 6/20/2018
0.1.12 950 6/20/2018
0.1.11 936 6/20/2018
0.1.10 930 6/20/2018
0.1.9 940 6/20/2018
0.1.8 916 6/20/2018
0.1.7 931 6/20/2018
0.1.6 930 6/20/2018
0.1.1 906 6/19/2018
0.1.0 2,775 1/28/2020