Skip to content

Commit

Permalink
doc: update readme (#215)
Browse files Browse the repository at this point in the history
* feat: add prove from program string in cli

* doc: update README
  • Loading branch information
zmalatrax authored Jan 15, 2025
1 parent 3659037 commit 5890328
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 11 deletions.
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,80 @@

`stwo-brainfuck` is a ZK-VM for the Brainfuck language[^1], based on Stwo[^2].

## Objectives
## CLI - Installation

Here are the steps to get the Brainfuck ZK-VM up and running.

You can either download the binaries from the [releases](https://github.com/kkrt-labs/stwo-brainfuck/releases), or build them from source.

### Build from Source

1. Clone the repository

```shell
git clone git@github.com:kkrt-labs/stwo-brainfuck.git
```

2. Build the `brainfuck_prover`

The `brainfuck_prover` has a feature flag which enables the CPU parallelization feature of Stwo.

```shell
# No feature flags
cargo --package brainfuck_prover --release
```

```shell
# Parallel feature flag
cargo --package brainfuck_prover --features parallel --release
```

## CLI - Usage

The `brainfuck_prover` CLI has two subcommands:

- `prove`, which generates a CSTARK proof from a given Brainfuck program file or code string.
- `verify`, which verify a CSTARK proof from a given Brainfuck proof file.

For more information, try `brainfuck_prover --help`, `brainfuck_prover prove --help` and `brainfuck_prover verify --help`.

### Example usage

Consider this Brainfuck program which, given an ASCII character from Stdin, outputs the following two characters in the ASCII table:

```brainfuck
++>,<[>+.<-]
```

### Prove

To generate a proof of this program, you can provide the Brainfuck program as a string, with the `--code` argument,
or store it in a file `my_program.bf` and provide the path to it with the `--file` argument.

Here, the proof will be serialized to a JSON file `my_program_proof.json`.
You can also print the proof to `stdout` with the `--print` flag.

1. Proof from a Brainfuck program given in the command

```shell
brainfuck_prover prove --code "++>,<[>+.<-]" --output my_program_proof.json
```

2. Proof from program file

```shell
brainfuck_prover prove --file my_program.bf --output my_program_proof.json
```

### Verify

To verify a proof, the proof must be stored in a JSON file (`--output` flag from the `prove` subcommand).

```shell
brainfuck_prover verify my_program_proof.json
```

## Project Objectives

- Capacity of generating and verifying a proof for arbitrary Brainfuck programs.
- Understanding of using Stwo for building ZK-VMs
Expand Down
32 changes: 22 additions & 10 deletions crates/brainfuck_prover/src/bin/brainfuck_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ struct Cli {
#[derive(Subcommand, Debug)]
enum Commands {
/// Generate a proof
#[command(group(
ArgGroup::new("input_mode")
.args(["file", "code"])
.required(true),
))]
#[command(group(
ArgGroup::new("output_mode")
.args(["output", "print"]),
))]
Prove {
/// Path to the Brainfuck program file.
#[clap(value_parser, value_hint=ValueHint::FilePath)]
filename: PathBuf,
#[clap(value_parser, long, value_hint=ValueHint::FilePath)]
file: Option<PathBuf>,
/// Direct Brainfuck code input.
#[clap(long)]
code: Option<String>,
/// Log Level.
#[clap(long, default_value = "info")]
log: String,
Expand All @@ -46,10 +54,6 @@ enum Commands {
print: bool,
},
/// Verify a proof
#[command(group(
ArgGroup::new("output_mode")
.args(["output", "print"]),
))]
Verify {
/// Path to the CSTARK proof of the Brainfuck program execution.
#[clap(value_parser, value_hint=ValueHint::FilePath)]
Expand All @@ -61,7 +65,8 @@ enum Commands {
}

struct ExecutionConfig {
filename: PathBuf,
file: Option<PathBuf>,
code: Option<String>,
trace: bool,
memory: bool,
ram_size: Option<usize>,
Expand All @@ -72,7 +77,13 @@ struct ExecutionConfig {
/// Generate a CSTARK Proof from a given Brainfuck filepath.
fn prove(execution_config: ExecutionConfig) -> Result<(), ProvingError> {
tracing::info!("Program compilation");
let code = fs::read_to_string(execution_config.filename).expect("Failed to read file");

let code = if let Some(path) = execution_config.file {
fs::read_to_string(path).expect("Failed to read file")
} else {
execution_config.code.unwrap()
};

let mut bf_compiler = Compiler::new(&code);
let ins = bf_compiler.compile();

Expand Down Expand Up @@ -131,11 +142,12 @@ fn main() {
let cli = Cli::parse();

match &cli.command {
Commands::Prove { filename, log, trace, memory, ram_size, output, print } => {
Commands::Prove { file, code, log, trace, memory, ram_size, output, print } => {
tracing_subscriber::fmt().with_env_filter(log).init();
tracing::info!("Brainfuck ZK-VM - Prove");
let execution_config = ExecutionConfig {
filename: filename.clone(),
file: file.clone(),
code: code.clone(),
trace: *trace,
memory: *memory,
ram_size: *ram_size,
Expand Down

0 comments on commit 5890328

Please sign in to comment.