Exemplo n.º 1
0
        public void Can_merkleize_int()
        {
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, 34 + byte.MaxValue + ushort.MaxValue);
            Assert.AreEqual("0x2001010000000000000000000000000000000000000000000000000000000000", root.ToHexString(true));
        }
Exemplo n.º 2
0
        public void Can_merkleize_bitvector()
        {
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, new byte[] { 123 });
            Assert.AreEqual("0x7b00000000000000000000000000000000000000000000000000000000000000", root.ToHexString(true));
        }
Exemplo n.º 3
0
        public void Can_merkleize_byte()
        {
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, (byte)34);
            Assert.AreEqual("0x2200000000000000000000000000000000000000000000000000000000000000", root.ToHexString(true));
        }
Exemplo n.º 4
0
        public void Can_merkleize_uint256_vector_longer()
        {
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, new UInt256[] { 1, 2, 3, 4 });
            Assert.AreEqual("0xbfe3c665d2e561f13b30606c580cb703b2041287e212ade110f0bfd8563e21bb", root.ToHexString(true));
        }
Exemplo n.º 5
0
        public void Can_merkleize_uint128_vector_full()
        {
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, new UInt128[] { 1, 3 });
            Assert.AreEqual("0x0100000000000000000000000000000003000000000000000000000000000000", root.ToHexString(true));
        }
Exemplo n.º 6
0
        public void Can_merkleize_uint128_vector()
        {
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, new UInt128[] { 1, 3, 5 });
            Assert.AreEqual("0xf189891181de961f99a35c1aa21c0d909bf30bb8bebb760050f3d06dc56e488a", root.ToHexString(true));
        }
Exemplo n.º 7
0
        public void Can_merkleize_ulong_vector()
        {
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, new [] { 1UL, 3UL });
            Assert.AreEqual("0x0100000000000000030000000000000000000000000000000000000000000000", root.ToHexString(true));
        }
Exemplo n.º 8
0
        public void Can_merkleize_bool_vector()
        {
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, new [] { true, false });
            Assert.AreEqual("0x0100000000000000000000000000000000000000000000000000000000000000", root.ToHexString(true));
        }
Exemplo n.º 9
0
        public void Can_merkleize_uint256()
        {
            UInt256 input = UInt256.Zero;

            input += 34;
            input += byte.MaxValue;
            input += ushort.MaxValue;
            input += uint.MaxValue;
            input += ulong.MaxValue;
            Span <byte> root = stackalloc byte[32];

            Merkle.Ize(root, input);
            Assert.AreEqual("0x1e01010001000000010000000000000000000000000000000000000000000000", root.ToHexString(true));
        }
Exemplo n.º 10
0
        private static bool RunGenericSszTests(string category)
        {
            bool success = true;

            string[] valid   = Directory.GetDirectories(Path.Combine("generic", category, "valid"));
            string[] invalid = Directory.GetDirectories(Path.Combine("generic", category, "invalid"));

            foreach (string validDir in valid)
            {
                TestContext.Out.WriteLine(validDir);
                string[] files = Directory.GetFiles(validDir);
                (YamlNode valueNode, YamlNodeType valueType) = LoadValue(Path.Combine(validDir, "value.yaml"));
                (YamlNode merkleRootYaml, _) = LoadValue(Path.Combine(validDir, "meta.yaml"));
                UInt256.CreateFromLittleEndian(out UInt256 expectedMerkleRoot, Bytes.FromHexString(((YamlScalarNode)merkleRootYaml["root"]).Value));

                Span <byte> output = null;
                Span <byte> ssz    = File.ReadAllBytes(Path.Combine(validDir, "serialized.ssz"));

                if (valueType == YamlNodeType.Sequence)
                {
                    YamlSequenceNode sequenceNode = (YamlSequenceNode)valueNode;
                    if (validDir.Contains("bool"))
                    {
                        bool[] value        = sequenceNode.Children.Cast <YamlScalarNode>().Select(sn => bool.Parse(sn?.Value ?? "false")).ToArray();
                        bool[] valueFromSsz = Nethermind.Ssz.Ssz.DecodeBools(ssz).ToArray();
                        output = new byte[value.Length];
                        Nethermind.Ssz.Ssz.Encode(output, value);
                        byte[] clone = output.ToArray();
                        Nethermind.Ssz.Ssz.Encode(output, valueFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint256"))
                    {
                        UInt256[] value        = sequenceNode.Children.Cast <YamlScalarNode>().Select(sn => UInt256.Parse(sn.Value)).ToArray();
                        UInt256[] valueFromSsz = Nethermind.Ssz.Ssz.DecodeUInts256(ssz);
                        output = new byte[value.Length * 32];
                        Nethermind.Ssz.Ssz.Encode(output, value);
                        byte[] clone = output.ToArray();
                        Nethermind.Ssz.Ssz.Encode(output, valueFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint128"))
                    {
                        UInt128[] value        = sequenceNode.Children.Cast <YamlScalarNode>().Select(sn => UInt128.Parse(sn.Value)).ToArray();
                        UInt128[] valueFromSsz = Nethermind.Ssz.Ssz.DecodeUInts128(ssz);
                        output = new byte[value.Length * 16];
                        Nethermind.Ssz.Ssz.Encode(output, value);
                        byte[] clone = output.ToArray();
                        Nethermind.Ssz.Ssz.Encode(output, valueFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint64"))
                    {
                        ulong[] value        = sequenceNode.Children.Cast <YamlScalarNode>().Select(sn => ulong.Parse(sn.Value ?? ulong.MinValue.ToString())).ToArray();
                        ulong[] valueFromSsz = Nethermind.Ssz.Ssz.DecodeULongs(ssz).ToArray();
                        output = new byte[value.Length * 8];
                        Nethermind.Ssz.Ssz.Encode(output, value);
                        byte[] clone = output.ToArray();
                        Nethermind.Ssz.Ssz.Encode(output, valueFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint32"))
                    {
                        uint[] value        = sequenceNode.Children.Cast <YamlScalarNode>().Select(sn => uint.Parse(sn.Value ?? uint.MinValue.ToString())).ToArray();
                        uint[] valueFromSsz = Nethermind.Ssz.Ssz.DecodeUInts(ssz).ToArray();
                        output = new byte[value.Length * 4];
                        Nethermind.Ssz.Ssz.Encode(output, value);
                        byte[] clone = output.ToArray();
                        Nethermind.Ssz.Ssz.Encode(output, valueFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint16"))
                    {
                        ushort[] value        = sequenceNode.Children.Cast <YamlScalarNode>().Select(sn => ushort.Parse(sn.Value ?? ushort.MinValue.ToString())).ToArray();
                        ushort[] valueFromSsz = Nethermind.Ssz.Ssz.DecodeUShorts(ssz).ToArray();
                        output = new byte[value.Length * 2];
                        Nethermind.Ssz.Ssz.Encode(output, value);
                        byte[] clone = output.ToArray();
                        Nethermind.Ssz.Ssz.Encode(output, valueFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint8"))
                    {
                        byte[] value        = sequenceNode.Children.Cast <YamlScalarNode>().Select(sn => byte.Parse(sn.Value ?? byte.MinValue.ToString())).ToArray();
                        byte[] valueFromSsz = Nethermind.Ssz.Ssz.DecodeBytes(ssz).ToArray();
                        output = new byte[value.Length];
                        Nethermind.Ssz.Ssz.Encode(output, value);
                        byte[] clone = output.ToArray();
                        Nethermind.Ssz.Ssz.Encode(output, valueFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                }
                else if (valueType == YamlNodeType.Scalar)
                {
                    if (validDir.Contains("bitvec") || validDir.Contains("bitlist"))
                    {
                        uint  limit = 0;
                        Match match = Regex.Match(validDir, "bitlist_(\\d+)", RegexOptions.IgnoreCase);
                        if (match.Success)
                        {
                            limit = (uint.Parse(match.Groups[1].Value) + 255) / 256;
                        }

                        byte[] value        = Bytes.FromHexString(((YamlScalarNode)valueNode).Value);
                        byte[] valueFromSsz = Nethermind.Ssz.Ssz.DecodeBytes(ssz).ToArray();
                        output = new byte[value.Length];
                        Nethermind.Ssz.Ssz.Encode(output, value);
                        byte[] clone = output.ToArray();
                        Nethermind.Ssz.Ssz.Encode(output, valueFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");


                        UInt256 root;
                        if (validDir.Contains("bitvec"))
                        {
                            Merkle.Ize(out root, valueFromSsz);
                        }
                        else
                        {
                            Merkle.IzeBits(out root, valueFromSsz, limit);
                        }

                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("boolean"))
                    {
                        string yamlValue    = ((YamlScalarNode)valueNode)?.Value;
                        bool?  value        = yamlValue is null ? null : (bool?)bool.Parse(yamlValue);
                        bool?  valueFromSsz = Nethermind.Ssz.Ssz.DecodeBool(ssz);
                        Assert.AreEqual(value, valueFromSsz);
                        output    = new byte[1];
                        output[0] = Nethermind.Ssz.Ssz.Encode(value ?? false);

                        Merkle.Ize(out UInt256 root, valueFromSsz ?? false);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint_256"))
                    {
                        UInt256 value        = UInt256.Parse(((YamlScalarNode)valueNode).Value);
                        UInt256 valueFromSsz = Nethermind.Ssz.Ssz.DecodeUInt256(ssz);
                        Assert.AreEqual(value, valueFromSsz);

                        output = new byte[32];
                        Nethermind.Ssz.Ssz.Encode(output, value);

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint_128"))
                    {
                        UInt128 value        = UInt128.Parse(((YamlScalarNode)valueNode).Value);
                        UInt128 valueFromSsz = Nethermind.Ssz.Ssz.DecodeUInt128(ssz);
                        Assert.AreEqual(value, valueFromSsz);

                        output = new byte[16];
                        Nethermind.Ssz.Ssz.Encode(output, value);

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint_64"))
                    {
                        ulong value        = ulong.Parse(((YamlScalarNode)valueNode).Value);
                        ulong valueFromSsz = Nethermind.Ssz.Ssz.DecodeULong(ssz);
                        Assert.AreEqual(value, valueFromSsz);

                        output = new byte[8];
                        Nethermind.Ssz.Ssz.Encode(output, value);

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint_32"))
                    {
                        uint value        = uint.Parse(((YamlScalarNode)valueNode).Value);
                        uint valueFromSsz = Nethermind.Ssz.Ssz.DecodeUInt(ssz);
                        Assert.AreEqual(value, valueFromSsz);

                        output = new byte[4];
                        Nethermind.Ssz.Ssz.Encode(output, value);

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint_16"))
                    {
                        ushort value        = ushort.Parse(((YamlScalarNode)valueNode).Value);
                        ushort valueFromSsz = Nethermind.Ssz.Ssz.DecodeUShort(ssz);
                        Assert.AreEqual(value, valueFromSsz);

                        output = new byte[2];
                        Nethermind.Ssz.Ssz.Encode(output, value);

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                    else if (validDir.Contains("uint_8"))
                    {
                        byte value        = byte.Parse(((YamlScalarNode)valueNode).Value);
                        byte valueFromSsz = Nethermind.Ssz.Ssz.DecodeByte(ssz);
                        Assert.AreEqual(value, valueFromSsz);

                        output = new byte[1];
                        Nethermind.Ssz.Ssz.Encode(output, value);

                        Merkle.Ize(out UInt256 root, valueFromSsz);
                        Assert.AreEqual(expectedMerkleRoot, root);
                    }
                }
                else if (valueType == YamlNodeType.Mapping)
                {
                    var mappingNode = (YamlMappingNode)valueNode;
                    if (validDir.Contains("BitsStruct"))
                    {
                        BitsStruct testStruct        = ParseBitsStruct(mappingNode);
                        BitsStruct testStructFromSsz = DecodeBitsStruct(ssz);
                        output = new byte[13];
                        Encode(output, testStruct);
                        byte[] clone = output.ToArray();
                        Encode(output, testStructFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");
                    }
                    else if (validDir.Contains("SmallTestStruct"))
                    {
                        SmallTestStruct testStruct        = ParseSmallTestStruct(mappingNode);
                        SmallTestStruct testStructFromSsz = DecodeSmallTestStruct(ssz);
                        output = new byte[4];
                        Encode(output, testStruct);
                        byte[] clone = output.ToArray();
                        Encode(output, testStructFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");
                    }
                    else if (validDir.Contains("SingleFieldTestStruct"))
                    {
                        SingleFieldTestStruct testStruct        = new SingleFieldTestStruct();
                        SingleFieldTestStruct testStructFromSsz = DecodeSingleFieldTestStruct(ssz);
                        testStruct.A = byte.Parse(((YamlScalarNode)mappingNode.Children["A"]).Value);
                        output       = new byte[1];
                        Encode(output, testStruct);
                        byte[] clone = output.ToArray();
                        Encode(output, testStructFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");
                    }
                    else if (validDir.Contains("VarTestStruct"))
                    {
                        VarTestStruct testStruct        = ParseVarTestStruct(mappingNode);
                        VarTestStruct testStructFromSsz = DecodeVarTestStruct(ssz);
                        output = new byte[7 + testStruct.B.Length * 2];
                        Encode(output, testStruct);
                        byte[] clone = output.ToArray();
                        Encode(output, testStructFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");
                    }
                    else if (validDir.Contains("FixedTestStruct"))
                    {
                        FixedTestStruct testStruct        = ParseFixedTestStruct(mappingNode);
                        FixedTestStruct testStructFromSsz = DecodeFixTestStruct(ssz);
                        output = new byte[13];
                        Encode(output, testStruct);
                        byte[] clone = output.ToArray();
                        Encode(output, testStructFromSsz);
                        Assert.AreEqual(clone.ToHexString(), output.ToHexString(), "ssz");
                    }
                    else if (validDir.Contains("ComplexTestStruct"))
                    {
                        ComplexTestStruct testStruct = ParseComplexTestStruct(mappingNode);
                        output = new byte[8236];
                        Encode(ref output, testStruct);
                    }
                }

                if (ssz.ToHexString() != output.ToHexString())
                {
                    TestContext.Out.WriteLine($"  expected {ssz.ToHexString()}");
                    TestContext.Out.WriteLine($"  actual   {output.ToHexString()}");
                    success = false;
                }
            }

            return(success);
        }