Skip to content

Commit 9ad142f

Browse files
committed
chore(contracts): for fully public contracts, remove proof verification in the shield contracts
1 parent 5a05bff commit 9ad142f

File tree

4 files changed

+143
-107
lines changed

4 files changed

+143
-107
lines changed

src/boilerplate/contract/solidity/nodes/ContractBoilerplateGenerator.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,30 @@ class ContractBoilerplateGenerator {
9292
oldCommitmentAccessRequired,
9393
newCommitmentsRequired,
9494
containsAccessedOnlyState,
95-
encryptionRequired
95+
encryptionRequired,
96+
fullyPublicContract: !scope.indicators.zkSnarkVerificationRequired,
9697
};
9798
},
9899

99100
constructor() {
101+
const { scope } = this;
102+
return {
103+
fullyPublicContract: !scope.indicators.zkSnarkVerificationRequired,
104+
};
100105
},
101106

102107
registerZKPPublicKey() {},
103108

104109
verify(circuitParams: Object ) {
105-
let {
106-
indicators: { nullifiersRequired, oldCommitmentAccessRequired, newCommitmentsRequired, containsAccessedOnlyState, encryptionRequired },
110+
const {
111+
indicators: {
112+
nullifiersRequired,
113+
oldCommitmentAccessRequired,
114+
newCommitmentsRequired,
115+
containsAccessedOnlyState,
116+
encryptionRequired,
117+
zkSnarkVerificationRequired,
118+
},
107119
} = this.scope;
108120
let isjoinSplitCommitmentsFunction : string[]=[];
109121
for(const [, binding ] of Object.entries(this.scope.bindings)){
@@ -177,7 +189,17 @@ class ContractBoilerplateGenerator {
177189
}
178190
}
179191
const constructorContainsSecret = Object.values(this.scope.bindings).some((binding: any) => binding.node.kind === 'constructor');
180-
return { nullifiersRequired, oldCommitmentAccessRequired, newCommitmentsRequired, containsAccessedOnlyState, encryptionRequired, constructorContainsSecret, circuitParams, isjoinSplitCommitmentsFunction};
192+
return {
193+
nullifiersRequired,
194+
oldCommitmentAccessRequired,
195+
newCommitmentsRequired,
196+
containsAccessedOnlyState,
197+
encryptionRequired,
198+
constructorContainsSecret,
199+
circuitParams,
200+
isjoinSplitCommitmentsFunction,
201+
fullyPublicContract: !zkSnarkVerificationRequired,
202+
};
181203
},
182204

183205
};

src/boilerplate/contract/solidity/raw/ContractBoilerplateGenerator.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ class ContractBoilerplateGenerator {
2525
containsAccessedOnlyState,
2626
newCommitmentsRequired,
2727
encryptionRequired,
28+
fullyPublicContract,
2829
//isInternalFunctionCall add it
2930
}): string[] {
3031
// prettier-ignore
3132
// Ignoring prettier because it's easier to read this if the strings we're inserting are at the beginning of a line.
33+
if (fullyPublicContract) {
34+
return [`
35+
mapping(address => uint256) public zkpPublicKeys;`,
36+
];
37+
}
3238
return [
3339
`
3440
enum FunctionNames { ${functionNames.join(', ')} }`,
@@ -78,7 +84,10 @@ class ContractBoilerplateGenerator {
7884
];
7985
},
8086

81-
constructor(): string[] {
87+
constructor({ fullyPublicContract }): string[] {
88+
if (fullyPublicContract) {
89+
return [``];
90+
}
8291
// This boilerplate will only be used if the .zol developer didn't write their own constructor. If they already wrote a constructor, we add this boilerplate in the FunctionBoilerplate generator.
8392
return [
8493
`
@@ -114,7 +123,11 @@ class ContractBoilerplateGenerator {
114123
circuitParams,
115124
constructorContainsSecret,
116125
isjoinSplitCommitmentsFunction,
126+
fullyPublicContract,
117127
}): string[] {
128+
if (fullyPublicContract) {
129+
return [``];
130+
}
118131
const verifyFunctionSignature = `
119132
function verify(
120133
uint256[] ${constructorContainsSecret ? `memory` : `calldata`} proof,

src/codeGenerators/contract/solidity/toContract.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ function codeGenerator(node: any) {
5050
const name = `${node.name}Shield`;
5151
const contractDeclaration = `contract ${name}`;
5252
// TODO: an InheritanceSpecifier is a nodeType in itself, so should be recursed into as its own 'case' in this 'switch' statement.
53-
const inheritanceSpecifiers = node.baseContracts
54-
? ` is ${node.baseContracts
55-
.reduce((acc: string[], cur: any) => {
56-
if (cur.nodeType === 'InheritanceSpecifier') {
57-
acc.push(cur.baseName.name);
58-
}
59-
return acc;
60-
}, [])
61-
.join(', ')}`
62-
: '';
53+
const inheritanceSpecifiers =
54+
node.baseContracts && node.baseContracts.length
55+
? ` is ${node.baseContracts
56+
.reduce((acc: string[], cur: any) => {
57+
if (cur.nodeType === 'InheritanceSpecifier') {
58+
acc.push(cur.baseName.name);
59+
}
60+
return acc;
61+
}, [])
62+
.join(', ')}`
63+
: '';
6364
const nodes = node.nodes.map(codeGenerator).join('\n\n');
6465
return `${contractDeclaration}${inheritanceSpecifiers} {\n\n${nodes}\n}`;
6566
}

src/transformers/visitors/toContractVisitor.ts

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -193,102 +193,102 @@ export default {
193193
}
194194
}
195195

196+
const contractIndex = sourceUnitNodes.findIndex(
197+
(n: any) => n.name === node.name,
198+
);
199+
if (scope.indicators.zkSnarkVerificationRequired) {
200+
sourceUnitNodes[contractIndex].baseContracts.push(
201+
buildNode('InheritanceSpecifier', {
202+
nodeType: 'UserDefinedTypeName',
203+
name: 'MerkleTree',
204+
}),
205+
);
206+
}
207+
sourceUnitNodes.splice(
208+
1,
209+
0,
210+
...buildNode('ContractBoilerplate', {
211+
bpSection: 'importStatements',
212+
scope,
213+
}),
214+
);
196215

197-
const contractIndex = sourceUnitNodes.findIndex(
198-
(n: any) => n.name === node.name,
199-
);
200-
sourceUnitNodes[contractIndex].baseContracts.push(
201-
buildNode('InheritanceSpecifier', {
202-
nodeType: 'UserDefinedTypeName',
203-
name: 'MerkleTree',
204-
}),
205-
);
206-
207-
sourceUnitNodes.splice(
208-
1,
209-
0,
210-
...buildNode('ContractBoilerplate', {
211-
bpSection: 'importStatements',
212-
scope,
213-
}),
214-
);
215-
216-
// unshift in reverse order from how we want them to appear
217-
contractNodes.unshift(
218-
...buildNode('ContractBoilerplate', {
219-
bpSection: 'verify',
220-
scope,
221-
circuitParams: state.circuitParams,
222-
})
223-
);
224-
contractNodes.unshift(
225-
...buildNode('ContractBoilerplate', {
226-
bpSection: 'registerZKPPublicKey',
227-
scope,
228-
}),
229-
);
230-
contractNodes.unshift(
231-
...buildNode('ContractBoilerplate', {
232-
bpSection: 'constructor',
233-
scope,
234-
}),
235-
);
236-
contractNodes.unshift(
237-
...buildNode('ContractBoilerplate', {
238-
bpSection: 'stateVariableDeclarations',
239-
scope,
240-
}),
241-
);
242-
if (state.mainPrivateFunctionName) {
243-
parent._newASTPointer[0].mainPrivateFunctionName =
244-
state.mainPrivateFunctionName; // TODO fix bodge
245-
parent._newASTPointer[0].nodes.forEach((node: any) => {
246-
if (node.nodeType === 'ContractDefinition')
247-
node.mainPrivateFunctionName = state.mainPrivateFunctionName;
248-
});
249-
}
216+
// unshift in reverse order from how we want them to appear
217+
contractNodes.unshift(
218+
...buildNode('ContractBoilerplate', {
219+
bpSection: 'verify',
220+
scope,
221+
circuitParams: state.circuitParams,
222+
})
223+
);
224+
contractNodes.unshift(
225+
...buildNode('ContractBoilerplate', {
226+
bpSection: 'registerZKPPublicKey',
227+
scope,
228+
}),
229+
);
230+
contractNodes.unshift(
231+
...buildNode('ContractBoilerplate', {
232+
bpSection: 'constructor',
233+
scope,
234+
}),
235+
);
236+
contractNodes.unshift(
237+
...buildNode('ContractBoilerplate', {
238+
bpSection: 'stateVariableDeclarations',
239+
scope,
240+
}),
241+
);
242+
if (state.mainPrivateFunctionName) {
243+
parent._newASTPointer[0].mainPrivateFunctionName =
244+
state.mainPrivateFunctionName; // TODO fix bodge
245+
parent._newASTPointer[0].nodes.forEach((node: any) => {
246+
if (node.nodeType === 'ContractDefinition')
247+
node.mainPrivateFunctionName = state.mainPrivateFunctionName;
248+
});
249+
}
250250

251-
node._newASTPointer.forEach(node => {
251+
node._newASTPointer.forEach(node => {
252252

253-
if(node.nodeType === 'FunctionDefinition' && node.kind === 'function'){
254-
state.internalFncName?.forEach( (name, index) => {
255-
if(node.name === name) {
256-
node.msgSigRequired = true;
257-
state.postStatements ??= [];
258-
state.postStatements = cloneDeep(node.body.postStatements);
259-
}
260-
if(node.name === state.callingFncName[index]){
261-
node.body.postStatements.forEach( childNode => {
262-
state.postStatements?.forEach(node => {
263-
if(!childNode.nullifiersRequired && node.nullifiersRequired)
264-
childNode.nullifiersRequired = node.nullifiersRequired;
265-
if(!childNode.oldCommitmentAccessRequired && node.oldCommitmentAccessRequired)
266-
childNode.oldCommitmentAccessRequired = node.oldCommitmentAccessRequired;
267-
if(!childNode.newCommitmentsRequired && node.newCommitmentsRequired)
268-
childNode.newCommitmentsRequired = node.newCommitmentsRequired;
269-
if(!childNode.encryptionRequired && node.encryptionRequired)
270-
childNode.encryptionRequired = node.encryptionRequired;
271-
})
272-
})
273-
node.parameters.parameters.forEach( childNode => {
274-
275-
state.postStatements?.forEach(node => {
276-
if(!childNode.nullifiersRequired && node.nullifiersRequired)
277-
childNode.nullifiersRequired = node.nullifiersRequired;
278-
if(!childNode.oldCommitmentAccessRequired && node.oldCommitmentAccessRequired)
279-
childNode.oldCommitmentAccessRequired = node.oldCommitmentAccessRequired;
280-
if(!childNode.newCommitmentsRequired && node.newCommitmentsRequired)
281-
childNode.newCommitmentsRequired = node.newCommitmentsRequired;
282-
if(!childNode.encryptionRequired && node.encryptionRequired)
283-
childNode.encryptionRequired = node.encryptionRequired;
284-
})
285-
})
286-
}
287-
});
253+
if(node.nodeType === 'FunctionDefinition' && node.kind === 'function'){
254+
state.internalFncName?.forEach( (name, index) => {
255+
if(node.name === name) {
256+
node.msgSigRequired = true;
257+
state.postStatements ??= [];
258+
state.postStatements = cloneDeep(node.body.postStatements);
288259
}
289-
})
290-
},
291-
},
260+
if(node.name === state.callingFncName[index]){
261+
node.body.postStatements.forEach( childNode => {
262+
state.postStatements?.forEach(node => {
263+
if(!childNode.nullifiersRequired && node.nullifiersRequired)
264+
childNode.nullifiersRequired = node.nullifiersRequired;
265+
if(!childNode.oldCommitmentAccessRequired && node.oldCommitmentAccessRequired)
266+
childNode.oldCommitmentAccessRequired = node.oldCommitmentAccessRequired;
267+
if(!childNode.newCommitmentsRequired && node.newCommitmentsRequired)
268+
childNode.newCommitmentsRequired = node.newCommitmentsRequired;
269+
if(!childNode.encryptionRequired && node.encryptionRequired)
270+
childNode.encryptionRequired = node.encryptionRequired;
271+
})
272+
})
273+
node.parameters.parameters.forEach( childNode => {
274+
275+
state.postStatements?.forEach(node => {
276+
if(!childNode.nullifiersRequired && node.nullifiersRequired)
277+
childNode.nullifiersRequired = node.nullifiersRequired;
278+
if(!childNode.oldCommitmentAccessRequired && node.oldCommitmentAccessRequired)
279+
childNode.oldCommitmentAccessRequired = node.oldCommitmentAccessRequired;
280+
if(!childNode.newCommitmentsRequired && node.newCommitmentsRequired)
281+
childNode.newCommitmentsRequired = node.newCommitmentsRequired;
282+
if(!childNode.encryptionRequired && node.encryptionRequired)
283+
childNode.encryptionRequired = node.encryptionRequired;
284+
})
285+
})
286+
}
287+
});
288+
}
289+
})
290+
},
291+
},
292292

293293
FunctionDefinition: {
294294
enter(path: NodePath, state: any) {

0 commit comments

Comments
 (0)