From b6e50f2e29e65330d6225691b4f7a3ffc334dd9f Mon Sep 17 00:00:00 2001 From: Swati Rawal Date: Tue, 23 Jul 2024 14:23:49 +0100 Subject: [PATCH] chore: added the requested comments --- .../visitors/checks/incrementedVisitor.ts | 30 ++++++++++++------- test/contracts/Assign-Increment.zol | 8 ++--- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/transformers/visitors/checks/incrementedVisitor.ts b/src/transformers/visitors/checks/incrementedVisitor.ts index 26857b7b..9b3fdc4c 100644 --- a/src/transformers/visitors/checks/incrementedVisitor.ts +++ b/src/transformers/visitors/checks/incrementedVisitor.ts @@ -25,6 +25,8 @@ const collectIncrements = (increments: any, incrementedIdentifier: any, assignme const { operands, precedingOperator } = increments; const newIncrements: any[] = []; for (const [index, operand] of operands.entries()) { +// This Logic, changes the sign in case of decrements when don't have a tuple expression as in the circuits/Orchestration we +// translate a = a - b + c - d as a = a - (b - c + d) if(assignmentOperator === '=' && precedingOperator[1] === '-' && index != 0){ if(index == 1) operand.precedingOperator = '+'; @@ -118,14 +120,15 @@ const getIncrementedPath = (path: NodePath, state: any) => { const binOpToIncrements = (path: NodePath, state: any) => { - const parentExpressionStatement = path.getAncestorOfType( + let parentExpressionStatement = path.getAncestorOfType( 'ExpressionStatement', ); const lhsNode = parentExpressionStatement?.node.expression?.leftHandSide; const assignmentOp = parentExpressionStatement?.node.expression?.operator; const { operator, leftExpression, rightExpression } = path.node ; - const operands = [leftExpression, rightExpression]; + let operands = [leftExpression, rightExpression]; + const precedingOperator = ['+', operator]; const isTupleExpression = operands[1].nodeType === 'TupleExpression'; // if we dont have any + or -, it can't be an incrementation @@ -139,32 +142,37 @@ const binOpToIncrements = (path: NodePath, state: any) => { return; } // correct the operands for case when a = a - (b + c + d). - if(assignmentOp === '=' && isTupleExpression) { - - operands[0] = operands[1].components[0].leftExpression; + if(isTupleExpression) { + operands[0] = operands[1].components[0].rightExpression; precedingOperator.push(operands[1].components[0].operator); - operands[1] = operands[1].components[0].rightExpression; + operands[1] = operands[1].components[0].leftExpression; for (const [index, operand] of operands.entries()) { if (operand.nodeType === 'BinaryOperation') { operands[index] = operand.leftExpression; - operands.splice(index+1, 0, operand.rightExpression); - precedingOperator.splice(index+2, 0, operand.operator); + operands.splice(0, 0, operand.rightExpression); + precedingOperator.splice(2, 0, operand.operator); } } + operands.splice(0, 0, operands[operands.length -1]).slice(0, -1); } // fills an array of operands // e.g. if we have a = b - c + a + d, operands = [b, c, a, d] if(!isTupleExpression){ + operands = operands.reverse(); for (const [index, operand] of operands.entries()) { if (operand.nodeType === 'BinaryOperation') { operands[index] = operand.leftExpression; - operands.splice(index+1, 0, operand.rightExpression); - precedingOperator.splice(index+1, 0, operand.operator); + operands.splice(0, 0, operand.rightExpression); + precedingOperator.splice(1, 0, operand.operator); } - }} + } + operands.splice(0, 0, operands[operands.length -1]); +} + // if we have mixed operators, we may have an underflow or not be able to tell whether this is increasing (incrementation) or decreasing (decrementation) the secret value + // Here we give out a warning when we don't use parentheses. if ( precedingOperator.length > 2 && precedingOperator.includes('+') && diff --git a/test/contracts/Assign-Increment.zol b/test/contracts/Assign-Increment.zol index 1db0cc54..68316e74 100644 --- a/test/contracts/Assign-Increment.zol +++ b/test/contracts/Assign-Increment.zol @@ -16,15 +16,15 @@ contract Assign { b -= value + value1; } - function add1(secret uint256 value, secret uint256 value1, secret uint256 value2, secret uint256 value3) public { + function add1(secret uint256 value, secret uint256 value1, secret uint256 value2, secret uint256 value3, secret uint256 value4) public { a = a + value - value1 + value3; - unknown b = b + value1 - value2 - value3; + unknown b = b + (value1 - value2 - value3 + value4); } -function remove1(secret uint256 value, secret uint256 value1, secret uint256 value2, secret uint256 value3) public { +function remove1(secret uint256 value, secret uint256 value1, secret uint256 value2, secret uint256 value3, secret uint256 value4) public { a = a - value - value1 + value3; - b = b - (value1 - value2 + value3); + b = b - value1 - value2 + value3 - value4; } } \ No newline at end of file