예제 #1
0
        public void VerifyThrowsIfInvalidSignature()
        {
            var blsKeyPair = BlsKeyPair.GenerateG2();
            var bbsKeyPair = blsKeyPair.GetBbsKey(1);

            Assert.Throws <BbsException>(() => Service.Verify(new VerifyRequest(blsKeyPair, Array.Empty <byte>(), new[] { "message_0" })), "Signature cannot be empty array");
        }
예제 #2
0
        public void CreateBbsKeyFromBlsSecretKey()
        {
            var secretKey = BlsKeyPair.GenerateG2();
            var publicKey = secretKey.GetBbsKey(1);

            Assert.NotNull(publicKey);
            Assert.AreEqual(196, publicKey.PublicKey.Length);
        }
예제 #3
0
 public static void TransientKeyPair(BlsKeyPair keyPair)
 {
     Assert.IsNotNull(keyPair.PK);
     Assert.IsNotNull(keyPair.SK);
     Assert.IsNotEmpty(keyPair.PK);
     Assert.IsNotEmpty(keyPair.SK);
     Assert.AreNotSame(keyPair.PK, keyPair.SK);
 }
예제 #4
0
        public void SignSingleMessageUsingApi()
        {
            var myKey = BlsKeyPair.GenerateG2();

            var signature = Service.Sign(new SignRequest(myKey, new[] { "message" }));

            Assert.NotNull(signature);
            Assert.AreEqual(signature.Length, NativeMethods.bbs_signature_size());
        }
예제 #5
0
        public void SignMultipleeMessages()
        {
            var keyPair = BlsKeyPair.GenerateG2();

            var signature = Service.Sign(new SignRequest(keyPair, new[] { "message_1", "message_2" }));

            Assert.NotNull(signature);
            Assert.AreEqual(BbsSignatureService.SignatureSize, signature.Length);
        }
예제 #6
0
        public static async Task PersistedKeyPair(string xorUrl, BlsKeyPair keyPair, Keys api)
        {
            Validate.TransientKeyPair(keyPair);
            await Validate.XorUrlAsync(xorUrl, DataType.SafeKey, ContentType.Raw, 0);

            var publicKey = await api.ValidateSkForUrlAsync(keyPair.SK, xorUrl);

            Assert.AreEqual(keyPair.PK, publicKey);
        }
예제 #7
0
        public void SignAndVerifyDifferentKeys()
        {
            var keyPair  = BlsKeyPair.GenerateG2();
            var messages = new[] { "message_1", "message_2" };

            var signature = Service.Sign(new SignRequest(keyPair, messages));

            var result = Service.Verify(new VerifyRequest(keyPair, signature, messages));

            Assert.True(result);
        }
예제 #8
0
        public void GenerateBlsKeyWithoutSeed()
        {
            var blsKeyPair = BlsKeyPair.GenerateG2();
            var dPublicKey = blsKeyPair.PublicKey;

            Assert.NotNull(blsKeyPair);
            Assert.NotNull(dPublicKey);
            Assert.NotNull(blsKeyPair.SecretKey);

            Assert.AreEqual(96, dPublicKey.Length);
            Assert.AreEqual(32, blsKeyPair.SecretKey.Length);
        }
예제 #9
0
        public void GenerateG1KeyWithSeed()
        {
            var seed = "my seed";

            var actual = BlsKeyPair.GenerateG1(seed);

            Assert.NotNull(actual);
            Assert.NotNull(actual.SecretKey);
            Assert.NotNull(actual.PublicKey);
            Assert.True(actual.IsG1());
            Assert.False(actual.IsG2());
            Assert.AreEqual(BlsKeyPair.SecretKeySize, actual.SecretKey.Length);
            Assert.AreEqual(BlsKeyPair.PublicKeyG1Size, actual.PublicKey.Length);
        }
예제 #10
0
        public void CreateBbsKeyFromBlsPublicKey()
        {
            var blsKeypair = BlsKeyPair.GenerateG2();
            var bbsKeyPair = new BlsKeyPair(blsKeypair.PublicKey.ToArray());

            Assert.IsNull(bbsKeyPair.SecretKey);

            var publicKey = bbsKeyPair.GetBbsKey(1);

            Assert.NotNull(publicKey);
            Assert.NotNull(bbsKeyPair.PublicKey);
            Assert.IsNull(bbsKeyPair.SecretKey);

            Assert.AreEqual(196, publicKey.PublicKey.Length);
            Assert.AreEqual(32, blsKeypair.SecretKey.Length);
        }
        public void BlindCommitmentSingleMessageUsingApi()
        {
            var myKey     = BlsKeyPair.GenerateG2();
            var publicKey = myKey.GetBbsKey(1);

            var commitment = Service.CreateBlindedCommitment(new CreateBlindedCommitmentRequest(
                                                                 publicKey: publicKey,
                                                                 messages: new[] { new IndexedMessage {
                                                                                       Index = 0, Message = "message_0"
                                                                                   } },
                                                                 nonce: new byte[] { 1, 2, 3 }));

            Assert.NotNull(commitment);
            Assert.NotNull(commitment.BlindingFactor);
            Assert.NotNull(commitment.BlindSignContext);
            Assert.NotNull(commitment.Commitment);
        }
        protected override Task VerifyAsync(IVerifyData payload, JToken proof, JToken verificationMethod, ProofOptions options)
        {
            var verifyData = payload as StringArray ?? throw new ArgumentException("Invalid data type");

            var blsVerificationMethod = new Bls12381G2Key2020(verificationMethod as JObject);

            var key       = new BlsKeyPair(Multibase.Base58.Decode(blsVerificationMethod.PublicKeyBase58));
            var signature = Helpers.FromBase64String(proof["proofValue"]?.ToString() ?? throw new Exception("Proof value not found"));

            var valid = SignatureService.Verify(new VerifyRequest(key, signature, verifyData.Data));

            if (!valid)
            {
                throw new Exception("Invalid signature");
            }
            return(Task.CompletedTask);
        }
예제 #13
0
        public void CreateBbsKeyFromBlsPublicKey()
        {
            var blsKeypair = BbsProvider.GenerateBlsKey();
            var bbsKeyPair = new BlsKeyPair(blsKeypair.PublicKey.ToArray());

            Assert.IsNull(bbsKeyPair.SecretKey);

            var publicKey = bbsKeyPair.GeyBbsKeyPair(1);

            Assert.NotNull(blsKeypair.SecretKey);
            Assert.NotNull(publicKey);
            Assert.NotNull(bbsKeyPair.PublicKey);
            Assert.IsNull(bbsKeyPair.SecretKey);

            Assert.AreEqual(196, publicKey.PublicKey.Count);
            Assert.AreEqual(32, blsKeypair.SecretKey.Count);
        }
예제 #14
0
        public async Task SignVerifiableCredential()
        {
            var keyPair  = BlsKeyPair.GenerateG2();
            var document = Utilities.LoadJson("Data/test_vc.json");

            var signedDocument = await LdSignatures.SignAsync(document, new ProofOptions
            {
                Suite = new BbsBlsSignature2020
                {
                    KeyPair            = keyPair,
                    VerificationMethod = "did:example:alice#key-1"
                },
                Purpose = new AssertionMethodPurpose()
            });

            signedDocument.Should().NotBeNull();
            signedDocument["proof"].Should().NotBeNull();
            signedDocument["proof"]["proofValue"].Should().NotBeNull();
        }
        public static Bls12381VerificationKey2020 ToVerificationMethod(this BlsKeyPair keyPair, string id = null, string controller = null)
        {
            var method = new Bls12381VerificationKey2020
            {
                PublicKeyBase58  = Multibase.Base58.Encode(keyPair.PublicKey.ToArray()),
                PrivateKeyBase58 = keyPair.SecretKey is null ? null : Multibase.Base58.Encode(keyPair.SecretKey.ToArray())
            };

            if (id != null)
            {
                method.Id = id;
            }
            if (controller != null)
            {
                method.Controller = controller;
            }

            return(method);
        }
    }
        public void BlindSignSingleMessageUsingApi()
        {
            var myKey     = BlsKeyPair.GenerateG2();
            var publicKey = myKey.GetBbsKey(2);

            var messages = new[]
            {
                new IndexedMessage {
                    Index = 0, Message = "message_0"
                },
                new IndexedMessage {
                    Index = 1, Message = "message_1"
                }
            };
            var nonce = new byte[] { 1, 2, 3 };

            var commitment = Service.CreateBlindedCommitment(new CreateBlindedCommitmentRequest(publicKey, messages, nonce));

            var blindSign = Service.BlindSign(new BlindSignRequest(myKey, publicKey, commitment.Commitment.ToArray(), messages));

            Assert.NotNull(blindSign);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SignRequest"/> class.
 /// </summary>
 /// <param name="keyPair">BLS12-381 key pair</param>
 /// <param name="messages">Messages to sign</param>
 public SignRequest(BlsKeyPair keyPair, string[] messages)
 {
     KeyPair  = keyPair;
     Messages = messages;
 }
        public void FullDemoTest()
        {
            var key       = BlsKeyPair.GenerateG2();
            var publicKey = key.GetBbsKey(5);

            var nonce    = new byte[] { 1, 2, 3 };
            var messages = new[]
            {
                "message_1",
                "message_2",
                "message_3",
                "message_4",
                "message_5"
            };

            {
                // Sign messages
                var signature = Service.Sign(new SignRequest(key, messages));

                Assert.NotNull(signature);
                Assert.AreEqual(BbsSignatureService.SignatureSize, signature.Length);

                // Verify messages
                var verifySignatureResult = Service.Verify(new VerifyRequest(key, signature, messages));

                Assert.True(verifySignatureResult);

                // Create proof
                var proofMessages1 = new[]
                {
                    new ProofMessage {
                        Message = messages[0], ProofType = ProofMessageType.Revealed
                    },
                    new ProofMessage {
                        Message = messages[1], ProofType = ProofMessageType.HiddenProofSpecificBlinding
                    },
                    new ProofMessage {
                        Message = messages[2], ProofType = ProofMessageType.Revealed
                    },
                    new ProofMessage {
                        Message = messages[3], ProofType = ProofMessageType.Revealed
                    },
                    new ProofMessage {
                        Message = messages[4], ProofType = ProofMessageType.HiddenProofSpecificBlinding
                    }
                };

                var proofResult = Service.CreateProof(new CreateProofRequest(publicKey, proofMessages1, signature, null, nonce));

                Assert.NotNull(proofResult);

                // Verify proof of revealed messages
                var verifyResult1 = Service.VerifyProof(new VerifyProofRequest(publicKey, proofResult, proofMessages1.Where(x => x.ProofType == ProofMessageType.Revealed).Select(x => x.Message).ToArray(), nonce));

                Assert.IsTrue(verifyResult1);
            }

            // Create blinded commitment
            var blindedMessages = new[]
            {
                new IndexedMessage {
                    Index = 0, Message = messages[0]
                }
            };
            var commitment = Service.CreateBlindedCommitment(new CreateBlindedCommitmentRequest(publicKey, blindedMessages, nonce));

            Assert.NotNull(commitment);

            // Verify blinded commitment
            var verifyResult = Service.VerifyBlindedCommitment(new VerifyBlindedCommitmentRequest(publicKey, commitment.BlindSignContext.ToArray(), new [] { 0u }, nonce));

            Assert.IsTrue(verifyResult);

            // Blind sign
            var messagesToSign = new[]
            {
                new IndexedMessage {
                    Index = 1, Message = messages[1]
                },
                new IndexedMessage {
                    Index = 2, Message = messages[2]
                },
                new IndexedMessage {
                    Index = 3, Message = messages[3]
                },
                new IndexedMessage {
                    Index = 4, Message = messages[4]
                }
            };
            var blindedSignature = Service.BlindSign(new BlindSignRequest(key, publicKey, commitment.Commitment.ToArray(), messagesToSign));

            Assert.NotNull(blindedSignature);
            Assert.AreEqual(BbsSignatureService.BlindSignatureSize, blindedSignature.Length);

            // Unblind signature
            var unblindedSignature = Service.UnblindSignature(new UnblindSignatureRequest(blindedSignature, commitment.BlindingFactor.ToArray()));

            Assert.NotNull(unblindedSignature);
            Assert.AreEqual(BbsSignatureService.SignatureSize, unblindedSignature.Length);

            // Verify signature
            var verifyUnblindedSignatureResult = Service.Verify(new VerifyRequest(key, unblindedSignature, messages));

            Assert.True(verifyUnblindedSignatureResult);

            // Create proof
            var proofMessages = new[]
            {
                new ProofMessage {
                    Message = messages[0], ProofType = ProofMessageType.Revealed
                },
                new ProofMessage {
                    Message = messages[1], ProofType = ProofMessageType.Revealed
                },
                new ProofMessage {
                    Message = messages[2], ProofType = ProofMessageType.HiddenExternalBlinding
                },
                new ProofMessage {
                    Message = messages[3], ProofType = ProofMessageType.HiddenExternalBlinding
                },
                new ProofMessage {
                    Message = messages[4], ProofType = ProofMessageType.HiddenExternalBlinding
                }
            };

            var proof = Service.CreateProof(new CreateProofRequest(
                                                publicKey: publicKey,
                                                messages: proofMessages,
                                                signature: unblindedSignature,
                                                blindingFactor: commitment.BlindingFactor.ToArray(),
                                                nonce: nonce));

            Assert.NotNull(proof);
            Assert.True(proof.Length > 0);

            // Verify proof
            var verifyProofMessages = proofMessages
                                      .Where(x => x.ProofType == ProofMessageType.Revealed)
                                      .Select(x => x.Message)
                                      .ToArray();
            var verifyProofResult = Service.VerifyProof(new VerifyProofRequest(publicKey, proof, verifyProofMessages, nonce));

            Assert.IsTrue(verifyProofResult);

            var messageCount = Service.GetTotalMessageCount(proof);

            Assert.AreEqual(5, messageCount);
        }