Skip to content

Commit

Permalink
store decoded pks while validating
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchhill committed Oct 29, 2024
1 parent d42d1af commit 72661cc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void Can_decrypt_data(string cipherTextHex, string decryptionKeyHex, stri
public void Can_verify_validator_registration_signature(string msgHex, string sigHex, string pkHex)
{
Assert.That(ShutterCrypto.CheckValidatorRegistrySignature(
Convert.FromHexString(pkHex),
new(Convert.FromHexString(pkHex)),
Convert.FromHexString(sigHex),
Convert.FromHexString(msgHex)
));
Expand Down
36 changes: 16 additions & 20 deletions src/Nethermind/Nethermind.Shutter/Config/ValidatorsInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.IO;
using Nethermind.Crypto;
Expand All @@ -12,42 +13,37 @@ namespace Nethermind.Shutter.Config;

public class ValidatorsInfo
{
public bool IsEmpty { get => _indexToPubKey is null || _indexToPubKey.Count == 0; }
public IEnumerable<ulong> ValidatorIndices { get => _indexToPubKey!.Keys; }
private Dictionary<ulong, byte[]>? _indexToPubKey;
public bool IsEmpty { get => _indexToPubKeyBytes is null || _indexToPubKeyBytes.Count == 0; }
public IEnumerable<ulong> ValidatorIndices { get => _indexToPubKeyBytes!.Keys; }
public class ShutterValidatorsInfoException(string message) : Exception(message);

private Dictionary<ulong, byte[]>? _indexToPubKeyBytes;
private readonly Dictionary<ulong, long[]> _indexToPubKey = [];

public void Load(string fp)
{
FileStream fstream = new(fp, FileMode.Open, FileAccess.Read, FileShare.None);
_indexToPubKey = new EthereumJsonSerializer().Deserialize<Dictionary<ulong, byte[]>>(fstream);
_indexToPubKeyBytes = new EthereumJsonSerializer().Deserialize<Dictionary<ulong, byte[]>>(fstream);
}

public bool Validate(out string err)
public void Validate()
{
if (_indexToPubKey is null)
{
err = "Validator info file not loaded.";
return false;
}

G1Affine pk = new(stackalloc long[G1Affine.Sz]);

foreach ((ulong index, byte[] pubkey) in _indexToPubKey)
foreach ((ulong index, byte[] pubkey) in _indexToPubKeyBytes!)
{
if (!pk.TryDecode(pubkey, out Bls.ERROR _))
{
err = $"Validator info file contains invalid public key with index {index}";
return false;
throw new ShutterValidatorsInfoException($"Validator info file contains invalid public key with index {index}.");
}
}

err = "";
return true;
_indexToPubKey.Add(index, pk.Point.ToArray());
}
}

public bool IsIndexRegistered(ulong index)
=> _indexToPubKey!.ContainsKey(index);
=> _indexToPubKeyBytes!.ContainsKey(index);

public byte[] GetPubKey(ulong index)
=> _indexToPubKey![index];
public G1Affine GetPubKey(ulong index)
=> new(_indexToPubKey[index]);
}
9 changes: 2 additions & 7 deletions src/Nethermind/Nethermind.Shutter/ShutterCrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,8 @@ public static bool CheckSlotDecryptionIdentitiesSignature(
}

[SkipLocalsInit]
public static bool CheckValidatorRegistrySignature(ReadOnlySpan<byte> pkBytes, ReadOnlySpan<byte> sigBytes, ReadOnlySpan<byte> msgBytes)
{
ValueHash256 h = ValueKeccak.Compute(msgBytes);
G1Affine pk = new(stackalloc long[G1Affine.Sz]);
pk.Decode(pkBytes);
return BlsSigner.Verify(pk, sigBytes, h.Bytes);
}
public static bool CheckValidatorRegistrySignature(G1Affine pk, ReadOnlySpan<byte> sigBytes, ReadOnlySpan<byte> msgBytes)
=> BlsSigner.Verify(pk, sigBytes, ValueKeccak.Compute(msgBytes).Bytes);

public static EncryptedMessage Encrypt(ReadOnlySpan<byte> msg, G1 identity, G2 eonKey, ReadOnlySpan<byte> sigma)
{
Expand Down
6 changes: 1 addition & 5 deletions src/Nethermind/Nethermind.Shutter/ShutterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,12 @@ public IBlockProducer InitBlockProducer(IBlockProducerFactory consensusPlugin, I
try
{
validatorsInfo.Load(_shutterConfig!.ValidatorInfoFile);
validatorsInfo.Validate();
}
catch (Exception e)
{
throw new ShutterLoadingException("Could not load Shutter validator info file", e);
}

if (!validatorsInfo.Validate(out string err))
{
throw new ShutterLoadingException(err);
}
}

_shutterApi = new ShutterApi(
Expand Down

0 comments on commit 72661cc

Please sign in to comment.