Skip to content

Commit

Permalink
Merge pull request #1204 from privacy-scaling-explorations/refactor/c…
Browse files Browse the repository at this point in the history
…ontracts-suggestions

refactor(contracts): add audit auggestions
  • Loading branch information
ctrlc03 authored Feb 19, 2024
2 parents 17613e4 + 415c3da commit d4d786d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 1 addition & 4 deletions contracts/contracts/MACI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ contract MACI is IMACI, Params, Utilities, Ownable {
/// @notice The number of signups
uint256 public numSignUps;

/// @notice A mapping of block timestamps to the number of state leaves
mapping(uint256 => uint256) public numStateLeaves;

/// @notice ERC20 contract that hold topup credits
TopupCredit public immutable topupCredit;

Expand Down Expand Up @@ -157,7 +154,7 @@ contract MACI is IMACI, Params, Utilities, Ownable {
bytes memory _initialVoiceCreditProxyData
) public virtual {
// ensure we do not have more signups than what the circuits support
if (numSignUps == uint256(TREE_ARITY) ** uint256(stateTreeDepth)) revert TooManySignups();
if (numSignUps >= uint256(TREE_ARITY) ** uint256(stateTreeDepth)) revert TooManySignups();

if (_pubKey.x >= SNARK_SCALAR_FIELD || _pubKey.y >= SNARK_SCALAR_FIELD) {
revert MaciPubKeyLargerThanSnarkFieldSize();
Expand Down
18 changes: 14 additions & 4 deletions contracts/contracts/Poll.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,22 @@ contract Poll is Params, Utilities, SnarkCommon, Ownable, EmptyBallotRoots, IPol
PubKey memory _coordinatorPubKey,
ExtContracts memory _extContracts
) payable {
extContracts = _extContracts;
// check that the coordinator public key is valid
if (_coordinatorPubKey.x >= SNARK_SCALAR_FIELD || _coordinatorPubKey.y >= SNARK_SCALAR_FIELD) {
revert MaciPubKeyLargerThanSnarkFieldSize();
}

// store the pub key as object then calculate the hash
coordinatorPubKey = _coordinatorPubKey;
// we hash it ourselves to ensure we record the correct value
// we hash it ourselves to ensure we store the correct value
coordinatorPubKeyHash = hashLeftRight(_coordinatorPubKey.x, _coordinatorPubKey.y);
// store the external contracts to interact with
extContracts = _extContracts;
// store duration of the poll
duration = _duration;
// store max values
maxValues = _maxValues;
// store tree depth
treeDepths = _treeDepths;
// Record the current timestamp
deployTime = block.timestamp;
Expand Down Expand Up @@ -144,7 +154,7 @@ contract Poll is Params, Utilities, SnarkCommon, Ownable, EmptyBallotRoots, IPol
/// @inheritdoc IPoll
function topup(uint256 stateIndex, uint256 amount) public virtual isWithinVotingDeadline {
// we check that we do not exceed the max number of messages
if (numMessages == maxValues.maxMessages) revert TooManyMessages();
if (numMessages >= maxValues.maxMessages) revert TooManyMessages();

// cannot realistically overflow
unchecked {
Expand All @@ -165,7 +175,7 @@ contract Poll is Params, Utilities, SnarkCommon, Ownable, EmptyBallotRoots, IPol
/// @inheritdoc IPoll
function publishMessage(Message memory _message, PubKey calldata _encPubKey) public virtual isWithinVotingDeadline {
// we check that we do not exceed the max number of messages
if (numMessages == maxValues.maxMessages) revert TooManyMessages();
if (numMessages >= maxValues.maxMessages) revert TooManyMessages();

// validate that the public key is valid
if (_encPubKey.x >= SNARK_SCALAR_FIELD || _encPubKey.y >= SNARK_SCALAR_FIELD) {
Expand Down
6 changes: 4 additions & 2 deletions contracts/contracts/utilities/Utilities.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ contract Utilities is SnarkConstants, DomainObjs, Hasher {
uint256[2] memory dataToPad,
uint256 msgType
) public pure returns (Message memory message, PubKey memory padKey, uint256 msgHash) {
// add data and pad it
uint256[10] memory dat = [dataToPad[0], dataToPad[1], 0, 0, 0, 0, 0, 0, 0, 0];
// add data and pad it to 10 elements (automatically cause it's the default value)
uint256[10] memory dat;
dat[0] = dataToPad[0];
dat[1] = dataToPad[1];

padKey = PubKey(PAD_PUBKEY_X, PAD_PUBKEY_Y);
message = Message({ msgType: msgType, data: dat });
Expand Down

0 comments on commit d4d786d

Please sign in to comment.