Skip to content

Commit

Permalink
fixe Arrays-Input
Browse files Browse the repository at this point in the history
  • Loading branch information
kKahina committed Aug 30, 2024
1 parent 20b44e4 commit 54878a0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class FunctionBoilerplateGenerator {
});
return { structName: structDef.name, properties: names, isParam: path.isFunctionParameter(node), isConstantArray: path.isConstantArray(node) ? node.typeName.length.value : false, inCircuit: node.interactsWithSecret };
}
return { name: node.name, type: node.typeName.name || node.typeName.baseType?.name || node.typeName.pathNode?.name, isParam: path.isFunctionParameter(node), isConstantArray: path.isConstantArray(node) ? node.typeName.length.value : false, inCircuit: node.interactsWithSecret || scope.getReferencedIndicator(node).interactsWithSecret };
return { name: node.name, type: node.typeName.name || node.typeName.baseType?.name || node.typeName.pathNode?.name, isParam: path.isFunctionParameter(node), isConstantArray: path.isConstantArray(node) ? node.typeName.length.value : false, inCircuit: node.interactsWithSecret || scope.getReferencedIndicator(node)?.interactsWithSecret };
}

const params = path.getFunctionParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class FunctionBoilerplateGenerator {
}
if (input.structName) customInputs[i] = input.properties;
});
console.log('cutomInputs in contract', customInputs);

let msgSigCheck = ([...(isConstructor ? [] : [`bytes4 sig = bytes4(keccak256("${functionName}(${parameter})")) ; \n \t \t \t if (sig == msg.sig)`])]);
customInputs = customInputs?.flat(Infinity).filter(p => (p.inCircuit || p.isReturn));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export const generateProofBoilerplate = (node: any) => {
? [`\n\t\t\t\t\t\t\t\t${stateName}_stateVarId_key.integer,`]
: [];
// we add any extra params the circuit needs
console.log('Node Parameters Before Filtering:', node.parameters);
node.parameters

.filter(
Expand All @@ -181,7 +180,6 @@ export const generateProofBoilerplate = (node: any) => {
else {
parameters.push(`\t${param}.integer,`);
}
console.log('Rania@@@@@@@@@@@@@@@@@@@@@@',param)

});

Expand Down
17 changes: 9 additions & 8 deletions src/transformers/visitors/toOrchestrationVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ if (name.endsWith('_')) {
if (path.containerName !== 'indexExpression') {
num_modifiers++;
}
//console.log('expNode!!!!!!!!!!!!!!!!!',expNode);
if (expNode) {
// if the public input is modified before here, it won't show up in the mjs file
// we have to go back and mark any editing statements as interactsWithSecret so they show up
Expand Down Expand Up @@ -342,7 +341,6 @@ if (name.endsWith('_')) {
} else {
IDnode.name = `${node.name}_${num_modifiers}`;
}
console.log('aaaaaaaaaabbbbbbbbbbbbb', IDnode);
}
}

Expand All @@ -368,8 +366,7 @@ if (name.endsWith('_')) {
state.publicInputs ??= [];
if (!(path.containerName === 'indexExpression' && !(path.parentPath.isSecret|| path.parent.containsSecret))) state.publicInputs.push(node);
}
//console.log('Public Inputs After Addition aaaaaaaaaaaaaaaaaaaaaaaaaaaa:', state.publicInputs);


if (['Identifier', 'IndexAccess'].includes(node.indexExpression?.nodeType)) addPublicInput(NodePath.getPath(node.indexExpression), state, null);
}
/**
Expand Down Expand Up @@ -990,12 +987,16 @@ const visitor = {
}

// this adds other values we need in the circuit

for (const param of node._newASTPointer.parameters.parameters) {
let oldParam : any ;
for(const para of node.parameters.parameters) {
if ( para?.name === param?.name )
oldParam = para ;
break;
}

scope.getReferencedIndicator(param);
console.log('KKKKKKKKKKKKKK param', scope.getReferencedIndicator(param).interactsWithSecret);

if (param.isPrivate || param.isSecret || param.interactsWithSecret || scope.getReferencedIndicator(param).interactsWithSecret) {
if (param.isPrivate || param.isSecret || param.interactsWithSecret || scope.getReferencedIndicator(oldParam)?.interactsWithSecret) {
if (param.typeName.isStruct) {
param.typeName.properties.forEach((prop: any) => {
newNodes.generateProofNode.parameters.push(`${param.name}.${prop.name}${param.typeName.isConstantArray ? '.all' : ''}`);
Expand Down
11 changes: 11 additions & 0 deletions src/traverse/NodePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,10 +984,21 @@ export default class NodePath {
isConstantArray(node: any = this.node): boolean {
if (!this.isArray(node)) return false;
let arrLen;
console.log('vvvvvvvvvvvvvvvvvNodenode', node);
switch (node.nodeType) {
case 'IndexAccess':
arrLen = node.baseExpression.typeDescriptions.typeString.match(/(?<=\[)(\d+)(?=\])/);
break;

// case 'VariableDeclaration':

// if (node.typeDescriptions && node.typeDescriptions.typeString) {
// arrLen = node.typeDescriptions.typeString.match(/(?<=\[)(\d+)(?=\])/);
// } else if (node.typeName && typeof node.typeName.name === 'string' && !isNaN(node.typeName.name)) {
// return false;
// }
// break;

case 'Identifier':
default:
arrLen = node.typeDescriptions.typeString.match(/(?<=\[)(\d+)(?=\])/);
Expand Down
29 changes: 19 additions & 10 deletions src/traverse/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,17 @@ export class Scope {
*/
getReferencedIndicator(referencingNode: any, mappingKeyIndicatorOnly: boolean = false): StateVariableIndicator | MappingKey | null {
const { path } = this;
if (!referencingNode) return null;
if (!referencingNode)
return null;

//if (referencingNode.nodeType === 'VariableDeclaration') return null ;


const indicator = this.getIndicatorById(
path.getReferencedDeclarationId(referencingNode) || referencingNode.id
path.getReferencedDeclarationId(referencingNode) || referencingNode?.id
);


if (!path.isMapping(referencingNode) && !path.isArray(referencingNode) && !path.isStruct(referencingNode)) return indicator;

if (path.isStruct(referencingNode) && NodePath.getPath(referencingNode).getAncestorOfType('MemberAccess') && path.isMapping(referencingNode)) {
Expand All @@ -407,39 +413,42 @@ export class Scope {
.node;

const indexAccessNode =
memberAccessNode.expression.nodeType === 'IndexAccess'
memberAccessNode.expression?.nodeType === 'IndexAccess'
? memberAccessNode.expression
: NodePath.getPath(memberAccessNode).getAncestorOfType('IndexAccess')
.node;
?.node;

return mappingKeyIndicatorOnly
? indicator.mappingKeys[this.getMappingKeyName(indexAccessNode)]
: indicator;
}

if (path.isStruct(referencingNode) && NodePath.getPath(referencingNode).getAncestorOfType('MemberAccess')) {
if (path.isStruct(referencingNode) && NodePath.getPath(referencingNode)?.getAncestorOfType('MemberAccess')) {
const memberAccessNode = referencingNode.nodeType === 'MemberAccess'
? referencingNode
: NodePath.getPath(referencingNode).getAncestorOfType('MemberAccess')
: NodePath.getPath(referencingNode)?.getAncestorOfType('MemberAccess')
.node;
return mappingKeyIndicatorOnly
? indicator.structProperties[memberAccessNode.memberName]
: indicator;
}


//if (path.isConstantArray(referencingNode) && referencingNode.nodeType === 'variableDeclaration') return null;
if ((path.isConstantArray(referencingNode) || referencingNode.memberName === 'length') && !NodePath.getPath(referencingNode).getAncestorOfType('IndexAccess')) return indicator;

// getMappingKeyName requires an indexAccessNode - referencingNode may be a baseExpression or indexExpression contained Identifier
const indexAccessNode =
referencingNode.nodeType === 'IndexAccess'
? referencingNode
: NodePath.getPath(referencingNode).getAncestorOfType('IndexAccess')
.node;
: NodePath.getPath(referencingNode)?.getAncestorOfType('IndexAccess')
?.node;

return mappingKeyIndicatorOnly
? indicator.mappingKeys[this.getMappingKeyName(indexAccessNode)]
? indicator?.mappingKeys[this.getMappingKeyName(indexAccessNode)]
: indicator;
}


/**
* @returns {Node || null} - the node (VariableDeclaration) being referred-to by the input referencingNode.
Expand Down
2 changes: 1 addition & 1 deletion test/contracts/Arrays-input.zol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract Assign {

function add(secret uint256[5] calldata value, uint256[5] calldata publicValue) public {
b = publicValue;
for (uint256 index = 0; index < 6; index++) {
for (uint256 index = 0; index < 2; index++) {
known a += value[index];
}
}
Expand Down

0 comments on commit 54878a0

Please sign in to comment.