Skip to content

Commit

Permalink
Implemented GetBytes/SetBytes for PriShare and PubShare classes (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx authored May 8, 2024
1 parent 3351cf4 commit 504bf4b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 29 deletions.
74 changes: 52 additions & 22 deletions dkgLibrary/poly/Share.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<PriShare>
public class PriShare: Share, IMarshalling, IEquatable<PriShare>
{
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;
Expand All @@ -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)
Expand All @@ -154,9 +175,19 @@ public override int GetHashCode()
}

// PubShare represents a public share.
public class PubShare(int I, IPoint V) : Share(I), IEquatable<PubShare>
public class PubShare : Share, IMarshalling, IEquatable<PubShare>
{
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;
Expand All @@ -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);
Expand Down
34 changes: 30 additions & 4 deletions dkgLibraryTests/testsShare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,15 +173,27 @@ 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(() =>
{
Assert.That(_priShare, Is.EqualTo(share2));
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
Expand Down Expand Up @@ -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(() =>
{
Expand All @@ -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()));
});
}
}
}
3 changes: 0 additions & 3 deletions dkgSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -374,7 +372,6 @@ public DkgNode (DkgNodeConfig[] configs, int index)

public void Start()
{

GRpcServer.Start();
TheThread.Start();
}
Expand Down

0 comments on commit 504bf4b

Please sign in to comment.