Skip to content

fixes several bugs in Encrypt.zol #385

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

Merged
merged 5 commits into from
May 16, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,20 @@ class FunctionBoilerplateGenerator {
encryptionRequired
}): string[] {
// prettier-ignore
let inputs_sig = `${[
`(`,
...(newNullifiers ? [`uint256[],`] : []),
...(checkNullifiers ? [`uint256[],`] : []),
...(commitmentRoot ? [`uint256,`] : []),
...(newCommitments? [`uint256[],`] : []),
...(encryptionRequired ? [`uint256[][],`] : []),
...(encryptionRequired ? [`uint256[2][],`] : []),
`uint256[])`, // TODO: consider whether we need to identify when / when not to include this.
].join('')}`;
let parameter = [
...(customInputs ? customInputs.filter(input => !input.dummy && input.isParam)
.map(input => input.structName ? `(${input.properties.map(p => p.type)})` : input.isConstantArray ? `${input.type}[${input.isConstantArray}]` : input.type) : []), // TODO arrays of structs/ structs of arrays
`(uint256[],uint256,uint256[],uint256[])`,
inputs_sig,
`uint256[]`,
`(string,uint256[],uint256)[]`,
].filter(para => para !== undefined); // Added for return parameter
Expand All @@ -86,7 +96,6 @@ class FunctionBoilerplateGenerator {
}
if (input.structName) customInputs[i] = input.properties;
});

let msgSigCheck = ([...(isConstructor ? [] : [`bytes4 sig = bytes4(keccak256("${functionName}(${parameter})")) ; \n \t \t \t if (sig == msg.sig)`])]);
customInputs = customInputs?.flat(Infinity).filter(p => (p.inCircuit || p.isReturn));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,20 @@ sendTransaction = {
isConstructor
}): string[] {
let value;
const errorCatch = `\n console.log("Added commitment", newCommitment.hex(32));
} catch (e) {
if (e.toString().includes("E11000 duplicate key")) {
console.log(
"encrypted-data-listener -",
"receiving EncryptedData event with balances.",
'This commitment for ${stateName} already exists. Ignore it.',
);
}
}`;
switch (stateType) {
case 'increment':
value = structProperties ? `{ ${structProperties.map((p, i) => `${p}: ${stateName}_newCommitmentValue.integer[${i}]`)} }` : `${stateName}_newCommitmentValue`;
return [`
return [`try {
\nawait storeCommitment({
hash: ${stateName}_newCommitment,
name: '${mappingName}',
Expand All @@ -645,12 +655,13 @@ sendTransaction = {
},
secretKey: ${stateName}_newOwnerPublicKey.integer === ${isSharedSecret ? `sharedPublicKey.integer` : `publicKey.integer`} ? ${isSharedSecret ? `sharedSecretKey` : `secretKey`}: null,
isNullified: false,
});`];
});` + errorCatch];
case 'decrement':
value = structProperties ? `{ ${structProperties.map((p, i) => `${p}: ${stateName}_change.integer[${i}]`)} }` : `${stateName}_change`;
return [`
\nawait markNullified(generalise(${stateName}_0_oldCommitment._id), secretKey.hex(32));
\nawait markNullified(generalise(${stateName}_1_oldCommitment._id), secretKey.hex(32));
\n try {
\nawait storeCommitment({
hash: ${stateName}_2_newCommitment,
name: '${mappingName}',
Expand All @@ -663,7 +674,7 @@ sendTransaction = {
},
secretKey: ${stateName}_newOwnerPublicKey.integer === ${isSharedSecret ? `sharedPublicKey.integer` : `publicKey.integer`} ? ${isSharedSecret ? `sharedSecretKey` : `secretKey`}: null,
isNullified: false,
});`];
});`+ errorCatch];
case 'whole':
switch (burnedOnly) {
case true:
Expand All @@ -674,6 +685,7 @@ sendTransaction = {
return [`
\n${reinitialisedOnly ? ' ': `if (${stateName}_commitmentExists) await markNullified(${stateName}_currentCommitment, secretKey.hex(32));
`}
\n try {
\nawait storeCommitment({
hash: ${stateName}_newCommitment,
name: '${mappingName}',
Expand All @@ -686,7 +698,7 @@ sendTransaction = {
},
secretKey: ${stateName}_newOwnerPublicKey.integer === ${isSharedSecret ? `sharedPublicKey.integer` : `publicKey.integer`} ? ${isSharedSecret ? `sharedSecretKey` : `secretKey`}: null,
isNullified: false,
});`];
});` + errorCatch];
}
default:
throw new TypeError(stateType);
Expand Down
15 changes: 7 additions & 8 deletions src/codeGenerators/orchestration/files/toOrchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ node.stateVariables?.forEach(
const ${structProp} = generalise(decrypted[${index+1}]);`
}) : '';
if(variable.isStruct) {

encryptedCommitmentCode += `
if (stateVarId.integer === ${variable.name}_stateVarId.integer) {
if (stateVarId.integer === generalise(${variable.id}).integer) {
${encryptedValue};
newCommitment = poseidonHash([
BigInt(stateVarId.hex(32)),
Expand All @@ -276,7 +277,7 @@ node.stateVariables?.forEach(
]);
}`
encryptedCode += `
if (stateVarId.integer === ${variable.name}_stateVarId.integer) {
if (stateVarId.integer === generalise(${variable.id}).integer) {
${encryptedValue};
const newCommitment = poseidonHash([
BigInt(stateVarId.hex(32)),
Expand Down Expand Up @@ -307,16 +308,15 @@ node.stateVariables?.forEach(
console.log(
'encrypted-data-listener -',
'receiving EncryptedData event with balances.',
'This ${variable.name} already exists. Ignore it.',
'This commitment for ${variable.name} already exists. Ignore it.',
);
}
}
}`;

} else {
encryptedCommitmentCode += `

if (stateVarId.integer === ${variable.name}_stateVarId.integer) {
if (stateVarId.integer === generalise(${variable.id}).integer) {
const value = generalise(decrypted[1]);

newCommitment = poseidonHash([
Expand All @@ -327,8 +327,7 @@ node.stateVariables?.forEach(
]);
}`
encryptedCode += `

if (stateVarId.integer === ${variable.name}_stateVarId.integer) {
if (stateVarId.integer === generalise(${variable.id}).integer) {
const value = generalise(decrypted[1]);

const newCommitment = poseidonHash([
Expand Down Expand Up @@ -358,7 +357,7 @@ node.stateVariables?.forEach(
console.log(
'encrypted-data-listener -',
'receiving EncryptedData event with balances.',
'This ${variable.name} already exists. Ignore it.',
'This commitment for ${variable.name} already exists. Ignore it.',
);
}
}
Expand Down
Loading