From 1f2bef53594866ddf1cdd30a4852cb46275603a8 Mon Sep 17 00:00:00 2001 From: skaunov Date: Wed, 17 Jul 2024 12:35:20 +0300 Subject: [PATCH 1/2] reorg code for better reading through --- javascript/src/lib.rs | 99 +++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 50 deletions(-) diff --git a/javascript/src/lib.rs b/javascript/src/lib.rs index 5d0c76a..bcedfc6 100644 --- a/javascript/src/lib.rs +++ b/javascript/src/lib.rs @@ -1,15 +1,14 @@ //! sadly `wasm-bindgen` doesn't support top-level @module docs yet -#[cfg(feature = "verify")] -use std::convert::TryInto; - use wasm_bindgen::prelude::*; -#[cfg(feature = "verify")] -use elliptic_curve::sec1::FromEncodedPoint; use elliptic_curve::sec1::ToEncodedPoint; use signature::RandomizedSigner; use zeroize::Zeroize; +#[cfg(feature = "verify")] +use elliptic_curve::sec1::FromEncodedPoint; +#[cfg(feature = "verify")] +use std::convert::TryInto; #[wasm_bindgen(getter_with_clone)] /// @typedef {Object} PlumeSignature - Wrapper around [`plume_rustcrypto::PlumeSignature`](https://docs.rs/plume_rustcrypto/latest/plume_rustcrypto/struct.PlumeSignature.html). @@ -45,13 +44,6 @@ impl PlumeSignatureV1Fields { #[wasm_bindgen] impl PlumeSignature { - #[cfg(feature = "verify")] - /// @deprecated Use this only for testing purposes. - /// @throws an error if the data in the object doesn't let it to properly run verification; message contains nature of the problem and indicates relevant property of the object. In case of other (crypto) problems returns `false`. - pub fn verify(self) -> Result { - Ok(plume_rustcrypto::PlumeSignature::verify(&self.try_into()?)) - } - /// there's no case for constructing it from values, so this only used internally and for testing /// `v1specific` discriminates if it's V1 or V2 scheme used. Pls, see wrapped docs for details. #[wasm_bindgen(constructor)] @@ -78,7 +70,7 @@ impl PlumeSignature { // js_sys::Object::from_entries(&values)? // values.get } - + #[wasm_bindgen(js_name = zeroizePrivateParts)] /// Zeroize private values of the object from Wasm memory. pub fn zeroize_privateparts(&mut self) { @@ -97,6 +89,13 @@ impl PlumeSignature { v1.r_point.zeroize(); } } + + #[cfg(feature = "verify")] + /// @deprecated Use this only for testing purposes. + /// @throws an error if the data in the object doesn't let it to properly run verification; message contains nature of the problem and indicates relevant property of the object. In case of other (crypto) problems returns `false`. + pub fn verify(self) -> Result { + Ok(plume_rustcrypto::PlumeSignature::verify(&self.try_into()?)) + } } #[wasm_bindgen(skip_jsdoc)] @@ -115,6 +114,42 @@ pub fn sign(v1: bool, sk: &mut [u8], msg: &[u8]) -> Result for PlumeSignature { + fn from(value: plume_rustcrypto::PlumeSignature) -> Self { + PlumeSignature { + message: value.message, + pk: value.pk.to_encoded_point(true).as_bytes().to_vec(), + nullifier: value.nullifier.to_encoded_point(true).as_bytes().to_vec(), + c: plume_rustcrypto::SecretKey::from(value.c).to_sec1_der().expect("`k256` restricts this type to proper keys, so it's serialized representation shouldn't have a chance to fail") + .to_vec(), + s: plume_rustcrypto::SecretKey::from(value.s).to_sec1_der().expect("`k256` restricts this type to proper keys, so it's serialized representation shouldn't have a chance to fail") + .to_vec(), + v1specific: value.v1specific.map(|v1| {PlumeSignatureV1Fields { + r_point: v1.r_point.to_encoded_point(true).as_bytes().to_vec(), + hashed_to_curve_r: v1.hashed_to_curve_r.to_encoded_point(true).as_bytes().to_vec(), + }}) + } + } +} + +#[wasm_bindgen(js_name = sec1DerScalarToBigint)] +/// This might leave values in memory! Don't use for private values. +/// JS most native format for scalar is `BigInt`, but it's not really transportable or secure, so for uniformity of approach `s` in `PlumeSignature` is defined similar to `c`; +/// but if you want to have it as a `BigInt` this util is left here. +pub fn sec1derscalar_to_bigint(scalar: &[u8]) -> Result { + Ok(js_sys::BigInt::new(&JsValue::from_str( + ("0x".to_owned() + + plume_rustcrypto::SecretKey::from_sec1_der(scalar)? + .to_nonzero_scalar() + .to_string() + .as_str()) + .as_str(), + )) + .expect( + "`BigInt` always can be created from hex string, and `v.to_string()` always produce that", + )) +} + // TODO deprecate when `verify` gone #[cfg(feature = "verify")] impl TryInto for PlumeSignature { @@ -158,40 +193,4 @@ impl TryInto for PlumeSignature { }, }) } -} - -impl From for PlumeSignature { - fn from(value: plume_rustcrypto::PlumeSignature) -> Self { - PlumeSignature { - message: value.message, - pk: value.pk.to_encoded_point(true).as_bytes().to_vec(), - nullifier: value.nullifier.to_encoded_point(true).as_bytes().to_vec(), - c: plume_rustcrypto::SecretKey::from(value.c).to_sec1_der().expect("`k256` restricts this type to proper keys, so it's serialized representation shouldn't have a chance to fail") - .to_vec(), - s: plume_rustcrypto::SecretKey::from(value.s).to_sec1_der().expect("`k256` restricts this type to proper keys, so it's serialized representation shouldn't have a chance to fail") - .to_vec(), - v1specific: value.v1specific.map(|v1| {PlumeSignatureV1Fields { - r_point: v1.r_point.to_encoded_point(true).as_bytes().to_vec(), - hashed_to_curve_r: v1.hashed_to_curve_r.to_encoded_point(true).as_bytes().to_vec(), - }}) - } - } -} - -#[wasm_bindgen(js_name = sec1DerScalarToBigint)] -/// This might leave values in memory! Don't use for private values. -/// JS most native format for scalar is `BigInt`, but it's not really transportable or secure, so for uniformity of approach `s` in `PlumeSignature` is defined similar to `c`; -/// but if you want to have it as a `BigInt` this util is left here. -pub fn sec1derscalar_to_bigint(scalar: &[u8]) -> Result { - Ok(js_sys::BigInt::new(&JsValue::from_str( - ("0x".to_owned() - + plume_rustcrypto::SecretKey::from_sec1_der(scalar)? - .to_nonzero_scalar() - .to_string() - .as_str()) - .as_str(), - )) - .expect( - "`BigInt` always can be created from hex string, and `v.to_string()` always produce that", - )) -} +} \ No newline at end of file From d91bc1a6dd0c235c287c5d8386aa82192ecf7fb9 Mon Sep 17 00:00:00 2001 From: skaunov Date: Wed, 17 Jul 2024 12:41:37 +0300 Subject: [PATCH 2/2] fmt --- javascript/Cargo.toml | 5 ++--- javascript/src/lib.rs | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/javascript/Cargo.toml b/javascript/Cargo.toml index fd4d09b..994bc15 100644 --- a/javascript/Cargo.toml +++ b/javascript/Cargo.toml @@ -12,7 +12,6 @@ license = "MIT" crate-type = ["cdylib", "rlib"] [features] -# I'd alias this to `sec1` if that won't be tricky verify = ["dep:sec1"] [dependencies] @@ -20,16 +19,16 @@ wasm-bindgen = "~0.2.84" js-sys = "0.3" plume_rustcrypto = {version = "~0.2.1", default-features = false} -sec1 = {version = "~0.7.3", optional = true} # match with `k256` elliptic-curve = {version = "~0.13.8"} zeroize = "1.8" signature = "^2.2.0" getrandom = { version = "0.2", features = ["js"] } anyhow = "1" +sec1 = {version = "~0.7.3", optional = true} # match with `k256` [dev-dependencies] wasm-bindgen-test = "~0.3.34" -[profile.release] # This comes from template; didn't touch this yet - docs doesn't tell much about it. +[profile.release] # This comes from template; docs doesn't tell much about it. # Tell `rustc` to optimize for small code size. # opt-level = "s" diff --git a/javascript/src/lib.rs b/javascript/src/lib.rs index bcedfc6..0ecfcaf 100644 --- a/javascript/src/lib.rs +++ b/javascript/src/lib.rs @@ -2,13 +2,13 @@ use wasm_bindgen::prelude::*; -use elliptic_curve::sec1::ToEncodedPoint; -use signature::RandomizedSigner; -use zeroize::Zeroize; #[cfg(feature = "verify")] use elliptic_curve::sec1::FromEncodedPoint; +use elliptic_curve::sec1::ToEncodedPoint; +use signature::RandomizedSigner; #[cfg(feature = "verify")] use std::convert::TryInto; +use zeroize::Zeroize; #[wasm_bindgen(getter_with_clone)] /// @typedef {Object} PlumeSignature - Wrapper around [`plume_rustcrypto::PlumeSignature`](https://docs.rs/plume_rustcrypto/latest/plume_rustcrypto/struct.PlumeSignature.html). @@ -70,7 +70,7 @@ impl PlumeSignature { // js_sys::Object::from_entries(&values)? // values.get } - + #[wasm_bindgen(js_name = zeroizePrivateParts)] /// Zeroize private values of the object from Wasm memory. pub fn zeroize_privateparts(&mut self) { @@ -193,4 +193,4 @@ impl TryInto for PlumeSignature { }, }) } -} \ No newline at end of file +}