Skip to content

Commit

Permalink
Merge pull request #303 from EYBlockchain/kahina/Statement-Circuit
Browse files Browse the repository at this point in the history
Public statement in Circuit
  • Loading branch information
SwatiEY authored Jul 12, 2024
2 parents f7f6259 + 9ea233e commit 8d563f7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/transformers/visitors/toCircuitVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ const publicVariables = (path: NodePath, state: any, IDnode: any) => {

// If there is a statment where a secret variable interacts with a public one, we need to adjust previous statements where the public variable was modified.


if (
binding instanceof VariableBinding &&
(node.interactsWithSecret || node.baseExpression?.interactsWithSecret) &&
(node.interactsWithPublic || node.baseExpression?.interactsWithPublic) &&
binding.stateVariable && !binding.isSecret
) {
)
{
const fnDefNode = path.getAncestorOfType('FunctionDefinition');
if (!fnDefNode) throw new Error(`Not in a function`);

Expand Down Expand Up @@ -803,7 +805,7 @@ const visitor = {
}
},

ExpressionStatement: {
ExpressionStatement: {
enter(path: NodePath, state: any) {
const { node, parent, scope } = path;
const { expression } = node;
Expand All @@ -814,22 +816,37 @@ 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;
}
const leftHandSideInteracts = expression.leftHandSide && scope.getReferencedIndicator(expression.leftHandSide)?.interactsWithSecret;


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

}, thisState);
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;

}

const { isIncremented, isDecremented } = expression;
let newNode: any;

Expand Down Expand Up @@ -950,7 +967,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
16 changes: 16 additions & 0 deletions test/contracts/c-circuit.zol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

// SPDX-License-Identifier: CC0
pragma solidity ^0.8.0;

contract Assign {
secret uint256 private a;
secret uint256 private b;
uint256 public c;

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

}

0 comments on commit 8d563f7

Please sign in to comment.