From a6ac92eaa1da424a108aa05030a622969dbbad08 Mon Sep 17 00:00:00 2001 From: Josh Crites Date: Mon, 5 Feb 2024 10:43:51 -0500 Subject: [PATCH] remove old code --- .../circuits/target/debug_foundry_voting.json | 1 + .../circuits/target/foundry_voting.json | 2 +- foundry-voting/package.json | 6 +- foundry-voting/test/integration.test.ts | 95 ------------------ foundry-voting/tsconfig.json | 18 +--- foundry-voting/utils/noirNode.ts | 99 ------------------- foundry-voting/utils/populate.ts | 46 --------- 7 files changed, 7 insertions(+), 260 deletions(-) create mode 100644 foundry-voting/circuits/target/debug_foundry_voting.json delete mode 100644 foundry-voting/test/integration.test.ts delete mode 100644 foundry-voting/utils/noirNode.ts delete mode 100644 foundry-voting/utils/populate.ts diff --git a/foundry-voting/circuits/target/debug_foundry_voting.json b/foundry-voting/circuits/target/debug_foundry_voting.json new file mode 100644 index 0000000..9666d3d --- /dev/null +++ b/foundry-voting/circuits/target/debug_foundry_voting.json @@ -0,0 +1 @@ +{"debug_symbols":[{"locations":{"0":[{"span":{"start":199,"end":233},"file":0},{"span":{"start":755,"end":793},"file":23}],"1":[{"span":{"start":255,"end":307},"file":0},{"span":{"start":755,"end":793},"file":23}],"2":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":529,"end":555},"file":25},{"span":{"start":122,"end":149},"file":15}],"3":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":529,"end":555},"file":25},{"span":{"start":122,"end":149},"file":15}],"4":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":529,"end":555},"file":25},{"span":{"start":122,"end":149},"file":15}],"5":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":529,"end":555},"file":25},{"span":{"start":122,"end":149},"file":15}],"6":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":716,"end":728},"file":25}],"7":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"8":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"9":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"10":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"11":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"12":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"13":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"14":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":716,"end":728},"file":25}],"15":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"16":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"17":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"18":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"19":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"20":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"21":[{"span":{"start":331,"end":398},"file":0},{"span":{"start":821,"end":872},"file":25},{"span":{"start":755,"end":793},"file":23}],"22":[{"span":{"start":411,"end":429},"file":0}]}}],"file_map":{"0":{"source":"use dep::std;\n\nfn main(\n root: pub Field,\n index: Field,\n hash_path: [Field; 2],\n secret: Field,\n proposalId: pub Field,\n vote: pub Field\n) -> pub Field {\n let note_commitment = std::hash::pedersen_hash([secret]);\n let nullifier = std::hash::pedersen_hash([root, secret, proposalId]);\n\n let check_root = std::merkle::compute_merkle_root(note_commitment, index, hash_path);\n assert(root == check_root);\n\n // Originally contrained the vote to avoid front-running,\n // but including the vote as a public input is sufficient\n\n // assert(vote <= 1);\n\n nullifier\n}\n\n#[test]\nfn test_valid_build_merkle_tree() {\n let commitment_0 = std::hash::pedersen_hash([1]);\n let commitment_1 = std::hash::pedersen_hash([2]);\n let commitment_2 = std::hash::pedersen_hash([3]);\n let commitment_3 = std::hash::pedersen_hash([4]);\n\n let left_branch = std::hash::pedersen_hash([commitment_0, commitment_1]);\n let right_branch = std::hash::pedersen_hash([commitment_2, commitment_3]);\n\n let root = std::hash::pedersen_hash([left_branch, right_branch]);\n\n let proposalId = 0;\n let vote = 1;\n\n let nullifier = main(root, 0, [commitment_1, right_branch], 1, proposalId, vote);\n\n let expected_nullifier = std::hash::pedersen_hash([root, 1, proposalId]);\n\n std::println(\"Merkle Tree:\");\n std::println([root]);\n std::println([left_branch, right_branch]);\n std::println([commitment_0, commitment_1, commitment_2, commitment_3]);\n\n assert(nullifier == expected_nullifier);\n}\n\n// fn main(root : pub Field, index : Field, hash_path : [Field; 2], secret: Field, priv_key: Field, proposalId: pub Field, vote: pub u8) -> pub Field {\n// let note_commitment = std::hash::pedersen([priv_key, secret]);\n// let nullifier = std::hash::pedersen([root, priv_key, proposalId]);\n\n// let check_root = std::merkle::compute_merkle_root(note_commitment[0], index, hash_path);\n// assert(root == check_root);\n\n// // Originally contrained the vote to avoid front-running,\n// // but including the vote as a public input is sufficient\n\n// assert(vote <= 1);\n\n// nullifier[0]\n// }\n\n// Helpers for getting note_commitments to build the merkle tree.\n// To view: nargo test --show-output\n\n#[test]\nfn test_build_merkle_tree() {\n let secret = 9;\n let commitment_0 = std::hash::pedersen_hash([0, secret]);\n let commitment_1 = std::hash::pedersen_hash([1, secret]);\n let commitment_2 = std::hash::pedersen_hash([2, secret]);\n let commitment_3 = std::hash::pedersen_hash([3, secret]);\n\n let left_branch = std::hash::pedersen_hash([commitment_0, commitment_1]);\n let right_branch = std::hash::pedersen_hash([commitment_2, commitment_3]);\n\n let root = std::hash::pedersen_hash([left_branch, right_branch]);\n\n std::println(\"Merkle Tree:\");\n std::println([root]);\n std::println([left_branch, right_branch]);\n std::println([commitment_0, commitment_1, commitment_2, commitment_3]);\n}\n","path":"/home/josh/Documents/Github/noir-examples/foundry-voting/circuits/src/main.nr"},"15":{"source":"impl Field {\n pub fn to_le_bits(self: Self, bit_size: u32) -> [u1] {\n crate::assert_constant(bit_size);\n self.__to_le_bits(bit_size)\n }\n \n pub fn to_be_bits(self: Self, bit_size: u32) -> [u1] {\n crate::assert_constant(bit_size);\n self.__to_be_bits(bit_size)\n }\n\n #[builtin(to_le_bits)]\n fn __to_le_bits(_self: Self, _bit_size: u32) -> [u1] {}\n \n #[builtin(to_be_bits)]\n fn __to_be_bits(_self: Self, _bit_size: u32) -> [u1] {}\n\n pub fn to_le_bytes(self: Self, byte_size: u32) -> [u8] {\n self.to_le_radix(256, byte_size)\n }\n\n pub fn to_be_bytes(self: Self, byte_size: u32) -> [u8] {\n self.to_be_radix(256, byte_size)\n }\n\n\n pub fn to_le_radix(self: Self, radix: u32, result_len: u32) -> [u8] {\n crate::assert_constant(radix);\n crate::assert_constant(result_len);\n self.__to_le_radix(radix, result_len)\n }\n\n pub fn to_be_radix(self: Self, radix: u32, result_len: u32) -> [u8] {\n crate::assert_constant(radix);\n crate::assert_constant(result_len);\n self.__to_be_radix(radix, result_len)\n }\n\n\n\n // decompose `_self` into a `_result_len` vector over the `_radix` basis\n // `_radix` must be less than 256\n #[builtin(to_le_radix)]\n fn __to_le_radix(_self: Self, _radix: u32, _result_len: u32) -> [u8] {}\n \n #[builtin(to_be_radix)]\n fn __to_be_radix(_self: Self, _radix: u32, _result_len: u32) -> [u8] {}\n\n\n // Returns self to the power of the given exponent value.\n // Caution: we assume the exponent fits into 32 bits\n // using a bigger bit size impacts negatively the performance and should be done only if the exponent does not fit in 32 bits\n pub fn pow_32(self, exponent: Field) -> Field {\n let mut r: Field = 1;\n let b = exponent.to_le_bits(32);\n\n for i in 1..33 {\n r *= r;\n r = (b[32-i] as Field) * (r * self) + (1 - b[32-i] as Field) * r;\n }\n r\n }\n\n // Parity of (prime) Field element, i.e. sgn0(x mod p) = 0 if x ∈ {0, ..., p-1} is even, otherwise sgn0(x mod p) = 1.\n pub fn sgn0(self) -> u1 {\n self as u1\n }\n}\n\n#[builtin(modulus_num_bits)]\npub fn modulus_num_bits() -> Field {}\n\n#[builtin(modulus_be_bits)]\npub fn modulus_be_bits() -> [u1] {}\n\n#[builtin(modulus_le_bits)]\npub fn modulus_le_bits() -> [u1] {}\n\n#[builtin(modulus_be_bytes)]\npub fn modulus_be_bytes() -> [u8] {}\n\n#[builtin(modulus_le_bytes)]\npub fn modulus_le_bytes() -> [u8] {}\n// Convert a 32 byte array to a field element\npub fn bytes32_to_field(bytes32: [u8; 32]) -> Field {\n // Convert it to a field element\n let mut v = 1;\n let mut high = 0 as Field;\n let mut low = 0 as Field;\n\n for i in 0..16 {\n high = high + (bytes32[15 - i] as Field) * v;\n low = low + (bytes32[16 + 15 - i] as Field) * v;\n v = v * 256;\n }\n // Abuse that a % p + b % p = (a + b) % p and that low < p\n low + high * v\n}\n","path":"std/field.nr"},"23":{"source":"mod poseidon;\nmod mimc;\n\n#[foreign(sha256)]\npub fn sha256(_input: [u8; N]) -> [u8; 32] {}\n\n#[foreign(blake2s)]\npub fn blake2s(_input: [u8; N]) -> [u8; 32] {}\n\nstruct PedersenPoint {\n x : Field,\n y : Field,\n}\n\npub fn pedersen_commitment(input: [Field; N]) -> PedersenPoint {\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[foreign(pedersen_commitment)]\npub fn __pedersen_commitment_with_separator(_input: [Field; N], _separator: u32) -> [Field; 2] {}\n\npub fn pedersen_commitment_with_separator(input: [Field; N], separator: u32) -> PedersenPoint {\n let values = __pedersen_commitment_with_separator(input, separator);\n PedersenPoint { x: values[0], y: values[1] }\n}\n\npub fn pedersen_hash(input: [Field; N]) -> Field {\n pedersen_hash_with_separator(input, 0)\n}\n\n#[foreign(pedersen_hash)]\npub fn pedersen_hash_with_separator(_input: [Field; N], _separator: u32) -> Field {}\n\npub fn hash_to_field(_input: [Field; N]) -> Field {\n let mut inputs_as_bytes = [];\n\n for i in 0..N {\n let input_bytes = _input[i].to_le_bytes(32);\n for i in 0..32 {\n inputs_as_bytes = inputs_as_bytes.push_back(input_bytes[i]);\n }\n }\n\n let hashed_input = blake2s(inputs_as_bytes);\n crate::field::bytes32_to_field(hashed_input)\n}\n\n#[foreign(keccak256)]\npub fn keccak256(_input: [u8; N], _message_size: u32) -> [u8; 32] {}\n\n","path":"std/hash.nr"},"25":{"source":"// Regular merkle tree means a append-only merkle tree (Explain why this is the only way to have privacy and alternatives if you don't want it)\n// Currently we assume that it is a binary tree, so depth k implies a width of 2^k\n// XXX: In the future we can add an arity parameter\n// Returns the merkle root of the tree from the provided leaf, its hashpath, using a pedersen hash function.\npub fn compute_merkle_root(leaf: Field, index: Field, hash_path: [Field; N]) -> Field {\n let n = hash_path.len();\n let index_bits = index.to_le_bits(n as u32);\n let mut current = leaf;\n for i in 0..n {\n let path_bit = index_bits[i] as bool;\n let (hash_left, hash_right) = if path_bit {\n (hash_path[i], current)\n } else {\n (current, hash_path[i])\n };\n current = crate::hash::pedersen_hash([hash_left, hash_right]);\n }\n current\n}\n","path":"std/merkle.nr"}},"warnings":[]} \ No newline at end of file diff --git a/foundry-voting/circuits/target/foundry_voting.json b/foundry-voting/circuits/target/foundry_voting.json index 67cf1d5..b8b9b7e 100644 --- a/foundry-voting/circuits/target/foundry_voting.json +++ b/foundry-voting/circuits/target/foundry_voting.json @@ -1 +1 @@ -{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"root","type":{"kind":"field"},"visibility":"public"},{"name":"index","type":{"kind":"field"},"visibility":"private"},{"name":"hash_path","type":{"kind":"array","length":2,"type":{"kind":"field"}},"visibility":"private"},{"name":"secret","type":{"kind":"field"},"visibility":"private"},{"name":"proposalId","type":{"kind":"field"},"visibility":"public"},{"name":"vote","type":{"kind":"field"},"visibility":"public"}],"param_witnesses":{"hash_path":[3,4],"index":[2],"proposalId":[6],"root":[1],"secret":[5],"vote":[7]},"return_type":{"kind":"field"},"return_witnesses":[10]},"bytecode":"H4sIAAAAAAAA/9VZbU/CMBC+bYBvGwiCIBJMNNFEY9KyDbpv/hWJ4///AmMbrqGb+8bTZGtyubKUa5/netfb+klEazq2PuuQdaSlpyXQMmBtx/1y/1LLlTMm4ucBj7FjB85/rrXc8Dx2LnLsf7EW5zUZ4mwJu85YS8LrDljH3Le/EwcLOVz6wpaKbZaVu00pU/ktNsVe5SLL91sllcxV/rNRaVqqTO2KfbEThczSUh7yIj2wsRhgqzwcWwLk3OUvrPHXBszW1hDo165gHoExo2PfxFvcYPdc3GvC7m80bpOPhx5wP5GfuI7A67wFcgn0tUTz5yNeRh72zXMH4sVHnnihbsTLGMgl0NcSyZ+tTe15anKEqUXHdKpJJ1ruqNrQ528CtDWlbtQcSMwzanfNYd6VEsLnkldqdw41sTP1gPuNupFD74FcAn0t0fz5iJeZh33z3nLcJkdMPOD+oG7EyxzIJdDXEslfveYwOcLUGnM61RwLLQ9UbejvRAHQbwugrSUOo4jo//c2InzcItfsrvfR6fdYhw17YuABE9XmqfM4bHjW6iJsyYSi7a4It/l94V7hfVQ5CNrMqRs49uKiT9ULCRNAFw4OcxHxB5AbxdoBGQAA","proving_key":null,"verification_key":null} \ No newline at end of file +{"noir_version":"0.22.0+3fae4a03fded4e3f5065e7461c563f7e39745604","hash":14491308408831426258,"abi":{"parameters":[{"name":"root","type":{"kind":"field"},"visibility":"public"},{"name":"index","type":{"kind":"field"},"visibility":"private"},{"name":"hash_path","type":{"kind":"array","length":2,"type":{"kind":"field"}},"visibility":"private"},{"name":"secret","type":{"kind":"field"},"visibility":"private"},{"name":"proposalId","type":{"kind":"field"},"visibility":"public"},{"name":"vote","type":{"kind":"field"},"visibility":"public"}],"param_witnesses":{"hash_path":[{"start":3,"end":5}],"index":[{"start":2,"end":3}],"proposalId":[{"start":6,"end":7}],"root":[{"start":1,"end":2}],"secret":[{"start":5,"end":6}],"vote":[{"start":7,"end":8}]},"return_type":{"abi_type":{"kind":"field"},"visibility":"public"},"return_witnesses":[9]},"bytecode":"H4sIAAAAAAAA/9VYbW6DMAx16cdW6Nqu3bRN2qROu0BCoIR/u8rQ4P4nqIrVoAZ+lucKLEUEET3iZ/s5yg8RvdHFJvV4cE+2eT1Obv7ofZ9660/euoW3flmPwMMib/7rnqqf6QCHpQKHE9YjcnuX2HPo/tHFNeqYJGUWl9roPxXnhU1VkhZHq61ObfofW2NKm9gsL/JM5Toxpa7S3FQOOARyIeF35HhF+x2B/W5sKpiryFjfilVWF5PiLxDM775YK2BcJWqFc09CI15o2BrBPWYl4Pcr3Ucj+u7zCcglMNYazZ9EvUjkzfsI6kVCJz5oHPWyBnIJjLVG8tecuZt+yhrBZ+w1Xc/aG2obuvdGQKwtDbv3zkjmnPpJw9YSzqGtgN9fNA4teQZyCYy1RvMnUS8SeXMYuN+sERsBv79pHPWyA3J5AGIh+ev2XtYI7rk7uvbePbWt23tVP9MTYMz2QG5mnr+cW/w+p/bd4ILa94lLj6czaflhJ3gUAAA="} \ No newline at end of file diff --git a/foundry-voting/package.json b/foundry-voting/package.json index a721bdc..4ac9cfc 100644 --- a/foundry-voting/package.json +++ b/foundry-voting/package.json @@ -8,16 +8,12 @@ "test": "test" }, "scripts": { - "test": "forge test", - "integration-test": "vitest", - "populate-prover-toml": "ts-node --esm --experimental-specifier-resolution=node ./utils/populate.ts" + "test": "forge test" }, "author": "", "license": "ISC", "dependencies": { - "@aztec/bb.js": "0.3.6", "@iarna/toml": "^2.2.5", - "@noir-lang/acvm_js": "git+https://github.com/noir-lang/acvm-simulator-wasm.git", "fflate": "^0.8.0", "toml": "^3.0.0" }, diff --git a/foundry-voting/test/integration.test.ts b/foundry-voting/test/integration.test.ts deleted file mode 100644 index aaa5c5d..0000000 --- a/foundry-voting/test/integration.test.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { expect } from 'chai'; -// import { describe, it } from 'mocha'; -import { NoirNode } from "../utils/noirNode"; -import { convertToHex } from "../utils/common"; - -import { beforeAll, afterAll, describe } from 'vitest'; -import circuit from '../circuits/target/foundry_voting.json' assert { type: "json" }; - -const noir = new NoirNode(); - -describe('Integration tests', function () { - let merkleData: any; - - beforeAll(async () => { - await noir.init(circuit); - merkleData = await getMerkleTree(); - }); - - afterAll(async () => { - await noir.destroy(); - }); - - function generateInitialWitness(input: any) { - const initialWitness = new Map(); - - initialWitness.set(1, input.root); - initialWitness.set(2, convertToHex(input.index)); - initialWitness.set(3, input.hash_path[0]); - initialWitness.set(4, input.hash_path[1]); - initialWitness.set(5, convertToHex(input.secret)); - initialWitness.set(6, convertToHex(input.proposalId)); - initialWitness.set(7, convertToHex(input.vote)); - - return initialWitness; - } - - async function getMerkleTree() { - let commitment1 = await noir.pedersenHash([BigInt(1)]); - let commitment2 = await noir.pedersenHash([BigInt(2)]); - let commitment3 = await noir.pedersenHash([BigInt(3)]); - let commitment4 = await noir.pedersenHash([BigInt(4)]); - - let leftSubtree = await noir.pedersenHash([commitment1, commitment2]); - let rightSubtree = await noir.pedersenHash([commitment3, commitment4]); - - let root = await noir.pedersenHash([leftSubtree, rightSubtree]); - - return { - root: convertToHex(root), - hashPath: [convertToHex(commitment2), convertToHex(rightSubtree)] - } - } - - it("Should be able to generate proof and verify it for valid inputs", async () => { - - let inputs = { - root: merkleData.root, - index: 0, - hash_path: merkleData.hashPath, - secret: 1, - proposalId: 0, - vote: 1 - } - - const initialWitness = generateInitialWitness(inputs) - - const witness = await noir.generateWitness(initialWitness); - const proof = await noir.generateProof(witness); - - expect(proof instanceof Uint8Array).to.be.true; - - const verified = await noir.verifyProof(proof); - - expect(verified).to.be.true; - }); - - it("Should fail to execute for failing contraints", async () => { - let inputs = { - root: "0x29fd5ee89e33f559a7b32ac39f57400aa5a6c77492e28c088f9eb511b0c73e70", - index: 0, - hash_path: merkleData.hashPath, - secret: 1, - proposalId: 0, - vote: 1 - } - try { - const initialWitness = generateInitialWitness(inputs) - await noir.generateWitness(initialWitness); - } - catch (err: any) { - expect(err).to.equal("could not satisfy all constraints"); - } - }); - -}); diff --git a/foundry-voting/tsconfig.json b/foundry-voting/tsconfig.json index 9722a3c..5977398 100644 --- a/foundry-voting/tsconfig.json +++ b/foundry-voting/tsconfig.json @@ -12,17 +12,7 @@ "resolveJsonModule": true, "noImplicitAny": true, "plugins": [], - "lib": [ - "dom", - "EsNext" - ] - }, - "include": [ - "./test/**/*" - ], - "exclude": [ - "node_modules", - "**/node_modules/**" - ] - -} \ No newline at end of file + "lib": ["dom", "EsNext"] + }, + "exclude": ["node_modules", "**/node_modules/**"] +} diff --git a/foundry-voting/utils/noirNode.ts b/foundry-voting/utils/noirNode.ts deleted file mode 100644 index acbe5f9..0000000 --- a/foundry-voting/utils/noirNode.ts +++ /dev/null @@ -1,99 +0,0 @@ -// TODO use the JSON directly for now -// import { compile } from '@noir-lang/noir_wasm'; -import { decompressSync } from 'fflate'; -import { - BarretenbergApiAsync, - Crs, - newBarretenbergApiAsync, - RawBuffer, -} from '@aztec/bb.js/dest/node/index.js'; -import { executeCircuit, compressWitness } from '@noir-lang/acvm_js'; -import { ethers } from 'ethers'; -import { Ptr, Fr } from '@aztec/bb.js/dest/node/types/index.js'; - - -export class NoirNode { - acir: string = ''; - acirBuffer: Uint8Array = Uint8Array.from([]); - acirBufferUncompressed: Uint8Array = Uint8Array.from([]); - - api = {} as BarretenbergApiAsync; - acirComposer = {} as Ptr; - - async init(circuit: any) { - this.acirBuffer = Buffer.from(circuit.bytecode, 'base64'); - this.acirBufferUncompressed = decompressSync(this.acirBuffer); - - this.api = await newBarretenbergApiAsync(4); - - const [exact, total, subgroup] = await this.api.acirGetCircuitSizes( - this.acirBufferUncompressed, - ); - const subgroupSize = Math.pow(2, Math.ceil(Math.log2(total))); - const crs = await Crs.new(subgroupSize + 1); - await this.api.commonInitSlabAllocator(subgroupSize); - await this.api.srsInitSrs( - new RawBuffer(crs.getG1Data()), - crs.numPoints, - new RawBuffer(crs.getG2Data()), - ); - - this.acirComposer = await this.api.acirNewAcirComposer(subgroupSize); - } - - async generateWitness(initialWitness: Map): Promise { - const witnessMap = await executeCircuit(this.acirBuffer, initialWitness, () => { - throw Error('unexpected oracle'); - }); - - const witnessBuff = compressWitness(witnessMap); - return witnessBuff; - } - - async generateProof(witness: Uint8Array) { - const proof = await this.api.acirCreateProof( - this.acirComposer, - this.acirBufferUncompressed, - decompressSync(witness), - false, - ); - return proof; - } - - async verifyProof(proof: Uint8Array) { - await this.api.acirInitProvingKey(this.acirComposer, this.acirBufferUncompressed); - const verified = await this.api.acirVerifyProof(this.acirComposer, proof, false); - return verified; - } - - async compressInputs(values: number[]) { - let serialised_inputs = [] - for (var i = 0; i < values.length; i++) { - let number_hex = values[i].toString(16); - let padded_number_hex = number_hex.length % 2 == 0 ? "0x" + number_hex : "0x0" + number_hex; // TOOD: this logic should be placed inside the `serialise_public_inputs` method - serialised_inputs.push( - Fr.fromString(padded_number_hex) - ); - } - const compressed_inputs = await this.api.pedersenHashMultiple(serialised_inputs); - return compressed_inputs; - } - - async pedersenHash(data: BigInt[]) { - let hexData = []; - - for (let dataPoint of data) { - let hexVal = ethers.utils.hexZeroPad(`0x${dataPoint.toString(16)}`, 32); - let num = Fr.fromString(hexVal); - - hexData.push(num); - } - - let hash = await this.api.pedersenPlookupCommit(hexData); - return hash.value; - } - - async destroy() { - await this.api.destroy(); - } -} \ No newline at end of file diff --git a/foundry-voting/utils/populate.ts b/foundry-voting/utils/populate.ts deleted file mode 100644 index ed67202..0000000 --- a/foundry-voting/utils/populate.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { NoirNode } from "./NoirNode"; -import { convertToHex, writeToToml } from "./common"; -import config from "../data/config.json" assert { type: "json" }; -import circuit from '../circuits/target/circuits.json' assert { type: "json" }; -import { fileURLToPath } from 'url'; -import { resolve, dirname } from "path"; - -async function getMerkleTree(noir: NoirNode) { - - let commitment1 = await noir.pedersenHash([BigInt(1)]); - let commitment2 = await noir.pedersenHash([BigInt(2)]); - let commitment3 = await noir.pedersenHash([BigInt(3)]); - let commitment4 = await noir.pedersenHash([BigInt(4)]); - - let leftSubtree = await noir.pedersenHash([commitment1, commitment2]); - let rightSubtree = await noir.pedersenHash([commitment3, commitment4]); - - let root = await noir.pedersenHash([leftSubtree, rightSubtree]); - - return { - root: convertToHex(root), - hashPath: [convertToHex(commitment2), convertToHex(rightSubtree)] - } -} - -async function main() { - const noir = new NoirNode(); - await noir.init(circuit); - - let merkleData = await getMerkleTree(noir); - - let data = { - hash_path: merkleData.hashPath, - index: config.index, - proposalId: config.proposalId, - root: merkleData.root, - secret: config.secret - } - - const dir = dirname(fileURLToPath(import.meta.url)); - let path = resolve(dir + "/../circuits/Prover.toml"); - - writeToToml(data, path); -} - -main(); \ No newline at end of file