diff --git a/.github/workflows/reference_tests.yml b/.github/workflows/reference_tests.yml index f947ff7..c328da3 100644 --- a/.github/workflows/reference_tests.yml +++ b/.github/workflows/reference_tests.yml @@ -9,15 +9,12 @@ on: jobs: test: runs-on: ubuntu-latest - defaults: - run: - working-directory: tests/references/bdhke-nutshell steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 with: python-version: "3.12" @@ -25,8 +22,33 @@ jobs: run: | curl -sSL https://install.python-poetry.org | python3 - + - name: Install Scarb + uses: software-mansion/setup-scarb@v1 + with: + scarb-version: "2.3.1" + - name: Install dependencies + working-directory: tests/references/bdhke-nutshell run: poetry install - name: Run reference tests - run: ./run_reference_tests.sh + run: | + ./tests/references/reference_tests.sh + + - name: Check test results + run: | + if grep -q "false" tests/references/target/results.json; then + echo "Some tests failed. Check the report for details." + exit 1 + fi + + - name: Upload test artifacts + uses: actions/upload-artifact@v3 + if: always() + with: + name: test-results + path: | + tests/references/target/report.txt + tests/references/target/results.json + tests/references/target/cairo_output.log + tests/references/target/nutshell_output.log diff --git a/.gitignore b/.gitignore index c78a8bd..2ce669b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,7 @@ Cargo.lock *.pyd *.pyo *.pyd -**/__pycache__/ \ No newline at end of file +**/__pycache__/ + +**/target +**/.DS_Store \ No newline at end of file diff --git a/src/main.cairo b/src/main.cairo index cdddd1b..3b97213 100644 --- a/src/main.cairo +++ b/src/main.cairo @@ -23,6 +23,7 @@ fn main() { .unwrap(); let B_ = step1_alice(secret_msg, blinding_factor); - let B_coordinates = B_.get_coordinates().unwrap(); - println!("Blinded message: {:?}", B_coordinates); + let (B_x, B_y) = B_.get_coordinates().unwrap(); + println!("S1_Blinded_message_x: {B_x}"); + println!("S1_Blinded_message_y: {B_y}"); } diff --git a/tests/references/bdhke-nutshell/bdhke_nutshell.py b/tests/references/bdhke-nutshell/bdhke_nutshell.py index 5db7520..570448c 100644 --- a/tests/references/bdhke-nutshell/bdhke_nutshell.py +++ b/tests/references/bdhke-nutshell/bdhke_nutshell.py @@ -54,9 +54,6 @@ def log(message): log(f"Blinding factor (r): {r.pubkey.serialize().hex()}") x_coord, y_coord = pubkey_to_xy(r.pubkey) - - log(f"Blinding factor public key r x_coord: {x_coord.hex()}") - log(f"Blinding factor public key r y_coord: {y_coord.hex()}") log("***********************************************************\n") log("\n***********************************************************") @@ -64,8 +61,8 @@ def log(message): B_, _ = step1_alice(secret_msg, r) log(f"Blinded message (B_): {B_.serialize().hex()}") x_coord, y_coord = pubkey_to_xy(B_) - log(f"Blinded message public key r x_coord: {int.from_bytes(x_coord, 'big')}") - log(f"Blinded message public key r y_coord: {int.from_bytes(y_coord, 'big')}") + log(f"S1_Blinded_message_x: {int.from_bytes(x_coord, 'big')}") + log(f"S1_Blinded_message_y: {int.from_bytes(y_coord, 'big')}") log("***********************************************************\n") log("\n***********************************************************") diff --git a/tests/references/bdhke-nutshell/run_reference_tests.sh b/tests/references/bdhke-nutshell/run_nutshell.sh similarity index 100% rename from tests/references/bdhke-nutshell/run_reference_tests.sh rename to tests/references/bdhke-nutshell/run_nutshell.sh diff --git a/tests/references/patterns.txt b/tests/references/patterns.txt new file mode 100644 index 0000000..2ab5ea4 --- /dev/null +++ b/tests/references/patterns.txt @@ -0,0 +1,2 @@ +S1_Blinded_message_x +S1_Blinded_message_y \ No newline at end of file diff --git a/tests/references/reference_tests.sh b/tests/references/reference_tests.sh new file mode 100755 index 0000000..935fe6f --- /dev/null +++ b/tests/references/reference_tests.sh @@ -0,0 +1,112 @@ +#!/bin/bash + +set -e + +# Set up variables +WORKING_DIR=$(pwd) +TARGET_DIR="$WORKING_DIR/tests/references/target" +CAIRO_LOG="$TARGET_DIR/cairo_output.log" +NUTSHELL_LOG="$TARGET_DIR/nutshell_output.log" +PATTERNS_FILE="$WORKING_DIR/tests/references/patterns.txt" +RESULTS_FILE="$TARGET_DIR/results.json" +REPORT_FILE="$TARGET_DIR/report.txt" + +# Create target directory if it doesn't exist +mkdir -p "$TARGET_DIR" + +# Function to run tests and save logs +run_tests() { + echo "Running Cairo implementation..." + cd "$WORKING_DIR" + scarb cairo-run --available-gas=200000000 > "$CAIRO_LOG" 2>&1 + + echo "Running Nutshell implementation..." + cd "$WORKING_DIR/tests/references/bdhke-nutshell" + ./run_nutshell.sh > "$NUTSHELL_LOG" 2>&1 +} + +# Function to extract values from logs +extract_values() { + local log_file=$1 + local pattern=$2 + grep "$pattern" "$log_file" | cut -d':' -f2 | tr -d ' ' +} + +# Function to compare values and update results +compare_values() { + local pattern=$1 + local cairo_value=$(extract_values "$CAIRO_LOG" "$pattern") + local nutshell_value=$(extract_values "$NUTSHELL_LOG" "$pattern") + + echo "Pattern: $pattern" >> "$REPORT_FILE" + echo "Cairo value: $cairo_value" >> "$REPORT_FILE" + echo "Nutshell value: $nutshell_value" >> "$REPORT_FILE" + + if [ -z "$cairo_value" ] && [ -z "$nutshell_value" ]; then + echo " \"$pattern\": false," >> "$RESULTS_FILE" + echo "❌ $pattern: Missing value in both implementations" >> "$REPORT_FILE" + elif [ -z "$cairo_value" ]; then + echo " \"$pattern\": false," >> "$RESULTS_FILE" + echo "❌ $pattern: Missing value in Cairo implementation" >> "$REPORT_FILE" + elif [ -z "$nutshell_value" ]; then + echo " \"$pattern\": false," >> "$RESULTS_FILE" + echo "❌ $pattern: Missing value in Nutshell implementation" >> "$REPORT_FILE" + elif [ "$cairo_value" = "$nutshell_value" ]; then + echo " \"$pattern\": true," >> "$RESULTS_FILE" + echo "✅ $pattern: Match" >> "$REPORT_FILE" + else + echo " \"$pattern\": false," >> "$RESULTS_FILE" + echo "❌ $pattern: Mismatch" >> "$REPORT_FILE" + echo " Cairo: $cairo_value" >> "$REPORT_FILE" + echo " Nutshell: $nutshell_value" >> "$REPORT_FILE" + fi + echo "" >> "$REPORT_FILE" +} + +# Main execution +echo "Starting reference tests..." + +# Run tests +run_tests + +# Initialize results file +echo "{" > "$RESULTS_FILE" + +# Initialize report file +echo "Reference Test Report" > "$REPORT_FILE" +echo "=====================" >> "$REPORT_FILE" +echo "" >> "$REPORT_FILE" + +# Compare values for each pattern +while IFS= read -r pattern +do + compare_values "$pattern" +done < "$PATTERNS_FILE" + +# Finalize results file +sed -i '' '$ s/,$//' "$RESULTS_FILE" # Remove trailing comma +echo "}" >> "$RESULTS_FILE" + +# Calculate and add summary to report +total_patterns=$(wc -l < "$PATTERNS_FILE") +matching_patterns=$(grep -c "true" "$RESULTS_FILE") +echo "Summary:" >> "$REPORT_FILE" +echo "--------" >> "$REPORT_FILE" +echo "Total patterns: $total_patterns" >> "$REPORT_FILE" +echo "Matching patterns: $matching_patterns" >> "$REPORT_FILE" + +if [ "$total_patterns" -eq "$matching_patterns" ]; then + echo "✅ All patterns match" >> "$REPORT_FILE" + test_result=0 +else + echo "❌ Some patterns do not match" >> "$REPORT_FILE" + test_result=1 +fi + +# Display report +cat "$REPORT_FILE" + +echo "Reference tests completed. See $REPORT_FILE for full report." + +# Exit with the appropriate status +exit $test_result \ No newline at end of file