From 54984ef915a3bf640e06015bd294bd2186b3a588 Mon Sep 17 00:00:00 2001 From: vnugent Date: Fri, 1 Dec 2023 21:00:49 -0500 Subject: internal polish, minor refactors & manifest v3 progression --- .../src/Secp256k1SecretKey.cs | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 back-end/libs/NVault.Crypto.Secp256k1/src/Secp256k1SecretKey.cs (limited to 'back-end/libs/NVault.Crypto.Secp256k1/src/Secp256k1SecretKey.cs') diff --git a/back-end/libs/NVault.Crypto.Secp256k1/src/Secp256k1SecretKey.cs b/back-end/libs/NVault.Crypto.Secp256k1/src/Secp256k1SecretKey.cs new file mode 100644 index 0000000..7224720 --- /dev/null +++ b/back-end/libs/NVault.Crypto.Secp256k1/src/Secp256k1SecretKey.cs @@ -0,0 +1,58 @@ +// Copyright (C) 2023 Vaughn Nugent +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +using System; +using System.Runtime.InteropServices; + +using VNLib.Utils.Memory; + +namespace NVault.Crypto.Secp256k1 +{ + /// + /// Represents a Secp256k1 secret key, the size is fixed, and should use + /// the sizeof() operator to get the size + /// + [StructLayout(LayoutKind.Sequential, Size = 32)] + public unsafe struct Secp256k1SecretKey + { + private fixed byte data[32]; + + /// + /// Implict cast to a span of raw bytes + /// + /// The secret key to cast + public static implicit operator Span(Secp256k1SecretKey key) => new(key.data, 32); + + /// + /// Casts the secret key span to a via a structure copy + /// + /// The key data to copy + /// + public static explicit operator Secp256k1SecretKey(ReadOnlySpan key) => FromSpan(key); + + /// + /// Creates a new from a span of bytes + /// by copying the bytes into the struct + /// + /// The secret key data to copy + /// An initilaized + public static Secp256k1SecretKey FromSpan(ReadOnlySpan span) + { + Secp256k1SecretKey newKey = new(); + MemoryUtil.CopyStruct(span, ref newKey); + return newKey; + } + } +} \ No newline at end of file -- cgit