Пример #1
0
 public static Commitment FromBytes(IEnumerable <byte> buffer)
 {
     return(new Commitment(buffer.Batch(G1.ByteSize)
                           .Select(x => x.ToArray())
                           .Select(b => G1.FromBytes(b.ToArray()))
                           ));
 }
Пример #2
0
        public void DeserializationG1Test()
        {
            var x   = Fr.GetRandom();
            var a   = G1.Generator * x;
            var enc = a.ToBytes();
            var b   = G1.FromBytes(enc);

            Assert.True(a.Equals(b));
        }
Пример #3
0
        public void SerializationRoundTrip()
        {
            var x = G1.Generator * Fr.GetRandom();

            Assert.IsTrue(x.IsValid());
            var serialized = x.ToBytes();

            Assert.AreEqual(48, serialized.Length);
            var restored = G1.FromBytes(serialized);

            Assert.AreEqual(x, restored);
        }
Пример #4
0
        public void Test_Mcl_Serializations()
        {
            Assert.AreEqual(
                "0x0000000000000000000000000000000000000000000000000000000000000000",
                Fr.FromInt(0).ToHex()
                );
            Assert.AreEqual(
                "0x0100000000000000000000000000000000000000000000000000000000000000",
                Fr.FromInt(1).ToHex()
                );
            var fr = Fr.GetRandom();

            Assert.AreEqual(fr, Fr.FromBytes(fr.ToBytes()));

            Assert.AreEqual(
                "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
                G1.Zero.ToHex()
                );
            Assert.AreEqual(
                "0xe9328f8eb8185341f22adaf2bf41f66258d97b2b5b2dbd2c27a77c81d9b5d76dd119bf7b1cd5d57b1273f9c4a654540e",
                G1.Generator.ToHex()
                );
            Assert.AreEqual(
                "0x2ac325b200d53184871fb8f8f5e5e43b302349ae6172de2899e88d2961f0bd2593b427667f5f85b4b59296ae8dcfc918",
                (G1.Generator * Fr.FromInt(2)).ToHex()
                );
            var g1 = G1.Generator * Fr.GetRandom();

            Assert.AreEqual(g1, G1.FromBytes(g1.ToBytes()));

            Assert.AreEqual(
                "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
                G2.Zero.ToHex()
                );
            Assert.AreEqual(
                "0xf1437606f337b000f00d69507434dbbd3b2abfa8a291d88551d92309fe3c222e0790fc847e849eb984bb807cba59170a0a63480e9457a375705ea2aba224a711e717ecddb224feeb738630945581bda24389f8fecf7865724361aec550e44919",
                G2.Generator.ToHex()
                );
            Assert.AreEqual(
                "0xd63f18cd560a715faef9b96c31e08eead86809ffd81642f6cdb55ed88ea2eca32cb2a28d818b7887cb35f1a7d4ab8a04eacc640851cb34b8a615729cd6d8cc4317125959b2012444b435508679e0a2f07d691d91aef2a18400a6696c6a3ae18d",
                (G2.Generator * Fr.FromInt(2)).ToHex()
                );
        }
Пример #5
0
 public static PublicKey FromBytes(ReadOnlyMemory <byte> bytes)
 {
     return(new PublicKey(G1.FromBytes(bytes.ToArray())));
 }
Пример #6
0
        internal static dynamic DeserializeType(ReadOnlyMemory <byte> bytes, Type type)
        {
            if (type == typeof(Fr))
            {
                return(Fr.FromBytes(bytes.ToArray()));
            }
            if (type == typeof(G1))
            {
                return(G1.FromBytes(bytes.ToArray()));
            }
            if (type == typeof(G2))
            {
                return(G2.FromBytes(bytes.ToArray()));
            }
            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Boolean:
                return(bytes.Span[0] != 0);

            case TypeCode.SByte:
                return((sbyte)bytes.Span[0]);

            case TypeCode.Byte:
                return(bytes.Span[0]);

            case TypeCode.UInt16:
                return(bytes.Span.ToUInt16());

            case TypeCode.Int16:
                return(bytes.Span.ToInt16());

            case TypeCode.UInt32:
                return(bytes.Span.ToUInt32());

            case TypeCode.Int32:
                return(bytes.Span.ToInt32());

            case TypeCode.UInt64:
                return(bytes.Span.ToUInt64());

            case TypeCode.Int64:
                return(bytes.Span.ToInt64());

            case TypeCode.Object:
                return((
                           type.GetMethod("FromBytes") ??
                           throw new InvalidOperationException($"Cannot deserialize type {type} without FromBytes method")
                           ).Invoke(null, new object[] { bytes }));

            case TypeCode.Single:
            case TypeCode.String:
            case TypeCode.Char:
            case TypeCode.DateTime:
            case TypeCode.DBNull:
            case TypeCode.Decimal:
            case TypeCode.Double:
            case TypeCode.Empty:
                throw new ArgumentOutOfRangeException();

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #7
0
        public PartiallyDecryptedShare Decode(TPKEPartiallyDecryptedShareMessage message)
        {
            var u = G1.FromBytes(message.Share.ToByteArray());

            return(new PartiallyDecryptedShare(u, message.DecryptorId, message.ShareId));
        }