Skip to content

Commit

Permalink
Prove with Scarb (#6)
Browse files Browse the repository at this point in the history
* Use scarb prove (nightly)

* Use the same stwo-cairo commit

* Use scarb nightly, add resource usage script

* Update test files

* Update test files
  • Loading branch information
m-kus authored Feb 4, 2025
1 parent c7c1a17 commit 248c45f
Show file tree
Hide file tree
Showing 16 changed files with 982 additions and 2,308 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/aggsig-checker-cairo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.10.0-rc.1"
scarb-version: "nightly"

- name: Check formatting
run: scarb fmt --check
Expand All @@ -32,10 +32,10 @@ jobs:
- name: Install Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.10.0-rc.1"
scarb-version: "nightly"

- name: Run tests
run: scarb test
run: make test
working-directory: packages/aggsig_checker

build:
Expand All @@ -47,9 +47,9 @@ jobs:
- name: Install Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.10.0-rc.1"
scarb-version: "nightly"

- name: Build project for proving
run: scarb --profile proving build
run: make build
working-directory: packages/aggsig_checker

6 changes: 4 additions & 2 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.10.0-rc.1"
scarb-version: "nightly"

- name: Install Stwo and Python dependencies
run: |
Expand All @@ -36,4 +36,6 @@ jobs:
- name: Run end-to-end test
run: |
cd packages/aggsig_checker
make e2e
make build
make execute
make prove
14 changes: 14 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "aggsig_checker"
version = "0.1.0"
dependencies = [
"garaga",
]

[[package]]
name = "garaga"
version = "0.15.3"
source = "git+https://github.com/m-kus/garaga?rev=65db99b8ba9ff19e7f19609cd06f8220d8406881#65db99b8ba9ff19e7f19609cd06f8220d8406881"
12 changes: 12 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[workspace]
members = ["packages/*"]

[profile.profiling]
unstable-add-statements-functions-debug-info = true
unstable-add-statements-code-locations-debug-info = true

[profile.proving]
inherits = "release"

[profile.proving.cairo]
enable-gas = false
29 changes: 16 additions & 13 deletions packages/aggsig_checker/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
NUM_EVENTS ?= 10
NUM_EVENTS ?= 5
OUTPUT_DIR ?= tests/data
TARGET_DIR ?= ../../target

install-stwo:
RUSTFLAGS="-C target-cpu=native -C opt-level=3" \
Expand All @@ -26,26 +27,28 @@ args:
python scripts/gen_args.py --file $(OUTPUT_DIR)/events.json --target cairo-run > $(OUTPUT_DIR)/args_test.json

execute:
rm -rf target/execute \
rm -rf $(TARGET_DIR)/execute \
&& scarb --profile proving execute \
--no-build \
--arguments-file $(OUTPUT_DIR)/args.json \
--print-program-output
--print-program-output \
--print-resource-usage

run:
scarb cairo-run --no-build --arguments-file $(OUTPUT_DIR)/args_test.json --print-resource-usage

prove:
adapted_stwo \
--priv_json target/execute/aggsig_checker/air_private_input.json \
--pub_json target/execute/aggsig_checker/air_public_input.json \
--priv_json $(TARGET_DIR)/execute/aggsig_checker/execution1/air_private_input.json \
--pub_json $(TARGET_DIR)/execute/aggsig_checker/execution1/air_public_input.json \
--proof_path $(TARGET_DIR)/proof.json \
--params_json prover_params.json \
--proof_path target/proof.json \
--verify

e2e:
$(MAKE) events
$(MAKE) args
$(MAKE) build
$(MAKE) execute
$(MAKE) prove

burn:
scarb burn --arguments-file tests/data/args.json --output-file target/graph.svg --open-in-browser

test:
scarb test
scarb build --target-kinds lib
$(MAKE) run
28 changes: 23 additions & 5 deletions packages/aggsig_checker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@

This package contains the Cairo implementation of a program that verifies a batch of Nostr events.

## Prerequisites
## Quickstart

Make sure you have **nightly** [Scarb](https://docs.swmansion.com/scarb/download.html) installed.

Generate a proof for sample events:

```sh
scarb --profile proving prove --execute --arguments-file tests/data/args.json
```

Verify the proof:

```sh
scarb verify --execution-id 1
```

## Development

### Prerequisites

- [Rust nightly toolchain](https://www.rust-lang.org/tools/install)
- [Python 3.10](https://www.python.org/downloads/) ([pyenv](https://github.com/pyenv/pyenv) recommended)
- [Scarb](https://docs.swmansion.com/scarb/download.html) 2.10.0-rc.1

## Setup
### Setup

Install dependencies:

```sh
make install
```
## Usage

### Usage

Build the Cairo program:

Expand All @@ -37,7 +55,7 @@ make prove

The command will generate a proof and verify it locally.

## Testing
### Testing

Run the test suite:

Expand Down
17 changes: 2 additions & 15 deletions packages/aggsig_checker/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,11 @@ edition = "2024_07"

[dependencies]
garaga = { git = "https://github.com/m-kus/garaga", rev = "65db99b8ba9ff19e7f19609cd06f8220d8406881" }
cairo_execute = "2.10.0-rc.1"
cairo_execute = "2.9.2"

[executable]

[lib]

[profile.profiling]
unstable-add-statements-functions-debug-info = true
unstable-add-statements-code-locations-debug-info = true

[profile.proving]
inherits = "release"

[profile.proving.cairo]
enable-gas = false

[dev-dependencies]
cairo_test = "2.10.0-rc.1"

[scripts]
test = "scarb cairo-test && scarb cairo-run --arguments-file tests/data/args_test.json"
cairo_test = "2.9.2"
5 changes: 2 additions & 3 deletions packages/aggsig_checker/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
mod nip01;
mod schnorr;
mod sha256;

use garaga::ec_ops::{DerivePointFromXHint, MSMHint};
use garaga::definitions::SECP256K1;
use schnorr::verify_schnorr;
use garaga::ec_ops::{DerivePointFromXHint, MSMHint};
use nip01::hash_challenge;
use schnorr::verify_schnorr;

/// Struct representing a signed Nostr event with hints for the signature verification.
#[derive(Drop, Serde)]
Expand Down
8 changes: 3 additions & 5 deletions packages/aggsig_checker/src/schnorr.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use garaga::definitions::Zero;
use garaga::ec_ops::G1PointTrait;
use garaga::definitions::SECP256K1;
use garaga::ec_ops::{DerivePointFromXHint, G1Point, MSMHint, msm_g1, u384};
use garaga::basic_field_ops::neg_mod_p;
use core::circuit::{CircuitModulus, u96};
use garaga::basic_field_ops::neg_mod_p;
use garaga::definitions::{SECP256K1, Zero};
use garaga::ec_ops::{DerivePointFromXHint, G1Point, G1PointTrait, MSMHint, msm_g1, u384};

pub const SECP256K1_G1_GENERATOR: G1Point = G1Point {
x: u384 {
Expand Down
4 changes: 2 additions & 2 deletions packages/aggsig_checker/src/sha256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ fn create_message_schedule(data: Span<u32>, i: usize) -> Span<u32> {
let mut result = array![];
for j in 0..16_usize {
result.append(*data[i * 16 + j]);
};
}
for i in 16..64_usize {
let s0 = ssig0(*result[i - 15]);
let s1 = ssig1(*result[i - 2]);
let (tmp, _) = (*result[i - 16]).overflowing_add(s0);
let (tmp, _) = tmp.overflowing_add(*result[i - 7]);
let (res, _) = tmp.overflowing_add(s1);
result.append(res);
};
}
result.span()
}

Expand Down
Loading

0 comments on commit 248c45f

Please sign in to comment.