Skip to content

Commit

Permalink
fix: hint if point can be decompressed through precompile syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Nov 29, 2024
1 parent a9d4628 commit bfe63b8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ resolver = "2"

[profile.dev]
opt-level = 2

2 changes: 1 addition & 1 deletion curve25519-dalek/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cpufeatures = "0.2.6"
fiat-crypto = { version = "0.2.1", default-features = false }

[target.'cfg(all(target_os = "zkvm", target_vendor = "succinct"))'.dependencies]
sp1-lib = "3.0.0"
sp1-lib = "3.4.0"
anyhow = "1.0"

[features]
Expand Down
19 changes: 15 additions & 4 deletions curve25519-dalek/src/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,22 @@ impl CompressedEdwardsY {
self.0
}

#[cfg(not(all(target_os = "zkvm", target_vendor = "succinct")))]
/// Attempt to decompress to an `EdwardsPoint`.
///
/// Returns `None` if the input is not the \\(y\\)-coordinate of a
/// curve point.
pub fn decompress(&self) -> Option<EdwardsPoint> {
#[cfg(all(target_os = "zkvm", target_vendor = "succinct"))]
{
sp1_lib::unconstrained! {
sp1_lib::io::write(sp1_lib::io::FD_EDDECOMPRESS, self.as_bytes());
}

if sp1_lib::io::read_vec().first().expect("We should have a status from the hook") == &1 {
return Some(self.decompress_with_syscall());
}
}

let (is_valid_y_coord, X, Y, Z) = decompress::step_1(self);

if is_valid_y_coord.into() {
Expand All @@ -252,7 +262,7 @@ impl CompressedEdwardsY {
/// curve point.
///
/// Accelerated with SP1's EdDecompress syscall.
pub fn decompress(&self) -> Option<EdwardsPoint> {
fn decompress_with_syscall(&self) -> EdwardsPoint {
let mut XY_bytes = [0_u8; 64];
XY_bytes[32..].copy_from_slice(self.as_bytes());
unsafe {
Expand All @@ -261,12 +271,13 @@ impl CompressedEdwardsY {
let X = FieldElement::from_bytes(&XY_bytes[0..32].try_into().unwrap());
let Y = FieldElement::from_bytes(&XY_bytes[32..].try_into().unwrap());
let Z = FieldElement::ONE;
return Some(EdwardsPoint {

EdwardsPoint {
X,
Y,
Z,
T: &X * &Y,
});
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions curve25519-dalek/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ extern crate alloc;
#[macro_use]
extern crate std;

#[cfg(all(target_os = "zkvm", target_vendor = "succinct"))]
extern crate sp1_lib;

#[cfg(feature = "digest")]
pub use digest;

Expand Down

0 comments on commit bfe63b8

Please sign in to comment.