mirror of
https://github.com/selfxyz/self.git
synced 2026-04-27 03:01:15 -04:00
update test scripts
This commit is contained in:
130
circuits/test/registerSha1.test.ts
Normal file
130
circuits/test/registerSha1.test.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
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_sha1WithRSAEncryption_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 - Register flow", function () {
|
||||
this.timeout(0);
|
||||
let inputs: any;
|
||||
let circuit: any;
|
||||
let passportData = mockPassportData_sha1WithRSAEncryption_65537;
|
||||
let attestation_id: string;
|
||||
|
||||
before(async () => {
|
||||
circuit = await wasm_tester(
|
||||
path.join(__dirname, "../circuits/register_sha1WithRSAEncryption_65537.circom"),
|
||||
{
|
||||
include: [
|
||||
"node_modules",
|
||||
"./node_modules/@zk-kit/binary-merkle-root.circom/src",
|
||||
"./node_modules/circomlib/circuits",
|
||||
"./node_modules/dmpierre/sha1-circom/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,
|
||||
{ developmentMode: true }
|
||||
);
|
||||
});
|
||||
|
||||
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 () {
|
||||
const w = await circuit.calculateWitness(inputs);
|
||||
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");
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,11 +1,11 @@
|
||||
// Copied from zk-email cuz it uses crypto so can't import it here.
|
||||
|
||||
export function shaPad(signatureAlgorithm: string, prehash_prepad_m: Uint8Array, maxShaBytes: number): [Uint8Array, number] {
|
||||
if (signatureAlgorithm == 'sha1WithRSAEncryption') {
|
||||
return sha1Pad(prehash_prepad_m, maxShaBytes);
|
||||
} else {
|
||||
// if (signatureAlgorithm == 'sha1WithRSAEncryption') {
|
||||
// return sha1Pad(prehash_prepad_m, maxShaBytes);
|
||||
// } else {
|
||||
return sha256Pad(prehash_prepad_m, maxShaBytes);
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
// Puts an end selector, a bunch of 0s, then the length, then fill the rest with 0s.
|
||||
@@ -13,12 +13,13 @@ export function sha1Pad(prehash_prepad_m: Uint8Array, maxShaBytes: number): [Uin
|
||||
let length_bits = prehash_prepad_m.length * 8; // bytes to bits
|
||||
let length_in_bytes = int64toBytes(length_bits);
|
||||
prehash_prepad_m = mergeUInt8Arrays(prehash_prepad_m, int8toBytes(2 ** 7)); // Add the 1 on the end, length 505
|
||||
// while ((prehash_prepad_m.length * 8 + length_in_bytes.length * 8) % 512 !== 0) {
|
||||
while ((prehash_prepad_m.length * 8 + length_in_bytes.length * 8) % 320 !== 0) {
|
||||
// while ((prehash_prepad_m.length * 8 + length_in_bytes.length * 8) % 320 !== 0) {
|
||||
while ((prehash_prepad_m.length * 8 + length_in_bytes.length * 8) % 512 !== 0) {
|
||||
prehash_prepad_m = mergeUInt8Arrays(prehash_prepad_m, int8toBytes(0));
|
||||
}
|
||||
prehash_prepad_m = mergeUInt8Arrays(prehash_prepad_m, length_in_bytes);
|
||||
assert((prehash_prepad_m.length * 8) % 320 === 0, "Padding did not complete properly!");
|
||||
assert((prehash_prepad_m.length * 8) % 512 === 0, "Padding did not complete properly!");
|
||||
// assert((prehash_prepad_m.length * 8) % 320 === 0, "Padding did not complete properly!");
|
||||
let messageLen = prehash_prepad_m.length;
|
||||
while (prehash_prepad_m.length < maxShaBytes) {
|
||||
prehash_prepad_m = mergeUInt8Arrays(prehash_prepad_m, int64toBytes(0));
|
||||
|
||||
Reference in New Issue
Block a user