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

feat: add compress to compiler #752

Merged
merged 1 commit into from
Jun 19, 2023
Merged

Conversation

vlopes11
Copy link
Contributor

@vlopes11 vlopes11 commented Apr 2, 2023

This commit introduces Compiler::compress and Compiler::decompress, two functions that will create a compressed representation of a circuit that can be used to generate prover and verifier keys.

The compression strategy takes advantage of the fact that circuit representations are sparse; meaning, most of the scalars are zeroes. We also have a higher incidence of 1 and -1, so any value that is equivalent to a i8 is compressed into a single byte, instead of the regular 32 bytes of a bls12-381.

This will result in expressive gains in terms of storage. For instance, a 2^12 circuit can be compressed into roughly 100Kb.

@codecov
Copy link

codecov bot commented Apr 2, 2023

Codecov Report

Merging #752 (4c24a8e) into master (0487cc4) will decrease coverage by 0.26%.
The diff coverage is 85.91%.

❗ Current head 4c24a8e differs from pull request most recent head c4fda09. Consider uploading reports for the commit c4fda09 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #752      +/-   ##
==========================================
- Coverage   85.33%   85.07%   -0.26%     
==========================================
  Files          54       56       +2     
  Lines        4166     4343     +177     
==========================================
+ Hits         3555     3695     +140     
- Misses        611      648      +37     
Impacted Files Coverage Δ
src/composer.rs 91.05% <ø> (ø)
src/composer/verifier.rs 89.47% <ø> (+3.26%) ⬆️
src/constraint_system/witness.rs 66.66% <ø> (ø)
src/error.rs 0.00% <ø> (ø)
tests/common/mod.rs 76.47% <ø> (ø)
src/constraint_system/constraint.rs 60.25% <33.33%> (ø)
src/composer/compiler/compress/hades.rs 80.95% <80.95%> (ø)
src/composer/compiler.rs 97.95% <85.71%> (-0.62%) ⬇️
src/composer/compiler/compress.rs 87.89% <87.89%> (ø)
src/composer/prover.rs 95.75% <100.00%> (-2.79%) ⬇️
... and 1 more

... and 8 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0487cc4...c4fda09. Read the comment docs.

@vlopes11 vlopes11 force-pushed the vlopes11-compiler-encoder branch 2 times, most recently from 709413a to 2a36291 Compare April 2, 2023 15:47
@vlopes11 vlopes11 requested review from ZER0 and moCello April 2, 2023 15:54
@vlopes11 vlopes11 changed the title feat: add encode to compiler feat: add compress to compiler Apr 4, 2023
Copy link
Member

@moCello moCello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it lgtm, just some requests for comments here and there.
The PR still needs an issue though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo it would be nice to have 'compressor' somewhere in the module path. Maybe add a compressor module with scalar and witness in it?

src/composer/compiler/scalar.rs Outdated Show resolved Hide resolved
src/composer/compiler/scalar.rs Outdated Show resolved Hide resolved
src/composer/compiler/scalar.rs Outdated Show resolved Hide resolved
];

#[cfg(test)]
mod tests {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am missing tests for compressing and decompressing scalars that are bigger than i8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are here:

#[test]
fn compress_decompress_works() {
let compressor = ScalarCompressor::default();
let mut buffer: Vec<u8> = vec![];
// check negatives
for v in 0..=u8::MAX {
buffer.clear();
let x = BlsScalar::zero() - BlsScalar::from(v as u64);
let n = compressor.compress(&x, &mut buffer);
let (p, y) = ScalarCompressor::decompress(&buffer[..n]);
assert_eq!(n, p);
assert_eq!(x, y);
}
// check positives
for v in 0..=u8::MAX {
buffer.clear();
let x = BlsScalar::from(v as u64);
let n = compressor.compress(&x, &mut buffer);
let (p, y) = ScalarCompressor::decompress(&buffer[..n]);
assert_eq!(n, p);
assert_eq!(x, y);
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for scalars bigger than u8?

src/composer/compiler/witness.rs Outdated Show resolved Hide resolved
src/composer/compiler/witness.rs Outdated Show resolved Hide resolved
src/composer/compiler/witness.rs Outdated Show resolved Hide resolved
src/composer/compiler.rs Outdated Show resolved Hide resolved
@vlopes11 vlopes11 requested a review from moCello April 9, 2023 22:03
Copy link
Member

@moCello moCello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vlopes11 vlopes11 force-pushed the vlopes11-compiler-encoder branch 2 times, most recently from 11b690f to 5d3d862 Compare June 13, 2023 15:21
This commit introduces `Compiler::compress` and `Compiler::decompress`, two
functions that will create a compressed representation of a circuit that
can be used to generate prover and verifier keys.

The compression strategy takes advantage of the fact that circuit
representations are sparse; meaning, most of the scalars are zeroes. We
also have a higher incidence of `1` and `-1`.

This will result in expressive gains in terms of storage. For instance,
a `2^16` circuit can be compressed into roughly 250Kb.
@vlopes11 vlopes11 merged commit 7cf13ef into master Jun 19, 2023
@vlopes11 vlopes11 deleted the vlopes11-compiler-encoder branch June 19, 2023 14:17
@ureeves ureeves mentioned this pull request Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants