11#! /usr/bin/env bash
2- # TODO: THIS SCRIPT SHOULD NOW BE ABLE TO REPLACE TRANSPILATION AND VK GENERATION WITH 'bb aztec_process'.
32#
43# Some notes if you have to work on this script.
54# - First of all, I'm sorry (edit: not sorry). It's a beautiful script but it's no fun to debug. I got carried away.
@@ -30,77 +29,12 @@ export PLATFORM_TAG=any
3029
3130export BB=${BB:- ../ ../ barretenberg/ cpp/ build/ bin/ bb}
3231export NARGO=${NARGO:- ../ ../ noir/ noir-repo/ target/ release/ nargo}
33- export TRANSPILER=${TRANSPILER:- ../ ../ avm-transpiler/ target/ release/ avm-transpiler}
34- export STRIP_AZTEC_NR_PREFIX=${STRIP_AZTEC_NR_PREFIX:- ./ scripts/ strip_aztec_nr_prefix.sh}
3532export BB_HASH=${BB_HASH:- $(../ ../ barretenberg/ cpp/ bootstrap.sh hash)}
3633export NOIR_HASH=${NOIR_HASH:- $(../ ../ noir/ bootstrap.sh hash)}
3734
38- export tmp_dir=./target/tmp
39-
40- # Remove our tmp dir from last run.
41- # Note: This can use BASH 'trap' for better cleanliness, but the script has been hitting edge-cases so is (temporarily?) simplified.
42- rm -rf $tmp_dir
43- mkdir -p $tmp_dir
44-
4535# Set common flags for parallel.
4636export PARALLEL_FLAGS=" -j${PARALLELISM:- 16} --halt now,fail=1 --memsuspend $( memsuspend_limit) "
4737
48- # This computes a vk and adds it to the input function json if it's private, else returns same input.
49- # stdin has the function json.
50- # stdout receives the function json with the vk added (if private).
51- # The function is exported and called by a sub-shell in parallel, so we must "set -eu" etc..
52- # If debugging, a set -x at the start can help.
53- function process_function {
54- set -euo pipefail
55- local func name bytecode_b64 hash vk
56-
57- contract_hash=$1
58- # Read the function json.
59- func=" $( cat) "
60- name=$( echo " $func " | jq -r ' .name' )
61- echo_stderr " Processing function: $name ..."
62-
63- # Check if the function is neither public nor unconstrained.
64- # TODO: Why do we need to gen keys for functions that are not marked private?
65- # We allow the jq call to error (set +e) because it returns an error code if the result is false.
66- # We then differentiate between a real error, and the result being false.
67- set +e
68- make_vk=$( echo " $func " | jq -e ' (.custom_attributes | index("public") == null) and (.is_unconstrained == false)' )
69- if [ $? -ne 0 ] && [ " $make_vk " != " false" ]; then
70- echo_stderr " Failed to check function $name is neither public nor unconstrained."
71- exit 1
72- fi
73- set -e
74-
75- if [ " $make_vk " == " true" ]; then
76- # It's a private function.
77- # Build hash, check if in cache.
78- # If it's in the cache it's extracted to $tmp_dir/$hash
79- bytecode_b64=$( echo " $func " | jq -r ' .bytecode' )
80- hash=$(( echo "$BB_HASH "; echo "$bytecode_b64 ") | sha256 sum | tr - d ' - ')
81-
82- if ! cache_download vk-$contract_hash -$hash .tar.gz >&2 ; then
83- # It's not in the cache. Generate the vk file and upload it to the cache.
84- echo_stderr "Generating vk for function: $name ..."
85-
86- local outdir=$(mktemp -d -p $tmp_dir )
87- echo "$bytecode_b64 " | base64 -d | gunzip | $BB write_vk --scheme chonk -b - -o $outdir -v
88- mv $outdir /vk $tmp_dir /$contract_hash /$hash
89-
90- cache_upload vk-$contract_hash -$hash .tar.gz $tmp_dir /$contract_hash /$hash
91- fi
92-
93- # Return (echo) json containing the base64 encoded verification key.
94- vk=$(cat $tmp_dir /$contract_hash /$hash | base64 -w 0 )
95- echo "$func " | jq -c --arg vk "$vk " '. + {verification_key: $vk }'
96- else
97- echo_stderr "Function $name is neither public nor unconstrained, skipping."
98- # Not a private function. Return the original function json.
99- echo "$func "
100- fi
101- }
102- export -f process_function
103-
10438# Compute hash for a given contract.
10539# $1 is the contract name, $2 is the folder name (e.g. "contracts" or "examples")
10640function get_contract_hash {
@@ -159,42 +93,26 @@ function get_contract_path {
15993}
16094export -f get_contract_path
16195
162- # This compiles a noir contract, transpile's public functions, and generates vk's for private functions.
96+ # This compiles a noir contract, transpiles public functions, strips internal prefixes,
97+ # and generates verification keys for private functions via 'bb aztec_process'.
16398# $1 is the input package name, $2 is the folder name (e.g. "contracts" or "examples")
164- # On exit it's fully processed json artifact is in the target dir.
99+ # On exit its fully processed json artifact is in the target dir.
165100# The function is exported and called by a sub-shell in parallel, so we must "set -eu" etc..
166101function compile {
167102 set -euo pipefail
168- local contract_name contract_hash
169103
170104 local contract_path=$( get_contract_path " $1 " " $2 " )
171105 local contract=$( grep -oP ' (?<=^name = ")[^"]+' " $2 /$contract_path /Nargo.toml" )
172106 # Calculate filename because nargo...
173- contract_name=$(cat $2 /$contract_path /src/main.nr | awk '/^contract / { print $2 } /^pub contract / { print $3 }')
107+ local contract_name=$( cat $2 /$contract_path /src/main.nr | awk ' /^contract / { print $2 } /^pub contract / { print $3 }' )
174108 local filename=" $contract -$contract_name .json"
175109 local json_path=" ./target/$filename "
176- contract_hash=$(get_contract_hash $1 $2 )
110+ local contract_hash=$( get_contract_hash $1 $2 )
177111 if ! cache_download contract-$contract_hash .tar.gz; then
178- $NARGO compile --package $contract --inliner-aggressiveness 0 --deny-warnings
179- $TRANSPILER $json_path $json_path
180- $STRIP_AZTEC_NR_PREFIX $json_path
112+ $NARGO compile --package $contract --inliner-aggressiveness 0 --deny-warnings
113+ $BB aztec_process -i $json_path
181114 cache_upload contract-$contract_hash .tar.gz $json_path
182115 fi
183-
184- # We segregate equivalent vk's created by process_function. This was done to narrow down potential edge cases with identical VKs
185- # reading from cache at the same time. Create this folder up-front.
186- mkdir -p $tmp_dir /$contract_hash
187-
188- # Pipe each contract function, one per line (jq -c), into parallel calls of process_function.
189- # The returned jsons from process_function are converted back to a json array in the second jq -s call.
190- # When slurping (-s) in the last jq, we get an array of two elements:
191- # .[0 ] is the original json (at $json_path )
192- # .[1 ] is the updated functions on stdin (-)
193- # * merges their fields.
194- jq -c '.functions[]' $json_path | \
195- parallel $PARALLEL_FLAGS --keep-order -N1 --block 8 M --pipe process_function $contract_hash | \
196- jq -s '{functions: .}' | jq -s '.[0 ] * {functions: .[1 ].functions}' $json_path - > $tmp_dir /$filename
197- mv $tmp_dir /$filename $json_path
198116}
199117export -f compile
200118
@@ -211,7 +129,7 @@ function build {
211129
212130 if [ " $# " -eq 0 ]; then
213131 rm -rf target
214- mkdir -p $tmp_dir
132+ mkdir -p target
215133 local contracts=$( grep -oP " (?<=$folder_name /)[^\" ]+" Nargo.toml)
216134
217135 # If pinned contracts exist, extract them and skip their compilation.
0 commit comments