Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(contracts): contracts optimizations #1371

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions contracts/contracts/Poll.sol
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ contract Poll is Params, Utilities, SnarkCommon, Ownable, EmptyBallotRoots, IPol
}

// init messageAq here by inserting placeholderLeaf
uint256[2] memory dat = [NOTHING_UP_MY_SLEEVE, 0];
uint256[2] memory dat;
dat[0] = NOTHING_UP_MY_SLEEVE;
dat[1] = 0;
kittybest marked this conversation as resolved.
Show resolved Hide resolved

(Message memory _message, PubKey memory _padKey, uint256 placeholderLeaf) = padAndHashMessage(dat, 1);
extContracts.messageAq.enqueue(placeholderLeaf);
Expand All @@ -165,7 +167,10 @@ 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 = [stateIndex, amount];
uint256[2] memory dat;
dat[0] = stateIndex;
dat[1] = amount;

(Message memory _message, , uint256 messageLeaf) = padAndHashMessage(dat, 2);

extContracts.messageAq.enqueue(messageLeaf);
Expand Down
13 changes: 7 additions & 6 deletions contracts/contracts/Tally.sol
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,16 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs {
);

if (mode == Mode.QV) {
uint256[3] memory tally = [
hashLeftRight(computedRoot, _tallyResultSalt),
_spentVoiceCreditsHash,
_perVOSpentVoiceCreditsHash
];
uint256[3] memory tally;
tally[0] = hashLeftRight(computedRoot, _tallyResultSalt);
tally[1] = _spentVoiceCreditsHash;
tally[2] = _perVOSpentVoiceCreditsHash;

isValid = hash3(tally) == tallyCommitment;
} else if (mode == Mode.NON_QV) {
uint256[2] memory tally = [hashLeftRight(computedRoot, _tallyResultSalt), _spentVoiceCreditsHash];
uint256[2] memory tally;
tally[0] = hashLeftRight(computedRoot, _tallyResultSalt);
tally[1] = _spentVoiceCreditsHash;

isValid = hash2(tally) == tallyCommitment;
}
Expand Down
14 changes: 12 additions & 2 deletions contracts/contracts/VkRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ contract VkRegistry is Ownable, DomainObjs, SnarkCommon, IVkRegistry {
processVk.gamma2 = _processVk.gamma2;
processVk.delta2 = _processVk.delta2;

for (uint8 i = 0; i < _processVk.ic.length; i++) {
uint256 processIcLength = _processVk.ic.length;
for (uint256 i = 0; i < processIcLength; ) {
processVk.ic.push(_processVk.ic[i]);

unchecked {
i++;
}
}

processVkSet[_mode][processVkSig] = true;
Expand All @@ -164,8 +169,13 @@ contract VkRegistry is Ownable, DomainObjs, SnarkCommon, IVkRegistry {
tallyVk.gamma2 = _tallyVk.gamma2;
tallyVk.delta2 = _tallyVk.delta2;

for (uint8 i = 0; i < _tallyVk.ic.length; i++) {
uint256 tallyIcLength = _tallyVk.ic.length;
for (uint256 i = 0; i < tallyIcLength; ) {
tallyVk.ic.push(_tallyVk.ic[i]);

unchecked {
i++;
}
}

tallyVkSet[_mode][tallyVkSig] = true;
Expand Down
21 changes: 17 additions & 4 deletions contracts/contracts/crypto/Pairing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,33 @@ library Pairing {
G1Point memory d1,
G2Point memory d2
) internal view returns (bool isValid) {
G1Point[4] memory p1 = [a1, b1, c1, d1];
G2Point[4] memory p2 = [a2, b2, c2, d2];
G1Point[4] memory p1;
p1[0] = a1;
p1[1] = b1;
p1[2] = c1;
p1[3] = d1;

G2Point[4] memory p2;
p2[0] = a2;
p2[1] = b2;
p2[2] = c2;
p2[3] = d2;

uint256 inputSize = 24;
uint256[] memory input = new uint256[](inputSize);

for (uint256 i = 0; i < 4; i++) {
uint256 j = i * 6;
for (uint8 i = 0; i < 4; ) {
uint8 j = i * 6;
input[j + 0] = p1[i].x;
input[j + 1] = p1[i].y;
input[j + 2] = p2[i].x[0];
input[j + 3] = p2[i].x[1];
input[j + 4] = p2[i].y[0];
input[j + 5] = p2[i].y[1];

unchecked {
i++;
}
}

uint256[1] memory out;
Expand Down
Loading