Skip to content

Commit

Permalink
Merge pull request #334 from EYBlockchain/swati/nullifierFix
Browse files Browse the repository at this point in the history
Swati/nullifier fix
  • Loading branch information
SwatiEY authored Sep 16, 2024
2 parents 09ed671 + dac56a7 commit 5a5e9df
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 400 deletions.
31 changes: 1 addition & 30 deletions circuits/common/joinCommitments.zok
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,16 @@ from "./common/hashes/poseidon/poseidon.zok" import main as poseidon
from "./common/casts/u32_array_to_field.zok" import main as u32_array_to_field
from "utils/pack/bool/pack256.zok" import main as bool_256_to_field
from "./common/merkle-tree/mimc/altbn254/verify-membership/height32.zok" import main as checkRoot
from "./common/merkle-tree/sparse-merkle-tree/checkproof.zok" import checkUpdatedPath as checkUpdatedPath
from "./common/merkle-tree/sparse-merkle-tree/checkproof.zok" import main as checkproof


def main(\
private field fromId,\
private field stateVarId,\
private bool isMapping,\
private field oldCommitment_0_owner_secretKey_field,\
private field oldCommitment_1_owner_secretKey_field,\
public field nullifierRoot,\
public field newNullifierRoot,\
public field oldCommitment_0_nullifier,\
private field[32] oldCommitment_0_nullifier_nonmembershipWitness_siblingPath,\
private field[32] oldCommitment_0_nullifier_nonmembershipWitness_newsiblingPath,\
public field oldCommitment_1_nullifier,\
private field[32] oldCommitment_1_nullifier_nonmembershipWitness_siblingPath,\
private field[32] oldCommitment_1_nullifier_nonmembershipWitness_newsiblingPath,\
private field oldCommitment_0_value,\
private field oldCommitment_0_salt_field,\
private field oldCommitment_1_value,\
Expand Down Expand Up @@ -94,17 +87,6 @@ def main(\
field_to_bool_256(oldCommitment_0_nullifier)[8..256] == field_to_bool_256(oldCommitment_0_nullifier_check_field)[8..256]\
)

assert(\
nullifierRoot == checkproof(\
oldCommitment_0_nullifier_nonmembershipWitness_siblingPath,\
oldCommitment_0_nullifier\
) )

assert( newNullifierRoot == checkUpdatedPath(\
oldCommitment_0_nullifier_nonmembershipWitness_newsiblingPath,\
oldCommitment_0_nullifier) )


// Nullify oldCommitment_1:

field oldCommitment_1_nullifier_check_field = poseidon([\
Expand All @@ -117,17 +99,6 @@ def main(\
field_to_bool_256(oldCommitment_1_nullifier)[8..256] == field_to_bool_256(oldCommitment_1_nullifier_check_field)[8..256]\
)

assert(\
nullifierRoot == checkproof(\
oldCommitment_1_nullifier_nonmembershipWitness_siblingPath,\
oldCommitment_1_nullifier\
) )

assert( newNullifierRoot == checkUpdatedPath(\
oldCommitment_1_nullifier_nonmembershipWitness_newsiblingPath,\
oldCommitment_1_nullifier) )


// oldCommitment_0_commitment: preimage check

field oldCommitment_0_commitment_field = poseidon([\
Expand Down
16 changes: 1 addition & 15 deletions circuits/common/splitCommitments.zok
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@ from "./common/hashes/poseidon/poseidon.zok" import main as poseidon
from "./common/casts/u32_array_to_field.zok" import main as u32_array_to_field
from "utils/pack/bool/pack256.zok" import main as bool_256_to_field
from "./common/merkle-tree/mimc/altbn254/verify-membership/height32.zok" import main as checkRoot
from "./common/merkle-tree/sparse-merkle-tree/checkproof.zok" import checkUpdatedPath as checkUpdatedPath
from "./common/merkle-tree/sparse-merkle-tree/checkproof.zok" import main as checkproof


def main(\
private field value,\
private field fromId,\
private field stateVarId,\
private bool isMapping,\
private field oldCommitment_0_owner_secretKey_field,\
public field nullifierRoot,\
public field newNullifierRoot,\
public field oldCommitment_0_nullifier,\
private field[32] oldCommitment_0_nullifier_nonmembershipWitness_siblingPath,\
private field[32] oldCommitment_0_nullifier_nonmembershipWitness_newsiblingPath,\
private field oldCommitment_0_value,\
private field oldCommitment_0_salt_field,\
public field commitmentRoot,\
Expand Down Expand Up @@ -74,15 +69,6 @@ def main(\
field_to_bool_256(oldCommitment_0_nullifier)[8..256] == field_to_bool_256(oldCommitment_0_nullifier_check_field)[8..256]\
)

assert(\
nullifierRoot == checkproof(\
oldCommitment_0_nullifier_nonmembershipWitness_siblingPath,\
oldCommitment_0_nullifier\
) )

assert( newNullifierRoot == checkUpdatedPath(oldCommitment_0_nullifier_nonmembershipWitness_newsiblingPath,\
oldCommitment_0_nullifier) )

// oldCommitment_0_commitment: preimage check

field oldCommitment_0_commitment_field = poseidon([\
Expand Down
47 changes: 0 additions & 47 deletions src/boilerplate/circuit/zokrates/raw/BoilerplateGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,9 @@ class BoilerplateGenerator {
parameters({ name: x, isAccessed, isNullified }): string[] {
let para = [
`private field ${x}_oldCommitment_owner_secretKey`,
`public field nullifierRoot`,
`public field newNullifierRoot`,
`public field ${x}_oldCommitment_nullifier`,
`private field[32] ${x}_nullifier_nonmembershipWitness_siblingPath`,
`private field[32] ${x}_nullifier_nonmembershipWitness_newsiblingPath`,

]
if(isAccessed && !isNullified)
para = [
`private field ${x}_oldCommitment_owner_secretKey`,
`public field nullifierRoot`,
`private field[32] ${x}_nullifier_nonmembershipWitness_siblingPath`,
]

return para;
},

Expand Down Expand Up @@ -104,42 +93,6 @@ class BoilerplateGenerator {
)
// ${x}_oldCommitment_nullifier : non-existence check
assert(\\
nullifierRoot == checkproof(\\
${x}_nullifier_nonmembershipWitness_siblingPath,\\
${x}_oldCommitment_nullifier\\
)\
)
assert(\\
newNullifierRoot == checkUpdatedPath(\\
${x}_nullifier_nonmembershipWitness_newsiblingPath,\\
${x}_oldCommitment_nullifier\\
)\
)
`,
];

if(isAccessed && !isNullified)
lines = [
`
// Create the Nullifier for ${x} and no need to nullify it as its accessed only:
field ${x}_oldCommitment_nullifier_check_field = poseidon([\\
${x}_stateVarId_field,\\
${x}_oldCommitment_owner_secretKey,\\
${x}_oldCommitment_salt\\
])
// ${x}_oldCommitment_nullifier : non-existence check
assert(\\
nullifierRoot == checkproof(\\
${x}_nullifier_nonmembershipWitness_siblingPath,\\
${x}_oldCommitment_nullifier_check_field\\
)\
)
`,
];

Expand Down
82 changes: 3 additions & 79 deletions src/boilerplate/common/commitment-storage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export async function markNullified(commitmentHash, secretKey = null) {
};
// updating the original tree
// eslint-disable-next-line camelcase
smt_tree = temp_smt_tree;
//smt_tree = temp_smt_tree;

return db.collection(COMMITMENTS_COLLECTION).updateOne(query, update);
}
Expand Down Expand Up @@ -446,43 +446,7 @@ export async function joinCommitments(
oldCommitment_0_nullifier = generalise(oldCommitment_0_nullifier.hex(32)); // truncate
oldCommitment_1_nullifier = generalise(oldCommitment_1_nullifier.hex(32)); // truncate

// Non-membership witness for Nullifier
const oldCommitment_0_nullifier_NonMembership_witness = getnullifierMembershipWitness(
oldCommitment_0_nullifier,
);
const oldCommitment_1_nullifier_NonMembership_witness = getnullifierMembershipWitness(
oldCommitment_1_nullifier,
);

const oldCommitment_nullifierRoot = generalise(
oldCommitment_0_nullifier_NonMembership_witness.root,
);
const oldCommitment_0_nullifier_path = generalise(
oldCommitment_0_nullifier_NonMembership_witness.path,
).all;
const oldCommitment_1_nullifier_path = generalise(
oldCommitment_1_nullifier_NonMembership_witness.path,
).all;

await temporaryUpdateNullifier(a_0_nullifier);
await temporaryUpdateNullifier(a_1_nullifier);

const oldCommitment_0_updated_nullifier_NonMembership_witness = getupdatedNullifierPaths(
oldCommitment_0_nullifier,
);
const oldCommitment_1_updated_nullifier_NonMembership_witness = getupdatedNullifierPaths(
oldCommitment_1_nullifier,
);

const oldCommitment_0_nullifier_newpath = generalise(
oldCommitment_0_updated_nullifier_NonMembership_witness.path,
).all;
const oldCommitment_1_nullifier_newpath = generalise(
oldCommitment_1_updated_nullifier_NonMembership_witness.path,
).all;
const oldCommitment_newNullifierRoot = generalise(
oldCommitment_0_updated_nullifier_NonMembership_witness.root,
);

// Calculate commitment(s):

const newCommitment_newSalt = generalise(utils.randomHex(31));
Expand Down Expand Up @@ -519,14 +483,8 @@ export async function joinCommitments(
secretKey.integer,
secretKey.integer,

oldCommitment_nullifierRoot.integer,
oldCommitment_newNullifierRoot.integer,
oldCommitment_0_nullifier.integer,
oldCommitment_0_nullifier_path.integer,
oldCommitment_0_nullifier_newpath.integer,
oldCommitment_1_nullifier.integer,
oldCommitment_1_nullifier_path.integer,
oldCommitment_1_nullifier_newpath.integer,
oldCommitment_0_prev.integer,
oldCommitment_0_prevSalt.integer,
oldCommitment_1_prev.integer,
Expand All @@ -549,8 +507,6 @@ export async function joinCommitments(

const txData = await instance.methods
.joinCommitments(
oldCommitment_nullifierRoot.integer,
oldCommitment_newNullifierRoot.integer,
[oldCommitment_0_nullifier.integer, oldCommitment_1_nullifier.integer],
oldCommitment_root.integer,
[newCommitment.integer],
Expand Down Expand Up @@ -646,32 +602,7 @@ export async function splitCommitments(

oldCommitment_0_nullifier = generalise(oldCommitment_0_nullifier.hex(32)); // truncate

// Non-membership witness for Nullifier
const oldCommitment_0_nullifier_NonMembership_witness = getnullifierMembershipWitness(
oldCommitment_0_nullifier,
);

const oldCommitment_nullifierRoot = generalise(
oldCommitment_0_nullifier_NonMembership_witness.root,
);
const oldCommitment_0_nullifier_path = generalise(
oldCommitment_0_nullifier_NonMembership_witness.path,
).all;

await temporaryUpdateNullifier(oldCommitment_0_nullifier);

const oldCommitment_0_updated_nullifier_NonMembership_witness = getupdatedNullifierPaths(
oldCommitment_0_nullifier,
);

const oldCommitment_0_nullifier_newpath = generalise(
oldCommitment_0_updated_nullifier_NonMembership_witness.path,
).all;

const oldCommitment_newNullifierRoot = generalise(
oldCommitment_0_updated_nullifier_NonMembership_witness.root,
);
// Calculate commitment(s):
// Calculate commitment(s):

const newCommitment_0_newSalt = generalise(utils.randomHex(31));

Expand Down Expand Up @@ -720,12 +651,7 @@ export async function splitCommitments(
stateVarID,
isMapping,
secretKey.integer,

oldCommitment_nullifierRoot.integer,
oldCommitment_newNullifierRoot.integer,
oldCommitment_0_nullifier.integer,
oldCommitment_0_nullifier_path.integer,
oldCommitment_0_nullifier_newpath.integer,
oldCommitment_0_prev.integer,
oldCommitment_0_prevSalt.integer,
oldCommitment_root.integer,
Expand All @@ -747,8 +673,6 @@ export async function splitCommitments(

const txData = await instance.methods
.splitCommitments(
oldCommitment_nullifierRoot.integer,
oldCommitment_newNullifierRoot.integer,
[oldCommitment_0_nullifier.integer],
oldCommitment_root.integer,
[newCommitment_0.integer, newCommitment_1.integer],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ class ContractBoilerplateGenerator {
},

constructor() {
const {indicators: { nullifiersRequired }} = this.scope;
return { nullifiersRequired };
},

registerZKPPublicKey() {},
Expand Down Expand Up @@ -129,14 +127,12 @@ class ContractBoilerplateGenerator {
params?.forEach(circuitParamNode => {
switch (circuitParamNode.bpType) {
case 'nullification':
if (!newList.includes('nullifierRoot'))
newList.push('nullifierRoot');
if (circuitParamNode.isNullified) {
if (!newList.includes('newNullifierRoot'))
newList.push('newNullifierRoot');
newList.push('nullifier');

}
} else {
// we use a nullification node for accessed, not nullified, states
newList.push('checkNullifier')
}
break;
case 'newCommitment':
newList.push(circuitParamNode.bpType);
Expand Down
Loading

0 comments on commit 5a5e9df

Please sign in to comment.