From 906400305206135f151fe419ba92d94a0bf8fb75 Mon Sep 17 00:00:00 2001 From: kkahina Date: Fri, 12 Jul 2024 00:58:50 +0100 Subject: [PATCH] fix operators ++ and __ --- src/transformers/visitors/toCircuitVisitor.ts | 43 ++++++++----------- test/contracts/c-circuit.zol | 3 +- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/transformers/visitors/toCircuitVisitor.ts b/src/transformers/visitors/toCircuitVisitor.ts index deac87b4..cd52ffde 100644 --- a/src/transformers/visitors/toCircuitVisitor.ts +++ b/src/transformers/visitors/toCircuitVisitor.ts @@ -757,7 +757,7 @@ const visitor = { } }, - ExpressionStatement: { + ExpressionStatement: { enter(path: NodePath, state: any) { const { node, parent, scope } = path; const { expression } = node; @@ -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; @@ -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 }); diff --git a/test/contracts/c-circuit.zol b/test/contracts/c-circuit.zol index e54fe18e..c18b4001 100644 --- a/test/contracts/c-circuit.zol +++ b/test/contracts/c-circuit.zol @@ -9,7 +9,8 @@ uint256 public c; function addB( uint256 value) public { c += value; -known a += value; +c++; +known a += c; } } \ No newline at end of file