public static State FromBytes(ReadOnlyMemory <byte> bytes) { var cLen = bytes.Slice(0, 4).Span.ToInt32(); var commitment = cLen != 0 ? Commitment.FromBytes(bytes.Slice(4, cLen).ToArray()) : null; var n = bytes.Slice(4 + cLen, 4).Span.ToInt32(); var values = bytes.Slice(8 + cLen, n * Fr.ByteSize).ToArray() .Batch(Fr.ByteSize) .Select(x => Fr.FromBytes(x.ToArray())) .ToArray(); var acks = bytes.Slice(8 + cLen + n * Fr.ByteSize).ToArray() .Select(b => b != 0) .ToArray(); var result = new State(values.Length) { Commitment = commitment }; for (var i = 0; i < n; ++i) { result.Values[i] = values[i]; result.Acks[i] = acks[i]; } return(result); }
public void DeserializationFrTest() { var a = Fr.GetRandom(); var enc = a.ToBytes(); Assert.AreEqual(enc.Length, 32); var b = Fr.FromBytes(enc); Assert.True(a.Equals(b)); }
public void SerializationRoundTrip() { var x = Fr.GetRandom(); Assert.IsTrue(x.IsValid()); var serialized = x.ToBytes(); Assert.AreEqual(serialized.Length, 32); var restored = Fr.FromBytes(serialized); Assert.AreEqual(x, restored); }
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() ); }
public bool HandleSendValue(int sender, ValueMessage message) { if (_keyGenStates[message.Proposer].Acks[sender]) { throw new ArgumentException("Already handled this value"); } _keyGenStates[message.Proposer].Acks[sender] = true; var myValue = Fr.FromBytes(Crypto.Secp256K1Decrypt( _keyPair.PrivateKey.Encode(), message.EncryptedValues[_myIdx] )); if (_keyGenStates[message.Proposer].Commitment is null) { throw new ArgumentException("Cannot handle value since there was no commitment yet"); } if (!_keyGenStates[message.Proposer].Commitment !.Evaluate(_myIdx + 1, sender + 1) .Equals(G1.Generator * myValue) ) { throw new ArgumentException("Decrypted value does not match commitment"); } _keyGenStates[message.Proposer].Values[sender] = myValue; if (_keyGenStates[message.Proposer].ValueCount() > 2 * Faulty && !_finished.Contains(message.Proposer)) { _finished.Add(message.Proposer); } if (_confirmSent) { return(false); } if (!Finished()) { return(false); } _confirmSent = true; return(true); }
public static PrivateKeyShare FromBytes(ReadOnlyMemory <byte> buffer) { return(new PrivateKeyShare(Fr.FromBytes(buffer.ToArray()))); }
private static IEnumerable <Fr> DecryptRow(byte[] encryptedRow, ECDSAPrivateKey privateKey) { return(Crypto.Secp256K1Decrypt(privateKey.Encode(), encryptedRow) .Batch(Fr.ByteSize) .Select(b => Fr.FromBytes(b.ToArray()))); }
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(); } }