Exemple #1
0
        public void TransitionAssetProofParserTest()
        {
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash = BinaryHelper.GetPowHash(1234);
            ushort version = 1;

            byte[] body;
            byte[] transactionPublicKey = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] destinationKey       = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] destinationKey2      = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] keyImage             = BinaryHelper.GetRandomPublicKey();
            byte[] assetCommitment      = ConfidentialAssetsHelper.GetRandomSeed();

            byte[] mask        = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] assetId     = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] assetIssuer = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] payload     = ConfidentialAssetsHelper.GetRandomSeed();

            ushort pubKeysCount = 10;

            byte[][] ownershipAssetCommitments = new byte[pubKeysCount][];
            byte[][] pubKeys = new byte[pubKeysCount][];

            byte[] secretKey      = null;
            ushort secretKeyIndex = 5;

            byte[]   e = ConfidentialAssetsHelper.GetRandomSeed();
            byte[][] s = new byte[pubKeysCount][];


            ushort eligibilityCommitmentsCount = 7;

            byte[][] eligibilityCommitments = new byte[eligibilityCommitmentsCount][];
            byte[]   eligibilityE           = ConfidentialAssetsHelper.GetRandomSeed();
            byte[][] eligibilityS           = new byte[eligibilityCommitmentsCount][];


            for (int i = 0; i < pubKeysCount; i++)
            {
                pubKeys[i] = BinaryHelper.GetRandomPublicKey(out byte[] secretKeyTemp);
                if (i == secretKeyIndex)
                {
                    secretKey = secretKeyTemp;
                }
                ownershipAssetCommitments[i] = ConfidentialAssetsHelper.GetRandomSeed();
                s[i] = ConfidentialAssetsHelper.GetRandomSeed();
            }

            for (int i = 0; i < eligibilityCommitmentsCount; i++)
            {
                eligibilityCommitments[i] = ConfidentialAssetsHelper.GetRandomSeed();
                eligibilityS[i]           = ConfidentialAssetsHelper.GetRandomSeed();
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write(assetCommitment);

                    bw.Write(mask);
                    bw.Write(assetId);
                    bw.Write(assetIssuer);
                    bw.Write(payload);

                    bw.Write(pubKeysCount);
                    for (int i = 0; i < pubKeysCount; i++)
                    {
                        bw.Write(ownershipAssetCommitments[i]);
                    }
                    bw.Write(e);
                    for (int i = 0; i < pubKeysCount; i++)
                    {
                        bw.Write(s[i]);
                    }

                    bw.Write(eligibilityCommitmentsCount);
                    for (int i = 0; i < eligibilityCommitmentsCount; i++)
                    {
                        bw.Write(eligibilityCommitments[i]);
                    }
                    bw.Write(eligibilityE);
                    for (int i = 0; i < eligibilityCommitmentsCount; i++)
                    {
                        bw.Write(eligibilityS[i]);
                    }
                }

                body = ms.ToArray();
            }

            byte[] packet = BinaryHelper.GetUtxoConfidentialPacket(PacketType.UtxoConfidential, syncBlockHeight, nonce, powHash, version,
                                                                   BlockTypes.UtxoConfidential_TransitionOnboardingDisclosingProofs, keyImage, destinationKey, destinationKey2, transactionPublicKey, body, pubKeys, secretKey, secretKeyIndex,
                                                                   out RingSignature[] ringSignatures);
            TransitionOnboardingDisclosingProofsParser parser = new TransitionOnboardingDisclosingProofsParser(_identityKeyProvidersRegistry);
            TransitionOnboardingDisclosingProofs       block  = (TransitionOnboardingDisclosingProofs)parser.Parse(packet);

            Assert.Equal(syncBlockHeight, block.SyncBlockHeight);
            Assert.Equal(nonce, block.Nonce);
            Assert.Equal(powHash, block.PowHash);
            Assert.Equal(version, block.Version);
            Assert.Equal(keyImage, block.KeyImage.Value.ToArray());
            Assert.Equal(destinationKey, block.DestinationKey);
            Assert.Equal(destinationKey2, block.DestinationKey2);
            Assert.Equal(transactionPublicKey, block.TransactionPublicKey);
            Assert.Equal(assetCommitment, block.AssetCommitment);
            Assert.Equal(mask, block.EcdhTuple.Mask);
            Assert.Equal(assetId, block.EcdhTuple.AssetId);
            Assert.Equal(assetIssuer, block.EcdhTuple.AssetIssuer);
            Assert.Equal(payload, block.EcdhTuple.Payload);

            Assert.Equal(pubKeysCount, block.OwnershipProof.AssetCommitments.Length);

            for (int i = 0; i < pubKeysCount; i++)
            {
                Assert.Equal(ownershipAssetCommitments[i], block.OwnershipProof.AssetCommitments[i]);
                Assert.Equal(s[i], block.OwnershipProof.Rs.S[i]);
                Assert.Equal(ringSignatures[i].C, block.Signatures[i].C);
                Assert.Equal(ringSignatures[i].R, block.Signatures[i].R);
            }

            Assert.Equal(e, block.OwnershipProof.Rs.E);

            Assert.Equal(eligibilityCommitmentsCount, block.EligibilityProof.AssetCommitments.Length);

            for (int i = 0; i < eligibilityCommitmentsCount; i++)
            {
                Assert.Equal(eligibilityCommitments[i], block.EligibilityProof.AssetCommitments[i]);
                Assert.Equal(eligibilityS[i], block.EligibilityProof.Rs.S[i]);
            }

            Assert.Equal(eligibilityE, block.EligibilityProof.Rs.E);
        }
Exemple #2
0
        public void TransitionAssetProofSerializerTest()
        {
            ulong syncBlockHeight = 1;
            uint  nonce           = 4;

            byte[] powHash = BinaryHelper.GetPowHash(1234);
            ushort version = 1;

            byte[] body;
            byte[] transactionPublicKey = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] destinationKey       = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] destinationKey2      = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] keyImage             = BinaryHelper.GetRandomPublicKey();
            byte[] assetCommitment      = ConfidentialAssetsHelper.GetRandomSeed();

            byte[] mask        = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] assetId     = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] assetIssuer = ConfidentialAssetsHelper.GetRandomSeed();
            byte[] payload     = ConfidentialAssetsHelper.GetRandomSeed();

            ushort pubKeysCount = 10;

            byte[][] ownershipAssetCommitments = new byte[pubKeysCount][];
            byte[][] pubKeys = new byte[pubKeysCount][];

            byte[] secretKey      = null;
            ushort secretKeyIndex = 5;

            byte[]   e = ConfidentialAssetsHelper.GetRandomSeed();
            byte[][] s = new byte[pubKeysCount][];


            ushort eligibilityCommitmentsCount = 7;

            byte[][] eligibilityCommitments = new byte[eligibilityCommitmentsCount][];
            byte[]   eligibilityE           = ConfidentialAssetsHelper.GetRandomSeed();
            byte[][] eligibilityS           = new byte[eligibilityCommitmentsCount][];


            for (int i = 0; i < pubKeysCount; i++)
            {
                if (i == secretKeyIndex)
                {
                    secretKey  = ConfidentialAssetsHelper.GetOTSK(transactionPublicKey, _privateViewKey, _privateKey);
                    pubKeys[i] = ConfidentialAssetsHelper.GetPublicKey(secretKey);
                    keyImage   = ConfidentialAssetsHelper.GenerateKeyImage(secretKey);
                }
                else
                {
                    pubKeys[i] = BinaryHelper.GetRandomPublicKey(out byte[] secretKeyTemp);
                }

                ownershipAssetCommitments[i] = ConfidentialAssetsHelper.GetRandomSeed();
                s[i] = ConfidentialAssetsHelper.GetRandomSeed();
            }

            for (int i = 0; i < eligibilityCommitmentsCount; i++)
            {
                eligibilityCommitments[i] = ConfidentialAssetsHelper.GetRandomSeed();
                eligibilityS[i]           = ConfidentialAssetsHelper.GetRandomSeed();
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    bw.Write(assetCommitment);

                    bw.Write(mask);
                    bw.Write(assetId);
                    bw.Write(assetIssuer);
                    bw.Write(payload);

                    bw.Write(pubKeysCount);
                    for (int i = 0; i < pubKeysCount; i++)
                    {
                        bw.Write(ownershipAssetCommitments[i]);
                    }
                    bw.Write(e);
                    for (int i = 0; i < pubKeysCount; i++)
                    {
                        bw.Write(s[i]);
                    }

                    bw.Write(eligibilityCommitmentsCount);
                    for (int i = 0; i < eligibilityCommitmentsCount; i++)
                    {
                        bw.Write(eligibilityCommitments[i]);
                    }
                    bw.Write(eligibilityE);
                    for (int i = 0; i < eligibilityCommitmentsCount; i++)
                    {
                        bw.Write(eligibilityS[i]);
                    }
                }

                body = ms.ToArray();
            }

            byte[] expectedPacket = BinaryHelper.GetUtxoConfidentialPacket(PacketType.UtxoConfidential, syncBlockHeight, nonce, powHash, version,
                                                                           BlockTypes.UtxoConfidential_TransitionOnboardingDisclosingProofs, keyImage, destinationKey, destinationKey2, transactionPublicKey, body, pubKeys, secretKey, secretKeyIndex,
                                                                           out RingSignature[] ringSignatures);

            TransitionOnboardingDisclosingProofs block = new TransitionOnboardingDisclosingProofs
            {
                SyncBlockHeight      = syncBlockHeight,
                Nonce                = nonce,
                PowHash              = powHash,
                DestinationKey       = destinationKey,
                DestinationKey2      = destinationKey2,
                KeyImage             = new Key32(keyImage),
                TransactionPublicKey = transactionPublicKey,
                AssetCommitment      = assetCommitment,
                EcdhTuple            = new EcdhTupleProofs
                {
                    Mask        = mask,
                    AssetId     = assetId,
                    AssetIssuer = assetIssuer,
                    Payload     = payload
                },
                OwnershipProof = new SurjectionProof
                {
                    AssetCommitments = ownershipAssetCommitments,
                    Rs = new BorromeanRingSignature
                    {
                        E = e,
                        S = s
                    }
                },
                EligibilityProof = new SurjectionProof
                {
                    AssetCommitments = eligibilityCommitments,
                    Rs = new BorromeanRingSignature
                    {
                        E = eligibilityE,
                        S = eligibilityS
                    }
                }
            };

            TransitionOnboardingDisclosingProofsSerializer serializer = new TransitionOnboardingDisclosingProofsSerializer();

            serializer.Initialize(block);
            serializer.SerializeBody();
            _utxoSigningService.Sign(block, new UtxoSignatureInput(transactionPublicKey, pubKeys, secretKeyIndex));

            byte[] actualPacket = serializer.GetBytes();

            Span <byte> expectedSpan = new Span <byte>(expectedPacket);
            Span <byte> actualSpan   = new Span <byte>(actualPacket);

            Assert.Equal(expectedSpan.Slice(0, expectedPacket.Length - pubKeysCount * 64).ToArray(), actualSpan.Slice(0, actualPacket.Length - pubKeysCount * 64).ToArray());
        }
Exemple #3
0
        protected override Memory <byte> ParseUtxoConfidential(ushort version, Memory <byte> spanBody, out UtxoConfidentialBase utxoConfidentialBase)
        {
            UtxoConfidentialBase block = null;

            if (version == 1)
            {
                int readBytes = 0;

                ReadCommitment(ref spanBody, ref readBytes, out byte[] assetCommitment);
                ReadEcdhTupleProofs(ref spanBody, ref readBytes, out EcdhTupleProofs ecdhTuple);
                ReadSurjectionProof(ref spanBody, ref readBytes, out SurjectionProof ownershipProofs);
                ReadSurjectionProof(ref spanBody, ref readBytes, out SurjectionProof eligibilityProofs);

                byte associatedProofsCount = spanBody.Slice(readBytes++).Span[0];

                AssociatedProofs[] associatedProofs = new AssociatedProofs[associatedProofsCount];

                for (int i = 0; i < associatedProofsCount; i++)
                {
                    byte associatedProofType = spanBody.Slice(readBytes++).Span[0];

                    AssociatedProofs associatedProof;

                    if (associatedProofType == 1)
                    {
                        ReadCommitment(ref spanBody, ref readBytes, out byte[] associatedAssetCommitment);
                        associatedProof = new AssociatedAssetProofs
                        {
                            AssociatedAssetCommitment = associatedAssetCommitment
                        };
                    }
                    else
                    {
                        associatedProof = new AssociatedProofs();
                    }

                    ReadCommitment(ref spanBody, ref readBytes, out byte[] associatedGroupId);
                    ReadSurjectionProof(ref spanBody, ref readBytes, out SurjectionProof associationProofs);
                    ReadSurjectionProof(ref spanBody, ref readBytes, out SurjectionProof rootProofs);

                    associatedProof.AssociatedAssetGroupId = associatedGroupId;
                    associatedProof.AssociationProofs      = associationProofs;
                    associatedProof.RootProofs             = rootProofs;

                    associatedProofs[i] = associatedProof;
                }

                block = new TransitionOnboardingDisclosingProofs
                {
                    AssetCommitment  = assetCommitment,
                    EcdhTuple        = ecdhTuple,
                    OwnershipProof   = ownershipProofs,
                    EligibilityProof = eligibilityProofs,
                    AssociatedProofs = associatedProofs
                };

                utxoConfidentialBase = block;

                return(spanBody.Slice(readBytes));
            }

            throw new BlockVersionNotSupportedException(version, BlockType);
        }