Skip to content

Commit

Permalink
fix circuit tests due to changes
Browse files Browse the repository at this point in the history
Signed-off-by: Chengxuan Xing <chengxuan.xing@kaleido.io>
  • Loading branch information
Chengxuan committed Sep 26, 2024
1 parent 273cf81 commit e057865
Show file tree
Hide file tree
Showing 4 changed files with 435 additions and 118 deletions.
120 changes: 96 additions & 24 deletions zkp/js/test/anon_enc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@
const { expect } = require('chai');
const { join } = require('path');
const { wasm: wasm_tester } = require('circom_tester');
const { genRandomSalt, genKeypair, genEcdhSharedKey, formatPrivKeyForBabyJub, stringifyBigInts } = require('maci-crypto');
const { Poseidon, newSalt, poseidonDecrypt, newEncryptionNonce } = require('../index.js');
const {
genRandomSalt,
genKeypair,
genEcdhSharedKey,
formatPrivKeyForBabyJub,
stringifyBigInts,
} = require('maci-crypto');
const {
Poseidon,
newSalt,
poseidonDecrypt,
newEncryptionNonce,
} = require('../index.js');

const poseidonHash = Poseidon.poseidon4;

Expand All @@ -31,7 +42,9 @@ describe('main circuit tests for Zeto fungible tokens with anonymity with encryp
before(async function () {
this.timeout(60000);

circuit = await wasm_tester(join(__dirname, '../../circuits/anon_enc.circom'));
circuit = await wasm_tester(
join(__dirname, '../../circuits/anon_enc.circom')
);

let keypair = genKeypair();
sender.privKey = keypair.privKey;
Expand All @@ -48,16 +61,32 @@ describe('main circuit tests for Zeto fungible tokens with anonymity with encryp

// create two input UTXOs, each has their own salt, but same owner
const salt1 = newSalt();
const input1 = poseidonHash([BigInt(inputValues[0]), salt1, ...sender.pubKey]);
const input1 = poseidonHash([
BigInt(inputValues[0]),
salt1,
...sender.pubKey,
]);
const salt2 = newSalt();
const input2 = poseidonHash([BigInt(inputValues[1]), salt2, ...sender.pubKey]);
const input2 = poseidonHash([
BigInt(inputValues[1]),
salt2,
...sender.pubKey,
]);
const inputCommitments = [input1, input2];

// create two output UTXOs, they share the same salt, and different owner
const salt3 = newSalt();
const output1 = poseidonHash([BigInt(outputValues[0]), salt3, ...receiver.pubKey]);
const output1 = poseidonHash([
BigInt(outputValues[0]),
salt3,
...receiver.pubKey,
]);
const salt4 = newSalt();
const output2 = poseidonHash([BigInt(outputValues[1]), salt4, ...sender.pubKey]);
const output2 = poseidonHash([
BigInt(outputValues[1]),
salt4,
...sender.pubKey,
]);
const outputCommitments = [output1, output2];

const encryptionNonce = newEncryptionNonce();
Expand Down Expand Up @@ -87,18 +116,27 @@ describe('main circuit tests for Zeto fungible tokens with anonymity with encryp
// console.log('receiverPublicKey', receiverPubKey);
// console.log('encryptionNonce', encryptionNonce);

expect(witness[5]).to.equal(BigInt(inputCommitments[0]));
expect(witness[6]).to.equal(BigInt(inputCommitments[1]));
expect(witness[7]).to.equal(BigInt(outputCommitments[0]));
expect(witness[8]).to.equal(BigInt(outputCommitments[1]));
expect(witness[8]).to.equal(BigInt(inputCommitments[0]));
expect(witness[9]).to.equal(BigInt(inputCommitments[1]));
expect(witness[10]).to.equal(BigInt(outputCommitments[0]));
expect(witness[11]).to.equal(BigInt(outputCommitments[1]));

// take the output from the proof circuit and attempt to decrypt
// as the receiver
const cipherText = witness.slice(1, 5);
const cipherText = witness.slice(1, 8);
const recoveredKey = genEcdhSharedKey(receiver.privKey, sender.pubKey);
const plainText = poseidonDecrypt(cipherText, recoveredKey, encryptionNonce, 2);
const plainText = poseidonDecrypt(
cipherText,
recoveredKey,
encryptionNonce,
4
);
// use the recovered value (plainText[0]) and salt (plainText[1]) to verify the output commitment
const calculatedHash = poseidonHash([BigInt(plainText[0]), BigInt(plainText[1]), ...receiver.pubKey]);
const calculatedHash = poseidonHash([
BigInt(plainText[0]),
BigInt(plainText[1]),
...receiver.pubKey,
]);
expect(calculatedHash).to.equal(outputCommitments[0]);
});

Expand All @@ -107,15 +145,31 @@ describe('main circuit tests for Zeto fungible tokens with anonymity with encryp
const outputValues = [90, 35];
// create two input UTXOs, each has their own salt, but same owner
const salt1 = newSalt();
const input1 = poseidonHash([BigInt(inputValues[0]), salt1, ...sender.pubKey]);
const input1 = poseidonHash([
BigInt(inputValues[0]),
salt1,
...sender.pubKey,
]);
const salt2 = newSalt();
const input2 = poseidonHash([BigInt(inputValues[1]), salt2, ...sender.pubKey]);
const input2 = poseidonHash([
BigInt(inputValues[1]),
salt2,
...sender.pubKey,
]);
const inputCommitments = [input1, input2];

// create two output UTXOs, they share the same salt, and different owner
const salt3 = newSalt();
const output1 = poseidonHash([BigInt(outputValues[0]), salt3, ...receiver.pubKey]);
const output2 = poseidonHash([BigInt(outputValues[1]), salt3, ...sender.pubKey]);
const output1 = poseidonHash([
BigInt(outputValues[0]),
salt3,
...receiver.pubKey,
]);
const output2 = poseidonHash([
BigInt(outputValues[1]),
salt3,
...sender.pubKey,
]);
const outputCommitments = [output1, output2];

const encryptionNonce = newEncryptionNonce();
Expand Down Expand Up @@ -143,7 +197,7 @@ describe('main circuit tests for Zeto fungible tokens with anonymity with encryp
err = e;
}
// console.log(err);
expect(err).to.match(/Error in template Zeto_105 line: 78/);
expect(err).to.match(/Error in template Zeto_105 line: 84/);
});

it('should failed to match output UTXO after decrypting the cipher texts from the events if using the wrong sender public keys', async () => {
Expand All @@ -152,16 +206,32 @@ describe('main circuit tests for Zeto fungible tokens with anonymity with encryp

// create two input UTXOs, each has their own salt, but same owner
const salt1 = newSalt();
const input1 = poseidonHash([BigInt(inputValues[0]), salt1, ...sender.pubKey]);
const input1 = poseidonHash([
BigInt(inputValues[0]),
salt1,
...sender.pubKey,
]);
const salt2 = newSalt();
const input2 = poseidonHash([BigInt(inputValues[1]), salt2, ...sender.pubKey]);
const input2 = poseidonHash([
BigInt(inputValues[1]),
salt2,
...sender.pubKey,
]);
const inputCommitments = [input1, input2];

// create two output UTXOs, they share the same salt, and different owner
const salt3 = newSalt();
const output1 = poseidonHash([BigInt(outputValues[0]), salt3, ...receiver.pubKey]);
const output1 = poseidonHash([
BigInt(outputValues[0]),
salt3,
...receiver.pubKey,
]);
const salt4 = newSalt();
const output2 = poseidonHash([BigInt(outputValues[1]), salt4, ...sender.pubKey]);
const output2 = poseidonHash([
BigInt(outputValues[1]),
salt4,
...sender.pubKey,
]);
const outputCommitments = [output1, output2];

const encryptionNonce = newEncryptionNonce();
Expand Down Expand Up @@ -192,6 +262,8 @@ describe('main circuit tests for Zeto fungible tokens with anonymity with encryp
// the decryption scheme has self-checking mechanism, so it should throw an error
expect(function () {
poseidonDecrypt(cipherText, recoveredKey, encryptionNonce, 2);
}).to.throw('The last ciphertext element must match the second item of the permuted state');
}).to.throw(
'The last ciphertext element must match the second item of the permuted state'
);
});
});
Loading

0 comments on commit e057865

Please sign in to comment.