Beispiel #1
0
        public static IEnumerable <object[]> GetBitvectorData()
        {
            yield return(new object[] {
                new[] { true, true, false, true, false, true, false, false },
                "2b",
                HashUtility.Chunk(new byte[] { 0x2b }).ToArray()
            });

            yield return(new object[] {
                new[] { false, true, false, true },
                "0a",
                HashUtility.Chunk(new byte[] { 0x0a }).ToArray()
            });

            yield return(new object[] {
                new[] { false, true, false },
                "02",
                HashUtility.Chunk(new byte[] { 0x02 }).ToArray()
            });

            yield return(new object[] {
                new[] { true, false, true, false, false, false, true, true, false, true },
                "c502",
                HashUtility.Chunk(new byte[] { 0xc5, 0x02 }).ToArray()
            });

            yield return(new object[] {
                new[] { true, false, true, false, false, false, true, true,
                        false, true, false, false, false, false, true, true },
                "c5c2",
                HashUtility.Chunk(new byte[] { 0xc5, 0xc2 }).ToArray()
            });

            yield return(new object[] {
                Enumerable.Repeat(true, 512).ToArray(),
                new string('f', 64 * 2),
                HashUtility.Hash(
                    Enumerable.Repeat((byte)0xff, 32).ToArray(),
                    Enumerable.Repeat((byte)0xff, 32).ToArray()
                    )
            });

            yield return(new object[] {
                Enumerable.Repeat(true, 513).ToArray(),
                new string('f', 64 * 2) + "01",
                HashUtility.Hash(
                    HashUtility.Hash(
                        Enumerable.Repeat((byte)0xff, 32).ToArray(),
                        Enumerable.Repeat((byte)0xff, 32).ToArray()
                        ),
                    HashUtility.Hash(
                        HashUtility.Chunk(new byte[] { 0x01 }).ToArray(),
                        new byte[32]
                        )
                    )
            });
        }
Beispiel #2
0
        public void Can_merkleize_attestion_data()
        {
            // arrange
            AttestationData attestationData = new AttestationData(
                Slot.One,
                new CommitteeIndex(2),
                new Root(Enumerable.Repeat((byte)0x12, 32).ToArray()),
                new Checkpoint(
                    new Epoch(3),
                    new Root(Enumerable.Repeat((byte)0x34, 32).ToArray())
                    ),
                new Checkpoint(
                    new Epoch(4),
                    new Root(Enumerable.Repeat((byte)0x56, 32).ToArray())
                    )
                );

            // act
            Merkleizer merklezier = new Merkleizer(0);

            merklezier.Feed(attestationData);
            UInt256     root  = merklezier.CalculateRoot();
            Span <byte> bytes = MemoryMarshal.Cast <UInt256, byte>(MemoryMarshal.CreateSpan(ref root, 1));

            // assert
            byte[] expected = HashUtility.Hash(
                HashUtility.Hash(
                    HashUtility.Hash(
                        HashUtility.Chunk(new byte[] { 0x01 }), // slot
                        HashUtility.Chunk(new byte[] { 0x02 })  // committee
                        ),
                    HashUtility.Hash(
                        Enumerable.Repeat((byte)0x12, 32).ToArray(), // beacon block root
                        HashUtility.Hash(                            // source
                            HashUtility.Chunk(new byte[] { 0x03 }),
                            Enumerable.Repeat((byte)0x34, 32).ToArray()
                            )
                        )
                    ),
                HashUtility.Merge(
                    HashUtility.Hash( // target
                        HashUtility.Chunk(new byte[] { 0x04 }),
                        Enumerable.Repeat((byte)0x56, 32).ToArray()
                        ),
                    HashUtility.ZeroHashes(0, 2)
                    )
                ).ToArray();
            bytes.ToArray().ShouldBe(expected);
        }
Beispiel #3
0
        public void Can_merkleize_epoch_1()
        {
            // arrange
            Epoch epoch = Epoch.One;

            // act
            Merkleizer merklezier = new Merkleizer(0);

            merklezier.Feed(epoch);
            UInt256     root  = merklezier.CalculateRoot();
            Span <byte> bytes = MemoryMarshal.Cast <UInt256, byte>(MemoryMarshal.CreateSpan(ref root, 1));

            // assert
            byte[] expected = HashUtility.Chunk(new byte[] { 0x1 }).ToArray();
            bytes.ToArray().ShouldBe(expected);
        }
Beispiel #4
0
        public void Can_merkleize_deposit_data()
        {
            // arrange
            DepositData depositData = new DepositData(
                new BlsPublicKey(Enumerable.Repeat((byte)0x12, BlsPublicKey.Length).ToArray()),
                new Bytes32(Enumerable.Repeat((byte)0x34, Bytes32.Length).ToArray()),
                new Gwei(5),
                new BlsSignature(Enumerable.Repeat((byte)0x67, BlsSignature.Length).ToArray())
                );

            // act
            Merkle.Ize(out UInt256 root, depositData);
            Span <byte> bytes = MemoryMarshal.Cast <UInt256, byte>(MemoryMarshal.CreateSpan(ref root, 1));

            // assert
            byte[] expected = HashUtility.Hash(
                HashUtility.Hash(
                    HashUtility.Hash( // public key
                        Enumerable.Repeat((byte)0x12, 32).ToArray(),
                        HashUtility.Chunk(Enumerable.Repeat((byte)0x12, 16).ToArray())
                        ),
                    Enumerable.Repeat((byte)0x34, Bytes32.Length).ToArray()  // withdrawal credentials
                    ),
                HashUtility.Hash(
                    HashUtility.Chunk(new byte[] { 0x05 }), // amount
                    HashUtility.Hash(                       // signature
                        HashUtility.Hash(
                            Enumerable.Repeat((byte)0x67, 32).ToArray(),
                            Enumerable.Repeat((byte)0x67, 32).ToArray()
                            ),
                        HashUtility.Hash(
                            Enumerable.Repeat((byte)0x67, 32).ToArray(),
                            Enumerable.Repeat((byte)0x00, 32).ToArray()
                            )
                        )
                    )
                );

            TestContext.WriteLine("root: {0:x}", root);
            TestContext.WriteLine("bytes: {0}", bytes.ToHexString(true));
            TestContext.WriteLine("expected: {0}", expected.ToHexString(true));

            bytes.ToArray().ShouldBe(expected);
        }
Beispiel #5
0
        public void Can_merkleize_checkpoint()
        {
            // arrange
            Checkpoint checkpoint = new Checkpoint(
                new Epoch(3),
                new Root(Enumerable.Repeat((byte)0x34, 32).ToArray())
                );

            // act
            Merkle.Ize(out UInt256 root, checkpoint);
            Span <byte> bytes = MemoryMarshal.Cast <UInt256, byte>(MemoryMarshal.CreateSpan(ref root, 1));

            // assert
            byte[] expected = HashUtility.Hash(
                HashUtility.Chunk(new byte[] { 0x03 }),
                Enumerable.Repeat((byte)0x34, 32).ToArray()
                ).ToArray();
            bytes.ToArray().ShouldBe(expected);
        }
Beispiel #6
0
        public static IEnumerable <object[]> GetBitlistData()
        {
            yield return(new object[] {
                new[] { true, true, false, true, false, true, false, false },
                (ulong)8,
                "2b01",
                HashUtility.Hash(
                    HashUtility.Chunk(new byte[] { 0x2b }).ToArray(),
                    HashUtility.Chunk(new byte[] { 0x08 }).ToArray()
                    )
            });

            yield return(new object[] {
                new[] { false, true, false, true },
                (ulong)4,
                "1a",
                HashUtility.Hash(
                    HashUtility.Chunk(new byte[] { 0x0a }).ToArray(),
                    HashUtility.Chunk(new byte[] { 0x04 }).ToArray()
                    )
            });

            yield return(new object[] {
                new[] { false, true, false },
                (ulong)3,
                "0a",
                HashUtility.Hash(
                    HashUtility.Chunk(new byte[] { 0x02 }).ToArray(),
                    HashUtility.Chunk(new byte[] { 0x03 }).ToArray()
                    )
            });

            yield return(new object[] {
                new[] { true, false, true, false, false, false, true, true, false, true },
                (ulong)16,
                "c506",
                HashUtility.Hash(
                    HashUtility.Chunk(new byte[] { 0xc5, 0x02 }).ToArray(),
                    HashUtility.Chunk(new byte[] { 0x0a }).ToArray()
                    )
            });

            yield return(new object[] {
                new[] { true, false, true, false, false, false, true, true,
                        false, true, false, false, false, false, true, true },
                (ulong)16,
                "c5c201",
                HashUtility.Hash(
                    HashUtility.Chunk(new byte[] { 0xc5, 0xc2 }).ToArray(),
                    HashUtility.Chunk(new byte[] { 0x10 }).ToArray()
                    )
            });

            yield return(new object[] {
                new[] { true },
                (ulong)512,
                "03",
                HashUtility.Hash(
                    HashUtility.Hash(
                        HashUtility.Chunk(new byte[] { 0x01 }).ToArray(),
                        new byte[32]
                        ),
                    HashUtility.Chunk(new byte[] { 0x01 }).ToArray()
                    )
            });

            yield return(new object[] {
                Enumerable.Repeat(true, 512).ToArray(),
                (ulong)512,
                new string('f', 64 * 2) + "01",
                HashUtility.Hash(
                    HashUtility.Hash(
                        Enumerable.Repeat((byte)0xff, 32).ToArray(),
                        Enumerable.Repeat((byte)0xff, 32).ToArray()
                        ),
                    HashUtility.Chunk(new byte[] { 0x00, 0x02 }).ToArray()
                    )
            });

            yield return(new object[] {
                Enumerable.Repeat(true, 513).ToArray(),
                (ulong)513,
                new string('f', 64 * 2) + "03",
                HashUtility.Hash(
                    HashUtility.Hash(
                        HashUtility.Hash(
                            Enumerable.Repeat((byte)0xff, 32).ToArray(),
                            Enumerable.Repeat((byte)0xff, 32).ToArray()
                            ),
                        HashUtility.Hash(
                            HashUtility.Chunk(new byte[] { 0x01 }).ToArray(),
                            new byte[32]
                            )
                        ),
                    HashUtility.Chunk(new byte[] { 0x01, 0x02 }).ToArray()
                    )
            });
        }