Skip to content

Commit

Permalink
Merge pull request zkcrypto#2 from xavierdmello/main
Browse files Browse the repository at this point in the history
feat: patch decompress_pubkey & add from_hex
  • Loading branch information
xavierdmello authored Jul 4, 2024
2 parents 1bcd44e + c4f6bc6 commit 0d643ce
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ edition = "2021"
[package.metadata.docs.rs]
rustdoc-args = [ "--html-in-header", "katex-header.html" ]

[dependencies]
sp1_precompiles = { git = "https://github.com/succinctlabs/sp1.git", package = "sp1-precompiles" }

[dev-dependencies]
csv = ">= 1.0, < 1.2" # csv 1.2 has MSRV 1.60
criterion = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.56.0"
components = [ "clippy", "rustfmt" ]
channel = "nightly-2024-04-17"
components = ["llvm-tools", "rustc-dev"]
8 changes: 5 additions & 3 deletions src/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use group::{
};
use rand_core::RngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

use sp1_precompiles::bls12381::decompress_pubkey;
#[cfg(feature = "alloc")]
use group::WnafGroup;

Expand Down Expand Up @@ -330,9 +330,11 @@ impl G1Affine {
/// for details about how group elements are serialized.
pub fn from_compressed(bytes: &[u8; 48]) -> CtOption<Self> {
// We already know the point is on the curve because this is established
// by the y-coordinate recovery procedure in from_compressed_unchecked().
// by the y-coordinate recovery procedure in decompress_pubkey().
let decompressed = decompress_pubkey(bytes).unwrap();

Self::from_compressed_unchecked(bytes).and_then(|p| CtOption::new(p, p.is_torsion_free()))
// Extra checks do not have to be done because because the precompile already does it for us.
G1Affine::from_uncompressed_unchecked(&decompressed)
}

/// Attempts to deserialize an uncompressed element, not checking if the
Expand Down
22 changes: 22 additions & 0 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ impl Scalar {
pub const fn from_raw(val: [u64; 4]) -> Self {
(&Scalar(val)).mul(&R2)
}

/// Converts from a hex string into its `Scalar` representation.
pub fn from_hex(hex: &str) -> Option<Self> {
if hex.len() != 64 {
return None;
}

let mut raw = [0u64; 4];
for (i, chunk) in hex.as_bytes().chunks(16).enumerate().take(4) {
if let Ok(hex_chunk) = core::str::from_utf8(chunk) {
if let Ok(value) = u64::from_str_radix(hex_chunk, 16) {
raw[3 - i] = value.to_le();
} else {
return None;
}
} else {
return None;
}
}

Some(Scalar::from_raw(raw))
}

/// Squares this element.
#[inline]
Expand Down

0 comments on commit 0d643ce

Please sign in to comment.