Skip to content

Commit

Permalink
Merge pull request #2955 from o1-labs/martin/saffron-skeleton
Browse files Browse the repository at this point in the history
encoding/decoding for saffron data
  • Loading branch information
martyall authored Jan 21, 2025
2 parents 4d6e1d0 + 0ea239e commit d7b7782
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 16 deletions.
50 changes: 35 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ members = [
"utils",
"internal-tracing",
"ivc",
"folding"
"folding",
"saffron"
]
resolver = "2"

Expand Down Expand Up @@ -96,6 +97,7 @@ o1-utils = { path = "./utils", version = "0.1.0" }
o1vm = { path = "./o1vm", version = "0.1.0" }
optimism = { path = "./optimism", version = "0.1.0" }
poly-commitment = { path = "./poly-commitment", version = "0.1.0" }
saffron = { path = "./poly-commitment", version = "0.1.0" }
signer = { path = "./signer", version = "0.1.0" }
turshi = { path = "./turshi", version = "0.1.0" }
utils = { path = "./utils", version = "0.1.0" }
Expand Down
3 changes: 3 additions & 0 deletions saffron/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
proptest-regressions
fixtures/lorem.bin
fixtures/lorem-decoded.txt
30 changes: 30 additions & 0 deletions saffron/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "saffron"
version = "0.1.0"
description = "He who controls the spice controls the universe."
repository = "https://github.com/o1-labs/proof-systems"
homepage = "https://o1-labs.github.io/proof-systems/"
documentation = "https://o1-labs.github.io/proof-systems/rustdoc/"
readme = "README.md"
edition = "2021"
license = "Apache-2.0"

# [lib]
# path = "src/lib.rs"

[[bin]]
name = "saffron"
path = "src/main.rs"

[dependencies]
anyhow = "1.0"
ark-ff.workspace = true
ark-serialize = { workspace = true, features = ["derive"]}
clap = { workspace = true, features = ["derive"] }
mina-curves.workspace = true
o1-utils.workspace = true


[dev-dependencies]
ark-std.workspace = true
proptest.workspace = true
1 change: 1 addition & 0 deletions saffron/fixtures/lorem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
42 changes: 42 additions & 0 deletions saffron/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use clap::{arg, Parser};

#[derive(Parser, Debug, Clone)]
pub struct EncodeFileArgs {
#[arg(long, short = 'i', value_name = "FILE", help = "input file")]
pub input: String,

#[arg(
long,
short = 'o',
value_name = "FILE",
help = "output file (encoded as field elements)"
)]
pub output: String,
}

#[derive(Parser, Debug, Clone)]
pub struct DecodeFileArgs {
#[arg(
long,
short = 'i',
value_name = "FILE",
help = "input file (encoded as field elements)"
)]
pub input: String,

#[arg(long, short = 'o', value_name = "FILE", help = "output file")]
pub output: String,
}

#[derive(Parser, Debug, Clone)]
#[command(
name = "saffron",
version = "0.1",
about = "saffron - a data availability layer"
)]
pub enum Commands {
#[command(name = "encode")]
Encode(EncodeFileArgs),
#[command(name = "decode")]
Decode(DecodeFileArgs),
}
3 changes: 3 additions & 0 deletions saffron/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod serialization;

pub mod cli;
40 changes: 40 additions & 0 deletions saffron/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use anyhow::Result;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use clap::Parser;
use mina_curves::pasta::Fp;
use saffron::{cli, serialization::FieldBlob};
use std::{
fs::File,
io::{Read, Write},
};

fn decode_file(args: cli::DecodeFileArgs) -> Result<()> {
let mut file = File::open(args.input)?;
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
let blob: FieldBlob<Fp> = FieldBlob::<Fp>::deserialize_compressed(&buf[..])?;
let data = FieldBlob::<Fp>::decode(blob);
let mut writer = File::create(args.output)?;
writer.write_all(&data)?;
Ok(())
}

fn encode_file(args: cli::EncodeFileArgs) -> Result<()> {
let mut file = File::open(args.input)?;
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
let blob = FieldBlob::<Fp>::encode(&buf);
let mut bytes_to_write = Vec::with_capacity(buf.len());
blob.serialize_compressed(&mut bytes_to_write)?;
let mut writer = File::create(args.output)?;
writer.write_all(&bytes_to_write)?;
Ok(())
}

pub fn main() -> Result<()> {
let args = cli::Commands::parse();
match args {
cli::Commands::Encode(args) => encode_file(args),
cli::Commands::Decode(args) => decode_file(args),
}
}
Loading

0 comments on commit d7b7782

Please sign in to comment.