onboard merkle tree serialization

This commit is contained in:
turnoffthiscomputer
2024-06-24 23:10:32 +02:00
parent cb2a5e911a
commit f94065ab12
4 changed files with 19 additions and 17 deletions

File diff suppressed because one or more lines are too long

View File

@@ -1,9 +1,10 @@
import { sha256Pad } from "./shaPad";
import forge from "node-forge";
import * as forge from "node-forge";
import { splitToWords } from "./utils";
import { CSCA_AKI_MODULUS, CSCA_TREE_DEPTH, PUBKEY_TREE_DEPTH } from "../constants/constants";
import { poseidon16, poseidon2, poseidon3 } from "poseidon-lite";
import { IMT } from "@zk-kit/imt";
import serialized_csca_tree from "../../pubkeys/serialized_csca_tree.json"
export function findStartIndex(modulus: string, messagePadded: Uint8Array): number {
@@ -152,20 +153,9 @@ export function computeLeafFromModulus(modulus_formatted: string[]) {
}
export function getCSCAModulusProof(leaf, n, k) {
const tree = new IMT(poseidon2, CSCA_TREE_DEPTH, 0, 2);
const csca_modulus_array = Object.values(CSCA_AKI_MODULUS);
const csca_modulus_array_number = csca_modulus_array.map((modulus) => {
const cleanedModulus = modulus.replace(/:/g, ''); // Remove colons
return BigInt(`0x${cleanedModulus}`);
});
const csca_modulus_formatted = csca_modulus_array_number.map((modulus) => splitToWords(modulus, BigInt(n), BigInt(k)));
const hashedModuliGroups = [];
for (let i = 0; i < csca_modulus_formatted.length; i++) {
const finalPoseidonHash = computeLeafFromModulus(csca_modulus_formatted[i]);
hashedModuliGroups.push(finalPoseidonHash.toString());
tree.insert(finalPoseidonHash.toString());
}
let tree = new IMT(poseidon2, CSCA_TREE_DEPTH, 0, 2);
tree.setNodes(serialized_csca_tree);
//const tree = getCSCAModulusMerkleTree(n, k);
const index = tree.indexOf(leaf);
if (index === -1) {
throw new Error("Your public key was not found in the registry");

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,9 @@
import * as fs from 'fs';
import { buildPubkeyTree } from '../../../common/src/utils/pubkeyTree'
import { getCSCAModulusMerkleTree } from '../../../common/src/utils/csca'
import { k_csca, n_csca } from '../../../common/src/constants/constants';
async function main() {
async function serialize_old_dsc_modulus_tree() {
const pubkeys = JSON.parse(fs.readFileSync("../common/pubkeys/public_keys_parsed.json") as unknown as string)
const tree = buildPubkeyTree(pubkeys);
const serializedTree = tree.nodes.map(layer => layer.map(node => node.toString()));
@@ -10,4 +12,12 @@ async function main() {
console.log("serialized_tree.json written and copied in common/pubkeys!")
}
main()
async function serialize_new_csca_modulus_tree() {
const tree = getCSCAModulusMerkleTree(n_csca, k_csca);
const serializedTree = tree.nodes.map(layer => layer.map(node => node.toString()));
fs.writeFileSync("outputs/serialized_csca_tree.json", JSON.stringify(serializedTree));
fs.copyFileSync("outputs/serialized_csca_tree.json", "../common/pubkeys/serialized_csca_tree.json");
console.log("serialized_csca_tree.json written and copied in common/pubkeys!")
}
serialize_new_csca_modulus_tree()