public void BlsRoundtripSignAndVerify() { // Arrange var privateKey = HexMate.Convert.FromHexString(PrivateKeys[1]); var messageHash = MessageHashes[0]; var domain = Domains[0]; Console.WriteLine("Input:"); Console.WriteLine("Private Key: [{0}] {1}", privateKey.Length, HexMate.Convert.ToHexString(privateKey)); Console.WriteLine("Domain: [{0}] {1}", domain.Length, HexMate.Convert.ToHexString(domain)); Console.WriteLine("MessageHash: [{0}] {1}", messageHash.Length, HexMate.Convert.ToHexString(messageHash)); // Act var parameters = new BLSParameters() { PrivateKey = privateKey }; using var bls = new BLSHerumi(parameters); var publicKey = new byte[48]; _ = bls.TryExportBLSPublicKey(publicKey, out var _); Console.WriteLine("Public Key: [{0}] {1}", publicKey.Length, HexMate.Convert.ToHexString(publicKey)); var initialX = new byte[96]; _ = bls.TryCombineHashAndDomain(messageHash, domain, initialX, out var _); Console.WriteLine("InitialX: [{0}] {1}", initialX.Length, HexMate.Convert.ToHexString(initialX)); var signature = new byte[96]; var signatureSuccess = bls.TrySignHash(initialX, signature.AsSpan(), out var bytesWritten); Console.WriteLine("Signature: {0} [{1}] {2}", signatureSuccess, bytesWritten, HexMate.Convert.ToHexString(signature)); //var expectedSignature = HexMate.Convert.FromHexString("b9d1bf921b3dd048bdce38c2ceac2a2a8093c864881f2415f22b198de935ffa791707855c1656dc21a7af2d502bb46590151d645f062634c3b2cb79c4ed1c4a4b8b3f19f0f5c76965c651553e83d153ff95353735156eff77692f7a62ae653fb"); //signature.ShouldBe(expectedSignature); var verifySuccess = bls.VerifyHash(initialX, signature); Console.WriteLine("Verify1: {0}", verifySuccess); var parameters2 = new BLSParameters() { PublicKey = publicKey }; using var bls2 = new BLSHerumi(parameters); var verifySuccess2 = bls2.VerifyHash(initialX, signature); Console.WriteLine("Verify2: {0}", verifySuccess2); verifySuccess2.ShouldBeTrue(); }
public void Case05VerifyMessages(byte[] publicKey, byte[] messageHash, byte[] domain, byte[] signature, bool expected) { // Arrange var parameters = new BLSParameters() { PublicKey = publicKey }; Console.WriteLine("Input:"); Console.WriteLine("Public Key: [{0}] {1}", publicKey.Length, HexMate.Convert.ToHexString(publicKey)); Console.WriteLine("MessageHash: [{0}] {1}", messageHash.Length, HexMate.Convert.ToHexString(messageHash)); Console.WriteLine("Domain: [{0}] {1}", domain.Length, HexMate.Convert.ToHexString(domain)); Console.WriteLine("Signature: [{0}] {1}", signature.Length, HexMate.Convert.ToHexString(signature)); // Act using var bls = new BLSHerumi(parameters); var success = bls.VerifyHash(messageHash, signature, domain); Console.WriteLine("Output: Success {0}", success); // Assert success.ShouldBe(expected); }
public void BlsRoundtripAggregatePublicKeyVerifySharedHash() { // Arrange var privateKey1 = HexMate.Convert.FromHexString(PrivateKeys[1]); var privateKey2 = HexMate.Convert.FromHexString(PrivateKeys[2]); var sharedMessageHash = MessageHashes[1]; var domain1 = Domains[1]; Console.WriteLine("Input:"); Console.WriteLine("Private Key 1: [{0}] {1}", privateKey1.Length, HexMate.Convert.ToHexString(privateKey1)); Console.WriteLine("Private Key 2: [{0}] {1}", privateKey2.Length, HexMate.Convert.ToHexString(privateKey2)); Console.WriteLine("MessageHash 1: [{0}] {1}", sharedMessageHash.Length, HexMate.Convert.ToHexString(sharedMessageHash)); Console.WriteLine("Domain: [{0}] {1}", domain1.Length, HexMate.Convert.ToHexString(domain1)); // Sign 1 using var bls1 = new BLSHerumi(new BLSParameters() { PrivateKey = privateKey1 }); var signature1 = new byte[96]; _ = bls1.TrySignHash(sharedMessageHash, signature1.AsSpan(), out var _, domain1); Console.WriteLine("Signature 1: [{0}] {1}", signature1.Length, HexMate.Convert.ToHexString(signature1)); var publicKey1 = new byte[48]; _ = bls1.TryExportBlsPublicKey(publicKey1, out var _); Console.WriteLine("Public Key 1: [{0}] {1}", publicKey1.Length, HexMate.Convert.ToHexString(publicKey1)); // Sign 2 using var bls2 = new BLSHerumi(new BLSParameters() { PrivateKey = privateKey2 }); var signature2 = new byte[96]; _ = bls2.TrySignHash(sharedMessageHash, signature2.AsSpan(), out var _, domain1); Console.WriteLine("Signature 2: [{0}] {1}", signature2.Length, HexMate.Convert.ToHexString(signature2)); var publicKey2 = new byte[48]; _ = bls2.TryExportBlsPublicKey(publicKey2, out var _); Console.WriteLine("Public Key 2: [{0}] {1}", publicKey2.Length, HexMate.Convert.ToHexString(publicKey2)); // Aggregate public keys var publicKeys = new Span <byte>(new byte[48 * 2]); publicKey1.CopyTo(publicKeys); publicKey2.CopyTo(publicKeys.Slice(48)); using var blsAggregateKeys = new BLSHerumi(new BLSParameters()); var aggregatePublicKey = new byte[48]; blsAggregateKeys.TryAggregatePublicKeys(publicKeys, aggregatePublicKey, out var _); Console.WriteLine("Aggregate Public Key: [{0}] {1}", aggregatePublicKey.Length, HexMate.Convert.ToHexString(aggregatePublicKey)); // Aggregate signatures var signatures = new Span <byte>(new byte[96 * 2]); signature1.CopyTo(signatures); signature2.CopyTo(signatures.Slice(96)); using var blsAggregate = new BLSHerumi(new BLSParameters()); var aggregateSignature = new byte[96]; blsAggregate.TryAggregateSignatures(signatures, aggregateSignature, out var _); Console.WriteLine("Aggregate Signature: [{0}] {1}", aggregateSignature.Length, HexMate.Convert.ToHexString(aggregateSignature)); // Verify aggregates // i.e. the combined aggregatePublicKey / aggregateSignature are a valid pair var aggregatePublicKeyParameters = new BLSParameters() { PublicKey = aggregatePublicKey }; using var blsVerify = new BLSHerumi(aggregatePublicKeyParameters); var verifySuccess = blsVerify.VerifyHash(sharedMessageHash, aggregateSignature, domain1); Console.WriteLine("Verify: {0}", verifySuccess); verifySuccess.ShouldBeTrue(); }