Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment(ark-zkey): Use rkyv for faster loading #118

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions ark-zkey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ color-eyre = "0.6"
memmap2 = "0.9"
flame = "0.2"
flamer = "0.5"
rkyv = "0.7.43"

ark-serialize = { version = "=0.4.1", features = ["derive"] }
ark-bn254 = { version = "=0.4.0" }
Expand Down
98 changes: 95 additions & 3 deletions ark-zkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,52 @@ use memmap2::Mmap;
use std::fs::File;
//use std::io::Cursor;
//use std::io::{Read,self};
use rkyv::{archived_root, Infallible};
use rkyv::{Archive, Deserialize, Serialize};
use std::io::BufReader;
use std::path::PathBuf;
use std::time::Instant;

#[derive(CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq)]
// NOTE: Archive, Serialize, Deserialize traits are for rkyv zero-copy deserialization experiment
// See https://github.com/oskarth/mopro/issues/25

// XXX: the trait `Archive` is not implemented for `ProvingKey<ark_ec::models::bn::Bn<ark_bn254::Config>>`
#[derive(
Archive,
Serialize,
Deserialize,
CanonicalSerialize,
CanonicalDeserialize,
Clone,
Debug,
PartialEq,
)]
pub struct SerializableProvingKey(pub ProvingKey<Bn254>);

#[derive(CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq)]
#[derive(
Archive,
Serialize,
Deserialize,
CanonicalSerialize,
CanonicalDeserialize,
Clone,
Debug,
PartialEq,
)]
pub struct SerializableMatrix<F: Field> {
pub data: Vec<Vec<(F, usize)>>,
}

#[derive(CanonicalSerialize, CanonicalDeserialize, Clone, Debug, PartialEq)]
#[derive(
Archive,
Serialize,
Deserialize,
CanonicalSerialize,
CanonicalDeserialize,
Clone,
Debug,
PartialEq,
)]
pub struct SerializableConstraintMatrices<F: Field> {
pub num_instance_variables: usize,
pub num_witness_variables: usize,
Expand Down Expand Up @@ -137,6 +170,23 @@ pub fn read_arkzkey_from_bytes(
Ok((proving_key, constraint_matrices))
}

// Function to read the arkzkey file using rkyv for zero-copy deserialization
pub fn read_arkzkey_with_rkyv(
arkzkey_path: &str,
) -> Result<(ProvingKey<Bn254>, ConstraintMatrices<Fr>), Box<dyn std::error::Error>> {
let arkzkey_file_path = PathBuf::from(arkzkey_path);
let arkzkey_file = File::open(arkzkey_file_path)?;

// Using mmap for zero-copy deserialization
let mmap = unsafe { Mmap::map(&arkzkey_file)? };
let archived_data = unsafe { archived_root::<SerializableConstraintMatrices<Fr>>(&mmap[..]) };

// Optionally deserialize if you need to manipulate the data
let constraint_matrices: ConstraintMatrices<Fr> = archived_data.deserialize(&mut Infallible)?;

Ok((archived_data.0, constraint_matrices))
}

pub fn read_proving_key_and_matrices_from_zkey(
zkey_path: &str,
) -> Result<(SerializableProvingKey, SerializableConstraintMatrices<Fr>)> {
Expand Down Expand Up @@ -247,6 +297,48 @@ mod tests {
Ok(())
}

#[test]
fn test_rkyv_serialization_deserialization() -> Result<(), Box<dyn std::error::Error>> {
let zkey_path =
"../mopro-core/examples/circom/keccak256/target/keccak256_256_test_final.zkey";
let arkzkey_path = zkey_path.replace(".zkey", ".arkzkey");

// Assuming `read_proving_key_and_matrices_from_zkey` reads the original data
let (original_proving_key, original_constraint_matrices) =
read_proving_key_and_matrices_from_zkey(zkey_path)?;

// Convert to arkzkey if necessary
// Assuming `convert_zkey` serializes the data into the arkzkey format
println!("[build] Writing arkzkey to: {}", arkzkey_path);
let now = Instant::now();
convert_zkey(
original_proving_key.clone(),
original_constraint_matrices.clone(),
&arkzkey_path,
)?;
println!("[build] Time to write arkzkey: {:?}", now.elapsed());

// Read the arkzkey using the rkyv-based function
println!("Reading arkzkey with rkyv from: {}", arkzkey_path);
let now = Instant::now();
let (deserialized_proving_key, deserialized_constraint_matrices) =
read_arkzkey_with_rkyv(&arkzkey_path)?;
println!("Time to read arkzkey with rkyv: {:?}", now.elapsed());

// Compare the original and deserialized data
assert_eq!(
original_proving_key, deserialized_proving_key,
"Original and deserialized proving keys do not match"
);

assert_eq!(
original_constraint_matrices, deserialized_constraint_matrices,
"Original and deserialized constraint matrices do not match"
);

Ok(())
}

#[test]
fn test_multiplier2_serialization_deserialization() -> Result<()> {
test_circuit_serialization_deserialization(
Expand Down
Loading