Skip to content

Commit

Permalink
✅ reference test against nutshell implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Aug 6, 2024
1 parent e1930b9 commit 57f8862
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 14 deletions.
34 changes: 28 additions & 6 deletions .github/workflows/reference_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,46 @@ 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"

- name: Install Poetry
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ Cargo.lock
*.pyd
*.pyo
*.pyd
**/__pycache__/
**/__pycache__/

**/target
**/.DS_Store
5 changes: 3 additions & 2 deletions src/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
7 changes: 2 additions & 5 deletions tests/references/bdhke-nutshell/bdhke_nutshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,15 @@ 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***********************************************************")
log("STEP 1: Alice blinds the 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***********************************************************")
Expand Down
2 changes: 2 additions & 0 deletions tests/references/patterns.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
S1_Blinded_message_x
S1_Blinded_message_y
112 changes: 112 additions & 0 deletions tests/references/reference_tests.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 57f8862

Please sign in to comment.