From 2ad47acab78185e33a719bc0ac03d04d6d5e6ce1 Mon Sep 17 00:00:00 2001 From: nhtyy Date: Wed, 27 Nov 2024 18:13:58 -0800 Subject: [PATCH] fix: hint if point can be decompressed through precompile syscall --- Cargo.toml | 5 +++++ curve25519-dalek/Cargo.toml | 1 + curve25519-dalek/src/edwards.rs | 19 +++++++++++++++---- curve25519-dalek/src/lib.rs | 3 --- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a891c6705..8bbefb99d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,8 @@ resolver = "2" [profile.dev] opt-level = 2 + +[patch.crates-io] +sp1-lib = { git = "https://github.com/succinctlabs/sp1", branch = "n/fix-eddcompress" } +sp1-curves = { git = "https://github.com/succinctlabs/sp1", branch = "n/fix-eddcompress" } + diff --git a/curve25519-dalek/Cargo.toml b/curve25519-dalek/Cargo.toml index 81918c6b1..65e8c6842 100644 --- a/curve25519-dalek/Cargo.toml +++ b/curve25519-dalek/Cargo.toml @@ -63,6 +63,7 @@ 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-curves = "3.0.0" anyhow = "1.0" [features] diff --git a/curve25519-dalek/src/edwards.rs b/curve25519-dalek/src/edwards.rs index 46dc56896..d18b35448 100644 --- a/curve25519-dalek/src/edwards.rs +++ b/curve25519-dalek/src/edwards.rs @@ -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 { + #[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() { @@ -252,7 +262,7 @@ impl CompressedEdwardsY { /// curve point. /// /// Accelerated with SP1's EdDecompress syscall. - pub fn decompress(&self) -> Option { + fn decompress_with_syscall(&self) -> EdwardsPoint { let mut XY_bytes = [0_u8; 64]; XY_bytes[32..].copy_from_slice(self.as_bytes()); unsafe { @@ -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, - }); + } } } diff --git a/curve25519-dalek/src/lib.rs b/curve25519-dalek/src/lib.rs index 7ad69412d..fecfe888c 100644 --- a/curve25519-dalek/src/lib.rs +++ b/curve25519-dalek/src/lib.rs @@ -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;