Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeytimoshin committed Mar 26, 2024
1 parent 78f865a commit a24d33b
Show file tree
Hide file tree
Showing 31 changed files with 2,353 additions and 206 deletions.
2 changes: 1 addition & 1 deletion circuit-lib/circuit-lib.circom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"license": "GPL-3.0",
"scripts": {
"build-merkle": "./scripts/buildMerkle.sh"
"build-circuits": "./scripts/buildCircuits.sh"
},
"author": "",
"devDependencies": {
Expand Down
82 changes: 0 additions & 82 deletions circuit-lib/circuit-lib.circom/scripts/buildMerkle.sh

This file was deleted.

82 changes: 0 additions & 82 deletions circuit-lib/circuit-lib.circom/scripts/buildNonInclusion.sh

This file was deleted.

105 changes: 105 additions & 0 deletions circuit-lib/circuit-lib.circom/scripts/buildNonInclusionCircuits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash -e

function download_ptau {
directory="$1"
ptau_number="$2"

if [ ! -f "$directory/ptau$ptau_number" ]; then
echo "Downloading powers of tau file"
curl -L "https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_$ptau_number.ptau" --create-dirs -o "$directory/ptau$ptau_number" || { echo "Download failed"; exit 1; }
fi
}

function execute_commands {
if [[ $# -ne 4 ]] && [[ $# -ne 5 ]]; then
echo "Invalid number of arguments"
exit 1;
fi

type="$1"
merkle_number="$2"
utxo_count="$3"
ptau_number="$4"
combined_non_inclusion_utxo_count="$5"


temp_directory="/tmp"
circuits_circom_directory="$REPO_TOP_DIR/circuit-lib/circuit-lib.circom"

if [ "$type" == "non-inclusion" ]; then
name="ni_${merkle_number}_$utxo_count"
circuit_dir="non-inclusion-merkle-proof"
elif [ "$type" == "inclusion" ]; then
name="i_${merkle_number}_$utxo_count"
circuit_dir="inclusion-merkle-proof"
elif [ "$type" == "combined" ]; then
name="combined_${merkle_number}_${utxo_count}_${combined_non_inclusion_utxo_count}"
circuit_dir="combined-merkle-proof"

else
echo "Invalid type"
exit 1;
fi

build_directory="$CIRCUIT_RS_DIR/test-data/$name"
src_directory="$CIRCUIT_RS_VERIFYINGKEY_DIR/$name"

mkdir -p "$build_directory"
mkdir -p "$src_directory"

download_ptau "$temp_directory" "$ptau_number" || { echo "download_ptau failed"; exit 1; }
echo "Compiling circuits..."
circom --r1cs --wasm --sym "$circuits_circom_directory/src/$circuit_dir/$name.circom" \
-o "$temp_directory" -l "$circuits_circom_directory/node_modules/circomlib/circuits" || { echo "circom failed"; exit 1; }

echo "Generating keys..."
npx snarkjs groth16 setup "$temp_directory/$name.r1cs" "$temp_directory/ptau$ptau_number" "$temp_directory/tmp_$name.zkey" \
|| { echo "snarkjs groth16 setup failed"; exit 1; }

echo "Contributing to powers of tau..."
npx snarkjs zkey contribute "$temp_directory/tmp_$name.zkey" "$temp_directory/$name.zkey" -e="321432151325321543215" \
|| { echo "snarkjs zkey contribute failed"; exit 1; }
rm "$temp_directory/tmp_$name.zkey"

echo "Verifying proof..."
npx snarkjs zkey verify "$temp_directory/$name.r1cs" "$temp_directory/ptau$ptau_number" "$temp_directory/$name.zkey" || { echo "snarkjs zkey verify failed"; exit 1; }

echo "Exporting verification key..."
npx snarkjs zkey export verificationkey "$temp_directory/$name.zkey" "$temp_directory/$name.json"

cp "$temp_directory/$name.zkey" "$build_directory/$name.zkey"
cp "$temp_directory/${name}_js/$name.wasm" "$build_directory/$name.wasm"

# echo "Parsing verification key to Rust..."
# npx ts-node "$circuits_circom_directory/scripts/parseVerifyingKeyToRust.js" "$temp_directory/$name.json" "$src_directory"
# echo "mod $name;" >> "$CIRCUIT_RS_VERIFYINGKEY_DIR/mod.rs";
# echo "pub use crate::verifying_keys::$name::VERIFYINGKEY as VK_$name;" >> "$CIRCUIT_RS_VERIFYINGKEY_DIR/mod.rs";

echo "Done"
}

REPO_TOP_DIR=$(git rev-parse --show-toplevel)

CIRCUIT_RS_DIR="$REPO_TOP_DIR/circuit-lib/circuitlib-rs"
CIRCUIT_RS_VERIFYINGKEY_DIR="$CIRCUIT_RS_DIR/src/verifying_keys"
#
#rm "$CIRCUIT_RS_VERIFYINGKEY_DIR/mod.rs"
#touch "$CIRCUIT_RS_VERIFYINGKEY_DIR/mod.rs"
#echo "mod helpers;" >> "$CIRCUIT_RS_VERIFYINGKEY_DIR/mod.rs";
#echo "pub use crate::verifying_keys::helpers::vk;" >> "$CIRCUIT_RS_VERIFYINGKEY_DIR/mod.rs";

POWERS_OF_TAU=16
MERKLE_TREE_HEIGHT=26
MAX_COUNT=4

#for ((i=1; i<=MAX_COUNT; i++)); do
# execute_commands "inclusion" "$MERKLE_TREE_HEIGHT" "$i" "$POWERS_OF_TAU" || exit
#done
#
#execute_commands "inclusion" "$MERKLE_TREE_HEIGHT" 8 "$POWERS_OF_TAU" || exit

for ((i=1; i<=MAX_COUNT; i++)); do
execute_commands "non-inclusion" "$MERKLE_TREE_HEIGHT" "$i" "$POWERS_OF_TAU" || exit
done

execute_commands "non-inclusion" "$MERKLE_TREE_HEIGHT" 8 "$POWERS_OF_TAU" || exit
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,36 @@ template NonInclusionProof(levels) {
signal input indexHashedIndexedElementLeaf;

// check that leafLowerRangeValue less than notIncludedValue
log("NonInclusionProof, checking higherThanLower...");
component higherThanLower = LessThan(252);
higherThanLower.in[0] <== leafLowerRangeValue;
higherThanLower.in[1] <== value;
signal leafLowerRangeValueLessThanNotIncludedValue <== higherThanLower.out;
leafLowerRangeValueLessThanNotIncludedValue === 1;

// check that notIncludedValue less than leafHigherRangeValue
log("NonInclusionProof, checking lessThanHigher...");
component lessThanHigher = LessThan(252);
lessThanHigher.in[0] <== value;
lessThanHigher.in[1] <== leafHigherRangeValue;
signal notIncludedValueLessThanLeafHigherRangeValue <== lessThanHigher.out;
notIncludedValueLessThanLeafHigherRangeValue === 1;

// Leaf Calculation
log("NonInclusionProof, calculating leaf...");
component poseidon = Poseidon(3);
poseidon.inputs[0] <== leafLowerRangeValue;
poseidon.inputs[1] <== leafIndex;
poseidon.inputs[2] <== leafHigherRangeValue;
signal leaf <== poseidon.out;

log("NonInclusionProof, poseidon(", leafLowerRangeValue, ", ", leafIndex, ", ", leafHigherRangeValue, ") = ", leaf);
// Inclusion Proof Attempt
component merkleProof = MerkleProof(levels);
merkleProof.leaf <== leaf;
merkleProof.pathElements <== merkleProofHashedIndexedElementLeaf;
merkleProof.pathIndices <== indexHashedIndexedElementLeaf;

log("NonInclusionProof, root = ", root, ", merkleProof.root = ", merkleProof.root);
merkleProof.root === root;
}
1 change: 1 addition & 0 deletions circuit-lib/circuit-lib.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"test": "pnpm test-gnark",
"test-gnark": "pnpm gnark-prover && ts-mocha --resolveJsonModule ./tsconfig.json -t 100000000 tests/gnark.test.ts --exit",
"test-circom": "ts-mocha --resolveJsonModule ./tsconfig.json -t 100000000 tests/circom.test.ts --exit",
"gnark-prover": "./scripts/prover.sh",
"format": "prettier --write \"tests/**/*.{ts,js}\" \"src/**/*.{ts,js}\"",
"lint": "pnpm prettier \"tests/**/*.{ts,js}\" \"src/**/*.{ts,js}\" --check",
Expand Down
Loading

0 comments on commit a24d33b

Please sign in to comment.