mirror of
https://github.com/selfxyz/self.git
synced 2026-04-27 03:01:15 -04:00
132 lines
4.9 KiB
TypeScript
132 lines
4.9 KiB
TypeScript
import { describe } from 'mocha'
|
|
import { assert, expect } from 'chai'
|
|
import path from "path";
|
|
const wasm_tester = require("circom_tester").wasm;
|
|
import { poseidon1, poseidon6 } from "poseidon-lite";
|
|
import { mockPassportData_sha256WithRSASSAPSS_65537 } from "../../common/src/utils/mockPassportData";
|
|
import { generateCircuitInputsRegister } from '../../common/src/utils/generateInputs';
|
|
import { getLeaf } from '../../common/src/utils/pubkeyTree';
|
|
import { packBytes } from '../../common/src/utils/utils';
|
|
|
|
describe("Proof of Passport - Circuits - RSASSAPSS", function () {
|
|
this.timeout(0);
|
|
let inputs: any;
|
|
let circuit: any;
|
|
let passportData = mockPassportData_sha256WithRSASSAPSS_65537;
|
|
let attestation_id: string;
|
|
|
|
before(async () => {
|
|
circuit = await wasm_tester(
|
|
path.join(__dirname, "../circuits/register_sha256WithRSASSAPSS_65537.circom"),
|
|
{
|
|
include: [
|
|
"node_modules",
|
|
"node_modules/@zk-email/circuits/helpers/sha.circom",
|
|
"./node_modules/@zk-kit/binary-merkle-root.circom/src",
|
|
"./node_modules/circomlib/circuits"
|
|
]
|
|
},
|
|
);
|
|
|
|
const secret = BigInt(Math.floor(Math.random() * Math.pow(2, 254))).toString();
|
|
console.log("secret", secret);
|
|
|
|
const attestation_name = "E-PASSPORT";
|
|
attestation_id = poseidon1([
|
|
BigInt(Buffer.from(attestation_name).readUIntBE(0, 6))
|
|
]).toString();
|
|
|
|
inputs = generateCircuitInputsRegister(
|
|
secret,
|
|
attestation_id,
|
|
passportData,
|
|
[passportData],
|
|
);
|
|
});
|
|
|
|
it("should compile and load the circuit", async function () {
|
|
expect(circuit).to.not.be.undefined;
|
|
});
|
|
|
|
it("should calculate the witness with correct inputs", async function () {
|
|
console.time('calculateWitness')
|
|
const w = await circuit.calculateWitness(inputs);
|
|
console.timeEnd('calculateWitness')
|
|
await circuit.checkConstraints(w);
|
|
|
|
console.log("nullifier", (await circuit.getOutput(w, ["nullifier"])).nullifier);
|
|
|
|
const commitment_circom = (await circuit.getOutput(w, ["commitment"])).commitment;
|
|
|
|
const mrz_bytes = packBytes(inputs.mrz);
|
|
const commitment_bytes = poseidon6([
|
|
inputs.secret[0],
|
|
attestation_id,
|
|
getLeaf({
|
|
signatureAlgorithm: passportData.signatureAlgorithm,
|
|
modulus: passportData.pubKey.modulus,
|
|
exponent: passportData.pubKey.exponent
|
|
}),
|
|
mrz_bytes[0],
|
|
mrz_bytes[1],
|
|
mrz_bytes[2]
|
|
]);
|
|
const commitment_js = commitment_bytes.toString();
|
|
console.log('commitment_js', commitment_js)
|
|
console.log('commitment_circom', commitment_circom)
|
|
expect(commitment_circom).to.be.equal(commitment_js);
|
|
});
|
|
|
|
it("should fail to calculate witness with invalid mrz", async function () {
|
|
try {
|
|
const invalidInputs = {
|
|
...inputs,
|
|
mrz: Array(93).fill(0).map(byte => BigInt(byte).toString())
|
|
}
|
|
await circuit.calculateWitness(invalidInputs);
|
|
expect.fail("Expected an error but none was thrown.");
|
|
} catch (error) {
|
|
expect(error.message).to.include("Assert Failed");
|
|
}
|
|
});
|
|
|
|
it("should fail to calculate witness with invalid econtent", async function () {
|
|
try {
|
|
const invalidInputs = {
|
|
...inputs,
|
|
econtent: inputs.econtent.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)),
|
|
}
|
|
await circuit.calculateWitness(invalidInputs);
|
|
expect.fail("Expected an error but none was thrown.");
|
|
} catch (error) {
|
|
expect(error.message).to.include("Assert Failed");
|
|
}
|
|
});
|
|
|
|
it("should fail to calculate witness with invalid signature", async function () {
|
|
try {
|
|
const invalidInputs = {
|
|
...inputs,
|
|
signature: inputs.signature.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)),
|
|
}
|
|
await circuit.calculateWitness(invalidInputs);
|
|
expect.fail("Expected an error but none was thrown.");
|
|
} catch (error) {
|
|
expect(error.message).to.include("Assert Failed");
|
|
}
|
|
});
|
|
|
|
it("should fail to calculate witness with invalid merkle root", async function () {
|
|
try {
|
|
const invalidInputs = {
|
|
...inputs,
|
|
merkle_root: inputs.merkle_root.map((byte: string) => String((parseInt(byte, 10) + 1) % 256)),
|
|
}
|
|
await circuit.calculateWitness(invalidInputs);
|
|
expect.fail("Expected an error but none was thrown.");
|
|
} catch (error) {
|
|
expect(error.message).to.include("Assert Failed");
|
|
}
|
|
});
|
|
|
|
}); |