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

fix: k256 decompress #202

Merged
merged 7 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ impl SuccinctStdout {
self.buffer.read()
}

/// Read a slice of bytes from the buffer.
pub fn read_slice(&mut self, slice: &mut [u8]) {
ctian1 marked this conversation as resolved.
Show resolved Hide resolved
self.buffer.read_slice(slice);
}

/// Write a value to the buffer.
pub fn write<T: Serialize + DeserializeOwned>(&mut self, data: &T) {
self.buffer.write(data);
Expand Down
30 changes: 14 additions & 16 deletions core/src/syscall/precompiles/k256/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@
let y_limbs = limbs_from_access(&self.y_access);
builder
.when(self.is_real)
.when(AB::Expr::one() - (y_is_odd.into() - should_be_odd.clone()))
.when(
ctian1 marked this conversation as resolved.
Show resolved Hide resolved
(y_is_odd.into() * should_be_odd.clone())
+ ((AB::Expr::one() - y_is_odd.into())
* (AB::Expr::one() - should_be_odd.clone())),
)
.assert_all_eq(self.y.multiplication.result, y_limbs);
builder
.when(self.is_real)
Expand Down Expand Up @@ -370,35 +374,29 @@
use rand::rngs::StdRng;
use rand::SeedableRng;

use crate::utils::setup_logger;
use crate::utils::tests::SECP256K1_DECOMPRESS_ELF;
use crate::utils::BabyBearBlake3;
use crate::{
runtime::{Program, Runtime},
utils::{prove_core, setup_logger},
};
use crate::{SuccinctProver, SuccinctStdin, SuccinctVerifier};

Check failure on line 379 in core/src/syscall/precompiles/k256/decompress.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

unresolved imports `crate::SuccinctProver`, `crate::SuccinctStdin`, `crate::SuccinctVerifier`

Check failure on line 379 in core/src/syscall/precompiles/k256/decompress.rs

View workflow job for this annotation

GitHub Actions / CI Test Suite

unresolved imports `crate::SuccinctProver`, `crate::SuccinctStdin`, `crate::SuccinctVerifier`
ctian1 marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn test_k256_decompress() {
setup_logger();
let mut rng = StdRng::seed_from_u64(2);

for _ in 0..4 {
for _ in 0..10 {
let secret_key = k256::SecretKey::random(&mut rng);
let public_key = secret_key.public_key();
let encoded = public_key.to_encoded_point(false);
let decompressed = encoded.as_bytes();
let compressed = public_key.to_sec1_bytes();
let mut result: [u8; 65] = [0; 65];

let program = Program::from(SECP256K1_DECOMPRESS_ELF);
let mut runtime = Runtime::new(program);
runtime.write_stdin_slice(&compressed);
runtime.run();
runtime.read_stdout_slice(&mut result);

let inputs = SuccinctStdin::from(&compressed);
let mut proof = SuccinctProver::prove(SECP256K1_DECOMPRESS_ELF, inputs).unwrap();
let mut result = [0; 65];
proof.stdout.read_slice(&mut result);
assert_eq!(result, decompressed);
let config = BabyBearBlake3::new();
prove_core(config, &mut runtime);

SuccinctVerifier::verify(SECP256K1_DECOMPRESS_ELF, &proof).unwrap();
}
}
}
6 changes: 6 additions & 0 deletions core/src/utils/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Buffer {
pub data: Vec<u8>,
#[serde(skip)]
pub ptr: usize,
}

Expand Down Expand Up @@ -36,6 +37,11 @@ impl Buffer {
result
}

pub fn read_slice(&mut self, slice: &mut [u8]) {
slice.copy_from_slice(&self.data[self.ptr..self.ptr + slice.len()]);
self.ptr += slice.len();
}

/// Write the serializable object from the buffer.
pub fn write<T: Serialize + DeserializeOwned>(&mut self, data: &T) {
let mut tmp = Vec::new();
Expand Down
Loading