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 6 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
20 changes: 20 additions & 0 deletions core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ impl CurtaStdin {
self.buffer.read()
}

/// Read a slice of bytes from the buffer.
pub fn read_slice(&mut self, slice: &mut [u8]) {
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);
}

/// Write a slice of bytes to the buffer.
pub fn write_slice(&mut self, slice: &[u8]) {
self.buffer.write_slice(slice);
}
}

impl CurtaStdout {
Expand All @@ -62,10 +72,20 @@ impl CurtaStdout {
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);
}

/// Write a slice of bytes to the buffer.
pub fn write_slice(&mut self, slice: &[u8]) {
self.buffer.write_slice(slice);
}
}

pub fn serialize_proof<S, SC: StarkGenericConfig + Serialize>(
Expand Down
32 changes: 15 additions & 17 deletions core/src/syscall/precompiles/k256/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,15 @@ impl<V: Copy> K256DecompressCols<V> {
// Interpret the lowest bit of Y as whether it is odd or not.
let y_is_odd = self.y_least_bits[0];

// When y_is_odd == should_be_odd, the result is y, otherwise it is -y.
// When y_is_odd and should_be_odd are both true or both false, result is y, else -y.
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 @@ pub mod tests {
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::{CurtaProver, CurtaStdin, CurtaVerifier};

#[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 = CurtaStdin::from(&compressed);
let mut proof = CurtaProver::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);

CurtaVerifier::verify(SECP256K1_DECOMPRESS_ELF, &proof).unwrap();
}
}
}
11 changes: 11 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,10 +37,20 @@ 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();
bincode::serialize_into(&mut tmp, data).expect("serialization failed");
self.data.extend(tmp);
}

/// Write the slice of bytes to the buffer.
pub fn write_slice(&mut self, slice: &[u8]) {
self.data.extend_from_slice(slice);
}
}
Loading