Skip to content

Commit

Permalink
Bugs in input_data/rom_data and enhanced verify tool (#196)
Browse files Browse the repository at this point in the history
* Verify all tools update to handle one elf and multiple inputs

* Bug in input data and rom data fixed

* cargo fmt
  • Loading branch information
hecmas authored Dec 20, 2024
1 parent ee6d994 commit 888312d
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 82 deletions.
11 changes: 3 additions & 8 deletions state-machines/mem/src/input_data_sm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub struct InputDataSM<F: PrimeField> {
// STD
std: Arc<Std<F>>,

num_rows: usize,
// Count of registered predecessors
registered_predecessors: AtomicU32,
}
Expand All @@ -46,12 +45,8 @@ impl<F: PrimeField> InputDataSM<F> {
pub fn new(wcm: Arc<WitnessManager<F>>, std: Arc<Std<F>>) -> Arc<Self> {
let pctx = wcm.get_pctx();
let air = pctx.pilout.get_air(ZISK_AIRGROUP_ID, INPUT_DATA_AIR_IDS[0]);
let input_data_sm = Self {
wcm: wcm.clone(),
std: std.clone(),
num_rows: air.num_rows(),
registered_predecessors: AtomicU32::new(0),
};
let input_data_sm =
Self { wcm: wcm.clone(), std: std.clone(), registered_predecessors: AtomicU32::new(0) };
let input_data_sm = Arc::new(input_data_sm);

wcm.register_component(
Expand Down Expand Up @@ -377,7 +372,7 @@ impl<F: PrimeField> MemModule<F> for InputDataSM<F> {
vec![(INPUT_ADDR as u32, (INPUT_ADDR + MAX_INPUT_SIZE - 1) as u32)]
}
fn get_flush_input_size(&self) -> u32 {
self.num_rows as u32
0
}
}

Expand Down
11 changes: 3 additions & 8 deletions state-machines/mem/src/rom_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub struct RomDataSM<F: PrimeField> {
// STD
std: Arc<Std<F>>,

num_rows: usize,
// Count of registered predecessors
registered_predecessors: AtomicU32,
}
Expand All @@ -42,12 +41,8 @@ impl<F: PrimeField> RomDataSM<F> {
pub fn new(wcm: Arc<WitnessManager<F>>, std: Arc<Std<F>>) -> Arc<Self> {
let pctx = wcm.get_pctx();
let air = pctx.pilout.get_air(ZISK_AIRGROUP_ID, ROM_DATA_AIR_IDS[0]);
let rom_data_sm = Self {
wcm: wcm.clone(),
std: std.clone(),
num_rows: air.num_rows(),
registered_predecessors: AtomicU32::new(0),
};
let rom_data_sm =
Self { wcm: wcm.clone(), std: std.clone(), registered_predecessors: AtomicU32::new(0) };
let rom_data_sm = Arc::new(rom_data_sm);

wcm.register_component(rom_data_sm.clone(), Some(ZISK_AIRGROUP_ID), Some(ROM_DATA_AIR_IDS));
Expand Down Expand Up @@ -332,7 +327,7 @@ impl<F: PrimeField> MemModule<F> for RomDataSM<F> {
vec![(ROM_ADDR as u32, ROM_ADDR_MAX as u32)]
}
fn get_flush_input_size(&self) -> u32 {
self.num_rows as u32
0
}
}

Expand Down
239 changes: 173 additions & 66 deletions tools/verify_all.sh
Original file line number Diff line number Diff line change
@@ -1,96 +1,203 @@
#!/bin/bash

echo "Verify all ELF files found in a directory"

# Check that at least one argument has been passed
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <dirname> [-l/--list -b/--begin <first_file> -e/--end <last_file> -d/--debug]"
echo "Usage: $0 <dirname|elf_file> [-l|--list] [-b|--begin <first_file>] [-e|--end <last_file>] [-d|--debug] [-i|--inputs <input_dir|input_file>]"
exit 1
fi

# Get the first argument as the directory path
if [[ "$1" != "" ]]; then
DIR="$1"
# Initialize variables
list=0
begin=0
end=0
debug=0
input_path=""
elf_mode=0
default_input="/tmp/empty_input.bin" # Default input file

# Check if the first argument is a file or directory
if [[ -f "$1" ]]; then
elf_file="$1"
elf_mode=1
elif [[ -d "$1" ]]; then
dir="$1"
else
DIR=.
echo "Invalid input. The first argument must be a directory or an ELF file."
exit 1
fi
shift
echo "Verifying ELF files found in directory ${DIR}"

# Parse optional arguments
LIST=0
BEGIN=0
END=0
DEBUG=0
while [[ "$#" -gt 0 ]]; do
case $1 in
-l|--list) LIST=1 ;;
-b|--begin) BEGIN=$2; shift; ;;
-e|--end) END=$2; shift; ;;
-d|--debug) DEBUG=1 ;;
-l|--list) list=1 ;;
-b|--begin) begin=$2; shift ;;
-e|--end) end=$2; shift ;;
-d|--debug) debug=1 ;;
-i|--inputs) input_path="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done

ELF_FILES=`find $DIR -type f -name my.elf |sort`

# List files with their corresponding index
COUNTER=0
for ELF_FILE in $ELF_FILES
do
COUNTER=$((COUNTER+1))
echo "File ${COUNTER}: ${ELF_FILE}"
done

# Log begin and end options, if provided
if [ $BEGIN -ne 0 ]; then
echo "Beginning at file ${BEGIN}";
fi
if [ $END -ne 0 ]; then
echo "Ending at file ${END}";
# Ensure the default input file exists
if [[ ! -f $default_input ]]; then
touch "$default_input"
fi

# If just listing, exit
if [ $LIST -eq 1 ]; then
echo "Exiting after listing files";
exit 0;
fi
# Function to verify an ELF file with one or more input files
verify_elf_with_inputs() {
local elf_file=$1
local input_path=$2
local input_counter=0
local input_total=0

echo "Verifying ELF file: $elf_file"

if [[ -z $input_path ]]; then
# No input path provided, use the default input file
echo "Using default input file: $default_input"

if [[ $debug -eq 1 ]]; then
# Run with debug flag
(cargo build --release && cd ../pil2-proofman; \
cargo run --release --bin proofman-cli verify-constraints \
--witness-lib ../zisk/target/release/libzisk_witness.so \
--rom "$elf_file" -i "$default_input" \
--proving-key ../zisk/build/provingKey -d)
else
# Run without debug flag
(cargo build --release && cd ../pil2-proofman; \
cargo run --release --bin proofman-cli verify-constraints \
--witness-lib ../zisk/target/release/libzisk_witness.so \
--rom "$elf_file" -i "$default_input" \
--proving-key ../zisk/build/provingKey)
fi
elif [[ -f $input_path ]]; then
# Single input file provided
echo "Using input file: $input_path"

if [[ $debug -eq 1 ]]; then
# Run with debug flag
(cargo build --release && cd ../pil2-proofman; \
cargo run --release --bin proofman-cli verify-constraints \
--witness-lib ../zisk/target/release/libzisk_witness.so \
--rom "$elf_file" -i "$input_path" \
--proving-key ../zisk/build/provingKey -d)
else
# Run without debug flag
(cargo build --release && cd ../pil2-proofman; \
cargo run --release --bin proofman-cli verify-constraints \
--witness-lib ../zisk/target/release/libzisk_witness.so \
--rom "$elf_file" -i "$input_path" \
--proving-key ../zisk/build/provingKey)
fi
elif [[ -d $input_path ]]; then
# Directory of input files provided
input_files=$(find "$input_path" -type f | sort)
input_total=$(echo "$input_files" | wc -l)
if [[ -z "$input_files" ]]; then
echo "No input files found in directory: $input_path"
return
fi

for input_file in $input_files; do
input_counter=$((input_counter + 1))
echo "Using input $input_counter of $input_total: $input_file"

if [[ $debug -eq 1 ]]; then
# Run with debug flag
(cargo build --release && cd ../pil2-proofman; \
cargo run --release --bin proofman-cli verify-constraints \
--witness-lib ../zisk/target/release/libzisk_witness.so \
--rom "$elf_file" -i "$input_file" \
--proving-key ../zisk/build/provingKey -d)
else
# Run without debug flag
(cargo build --release && cd ../pil2-proofman; \
cargo run --release --bin proofman-cli verify-constraints \
--witness-lib ../zisk/target/release/libzisk_witness.so \
--rom "$elf_file" -i "$input_file" \
--proving-key ../zisk/build/provingKey)
fi
done
else
echo "Invalid input path: $input_path"
exit 1
fi
}

# Record the number of files
MAX_COUNTER=${COUNTER}
# Logic for multiple ELF files in a directory
if [[ $elf_mode -eq 0 ]]; then
echo "Verifying ELF files found in directory: $dir"

# Create an empty input file
INPUT_FILE="/tmp/empty_input.bin"
touch $INPUT_FILE
# Find ELF files in the directory
elf_files=$(find "$dir" -type f -name my.elf | sort)
if [[ -z "$elf_files" ]]; then
echo "No ELF files found in directory: $dir"
exit 0
fi

# For all files
COUNTER=0
for ELF_FILE in $ELF_FILES
do
# Increase file counter
COUNTER=$((COUNTER+1))
# List files with their corresponding index
counter=0
for elf_file in $elf_files; do
counter=$((counter + 1))
echo "File $counter: $elf_file"
done

# Skip files lower than BEGIN
if [ $BEGIN -ne 0 ] && [ $COUNTER -lt $BEGIN ]; then
continue;
# Log begin and end options, if provided
if [ $begin -ne 0 ]; then
echo "Beginning at file $begin"
fi

# Skip files higher than END
if [ $END -ne 0 ] && [ $COUNTER -gt $END ]; then
continue;
if [ $end -ne 0 ]; then
echo "Ending at file $end"
fi

# Varify the contraints for this file
echo ""
echo "Verifying file ${COUNTER} of ${MAX_COUNTER}: ${ELF_FILE}"

if [ $DEBUG -eq 1 ]; then
# Run with debug flag
(cargo build --release && cd ../pil2-proofman; cargo run --release --bin proofman-cli verify-constraints --witness-lib ../zisk/target/release/libzisk_witness.so --rom $ELF_FILE -i $INPUT_FILE --proving-key ../zisk/build/provingKey -d)
else
# Run without debug flag
(cargo build --release && cd ../pil2-proofman; cargo run --release --bin proofman-cli verify-constraints --witness-lib ../zisk/target/release/libzisk_witness.so --rom $ELF_FILE -i $INPUT_FILE --proving-key ../zisk/build/provingKey)
# If just listing, exit
if [ $list -eq 1 ]; then
echo "Exiting after listing files"
exit 0
fi
done

# Record the number of files
max_counter=$counter

# For all ELF files
counter=0
for elf_file in $elf_files; do
counter=$((counter + 1))

# Skip files lower than begin
if [ $begin -ne 0 ] && [ $counter -lt $begin ]; then
continue
fi

# Skip files higher than end
if [ $end -ne 0 ] && [ $counter -gt $end ]; then
continue
fi

echo ""
echo "Verifying file $counter of $max_counter: $elf_file"

if [ $debug -eq 1 ]; then
# Run with debug flag
(cargo build --release && cd ../pil2-proofman; \
cargo run --release --bin proofman-cli verify-constraints \
--witness-lib ../zisk/target/release/libzisk_witness.so \
--rom "$elf_file" -i "$default_input" \
--proving-key ../zisk/build/provingKey -d)
else
# Run without debug flag
(cargo build --release && cd ../pil2-proofman; \
cargo run --release --bin proofman-cli verify-constraints \
--witness-lib ../zisk/target/release/libzisk_witness.so \
--rom "$elf_file" -i "$default_input" \
--proving-key ../zisk/build/provingKey)
fi
done
else
# Logic for single ELF file with input directory or file
verify_elf_with_inputs "$elf_file" "$input_path"
fi

0 comments on commit 888312d

Please sign in to comment.