Exemplo n.º 1
0
        public void BlsRoundtripSignAndAggregateVerify()
        {
            // Arrange
            var privateKey1  = HexMate.Convert.FromHexString(PrivateKeys[1]);
            var privateKey2  = HexMate.Convert.FromHexString(PrivateKeys[2]);
            var messageHash1 = MessageHashes[1];
            var messageHash2 = MessageHashes[2];
            var domain1      = Domains[1];

            Console.WriteLine("Input:");
            Console.WriteLine("Private Key 1: [{0}] {1}", privateKey1.Length, HexMate.Convert.ToHexString(privateKey1));
            Console.WriteLine("MessageHash 1: [{0}] {1}", messageHash1.Length, HexMate.Convert.ToHexString(messageHash1));
            Console.WriteLine("Private Key 2: [{0}] {1}", privateKey2.Length, HexMate.Convert.ToHexString(privateKey2));
            Console.WriteLine("MessageHash 2: [{0}] {1}", messageHash2.Length, HexMate.Convert.ToHexString(messageHash2));
            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(messageHash1, 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(messageHash2, 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 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));

            // Aggregate verify
            using var blsVerify = new BLSHerumi(new BLSParameters());
            var publicKeys = new Span <byte>(new byte[48 * 2]);

            publicKey1.CopyTo(publicKeys);
            publicKey2.CopyTo(publicKeys.Slice(48));
            var hashes = new Span <byte>(new byte[32 * 2]);

            messageHash1.CopyTo(hashes);
            messageHash2.CopyTo(hashes.Slice(32));
            var verifySuccess = blsVerify.AggregateVerifyHashes(publicKeys, hashes, aggregateSignature, domain1);

            Console.WriteLine("Verify: {0}", verifySuccess);

            verifySuccess.ShouldBeTrue();
        }