Skip to content

Commit

Permalink
refactor(contracts): optimize utilities.padAndHashMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Feb 5, 2024
1 parent 19b9f0e commit 0cc29ca
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
10 changes: 4 additions & 6 deletions contracts/contracts/Poll.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ contract Poll is Params, Utilities, SnarkCommon, Ownable, EmptyBallotRoots, IPol
}

// init messageAq here by inserting placeholderLeaf
uint256[2] memory dat;
dat[0] = NOTHING_UP_MY_SLEEVE;
dat[1] = 0;
uint256[2] memory dat = [NOTHING_UP_MY_SLEEVE, 0];

(Message memory _message, PubKey memory _padKey, uint256 placeholderLeaf) = padAndHashMessage(dat, 1);
extContracts.messageAq.enqueue(placeholderLeaf);

Expand All @@ -155,10 +154,9 @@ contract Poll is Params, Utilities, SnarkCommon, Ownable, EmptyBallotRoots, IPol
/// @notice topupCredit is a trusted token contract which reverts if the transfer fails
extContracts.topupCredit.transferFrom(msg.sender, address(this), amount);

uint256[2] memory dat;
dat[0] = stateIndex;
dat[1] = amount;
uint256[2] memory dat = [stateIndex, amount];
(Message memory _message, , uint256 messageLeaf) = padAndHashMessage(dat, 2);

extContracts.messageAq.enqueue(messageLeaf);

emit TopupMessage(_message);
Expand Down
12 changes: 3 additions & 9 deletions contracts/contracts/utilities/Utilities.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ contract Utilities is SnarkConstants, DomainObjs, Hasher {
uint256[2] memory dataToPad,
uint256 msgType
) public pure returns (Message memory message, PubKey memory padKey, uint256 msgHash) {
uint256[10] memory dat;
dat[0] = dataToPad[0];
dat[1] = dataToPad[1];
for (uint256 i = 2; i < 10; ) {
dat[i] = 0;
unchecked {
++i;
}
}
// add data and pad it
uint256[10] memory dat = [dataToPad[0], dataToPad[1], 0, 0, 0, 0, 0, 0, 0, 0];

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

0 comments on commit 0cc29ca

Please sign in to comment.