Skip to content

Commit

Permalink
fix operators ++ and __
Browse files Browse the repository at this point in the history
  • Loading branch information
kKahina committed Jul 11, 2024
1 parent 7a31ec7 commit 9064003
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
43 changes: 19 additions & 24 deletions src/transformers/visitors/toCircuitVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ const visitor = {
}
},

ExpressionStatement: {
ExpressionStatement: {
enter(path: NodePath, state: any) {
const { node, parent, scope } = path;
const { expression } = node;
Expand All @@ -768,40 +768,35 @@ const visitor = {
if((scope.getReferencedNode(expression.expression))?.containsSecret)
node.containsSecret = 'true';
}
let childOfSecret = path.getAncestorOfType('ForStatement')?.containsSecret;
let childOfSecret = path.getAncestorOfType('ForStatement')?.containsSecret;
if(path.getAncestorOfType('ForStatement') && expression.containsPublic ){
childOfSecret = false;
}

const thisState = { interactsWithSecretInScope: false };

path.traverseNodesFast(n => {
if (n.nodeType === 'Identifier' && scope.getReferencedIndicator(n)?.interactsWithSecret){
thisState.interactsWithSecretInScope = true;
}

}, thisState);

const leftHandSideInteracts = expression.leftHandSide && scope.getReferencedIndicator(expression.leftHandSide)?.interactsWithSecret;
const rightHandSideInteracts = expression.rightHandSide && scope.getReferencedIndicator(expression.rightHandSide)?.interactsWithSecret;
const leftHandSideInteracts = expression.leftHandSide && scope.getReferencedIndicator(expression.leftHandSide)?.interactsWithSecret;

expression.leftHandSide.containsSecret = false;
expression.rightHandSide.containsSecret = false;

if (leftHandSideInteracts) {
thisState.interactsWithSecretInScope = true; // Update thisState flag
}

// Mark leftHandSide and rightHandSide if they interact with a secret
if (leftHandSideInteracts) {
expression.leftHandSide.containsSecret = true;
}
if (rightHandSideInteracts) {
expression.rightHandSide.containsSecret = true;
}
if (expression.nodeType === 'UnaryOperation') {
const { operator, subExpression } = expression;
if ((operator === '++' || operator === '--') && subExpression.nodeType === 'Identifier') {
const referencedIndicator = scope.getReferencedIndicator(subExpression);
if (referencedIndicator?.interactsWithSecret) {
thisState.interactsWithSecretInScope = true;
}
}
}


if (!node.containsSecret && !childOfSecret && !thisState.interactsWithSecretInScope) {
state.skipSubNodes = true;
return;
// if (!expression.leftHandSide.containsSecret && !expression.rightHandSide.containsSecret && !thisState.interactsWithSecretInScope) {
// state.skipSubNodes = true;
// return;

}

const { isIncremented, isDecremented } = expression;
Expand Down Expand Up @@ -919,7 +914,7 @@ let childOfSecret = path.getAncestorOfType('ForStatement')?.containsSecret;
}
if (referencedIndicator instanceof LocalVariableIndicator && firstInstanceOfNewName && names[names.length - 1].name !== referencedIndicator.name){
isVarDec = true;
}
}
}
let nodeID = node.id;
newNode = buildNode('ExpressionStatement', { isVarDec });
Expand Down
3 changes: 2 additions & 1 deletion test/contracts/c-circuit.zol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ uint256 public c;

function addB( uint256 value) public {
c += value;
known a += value;
c++;
known a += c;
}

}

0 comments on commit 9064003

Please sign in to comment.