Skip to content

Commit

Permalink
fix: zappify errors for InternalFunctionCalls
Browse files Browse the repository at this point in the history
  • Loading branch information
lydiagarms committed May 14, 2024
1 parent 0900e2e commit 53d8faa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/transformers/visitors/circuitInternalFunctionCallVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const internalCallVisitor = {

state.newParameterList.forEach((node, nodeIndex) => {
if(node.nodeType === 'Boilerplate') {
for(const [id, oldStateName] of state.oldStateArray.entries()) {
for(const [id, oldStateName] of state.oldStateArray[name].entries()) {
node.name = node.name.replace('_'+oldStateName, '_'+state.newStateArray[name][id].name)
if(node.newCommitmentValue === oldStateName)
node.newCommitmentValue = node.newCommitmentValue.replace(oldStateName, state.newStateArray[name][id].name)
Expand All @@ -36,7 +36,7 @@ const internalCallVisitor = {
}
}
if(node.nodeType === 'VariableDeclaration'){
for(const [id, oldStateName] of state.oldStateArray.entries()) {
for(const [id, oldStateName] of state.oldStateArray[name].entries()) {
if(oldStateName !== state.newStateArray[name][id].name)
node.name = state.newStateArray[name][id].name;
node.name = node.name.replace('_'+oldStateName, '_'+state.newStateArray[name][id].name)
Expand All @@ -46,7 +46,7 @@ const internalCallVisitor = {
}
})
state.newReturnParameterList.forEach((node,nodeIndex) => {
for(const [id, oldStateName] of state.oldStateArray.entries()) {
for(const [id, oldStateName] of state.oldStateArray[name].entries()) {
if(oldStateName !== state.newStateArray[name][id].name)
node.name = state.newStateArray[name][id].name;
node.name = node.name.replace('_'+oldStateName, '_'+state.newStateArray[name][id].name)
Expand Down Expand Up @@ -171,7 +171,7 @@ const internalCallVisitor = {
if(node.nodeType === 'ExpressionStatement') {
if(node.expression.nodeType === 'Assignment') {
let expressionList = cloneDeep(node);
for(const [id, oldStateName] of state.oldStateArray.entries()) {
for(const [id, oldStateName] of state.oldStateArray[name].entries()) {
if(state.newStateArray[name][id].memberName ){
if(node.expression.rightHandSide.rightExpression.name === oldStateName)
expressionList.expression.rightHandSide.rightExpression.name = expressionList.expression.rightHandSide.rightExpression.name.replace(oldStateName, state.newStateArray[name][id].name+'.'+state.newStateArray[name][id].memberName)
Expand All @@ -198,7 +198,7 @@ const internalCallVisitor = {
childNode.body.preStatements.forEach(node => {
if(node.isPartitioned){
commitmentValue = node.newCommitmentValue;
for(const [id, oldStateName] of state.oldStateArray.entries()) {
for(const [id, oldStateName] of state.oldStateArray[name].entries()) {
if(commitmentValue.includes(oldStateName)){
if(state.newStateArray[name][id].memberName)
commitmentValue = commitmentValue.replace(oldStateName,state.newStateArray[name][id].name+'.'+state.newStateArray[name][id].memberName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ const internalCallVisitor = {
if(childNode.nodeType === 'FunctionDefinition'){
state.newParametersList = cloneDeep(childNode.parameters.modifiedStateVariables);
state.newParametersList.forEach(node => {
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
node.name = node.name.replace('_'+oldStateName, '_'+state.newStateArray[name][index])
}
})
if(childNode.decrementedSecretStates){
newdecrementedSecretStates = cloneDeep(childNode.decrementedSecretStates);
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
node.name = node.name.replace('_'+oldStateName, '_'+state.newStateArray[name][index])
}
}
Expand All @@ -48,7 +48,7 @@ const internalCallVisitor = {
let stateNode: any;
let newstateName: string;
for( [stateName, stateNode] of Object.entries(node.privateStates)){
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
newstateName = stateName.replace('_'+oldStateName, '_'+ state.newStateArray[name][index])
if(newstateName != stateName ){
node.privateStates[ newstateName ] = node.privateStates[stateName];
Expand Down Expand Up @@ -89,17 +89,17 @@ const internalCallVisitor = {
state.newStatementList.forEach(node => {
if(node.nodeType === 'VariableDeclarationStatement'){
node.declarations.forEach(node => {
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
node.name = node.name.replace('_'+oldStateName, '_'+ state.newStateArray[name][index]);
}
});
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
node.initialValue.leftHandSide.name = node.initialValue.leftHandSide.name.replace('_'+oldStateName, '_'+ state.newStateArray[name][index]);
if (node.initialValue.rightHandSide.name) node.initialValue.rightHandSide.name = node.initialValue.rightHandSide.name.replace(oldStateName, state.newStateArray[name][index]);
}
}
if(node.nodeType === 'Assignment'){
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
node.leftHandSide.name = node.leftHandSide.name.replace('_'+oldStateName, '_'+ state.newStateArray[name][index]);
if (node.rightHandSide.name) node.rightHandSide.name = node.rightHandSide.name.replace('_'+oldStateName, '_'+ state.newStateArray[name][index]);
}
Expand All @@ -112,7 +112,7 @@ const internalCallVisitor = {
let stateNode: any;
let newstateName: string;
for( [stateName, stateNode] of Object.entries(node.privateStates)){
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
newstateName = stateName.replace('_'+oldStateName, '_'+ state.newStateArray[name][index])
if(newstateName != stateName ){
node.privateStates[ newstateName ] = node.privateStates[stateName];
Expand All @@ -126,7 +126,7 @@ const internalCallVisitor = {
let stateNode: any;
let newstateName: string;
for( [stateName, stateNode] of Object.entries(node.privateStates)){
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
newstateName = stateName.replace('_'+oldStateName, '_'+ state.newStateArray[name][index])
if(newstateName != stateName ){
node.privateStates[ newstateName ] = node.privateStates[stateName];
Expand All @@ -146,7 +146,7 @@ const internalCallVisitor = {
let stateName: string;
let newstateName: string;
for( stateName of Object.keys(generateProofNode.privateStates)) {
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
newstateName = stateName.replace('_'+oldStateName, '_'+state.newStateArray[name][index])
if(newstateName != stateName ){
generateProofNode.privateStates[ newstateName ] = generateProofNode.privateStates[stateName];
Expand All @@ -169,7 +169,7 @@ const internalCallVisitor = {
let stateNode: any;
let newstateName: string;
for( [stateName, stateNode] of Object.entries(sendTransactionNode.privateStates)){
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
newstateName = stateName.replace('_'+oldStateName, '_'+state.newStateArray[name][index])
if(newstateName != stateName ){
sendTransactionNode.privateStates[ newstateName ] = sendTransactionNode.privateStates[stateName];
Expand All @@ -184,7 +184,7 @@ const internalCallVisitor = {
let stateNode: any;
let newstateName: string;
for( [stateName, stateNode] of Object.entries(writePreimageNode.privateStates)){
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
newstateName = stateName.replace('_'+oldStateName, '_'+state.newStateArray[name][index])
if(newstateName != stateName ){
writePreimageNode.privateStates[ newstateName ] = writePreimageNode.privateStates[stateName];
Expand Down Expand Up @@ -325,7 +325,7 @@ const internalCallVisitor = {
state.newStatementList = cloneDeep(childNode.body.statements);
state.newStatementList.forEach(node => {
if(node.nodeType === 'VariableDeclarationStatement') {
for(const [index, oldStateName] of oldStateArray.entries()) {
for(const [index, oldStateName] of state.oldStateArray[name].entries()) {
node.initialValue.leftHandSide.name = node.initialValue.leftHandSide.name?.replace('_'+oldStateName, '_'+ state.newStateArray[name][index]);
node.initialValue.rightHandSide.name = node.initialValue.rightHandSide.name?.replace(oldStateName, state.newStateArray[name][index]);
}
Expand Down Expand Up @@ -388,7 +388,8 @@ FunctionCall: {
}
let internalFunctionInteractsWithSecret = false;
const newState: any = {};
oldStateArray = internalFunctionCallVisitor(path, newState)
state.oldStateArray = state.oldStateArray ? state.oldStateArray : {};
state.oldStateArray[fn_name] = internalFunctionCallVisitor(path, newState);
internalFunctionInteractsWithSecret ||= newState.internalFunctionInteractsWithSecret;
state.internalFncName ??= [];
state.internalFncName.push(node.expression.name);
Expand Down
3 changes: 2 additions & 1 deletion src/transformers/visitors/toCircuitVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,8 @@ let childOfSecret = path.getAncestorOfType('ForStatement')?.containsSecret;
}
let internalFunctionInteractsWithSecret = false;
const newState: any = {};
state.oldStateArray = internalFunctionCallVisitor(path, newState)
state.oldStateArray = state.oldStateArray ? state.oldStateArray : {};
state.oldStateArray[name] = internalFunctionCallVisitor(path, newState);
internalFunctionInteractsWithSecret ||= newState.internalFunctionInteractsWithSecret;
state.internalFncName ??= [];
state.internalFncName.push(node.expression.name);
Expand Down
5 changes: 1 addition & 4 deletions test/error-checks/internalFunctionCallTest2.zol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
pragma solidity ^0.8.0;
contract Assign {
secret uint256 private a;
//Remove below
secret uint256 private d;
secret uint256 private b;
uint256 public c;

Expand All @@ -14,8 +12,7 @@ contract Assign {
known a += value;
}
function addA( uint256 value) public {
//known a += value;
d = a+ value;
known a += value;
}
function remove( uint256 value, uint256 value1 ) public {
unknown b += value;
Expand Down

0 comments on commit 53d8faa

Please sign in to comment.