From 4cd1da5058998b8527f2ab7c8ed1d087d29e4313 Mon Sep 17 00:00:00 2001 From: Lydia Garms Date: Tue, 9 Jul 2024 14:18:04 +0100 Subject: [PATCH 1/2] fix: in orchestration do not input mapping key to the proof if it is a local variable --- .../javascript/nodes/boilerplate-generator.ts | 2 ++ .../orchestration/javascript/raw/toOrchestration.ts | 3 +-- src/transformers/visitors/toOrchestrationVisitor.ts | 9 ++++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/boilerplate/orchestration/javascript/nodes/boilerplate-generator.ts b/src/boilerplate/orchestration/javascript/nodes/boilerplate-generator.ts index 537ebeec..b7ef765f 100644 --- a/src/boilerplate/orchestration/javascript/nodes/boilerplate-generator.ts +++ b/src/boilerplate/orchestration/javascript/nodes/boilerplate-generator.ts @@ -131,6 +131,7 @@ export function buildPrivateStateNode(nodeType: string, fields: any = {}): any { accessedOnly, privateStateName, indicator = {}, + localMappingKey, } = fields; const structProperties = !indicator.isStruct ? null : indicator.isAccessed ? indicator.referencingPaths[0]?.getStructDeclaration()?.members.map(m => m.name) : Object.keys(indicator.structProperties); return { @@ -156,6 +157,7 @@ export function buildPrivateStateNode(nodeType: string, fields: any = {}): any { ownerIsSecret: indicator.isOwned ? indicator.owner.isSecret || indicator.owner.node?.isSecret : null, + localMappingKey: localMappingKey, }; } diff --git a/src/boilerplate/orchestration/javascript/raw/toOrchestration.ts b/src/boilerplate/orchestration/javascript/raw/toOrchestration.ts index a982206e..0e924191 100644 --- a/src/boilerplate/orchestration/javascript/raw/toOrchestration.ts +++ b/src/boilerplate/orchestration/javascript/raw/toOrchestration.ts @@ -150,9 +150,8 @@ export const generateProofBoilerplate = (node: any) => { // isIncluded = true; // } // } - const stateVarIdLines = - stateNode.isMapping && !(node.parameters.includes(stateNode.stateVarId[1])) && !(node.parameters.includes(stateNode.stateVarId[2])) && !msgSenderParamAndMappingKey && !msgValueParamAndMappingKey && !constantMappingKey + !stateNode.localMappingKey && stateNode.isMapping && !(node.parameters.includes(stateNode.stateVarId[1])) && !(node.parameters.includes(stateNode.stateVarId[2])) && !msgSenderParamAndMappingKey && !msgValueParamAndMappingKey && !constantMappingKey ? [`\n\t\t\t\t\t\t\t\t${stateName}_stateVarId_key.integer,`] : []; // we add any extra params the circuit needs diff --git a/src/transformers/visitors/toOrchestrationVisitor.ts b/src/transformers/visitors/toOrchestrationVisitor.ts index 6ef99156..98048d7b 100644 --- a/src/transformers/visitors/toOrchestrationVisitor.ts +++ b/src/transformers/visitors/toOrchestrationVisitor.ts @@ -1,6 +1,6 @@ /* eslint-disable no-param-reassign, no-shadow, no-unused-vars, no-continue */ import NodePath from '../../traverse/NodePath.js'; -import { StateVariableIndicator, FunctionDefinitionIndicator } from '../../traverse/Indicator.js'; +import { StateVariableIndicator, FunctionDefinitionIndicator, LocalVariableIndicator } from '../../traverse/Indicator.js'; import { VariableBinding } from '../../traverse/Binding.js'; import MappingKey from '../../traverse/MappingKey.js'; import cloneDeep from 'lodash.clonedeep'; @@ -737,6 +737,12 @@ const visitor = { }); } if (secretModified || accessedOnly) { + let localMappingKey = false; + if (stateVarIndicator[`keyPath`]){ + const keyIndicator = path.scope.getReferencedIndicator(stateVarIndicator[`keyPath`].node, true); + if (keyIndicator instanceof LocalVariableIndicator && !keyIndicator.isParam) localMappingKey = true; + } + newNodes.generateProofNode.privateStates[ name ] = buildPrivateStateNode('GenerateProof', { @@ -751,6 +757,7 @@ const visitor = { increment: isIncremented ? incrementsArray : undefined, accessedOnly, indicator: stateVarIndicator, + localMappingKey: localMappingKey, }); } From e427ca223254b0fdd38de35c62a7e0537972a55b Mon Sep 17 00:00:00 2001 From: Lydia Garms Date: Tue, 9 Jul 2024 15:25:04 +0100 Subject: [PATCH 2/2] add test contracts --- test/contracts/MappingtoStruct.zol | 42 ++++++++++++++++++++++++++++++ test/contracts/mapping-4.zol | 30 +++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 test/contracts/MappingtoStruct.zol create mode 100644 test/contracts/mapping-4.zol diff --git a/test/contracts/MappingtoStruct.zol b/test/contracts/MappingtoStruct.zol new file mode 100644 index 00000000..9a4cbc67 --- /dev/null +++ b/test/contracts/MappingtoStruct.zol @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: CC0 + +pragma solidity ^0.8.0; +contract Assign { + secret uint256 private a; + address public immutable owner; + secret mapping(uint256 => uint256) private b; + secret mapping(uint256 => uint256) private c; + + struct myStruct { + uint256 prop1; + uint256 prop2; + uint256 prop3; + } + secret mapping(uint256 => myStruct) private d; + + + modifier onlyOwner() { + require( + msg.sender == owner + ); + _; + } + + constructor() { + owner = msg.sender; + } + + + function add(secret uint256 value) public onlyOwner { + secret uint256 index1 = 0; + d[index1].prop1 = value; + d[index1].prop2 = value +1; + d[index1].prop3 = value +2; + } + + + + + + +} \ No newline at end of file diff --git a/test/contracts/mapping-4.zol b/test/contracts/mapping-4.zol new file mode 100644 index 00000000..8b0a28b4 --- /dev/null +++ b/test/contracts/mapping-4.zol @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: CC0 + +pragma solidity ^0.8.0; +contract Assign { + + secret mapping(uint256 => uint256) private d; + + uint256 public j; + + + function add(secret uint256 value) public { + secret uint256 index1 = 0; + d[index1] = value; + } + + function add1(secret uint256 value, secret uint256 index1) public { + d[index1] = value; + } + + + function add2(secret uint256 value) public { + j++; + d[j] = value; + } + + + + + +} \ No newline at end of file