From 504bf4b344415db68b86dfa112479be87375c21c Mon Sep 17 00:00:00 2001 From: Maxim Samsonov Date: Thu, 9 May 2024 00:30:56 +0300 Subject: [PATCH] Implemented GetBytes/SetBytes for PriShare and PubShare classes (#7) --- dkgLibrary/poly/Share.cs | 74 ++++++++++++++++++++++++----------- dkgLibraryTests/testsShare.cs | 34 ++++++++++++++-- dkgSample/Program.cs | 3 -- 3 files changed, 82 insertions(+), 29 deletions(-) diff --git a/dkgLibrary/poly/Share.cs b/dkgLibrary/poly/Share.cs index 84b7cfb..b6aa269 100644 --- a/dkgLibrary/poly/Share.cs +++ b/dkgLibrary/poly/Share.cs @@ -52,23 +52,36 @@ public bool Equals(Share? other) return I == other.I; } - public void MarshalBinary(Stream s) + public virtual void MarshalBinary(Stream s) { BinaryWriter w = new(s); w.Write(I); } - public int MarshalSize() + public virtual int MarshalSize() { return sizeof(int); } - public void UnmarshalBinary(Stream s) + public virtual void UnmarshalBinary(Stream s) { BinaryReader reader = new(s); I = reader.ReadInt32(); } + public virtual byte[] GetBytes() + { + using MemoryStream stream = new MemoryStream(); + MarshalBinary(stream); + return stream.ToArray(); + } + + public virtual void SetBytes(byte[] bytes) + { + using MemoryStream stream = new MemoryStream(bytes); + UnmarshalBinary(stream); + } + public override bool Equals(object? obj) { return Equals(obj as Share); @@ -94,10 +107,18 @@ public int Compare(Share? x, Share? y) } // PriShare represents a private share. - public class PriShare(int I, IScalar V) : Share(I), IMarshalling, IEquatable + public class PriShare: Share, IMarshalling, IEquatable { - public IScalar V { get; set; } = V; + public IScalar V { get; set; } + public PriShare(): base(0) + { + V = new Secp256k1Scalar(); + } + public PriShare(int I, IScalar V) : base(I) + { + this.V = V; + } public bool Equals(PriShare? other) { if (other == null) return false; @@ -117,26 +138,26 @@ public byte[] Hash() return h; } - public new void MarshalBinary(Stream s) + public override void MarshalBinary(Stream s) { base.MarshalBinary(s); V.MarshalBinary(s); } - public new int MarshalSize() + public override int MarshalSize() { return base.MarshalSize() + V.MarshalSize(); } - public override string ToString() + public override void UnmarshalBinary(Stream s) { - return $"{{PriShare: I = {I}; V = {V}}}"; + base.UnmarshalBinary(s); + V.UnmarshalBinary(s); } - public new void UnmarshalBinary(Stream s) + public override string ToString() { - base.UnmarshalBinary(s); - V.UnmarshalBinary(s); + return $"{{PriShare: I = {I}; V = {V}}}"; } public override bool Equals(object? obj) @@ -154,9 +175,19 @@ public override int GetHashCode() } // PubShare represents a public share. - public class PubShare(int I, IPoint V) : Share(I), IEquatable + public class PubShare : Share, IMarshalling, IEquatable { - internal IPoint V { get; set; } = V; + internal IPoint V { get; set; } + + public PubShare() : base(0) + { + V = new Secp256k1Point(); + } + + public PubShare(int I, IPoint V) : base(I) + { + this.V = V; + } public bool Equals(PubShare? other) { if (other == null) return false; @@ -175,26 +206,25 @@ public byte[] Hash() h = [.. h, .. iBytes]; return h; } - public new void MarshalBinary(Stream s) + public override void MarshalBinary(Stream s) { base.MarshalBinary(s); V.MarshalBinary(s); } - public new int MarshalSize() + public override int MarshalSize() { return base.MarshalSize() + V.MarshalSize(); } - public override string ToString() - { - return $"{{PubShare: I = {I}; V = {V}}}"; - } - public new void UnmarshalBinary(Stream s) + public override void UnmarshalBinary(Stream s) { base.UnmarshalBinary(s); V.UnmarshalBinary(s); } - + public override string ToString() + { + return $"{{PubShare: I = {I}; V = {V}}}"; + } public override bool Equals(object? obj) { return Equals(obj as PubShare); diff --git a/dkgLibraryTests/testsShare.cs b/dkgLibraryTests/testsShare.cs index e19128f..0687611 100644 --- a/dkgLibraryTests/testsShare.cs +++ b/dkgLibraryTests/testsShare.cs @@ -23,6 +23,9 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +using dkg.group; +using dkg.poly; + namespace ShareTests { internal class ShareComparerTests @@ -170,8 +173,7 @@ public void TestMarshalUnmarshalBinary() _priShare.MarshalBinary(stream); Assert.That(stream.Length, Is.EqualTo(_priShare.MarshalSize())); stream.Position = 0; - Secp256k1Scalar scalar2 = new Secp256k1Scalar(); - PriShare share2 = new(20, scalar2); + PriShare share2 = new(); share2.UnmarshalBinary(stream); Assert.Multiple(() => { @@ -179,6 +181,19 @@ public void TestMarshalUnmarshalBinary() Assert.That(_priShare.GetHashCode(), Is.EqualTo(share2.GetHashCode())); }); } + + [Test] + public void TestGetSetBytes() + { + byte[] bytes = _priShare.GetBytes(); + PriShare share2 = new(); + share2.SetBytes(bytes); + Assert.Multiple(() => + { + Assert.That(_priShare, Is.EqualTo(share2)); + Assert.That(_priShare.GetHashCode(), Is.EqualTo(share2.GetHashCode())); + }); + } } public class PubShareTests @@ -245,8 +260,7 @@ public void TestMarshalUnmarshalBinary() _pubShare.MarshalBinary(stream); Assert.That(stream.Length, Is.EqualTo(_pubShare.MarshalSize())); stream.Position = 0; - IPoint point2 = new Secp256k1Point().Base().Mul(new Secp256k1Scalar().SetInt64(67)); - PubShare share2 = new(20, point2); + PubShare share2 = new(); share2.UnmarshalBinary(stream); Assert.Multiple(() => { @@ -255,5 +269,17 @@ public void TestMarshalUnmarshalBinary() }); } + [Test] + public void TestGetSetBytes() + { + byte[] bytes = _pubShare.GetBytes(); + PubShare share2 = new(); + share2.SetBytes(bytes); + Assert.Multiple(() => + { + Assert.That(_pubShare, Is.EqualTo(share2)); + Assert.That(_pubShare.GetHashCode(), Is.EqualTo(share2.GetHashCode())); + }); + } } } diff --git a/dkgSample/Program.cs b/dkgSample/Program.cs index 9769f57..3258e5c 100644 --- a/dkgSample/Program.cs +++ b/dkgSample/Program.cs @@ -32,8 +32,6 @@ using Grpc.Core; using System.Text; using static DkgNodeApi.DkgNode; -using static Org.BouncyCastle.Math.EC.ECCurve; -using static System.Net.Mime.MediaTypeNames; namespace dkg.sample { @@ -374,7 +372,6 @@ public DkgNode (DkgNodeConfig[] configs, int index) public void Start() { - GRpcServer.Start(); TheThread.Start(); }