Beispiel #1
0
        public void InfinityDeserialization(string infinityHex)
        {
            var infinity = GroupElement.FromBytes(ByteHelpers.FromHex(infinityHex));

            Assert.True(infinity.IsInfinity);
            Assert.Equal(GroupElement.Infinity, infinity);
        }
Beispiel #2
0
 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
     if (reader.Value is string serialized)
     {
         return(GroupElement.FromBytes(ByteHelpers.FromHex(serialized)));
     }
     throw new ArgumentException($"No valid serialized {nameof(GroupElement)} passed.");
 }
Beispiel #3
0
        public void PointOutOfCurveDeserialization()
        {
            var serialized = FillByteArray(length: 33, character: 0);

            serialized[0] = GE.SECP256K1_TAG_PUBKEY_EVEN;
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(serialized));

            serialized[0] = GE.SECP256K1_TAG_PUBKEY_ODD;
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(serialized));
        }
Beispiel #4
0
        public void EvenOddSerialization()
        {
            var hexG    = "0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798";
            var hexGodd = "0379BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798";

            Assert.Equal(hexG, ByteHelpers.ToHex(Generators.G.ToBytes()));
            Assert.Equal(Generators.G, GroupElement.FromBytes(ByteHelpers.FromHex(hexG)));

            Assert.Equal(hexGodd, ByteHelpers.ToHex(Generators.G.Negate().ToBytes()));
            Assert.Equal(Generators.G.Negate(), GroupElement.FromBytes(ByteHelpers.FromHex(hexGodd)));
        }
Beispiel #5
0
        public void Serialization()
        {
            var ge  = Generators.G;
            var ge2 = GroupElement.FromBytes(ge.ToBytes());

            Assert.Equal(ge, ge2);

            ge  = GroupElement.Infinity;
            ge2 = GroupElement.FromBytes(ge.ToBytes());
            Assert.Equal(ge, ge2);

            // Make sure infinity definition isn't f*d up in the constructor as the frombytes relies on special zero coordinates for infinity.
            // 1. Try defining infinity with non-zero coordinates should work:
            ge = new GroupElement(new GE(EC.G.x, EC.G.y, infinity: true));
            byte[] zeroBytes = ge.ToBytes();
            ge2 = GroupElement.FromBytes(zeroBytes);
            Assert.Equal(GroupElement.Infinity, ge2);

            // 2. Try defining non-infinity with zero coordinates should not work.
            Assert.ThrowsAny <ArgumentException>(() => new GroupElement(new GE(FE.Zero, FE.Zero, infinity: false)));

            ge  = new GroupElement(EC.G) * new Scalar(1);
            ge2 = GroupElement.FromBytes(ge.ToBytes());
            Assert.Equal(ge, ge2);

            ge  = new GroupElement(EC.G) * new Scalar(2);
            ge2 = GroupElement.FromBytes(ge.ToBytes());
            Assert.Equal(ge, ge2);

            ge  = new GroupElement(EC.G) * new Scalar(3);
            ge2 = GroupElement.FromBytes(ge.ToBytes());
            Assert.Equal(ge, ge2);

            ge  = new GroupElement(EC.G) * new Scalar(21);
            ge2 = GroupElement.FromBytes(ge.ToBytes());
            Assert.Equal(ge, ge2);

            ge  = new GroupElement(EC.G) * EC.NC;
            ge2 = GroupElement.FromBytes(ge.ToBytes());
            Assert.Equal(ge, ge2);

            ge  = new GroupElement(EC.G) * EC.N;
            ge2 = GroupElement.FromBytes(ge.ToBytes());
            Assert.Equal(ge, ge2);
        }
Beispiel #6
0
        public void DeserializationThrows()
        {
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(Array.Empty <byte>()));
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(new byte[] { 0 }));
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(new byte[] { 1 }));
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(new byte[] { 0, 1 }));
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(new byte[] { 0, 1 }));

            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(FillByteArray(length: 32, character: 0)));
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(FillByteArray(length: 63, character: 0)));
            var infinity = GroupElement.FromBytes(FillByteArray(length: 33, character: 0));

            Assert.True(infinity.IsInfinity);
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(FillByteArray(length: 64, character: 1)));
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(FillByteArray(length: 64, character: 2)));
            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(FillByteArray(length: 65, character: 0)));

            Assert.ThrowsAny <ArgumentException>(() => GroupElement.FromBytes(ByteHelpers.FromHex("100000000000000000000000000000000000000000000000000000000000000000")));
        }
Beispiel #7
0
        public void NonNormalizedPointDeserialization()
        {
            var p = FE.CONST(
                0xFFFFFC2FU,
                0xFFFFFFFFU,
                0xFFFFFFFFU,
                0xFFFFFFFFU,
                0xFFFFFFFFU,
                0xFFFFFFFFU,
                0xFFFFFFFFU,
                0xFFFFFFFFU);
            var x = EC.G.x;

            x = x.Add(p);
            var x3 = x * x * x;
            var y2 = x3 + new FE(EC.CURVE_B);

            Assert.True(y2.Sqrt(out var y));
            var ge = new GroupElement(new GE(x, y));

            var ge2 = GroupElement.FromBytes(ge.ToBytes());

            Assert.Equal(ge, ge2);
        }