Skip to content

Commit

Permalink
Use signatureGroupSize and groupSize extracted contants in DKG library
Browse files Browse the repository at this point in the history
No more magic numbers!
  • Loading branch information
pdyraga committed Nov 30, 2021
1 parent 77b66ee commit 5bc9536
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions solidity/random-beacon/contracts/DKGValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ contract DKGValidator {
/// entry.
uint256 public constant activeThreshold = 58; // 90% of groupSize

/// @dev Size in bytes of a single signature produced by operator supporting
/// DKG result.
uint256 public constant signatureByteSize = 65;

SortitionPool public sortitionPool;

constructor(SortitionPool _sortitionPool) {
Expand Down Expand Up @@ -85,7 +89,7 @@ contract DKGValidator {
}

// The number of misbehaved members can not exceed the threshold.
// Misbehaved member indices needs to be unique, between [1,64],
// Misbehaved member indices needs to be unique, between [1, groupSize],
// and sorted in ascending order.
uint8[] calldata misbehavedMembersIndices = result
.misbehavedMembersIndices;
Expand All @@ -96,7 +100,7 @@ contract DKGValidator {
if (
misbehavedMembersIndices[0] < 1 ||
misbehavedMembersIndices[misbehavedMembersIndices.length - 1] >
64
groupSize
) {
return (false, "Corrupted misbehaved members indices");
}
Expand All @@ -110,13 +114,13 @@ contract DKGValidator {
}
}

// Each signature needs to be 65 bytes long and signatures need to be
// provided.
uint256 signaturesCount = result.signatures.length / 65;
// Each signature needs to have a correct length and signatures need to
// be provided.
uint256 signaturesCount = result.signatures.length / signatureByteSize;
if (result.signatures.length == 0) {
return (false, "No signatures provided");
}
if (result.signatures.length % 65 != 0) {
if (result.signatures.length % signatureByteSize != 0) {
return (false, "Malformed signatures array");
}

Expand All @@ -133,11 +137,11 @@ contract DKGValidator {
return (false, "Too many signatures");
}

// Signing member indices needs to be unique, between [1,64], and sorted
// in ascending order.
// Signing member indices needs to be unique, between [1,groupSize],
// and sorted in ascending order.
if (
signingMembersIndices[0] < 1 ||
signingMembersIndices[signingMembersIndices.length - 1] > 64
signingMembersIndices[signingMembersIndices.length - 1] > groupSize
) {
return (false, "Corrupted signing member indices");
}
Expand Down Expand Up @@ -199,11 +203,14 @@ contract DKGValidator {

bytes memory current; // Current signature to be checked.

uint256 signaturesCount = result.signatures.length / 65;
uint256 signaturesCount = result.signatures.length / signatureByteSize;
for (uint256 i = 0; i < signaturesCount; i++) {
uint256 memberIndex = result.signingMembersIndices[i];

current = result.signatures.slice(65 * i, 65);
current = result.signatures.slice(
signatureByteSize * i,
signatureByteSize
);
address recoveredAddress = hash.toEthSignedMessageHash().recover(
current
);
Expand Down

0 comments on commit 5bc9536

Please sign in to comment.