Skip to content

Commit

Permalink
Получение результата раунда (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Jun 23, 2024
1 parent 0784982 commit 1f9f099
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 25 deletions.
41 changes: 34 additions & 7 deletions dkgServiceNode/Controllers/NodesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,40 @@ public async Task<ActionResult<Reference>> RegisterNode(Node node)
}
else
{
xNode.Name = node.Name;
xNode.RoundId = roundId;
xNode.PublicKey = node.PublicKey;
xNode.CalculateRandom();
if (roundId == null) xNode.StatusValue = (short)NStatus.NotRegistered;
dkgContext.Entry(xNode).State = EntityState.Modified;
await dkgContext.SaveChangesAsync();
bool modified = false;
if (xNode.Name != node.Name)
{
xNode.Name = node.Name;
modified = true;
}

if (xNode.RoundId != roundId)
{
xNode.RoundId = roundId;
modified = true;
}

if (xNode.PublicKey != node.PublicKey)
{
xNode.PublicKey = node.PublicKey;
xNode.CalculateRandom();
modified = true;
}

if (roundId == null)
{
if (xNode.StatusValue != (short)NStatus.NotRegistered)
{
xNode.StatusValue = (short)NStatus.NotRegistered;
modified = true;
}
}

if (modified)
{
dkgContext.Entry(xNode).State = EntityState.Modified;
await dkgContext.SaveChangesAsync();
}
}

roundId ??= 0;
Expand Down
19 changes: 2 additions & 17 deletions dkgServiceNode/Models/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// POSSIBILITY OF SUCH DAMAGE.

using dkgCommon.Constants;
using dkgServiceNode.Services.CRandom;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -76,23 +77,7 @@ public NodeStatus Status

public void CalculateRandom()
{
int? IntValue = null;
try
{
byte[] decodedBytes = Convert.FromBase64String(PublicKey);
decodedBytes[0] = 0;
if (BitConverter.IsLittleEndian)
{
Array.Resize(ref decodedBytes, 4);
decodedBytes = decodedBytes.Reverse().ToArray();
}
IntValue = BitConverter.ToInt32(decodedBytes, 0);
}
catch
{

}
Random = IntValue;
Random = CR.Calculate(PublicKey);
}
}
}
62 changes: 62 additions & 0 deletions dkgServiceNode/Services/CRandom/CRandom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (C) 2024 Maxim [maxirmx] Samsonov (www.sw.consulting)
// All rights reserved.
// This file is a part of dkg service node
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.


namespace dkgServiceNode.Services.CRandom;

public static class CR
{
public static int? Calculate(string publicKey)
{
int? IntValue = null;
try
{
byte[] decodedBytes = Convert.FromBase64String(publicKey);
decodedBytes[0] = 0;
IntValue = Calculate(decodedBytes);
}
catch { }
return IntValue;
}

public static int? Calculate(byte[] decodedBytes)
{
int? IntValue = null;
try
{
decodedBytes[0] = 0;
if (BitConverter.IsLittleEndian)
{
Array.Resize(ref decodedBytes, 4);
decodedBytes = decodedBytes.Reverse().ToArray();
}
IntValue = BitConverter.ToInt32(decodedBytes, 0);
}
catch { }
return IntValue;
}

}

7 changes: 6 additions & 1 deletion dkgServiceNode/Services/RoundRunner/ActiveRound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
using dkg.poly;
using dkg.vss;

using dkgServiceNode.Services.CRandom;

using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("dkgNodesTests")]

Expand All @@ -36,6 +38,8 @@ namespace dkgServiceNode.Services.RoundRunner
{
public class ActiveRound
{
internal Secp256k1Group G { get; } = new();

internal ActiveNode[]? Nodes { get; set; } = null;
internal Round Round { get; set; }
public int Id { get { return Round.Id; } }
Expand Down Expand Up @@ -84,7 +88,8 @@ public void Clear()
}
}
IScalar secretKey = PriPoly.RecoverSecret(new Secp256k1Group(), [.. shares], VssTools.MinimumT(Nodes.Length));
result = BitConverter.ToInt32(secretKey.GetBytes(), 0);
IPoint publicKey = G.Base().Mul(secretKey);
result = CR.Calculate(publicKey.GetBytes());
}
catch (Exception ex)
{
Expand Down

0 comments on commit 1f9f099

Please sign in to comment.