Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Change to internal + obsolete FromBytes085 and ToBytes() from SecretKey
  • Loading branch information
Apolixit committed Feb 10, 2024
commit 4c2b0d5542ee1bb7a3a8196e7fb0d180b3469c47
6 changes: 4 additions & 2 deletions Substrate.NET.Schnorrkel/Keys/SecretKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public struct SecretKey
/// 32 bytes being the seed for nonces.
/// </summary>
/// <returns></returns>
public byte[] ToBytes()
[Obsolete("Use ToEd25519Bytes instead")]
internal byte[] ToBytes()
{
var result = new byte[64];
key.ScalarBytes.CopyTo(result, 0);
Expand All @@ -85,7 +86,8 @@ public PublicKey ExpandToPublic()
/// <param name="data"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static SecretKey FromBytes085(byte[] data)
[Obsolete("Use FromBytes011 instead")]
internal static SecretKey FromBytes085(byte[] data)
{
if (data.Length != Consts.SIGNATURE_LENGTH)
throw new SignatureException("SecretKey - SignatureError::BytesLengthError");
Expand Down
24 changes: 23 additions & 1 deletion Substrate.NET.Schnorrkel/Sr25519v085.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Substrate.NET.Schnorrkel.Ristretto;
using Substrate.NET.Schnorrkel.Scalars;
using Substrate.NET.Schnorrkel.Signed;
using System;
using System.Text;

namespace Substrate.NET.Schnorrkel
Expand All @@ -44,7 +45,8 @@ public Sr25519v085(SchnorrkelSettings settings) : base(settings)
/// <param name="secretKey"></param>
/// <param name="message"></param>
/// <returns></returns>
public static byte[] SignSimple(byte[] publicKey, byte[] secretKey, byte[] message)
[Obsolete("Use SignSimpleFromEd25519 instead")]
internal static byte[] SignSimple(byte[] publicKey, byte[] secretKey, byte[] message)
{
var sk = SecretKey.FromBytes085(secretKey);
var pk = new PublicKey(publicKey);
Expand All @@ -60,6 +62,26 @@ public static byte[] SignSimple(byte[] publicKey, byte[] secretKey, byte[] messa
return sig.ToBytes();
}

/// <summary>
/// Sign from Secret key from Ed25519 bytes (need to DivideScalarBytesByCofactor)
/// </summary>
/// <param name="publicKey"></param>
/// <param name="secretKey"></param>
/// <param name="message"></param>
/// <returns></returns>
public static byte[] SignSimpleFromEd25519(byte[] publicKey, byte[] secretKey, byte[] message)
{
var sk = SecretKey.FromEd25519Bytes(secretKey);
var pk = new PublicKey(publicKey);
var signingContext = new SigningContext085(Encoding.UTF8.GetBytes("substrate"));
var st = new SigningTranscript(signingContext);
signingContext.ts = signingContext.Bytes(message);
var rng = new Simple();
var sig = Sign(st, sk, pk, rng);

return sig.ToBytes();
}

/// <summary>
/// Sign a message with a given public and secret key.
/// </summary>
Expand Down
18 changes: 17 additions & 1 deletion Substrate.NET.Schnorrkel/Sr25519v091.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Substrate.NET.Schnorrkel.Ristretto;
using Substrate.NET.Schnorrkel.Scalars;
using Substrate.NET.Schnorrkel.Signed;
using System;
using System.Text;

namespace Substrate.NET.Schnorrkel
Expand Down Expand Up @@ -65,7 +66,8 @@ public static byte[] SignSimple(KeyPair pair, byte[] message)
/// <param name="secretKey"></param>
/// <param name="message"></param>
/// <returns></returns>
public static byte[] SignSimple(byte[] publicKey, byte[] secretKey, byte[] message)
[Obsolete("Use SignSimpleFromEd25519 instead")]
internal static byte[] SignSimple(byte[] publicKey, byte[] secretKey, byte[] message)
{
var sk = SecretKey.FromBytes085(secretKey);
var pk = new PublicKey(publicKey);
Expand Down Expand Up @@ -98,6 +100,13 @@ public static byte[] SignSimpleFromEd25519(byte[] publicKey, byte[] secretKey, b
return sig.ToBytes();
}

/// <summary>
/// Verify a signature
/// </summary>
/// <param name="signature"></param>
/// <param name="publicKey"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool Verify(byte[] signature, PublicKey publicKey, byte[] message)
{
var s = new Signature();
Expand All @@ -109,6 +118,13 @@ public static bool Verify(byte[] signature, PublicKey publicKey, byte[] message)
return Verify(st, s, publicKey);
}

/// <summary>
/// Verify a signature
/// </summary>
/// <param name="signature"></param>
/// <param name="publicKey"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool Verify(byte[] signature, byte[] publicKey, byte[] message)
{
var s = new Signature();
Expand Down