update circuits inputs, clean

This commit is contained in:
turnoffthiscomputer
2024-05-10 07:23:18 +02:00
committed by 0xturboblitz
parent aa304a8366
commit ee9e495ffb
4 changed files with 73 additions and 61 deletions

View File

@@ -3,12 +3,13 @@ import { assert, shaPad } from "./shaPad";
import { PassportData } from "./types";
import {
arraysAreEqual, bytesToBigDecimal, formatMrz, formatSigAlg, hash, splitToWords,
toUnsignedByte, getDigestLengthBytes
toUnsignedByte, getDigestLengthBytes, getCurrentDateYYMMDD
} from "./utils";
import { IMT } from "@zk-kit/imt";
import { getLeaf } from "./pubkeyTree";
import serializedTree from "../../pubkeys/serialized_tree.json";
import { poseidon2 } from "poseidon-lite";
import { packBytes } from "../utils/utils";
export function generateCircuitInputs_Register(
passportData: PassportData,
@@ -33,7 +34,7 @@ export function generateCircuitInputs_Register(
const formattedMrz = formatMrz(passportData.mrz);
const concatenatedDataHashesHashDigest = hash(passportData.signatureAlgorithm, passportData.dataGroupHashes);
console.log('concatenatedDataHashesHashDigest', concatenatedDataHashesHashDigest);
// console.log('concatenatedDataHashesHashDigest', concatenatedDataHashesHashDigest);
assert(
arraysAreEqual(passportData.eContent.slice(72, 72 + getDigestLengthBytes(passportData.signatureAlgorithm)),
@@ -41,7 +42,7 @@ export function generateCircuitInputs_Register(
'concatenatedDataHashesHashDigest is at the right place in passportData.eContent'
);
console.log('passportData.pubKey.exponent', passportData.pubKey.exponent);
// console.log('passportData.pubKey.exponent', passportData.pubKey.exponent);
const sigAlgFormatted = formatSigAlg(passportData.signatureAlgorithm, passportData.pubKey.exponent);
@@ -49,16 +50,16 @@ export function generateCircuitInputs_Register(
signatureAlgorithm: passportData.signatureAlgorithm,
...passportData.pubKey,
}).toString();
console.log('leaf', leaf);
// console.log('leaf', leaf);
const index = tree.indexOf(leaf);
console.log(`Index of pubkey in the registry: ${index}`);
// console.log(`Index of pubkey in the registry: ${index}`);
if (index === -1) {
throw new Error("Your public key was not found in the registry");
}
const proof = tree.createProof(index);
console.log("verifyProof", tree.verifyProof(proof));
// console.log("verifyProof", tree.verifyProof(proof));
if (passportData.dataGroupHashes.length > MAX_DATAHASHES_LEN) {
console.error(`Data hashes too long. Max length is ${MAX_DATAHASHES_LEN} bytes.`);
@@ -93,3 +94,45 @@ export function generateCircuitInputs_Register(
siblings: proof.siblings.flat().map(index => index.toString()),
};
}
export function generateCircuitInputs_Disclose(
poseidon: any, secret: number, mrz: any, merkletree: IMT, majority: [number, number], address: string
) {
// const tree = new IMT(poseidon2, TREE_DEPTH, 0, 2);
// tree.setNodes(serializedTree);
// if (options.developmentMode) {
// tree.insert(getLeaf({
// signatureAlgorithm: passportData.signatureAlgorithm,
// issuer: 'C = TS, O = Government of Syldavia, OU = Ministry of tests, CN = CSCA-TEST',
// modulus: passportData.pubKey.modulus,
// exponent: passportData.pubKey.exponent
// }).toString());
// }
const formattedMrz = formatMrz(mrz);
const mrz_bytes = packBytes(formattedMrz);
const commitment_bytes = poseidon([secret, mrz_bytes[0], mrz_bytes[1], mrz_bytes[2]]);
const commitment = BigInt(poseidon.F.toString(commitment_bytes)).toString();
const index = merkletree.indexOf(commitment);
console.log(`Index of pubkey in the registry: ${index}`);
if (index === -1) {
throw new Error("Your public key was not found in the registry");
}
const proof = merkletree.createProof(index);
console.log("verifyProof", merkletree.verifyProof(proof));
return {
secret: [BigInt(0).toString()],
commitment: commitment,
mrz: formattedMrz.map(byte => String(byte)),
merkle_root: [merkletree.root.toString()],
path: proof.pathIndices.map(index => index.toString()),
siblings: proof.siblings.flat().map(index => index.toString()),
bitmap: Array(90).fill(1).map(num => num.toString()),
scope: [BigInt(0).toString()],
current_date: getCurrentDateYYMMDD().map(datePart => BigInt(datePart).toString()),
majority: majority.map(num => BigInt(num).toString()),
address: [BigInt(address).toString()],
};
}