Skip to content

Commit

Permalink
remove GA
Browse files Browse the repository at this point in the history
this one should be tested like a package I guess,
ideas for such tests are welcome as issues
  • Loading branch information
skaunov committed Jul 9, 2024
1 parent 533bf84 commit 6b18e3d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 79 deletions.
41 changes: 0 additions & 41 deletions .github/workflows/javascript.yml

This file was deleted.

4 changes: 3 additions & 1 deletion javascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ authors = ["skaunov"]
edition = "2018"
keywords = ["nullifier", "zero-knowledge", "ECDSA", "PLUME"]
repository = "https://github.com/plume-sig/zk-nullifier-sig/"
description = "wrapper around `plume_rustcrypto` crate to produce PLUME signatures in JS contexts using Wasm"
license = "MIT"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
# I'd alias this to `sec1` if that won't be trickyt
# I'd alias this to `sec1` if that won't be tricky
verify = ["dep:sec1"]

[dependencies]
Expand Down
31 changes: 27 additions & 4 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Get the package from NPM. The repository contains Rust code for generating Wasm
The package usage outline; see the details in subsections.
```js
// ...
let result = plume.sign(true, secretKeySec1Der, msg);
let result = plume.sign(isV1, secretKeySec1Der, msg);
console.log(result.nullifier);
result.zeroizePrivateParts();
```
Expand All @@ -19,7 +19,7 @@ Please, refer to the JS-doc for types description, function signatures, and exce
Values in the following examples are in line with tests in the wrapped crate.
## producing the signature
```js
import * as plume from "TODO";
import * as plume from "plume-sig";

let result = plume.sign(
false,
Expand All @@ -34,16 +34,39 @@ let result = plume.sign(
```js
// ...
console.log(result.nullifier);
/* Uint8Array(33) [
3, 87, 188, 62, 210, 129, 114, 239,
138, 221, 228, 185, 224, 194, 204, 231,
69, 252, 197, 166, 100, 115, 164, 92,
30, 98, 111, 29, 12, 103, 229, 88,
48
] */
console.log(result.s);
/* Uint8Array(109) [
48, 107, 2, 1, 1, 4, 32, 73, 27, 195, 183, 106,
202, 136, 167, 50, 193, 119, 152, 153, 233, 56, 176, 58,
221, 183, 4, 126, 189, 69, 201, 173, 102, 98, 248, 36,
112, 183, 176, 161, 68, 3, 66, 0, 4, 13, 18, 115,
220, 215, 120, 156, 20, 128, 225, 106, 29, 255, 16, 218,
5, 19, 179, 80, 204, 25, 144, 61, 150, 121, 83, 76,
174, 21, 232, 58, 153, 97, 227, 239, 78, 114, 199, 53,
138, 93, 108, 150, 98, 141, 89, 159, 219, 243, 182, 188,
22, 224, 154, 171,
... 9 more items
] */
console.log(result.c);
console.log(result.pk);
console.log(result.message);
console.log(result.v1specific);
// undefined
```
Note that variant is specified by `v1specific`; if it's `undefined` then the object contains V2, otherwise it's V1.
```js
// ...
console.log(result.v1specific.r_point);
console.log(result.v1specific.hashed_to_curve_r);
if (result.v1specific) {
console.log(result.v1specific.r_point);
console.log(result.v1specific.hashed_to_curve_r);
}
```
Also there's #convertion utility provided.
## zeroization
Expand Down
93 changes: 60 additions & 33 deletions javascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use wasm_bindgen::prelude::*;

#[cfg(feature = "verify")]
use elliptic_curve::sec1::FromEncodedPoint;
use zeroize::Zeroize;
use elliptic_curve::sec1::ToEncodedPoint;
use signature::RandomizedSigner;
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).
Expand All @@ -18,10 +18,10 @@ use signature::RandomizedSigner;
/// `Option` can be `undefined` or instance of [`PlumeSignatureV1Fields`].
pub struct PlumeSignature {
pub message: Vec<u8>,
pub pk: Vec<u8>,
pub pk: Vec<u8>,
pub nullifier: Vec<u8>,
pub c: Vec<u8>,
pub s: Vec<u8>,
pub c: Vec<u8>,
pub s: Vec<u8>,
pub v1specific: Option<PlumeSignatureV1Fields>,
}

Expand All @@ -36,7 +36,10 @@ pub struct PlumeSignatureV1Fields {
impl PlumeSignatureV1Fields {
#[wasm_bindgen(constructor)]
pub fn new(r_point: Vec<u8>, hashed_to_curve_r: Vec<u8>) -> PlumeSignatureV1Fields {
PlumeSignatureV1Fields { r_point, hashed_to_curve_r }
PlumeSignatureV1Fields {
r_point,
hashed_to_curve_r,
}
}
}

Expand All @@ -45,7 +48,9 @@ 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<bool, JsError> {Ok(plume_rustcrypto::PlumeSignature::verify(&self.try_into()?))}
pub fn verify(self) -> Result<bool, JsError> {
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.
Expand All @@ -56,14 +61,18 @@ impl PlumeSignature {
nullifier: Vec<u8>,
c: Vec<u8>,
s: Vec<u8>,
v1specific: Option<PlumeSignatureV1Fields>
v1specific: Option<PlumeSignatureV1Fields>,
) -> PlumeSignature {
PlumeSignature {
/* I really wonder how good is this pattern. But taking so much of args isn't good, and builder pattern seems redundant as all
/* I really wonder how good is this pattern. But taking so much of args isn't good, and builder pattern seems redundant as all
of the fields are required, and setters are just assignments. */
// Actually there's no case for constructing it from values, so this only used internally and for testing.
message, pk, nullifier, c, s,
v1specific//: if v1specific.is_falsy() {None} else {Some(v1specific)}
message,
pk,
nullifier,
c,
s,
v1specific, //: if v1specific.is_falsy() {None} else {Some(v1specific)}
}

// js_sys::Object::from_entries(&values)?
Expand Down Expand Up @@ -101,10 +110,9 @@ pub fn sign(v1: bool, sk: &mut [u8], msg: &[u8]) -> Result<PlumeSignature, JsErr
sk.zeroize();
let signer = plume_rustcrypto::randomizedsigner::PlumeSigner::new(&sk_z, v1);

Ok(signer.sign_with_rng(
&mut signature::rand_core::OsRng,
msg
).into())
Ok(signer
.sign_with_rng(&mut signature::rand_core::OsRng, msg)
.into())
}

// TODO deprecate when `verify` gone
Expand All @@ -116,27 +124,38 @@ impl TryInto<plume_rustcrypto::PlumeSignature> for PlumeSignature {
let point_check = |point_bytes: Vec<u8>| -> Result<AffinePoint, anyhow::Error> {
let point_encoded = sec1::point::EncodedPoint::from_bytes(point_bytes)?; // TODO improve formatting (quotes e.g.)
let result = plume_rustcrypto::AffinePoint::from_encoded_point(&point_encoded);
if result.is_none().into() {Err(anyhow::Error::msg("the point isn't on the curve"))}
else {Ok(result.expect("`None` is processed the line above"))}
if result.is_none().into() {
Err(anyhow::Error::msg("the point isn't on the curve"))
} else {
Ok(result.expect("`None` is processed the line above"))
}
};

let err_field_wrap = |name_field: &str, er: anyhow::Error| -> JsError {JsError::new(
("while proccessing ".to_owned() + name_field + " :" + er.to_string().as_str()).as_str()
)};
let err_field_wrap = |name_field: &str, er: anyhow::Error| -> JsError {
JsError::new(
("while proccessing ".to_owned() + name_field + " :" + er.to_string().as_str())
.as_str(),
)
};

Ok(plume_rustcrypto::PlumeSignature{
Ok(plume_rustcrypto::PlumeSignature {
message: self.message,
pk: point_check(self.pk).map_err(|er| err_field_wrap("`pk`", er))?,
// plume_rustcrypto::AffinePoint::try_from(self.pk)?, //.try_into<[u8; 33]>()?.into(),
nullifier: point_check(self.nullifier).map_err(|er| err_field_wrap("`nullifier`", er))?,
nullifier: point_check(self.nullifier)
.map_err(|er| err_field_wrap("`nullifier`", er))?,
c: plume_rustcrypto::SecretKey::from_sec1_der(&self.c)?.into(),
s: plume_rustcrypto::SecretKey::from_sec1_der(&self.s)?.into(),//scalar_from_bigint(self.s).map_err(|er| err_field_wrap("`s`", er))?,
v1specific: if let Some(v1) = self.v1specific {Some(
plume_rustcrypto::PlumeSignatureV1Fields{
r_point: point_check(v1.r_point).map_err(|er| err_field_wrap("`r_point`", er))?,
hashed_to_curve_r: point_check(v1.hashed_to_curve_r).map_err(|er| err_field_wrap("`hashed_to_curve_r`", er))?,
}
)} else {None},
s: plume_rustcrypto::SecretKey::from_sec1_der(&self.s)?.into(), //scalar_from_bigint(self.s).map_err(|er| err_field_wrap("`s`", er))?,
v1specific: if let Some(v1) = self.v1specific {
Some(plume_rustcrypto::PlumeSignatureV1Fields {
r_point: point_check(v1.r_point)
.map_err(|er| err_field_wrap("`r_point`", er))?,
hashed_to_curve_r: point_check(v1.hashed_to_curve_r)
.map_err(|er| err_field_wrap("`hashed_to_curve_r`", er))?,
})
} else {
None
},
})
}
}
Expand All @@ -161,10 +180,18 @@ impl From<plume_rustcrypto::PlumeSignature> for PlumeSignature {

#[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`;
/// 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<js_sys::BigInt, JsError> {
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"))
}
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",
))
}

0 comments on commit 6b18e3d

Please sign in to comment.