Skip to content

Commit

Permalink
Add forging of BLS keys and signature, scaffold for BLS curve
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Feb 8, 2023
1 parent 1561f3d commit 3b110c3
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Netezos.Tests/Rpc/TestHelpersQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void TestHelpersScriptsRunOperation()
}

[Fact]
public async Task TestHelpersScriptsRunScriptView()
public void TestHelpersScriptsRunScriptView()
{
var query = Rpc.Blocks.Head.Helpers.Scripts.RunScriptView;
Assert.Equal("chains/main/blocks/head/helpers/scripts/run_script_view/", query.ToString());
Expand Down
19 changes: 12 additions & 7 deletions Netezos/Contracts/Schemas/KeySchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ public string Flatten(IMicheline value)
}
else if (value is MichelineBytes micheBytes)
{
var prefix = micheBytes.Value[0] == 0 && micheBytes.Value.Length == 33
? Prefix.edpk
: micheBytes.Value[0] == 1 && micheBytes.Value.Length == 34
? Prefix.sppk
: micheBytes.Value[0] == 2 && micheBytes.Value.Length == 34
? Prefix.p2pk
: null;
var prefix = (micheBytes.Value[0], micheBytes.Value.Length) switch
{
(0, 33) => Prefix.edpk,
(1, 34) => Prefix.sppk,
(2, 34) => Prefix.p2pk,
(3, 49) => Prefix.BLpk,
_ => null
};

if (prefix == null)
return Hex.Convert(micheBytes.Value);
Expand Down Expand Up @@ -81,6 +82,10 @@ public override IMicheline Optimize(IMicheline value)
res = new byte[34];
res[0] = 2;
break;
case "BLpk":
res = new byte[49];
res[0] = 3;
break;
default:
throw FormatException(value);
}
Expand Down
1 change: 1 addition & 0 deletions Netezos/Contracts/Schemas/SignatureSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public override IMicheline Optimize(IMicheline value)
res = Base58.Parse(micheStr.Value, 5);
break;
case "p2s":
case "BLs":
res = Base58.Parse(micheStr.Value, 4);
break;
case "sig":
Expand Down
1 change: 1 addition & 0 deletions Netezos/Forging/Local/ForgedReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public string ReadPublicKey()
case 0: prefix = Prefix.edpk; break;
case 1: prefix = Prefix.sppk; break;
case 2: prefix = Prefix.p2pk; break;
case 3: prefix = Prefix.BLpk; break;
default: throw new ArgumentException($"Invalid public key prefix {id}");
};

Expand Down
1 change: 1 addition & 0 deletions Netezos/Forging/Local/LocalForge.Forgers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static byte[] ForgePublicKey(string value)
case "edpk": return new byte[] { 0 }.Concat(res);
case "sppk": return new byte[] { 1 }.Concat(res);
case "p2pk": return new byte[] { 2 }.Concat(res);
case "BLpk": return new byte[] { 3 }.Concat(res);
default:
throw new ArgumentException($"Invalid public key prefix {prefix}");
}
Expand Down
48 changes: 15 additions & 33 deletions Netezos/Keys/Crypto/Curve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,23 @@ abstract class Curve
public abstract bool Verify(byte[] bytes, byte[] signature, byte[] pubKey);

#region static
public static Curve FromKind(ECKind kind)
public static Curve FromKind(ECKind kind) => kind switch
{
return kind == ECKind.Ed25519
? new Ed25519()
: kind == ECKind.NistP256
? (Curve)new NistP256()
: new Secp256k1();
}

public static Curve FromPrefix(string prefix)
ECKind.Ed25519 => new Ed25519(),
ECKind.Secp256k1 => new Secp256k1(),
ECKind.NistP256 => new NistP256(),
ECKind.Bls12381 => new Bls12381(),
_ => throw new ArgumentException("Invalid EC kind")
};

public static Curve FromPrefix(string prefix) => prefix switch
{
switch (prefix)
{
case "edpk":
case "edsk":
case "tz1":
case "edesk":
case "edsig":
return new Ed25519();
case "sppk":
case "spsk":
case "tz2":
case "spesk":
case "spsig":
return new Secp256k1();
case "p2pk":
case "p2sk":
case "tz3":
case "p2esk":
case "p2sig":
return new NistP256();
default:
throw new ArgumentException("Invalid prefix");
}
}
"edpk" or "edsk" or "tz1" or "edesk" or "edsig" => new Ed25519(),
"sppk" or "spsk" or "tz2" or "spesk" or "spsig" => new Secp256k1(),
"p2pk" or "p2sk" or "tz3" or "p2esk" or "p2sig" => new NistP256(),
"BLpk" or "BLsk" or "tz4" or "BLesk" or "BLsig" => new Bls12381(),
_ => throw new ArgumentException("Invalid prefix"),
};
#endregion
}
}
35 changes: 35 additions & 0 deletions Netezos/Keys/Crypto/Curves/Bls12381.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

namespace Netezos.Keys
{
class Bls12381 : Curve
{
public override ECKind Kind => ECKind.Bls12381;

public override byte[] AddressPrefix => Prefix.tz4;
public override byte[] PublicKeyPrefix => Prefix.BLpk;
public override byte[] PrivateKeyPrefix => Prefix.BLsk;
public override byte[] SignaturePrefix => Prefix.BLsig;
public override byte[] SeedKey => throw new NotImplementedException("BLS12-381 curve is not implemented yet");

public override byte[] GeneratePrivateKey()
{
throw new NotImplementedException("BLS12-381 curve is not implemented yet");
}

public override byte[] GetPublicKey(byte[] privateKey)
{
throw new NotImplementedException("BLS12-381 curve is not implemented yet");
}

public override Signature Sign(byte[] msg, byte[] prvKey)
{
throw new NotImplementedException("BLS12-381 curve is not implemented yet");
}

public override bool Verify(byte[] msg, byte[] sig, byte[] pubKey)
{
throw new NotImplementedException("BLS12-381 curve is not implemented yet");
}
}
}
3 changes: 2 additions & 1 deletion Netezos/Keys/Crypto/ECKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum ECKind
{
Ed25519 = 1,
Secp256k1 = 2,
NistP256 = 3
NistP256 = 3,
Bls12381 = 4
}
}
11 changes: 11 additions & 0 deletions Netezos/Keys/HDKeys/Standards/Slip10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Slip10 : HDStandard
{
public override (byte[], byte[]) GenerateMasterKey(Curve curve, byte[] seed)
{
if (curve.Kind == ECKind.Bls12381)
throw new NotSupportedException("BLS12-381 curve is not supported by the SLIP-0010 standard");

using var hmacSha512 = new HMACSHA512(curve.SeedKey);
while (true)
{
Expand Down Expand Up @@ -45,6 +48,9 @@ public override (byte[], byte[]) GenerateMasterKey(Curve curve, byte[] seed)

public override (byte[], byte[]) GetChildPrivateKey(Curve curve, byte[] privateKey, byte[] chainCode, uint index)
{
if (curve.Kind == ECKind.Bls12381)
throw new NotSupportedException("BLS12-381 curve is not supported by the SLIP-0010 standard");

byte[] l;

if ((index & 0x80000000) != 0) // hardened
Expand Down Expand Up @@ -104,10 +110,15 @@ public override (byte[], byte[]) GetChildPrivateKey(Curve curve, byte[] privateK

public override (byte[], byte[]) GetChildPublicKey(Curve curve, byte[] pubKey, byte[] chainCode, uint index)
{
if (curve.Kind == ECKind.Bls12381)
throw new NotSupportedException("BLS12-381 curve is not supported by the SLIP-0010 standard");

if (curve.Kind == ECKind.Ed25519)
throw new NotSupportedException("Ed25519 public key derivation not supported by slip-10");

if (pubKey.Length != 33)
throw new NotSupportedException("Invalid public key size (expected 33 bytes)");

if ((index & 0x80000000) != 0)
throw new InvalidOperationException("Can't derive a hardened child key from a public key");

Expand Down
5 changes: 4 additions & 1 deletion Netezos/Keys/PubKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public string Address

internal PubKey(byte[] bytes, ECKind kind, bool flush = false)
{
if (kind == ECKind.Ed25519 && bytes.Length != 32 || kind != ECKind.Ed25519 && bytes.Length != 33)
if (kind == ECKind.Ed25519 && bytes.Length != 32 ||
kind == ECKind.Secp256k1 && bytes.Length != 33 ||
kind == ECKind.NistP256 && bytes.Length != 33 ||
kind == ECKind.Bls12381 && bytes.Length != 48)
throw new ArgumentException("Invalid public key length", nameof(bytes));

Curve = Curve.FromKind(kind);
Expand Down
6 changes: 1 addition & 5 deletions Netezos/Keys/SecretStore/SecureSecretStore.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Netezos.Keys
namespace Netezos.Keys
{
class SecureSecretStore
{
Expand Down
2 changes: 1 addition & 1 deletion Netezos/Keys/SecretStore/StoreLocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Netezos.Keys
{
class StoreLocker : IDisposable
{
ISecretStore Store;
readonly ISecretStore Store;

public StoreLocker(ISecretStore store) => Store = store;

Expand Down
17 changes: 16 additions & 1 deletion Netezos/Utils/Prefix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static class Prefix
public static readonly byte[] tz3 = new byte[] { 6, 161, 164 };

/// <summary>
/// Address prefix for BLS-MinPk
/// Address prefix for BLS12-381
/// </summary>
public static readonly byte[] tz4 = new byte[] { 6, 161, 166 };

Expand Down Expand Up @@ -92,6 +92,11 @@ static class Prefix
/// </summary>
public static readonly byte[] p2pk = new byte[] { 3, 178, 139, 127 };

/// <summary>
/// Public key prefix for BLS12-381 (tz4)
/// </summary>
public static readonly byte[] BLpk = new byte[] { 6, 149, 135, 204 };

/// <summary>
/// Private key prefix for Ed25519 (tz1)
/// </summary>
Expand All @@ -107,6 +112,11 @@ static class Prefix
/// </summary>
public static readonly byte[] p2sk = { 16, 81, 238, 189 };

/// <summary>
/// Private key prefix for BLS12-381 (tz4)
/// </summary>
public static readonly byte[] BLsk = { 3, 150, 192, 40 };

/// <summary>
/// Signature prefix for Ed25519 (tz1)
/// </summary>
Expand All @@ -122,6 +132,11 @@ static class Prefix
/// </summary>
public static readonly byte[] p2sig = { 54, 240, 44, 52 };

/// <summary>
/// Signature prefix for BLS12-381 (tz4)
/// </summary>
public static readonly byte[] BLsig = { 40, 171, 64, 207 };

/// <summary>
/// Signature prefix
/// </summary>
Expand Down

0 comments on commit 3b110c3

Please sign in to comment.