Skip to content

Commit

Permalink
Reveiwed RecoverSecret, removed unused paramater (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx authored May 9, 2024
1 parent 504bf4b commit 36b41ae
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
10 changes: 5 additions & 5 deletions dkgLibrary/poly/PriPoly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ public PriPoly Mul(PriPoly q)

// RecoverSecret reconstructs the shared secret p(0) from a list of private
// shares using Lagrange interpolation.
public static IScalar RecoverSecret(IGroup g, PriShare[] shares, int t, int n)
public static IScalar RecoverSecret(IGroup g, PriShare[] shares, int t)
{
var (x, y) = XyScalar(g, shares, t, n);
var (x, y) = XyScalar(g, shares, t);

if (x.Count < t)
{
Expand Down Expand Up @@ -249,7 +249,7 @@ public static IScalar RecoverSecret(IGroup g, PriShare[] shares, int t, int n)
// xyScalar returns the list of (x_i, y_i) pairs indexed. The first map returned
// is the list of x_i and the second map is the list of y_i, both indexed in
// their respective map at index i.
public static (Dictionary<int, IScalar>, Dictionary<int, IScalar>) XyScalar(IGroup g, PriShare[] shares, int t, int n)
public static (Dictionary<int, IScalar>, Dictionary<int, IScalar>) XyScalar(IGroup g, PriShare[] shares, int t)
{
List<PriShare> sorted = shares.Where(s => s != null).ToList();
sorted.Sort(new ShareComparer());
Expand Down Expand Up @@ -277,9 +277,9 @@ public static (Dictionary<int, IScalar>, Dictionary<int, IScalar>) XyScalar(IGro
// coefficients. It is up to the caller to make sure that there are enough
// shares to correctly re-construct the polynomial. There must be at least t
// shares.
public static PriPoly? RecoverPriPoly(IGroup g, PriShare[] shares, int t, int n)
public static PriPoly? RecoverPriPoly(IGroup g, PriShare[] shares, int t)
{
var (x, y) = XyScalar(g, shares, t, n);
var (x, y) = XyScalar(g, shares, t);
if (x.Count != t)
{
throw new ArgumentException("PriPoly.RecoverPriPoly: Not enough shares to recover private polynomial");
Expand Down
2 changes: 1 addition & 1 deletion dkgLibrary/share/DistKeyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ public DistKeyShare ResharingKey()

// the private polynomial is generated from the old nodes, thus inheriting
// the old threshold condition
var priPoly = PriPoly.RecoverPriPoly(G, shares, OldT, C.OldNodes.Length) ??
var priPoly = PriPoly.RecoverPriPoly(G, shares, OldT) ??
throw new DkgError("Could not recover PriPoly", GetType().Name);

var privateShare = new PriShare(Nidx, priPoly.Secret());
Expand Down
4 changes: 2 additions & 2 deletions dkgLibrary/vss/Dealer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void SetTimeout()
// RecoverSecret recovers the secret shared by a Dealer by gathering at least t
// GetDistDeals from the verifiers. It returns an error if there is not enough GetDistDeals or
// if all GetDistDeals don't have the same SessionID.
public static IScalar RecoverSecret(IGroup group, Deal[] deals, int n, int t)
public static IScalar RecoverSecret(IGroup group, Deal[] deals, int t)
{
PriShare[] shares = new PriShare[deals.Length];
for (int i = 0; i < deals.Length; i++)
Expand All @@ -185,7 +185,7 @@ public static IScalar RecoverSecret(IGroup group, Deal[] deals, int n, int t)
throw new DkgError("All deals need to have same session id", "RecoverSecret");
}
}
return PriPoly.RecoverSecret(group, shares, t, n);
return PriPoly.RecoverSecret(group, shares, t);
}
}
}
2 changes: 1 addition & 1 deletion dkgLibraryTests/AnEndToEndExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void EndToEndExample()
string message = "Hello world";

var cipher = ECElGamalEncryption.Encrypt(g, publicKey!, message);
IScalar secretKey = PriPoly.RecoverSecret(g, shares, n, n);
IScalar secretKey = PriPoly.RecoverSecret(g, shares, n);

var decryptedMessage = ECElGamalEncryption.DecryptString(g, secretKey, cipher);
Assert.That(decryptedMessage, Is.EqualTo(message));
Expand Down
26 changes: 13 additions & 13 deletions dkgLibraryTests/testDkgPedersen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void TestDistKeySharing()
shares[i] = dks.Share;
}

var secret = PriPoly.RecoverSecret(_g, shares, _defaultN, _defaultN);
var secret = PriPoly.RecoverSecret(_g, shares, _defaultN);
Assert.That(secret, Is.Not.Null);

var secretCoeffs = poly!.Coeffs;
Expand Down Expand Up @@ -542,8 +542,8 @@ public void TestDistKeyResharingThreshold()
}
}
// 2.
var oldSecret = PriPoly.RecoverSecret(_g, sshares, oldT, n);
var newSecret = PriPoly.RecoverSecret(_g, [.. newShares], newT, newN);
var oldSecret = PriPoly.RecoverSecret(_g, sshares, oldT);
var newSecret = PriPoly.RecoverSecret(_g, [.. newShares], newT);
Assert.That(newSecret, Is.EqualTo(oldSecret));
}

Expand Down Expand Up @@ -598,8 +598,8 @@ public void TestDistKeyResharing()
}
var thr = VssTools.MinimumT(_defaultN);
// 2.
var oldSecret = PriPoly.RecoverSecret(_g, sshares, thr, _defaultN);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, thr, _defaultN);
var oldSecret = PriPoly.RecoverSecret(_g, sshares, thr);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, thr);
Assert.That(newSecret, Is.EqualTo(oldSecret));
}

Expand Down Expand Up @@ -680,8 +680,8 @@ public void TestDistKeyResharingRemoveNode()
// 2.
try
{
var oldSecret = PriPoly.RecoverSecret(_g, sshares.Take(newN).ToArray(), thr, newN);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, thr, newN);
var oldSecret = PriPoly.RecoverSecret(_g, sshares.Take(newN).ToArray(), thr);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, thr);
Assert.That(newSecret, Is.EqualTo(oldSecret));
}
catch (DkgError ex)
Expand Down Expand Up @@ -922,8 +922,8 @@ public void TestDistKeyResharingNewNodesThreshold()
// check shares reconstruct to the same secret
try
{
var oldSecret = PriPoly.RecoverSecret(_g, sshares, oldT, oldN);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, newT, newN);
var oldSecret = PriPoly.RecoverSecret(_g, sshares, oldT);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, newT);
Assert.That(newSecret, Is.EqualTo(oldSecret));
}
catch (DkgError ex)
Expand Down Expand Up @@ -1193,8 +1193,8 @@ public void TestDistKeyResharingNewNodes()
// check shares reconstruct to the same secret
try
{
var oldSecret = PriPoly.RecoverSecret(_g, sshares, oldT, oldN);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, newT, newN);
var oldSecret = PriPoly.RecoverSecret(_g, sshares, oldT);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, newT);
Assert.That(newSecret, Is.EqualTo(oldSecret));
}
catch (DkgError ex)
Expand Down Expand Up @@ -1424,8 +1424,8 @@ public void TestDistKeyResharingPartialNewNodes()
}

// check shares reconstruct to the same secret
var oldSecret = PriPoly.RecoverSecret(_g, sshares, oldT, oldN);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, newT, newN);
var oldSecret = PriPoly.RecoverSecret(_g, sshares, oldT);
var newSecret = PriPoly.RecoverSecret(_g, newSShares, newT);

Assert.That(newSecret, Is.EqualTo(oldSecret));
}
Expand Down
12 changes: 6 additions & 6 deletions dkgLibraryTests/testShamirAndPoly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void TestSecretRecovery()
var poly = new PriPoly(g, t, null);
var shares = poly.Shares(n);

var recovered = PriPoly.RecoverSecret(g, shares, t, n);
var recovered = PriPoly.RecoverSecret(g, shares, t);
if (recovered == null)
{
Assert.Fail("Error recovering secret");
Expand Down Expand Up @@ -67,7 +67,7 @@ public void TestSecretRecoveryOutIndex()
var selected = shares.ToList().GetRange(n - t, t);
Assert.That(selected, Has.Count.EqualTo(t));

var recovered = PriPoly.RecoverSecret(g, [.. selected], t, t + 1);
var recovered = PriPoly.RecoverSecret(g, [.. selected], t);
if (recovered == null)
{
Assert.Fail("Error recovering secret");
Expand All @@ -88,7 +88,7 @@ public void TestSecretRecoveryNotEnough()
var selected = shares.ToList().GetRange(n - t, t - 1);
Assert.That(selected, Has.Count.EqualTo(t - 1));

Assert.Throws<ArgumentException>(() => PriPoly.RecoverSecret(g, [.. selected], t, t));
Assert.Throws<ArgumentException>(() => PriPoly.RecoverSecret(g, [.. selected], t));
}

[Test]
Expand All @@ -106,7 +106,7 @@ public void TestSecretRecoveryDelete()
shares.RemoveAt(2);
shares.RemoveAt(1);

var recovered = PriPoly.RecoverSecret(g, [.. shares], shares.Count, n);
var recovered = PriPoly.RecoverSecret(g, [.. shares], shares.Count);
if (recovered == null)
{
Assert.Fail("Error recovering secret");
Expand Down Expand Up @@ -203,10 +203,10 @@ public void TestPriPolyRecover()
var reverses = shares;
reverses.Reverse();

var recovered = PriPoly.RecoverPriPoly(g, shares, t, n);
var recovered = PriPoly.RecoverPriPoly(g, shares, t);
Assert.That(recovered, Is.Not.Null);

var reverseRecovered = PriPoly.RecoverPriPoly(g, reverses, t, n);
var reverseRecovered = PriPoly.RecoverPriPoly(g, reverses, t);
Assert.That(reverseRecovered, Is.Not.Null);

for (int i = 0; i < t; i++)
Expand Down
2 changes: 1 addition & 1 deletion dkgLibraryTests/testVssPedersen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void TestWhole()
}

// 5. recover
var sec = Dealer.RecoverSecret(_g, deals, _nbVerifiers, VssTools.MinimumT(_nbVerifiers));
var sec = Dealer.RecoverSecret(_g, deals, VssTools.MinimumT(_nbVerifiers));
Assert.That(sec, Is.Not.Null);
Assert.That(sec, Is.EqualTo(_secret));

Expand Down
4 changes: 2 additions & 2 deletions dkgLibraryTests/testVssRefresh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void TestVssRefresh()
Assert.That(com, Is.EqualTo(dkgCommits[0]));

// Compute the refreshed private DKG share of node i
var s = PriPoly.RecoverSecret(g, tmpPriShares, t, n);
var s = PriPoly.RecoverSecret(g, tmpPriShares, t);
newDKGShares[i] = new PriShare(i, s);
}

Expand Down Expand Up @@ -166,7 +166,7 @@ public void TestVssRefresh()
}

// Recover the private polynomial
var refreshedPriPoly = PriPoly.RecoverPriPoly(g, newDKGShares, t, n);
var refreshedPriPoly = PriPoly.RecoverPriPoly(g, newDKGShares, t);

// Check that the secret and the corresponding (old) public commit match
Assert.That(refreshedPriPoly, Is.Not.Null);
Expand Down

0 comments on commit 36b41ae

Please sign in to comment.