Skip to content

Commit

Permalink
feels like the thing is finished
Browse files Browse the repository at this point in the history
will give it a look like tomorrow
I should check that `rc` makes it opt-in and publish to NPM
  • Loading branch information
skaunov committed Jul 8, 2024
1 parent e011081 commit 533bf84
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 89 deletions.
10 changes: 2 additions & 8 deletions javascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ wasm-bindgen = "~0.2.84"
js-sys = "0.3"

plume_rustcrypto = {version = "~0.2.1", default-features = false}
# rand_chacha = "0.3"
sec1 = {version = "0.7.3", optional = true} # match with `k256`
# elliptic-curve = {version = "0.13.8", features = ["sec1"]} # match with `k256`
elliptic-curve = {version = "0.13.8"}
# hex = "0.4"
# k256 = "~0.13.3" # match with `k256`
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"] }
# getrandom_or_panic = "0.0.3"
# rand = {version = "0.8"}
anyhow = "1"

[dev-dependencies]
Expand Down
117 changes: 50 additions & 67 deletions javascript/README.md
Original file line number Diff line number Diff line change
@@ -1,84 +1,67 @@
<div align="center">
This is wrapper around `plume_rustcrypto` crate to produce PLUME signatures in JS contexts using Wasm.

<h1><code>wasm-pack-template</code></h1>
TODO add here couple of examples from systems which uses this.

<strong>A template for kick starting a Rust and WebAssembly project using <a href="https://github.com/rustwasm/wasm-pack">wasm-pack</a>.</strong>
# Getting Started

<p>
<a href="https://travis-ci.org/rustwasm/wasm-pack-template"><img src="https://img.shields.io/travis/rustwasm/wasm-pack-template.svg?style=flat-square" alt="Build Status" /></a>
</p>

<h3>
<a href="https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html">Tutorial</a>
<span> | </span>
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a>
</h3>

<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
</div>

## About

[**📚 Read this template tutorial! 📚**][template-docs]

This template is designed for compiling Rust libraries into WebAssembly and
publishing the resulting package to NPM.

Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other
templates and usages of `wasm-pack`.

[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html
[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html

## 🚴 Usage

### 🐑 Use `cargo generate` to Clone this Template

[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate)
Get the package from NPM. The repository contains Rust code for generating Wasm and packaging it.

```
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
cd my-project
The package usage outline; see the details in subsections.
```js
// ...
let result = plume.sign(true, secretKeySec1Der, msg);
console.log(result.nullifier);
result.zeroizePrivateParts();
```

### 🛠️ Build with `wasm-pack build`
Please, refer to the JS-doc for types description, function signatures, and exceptions notes.

```
wasm-pack build
```

### 🔬 Test in Headless Browsers with `wasm-pack test`
Values in the following examples are in line with tests in the wrapped crate.
## producing the signature
```js
import * as plume from "TODO";

let result = plume.sign(
false,
new Uint8Array([48, 107, 2, 1, 1, 4, 32, 81, 155, 66, 61, 113, 95, 139, 88, 31, 79, 168, 238, 89, 244, 119, 26, 91, 68, 200, 19, 11, 78, 62, 172, 202, 84, 165, 109, 218, 114, 180, 100, 161, 68, 3, 66, 0, 4, 12, 236, 2, 142, 224, 141, 9, 224, 38, 114, 166, 131, 16, 129, 67, 84, 249, 234, 191, 255, 13, 230, 218, 204, 28, 211, 167, 116, 73, 96, 118, 174, 239, 244, 113, 251, 160, 64, 152, 151, 182, 164, 142, 136, 1, 173, 18, 249, 93, 0, 9, 183, 83, 207, 143, 81, 193, 40, 191, 107, 11, 210, 127, 189]),
new Uint8Array([
65, 110, 32, 101, 120, 97, 109, 112, 108, 101, 32, 97, 112, 112, 32, 109, 101, 115, 115, 97, 103, 101, 32, 115, 116, 114, 105, 110, 103
])
);
```
wasm-pack test --headless --firefox
## getters
`PlumeSignature` provide getters for each property of it, so you have access to any of them upon signing.
```js
// ...
console.log(result.nullifier);
console.log(result.s);
console.log(result.c);
console.log(result.pk);
console.log(result.message);
```

### 🎁 Publish to NPM with `wasm-pack publish`

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);
```
wasm-pack publish
Also there's #convertion utility provided.
## zeroization
Depending on your context you might want to clear values of the result from Wasm memory after getting the values.
```js
// ...
result.zeroizePrivateParts();
result.zeroizeAll();
```

## 🔋 Batteries Included

* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating
between WebAssembly and JavaScript.
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook)
for logging panic messages to the developer console.
* `LICENSE-APACHE` and `LICENSE-MIT`: most Rust projects are licensed this way, so these are included for you

## License

Licensed under either of
# #convertion of `s` to `BigInt`
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` there's `sec1DerScalarToBigint` helper funtion.

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# Working with source files

at your option.
This package is built with the tech provided by <https://github.com/rustwasm> which contains everything needed to work with it. Also the wrapper crate was initiated with `wasm-pack-template`.

### Contribution
Note that the wrapper crate has `verify` feature which can check the resulting signature.

Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.
# License
See <https://github.com/plume-sig/zk-nullifier-sig/blob/main/LICENSE>.
33 changes: 19 additions & 14 deletions javascript/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
//! TODO is it possible to add top-level?
// TODO should I do examples rustdoc style or javadoc?
/*
I want to have a look at good and *small* example for...
* JS tests and CI
* documenting */

use std::convert::TryFrom;
//! sadly `wasm-bindgen` doesn't support top-level @module docs yet

#[cfg(feature = "verify")]
use std::convert::TryInto;

Expand Down Expand Up @@ -76,23 +70,34 @@ impl PlumeSignature {
// values.get
}

/// Depending on your context you may want to zeroize from Wasm memory private parts of the result after getting the values.
#[wasm_bindgen(js_name = zeroizePrivateParts)]
/// Zeroize private values of the object from Wasm memory.
pub fn zeroize_privateparts(&mut self) {
self.c.zeroize();
self.pk.zeroize();
}
#[wasm_bindgen(js_name = zeroizeAll)]
/// Zeroize all values of the object from Wasm memory.
pub fn zeroize_all(&mut self) {
self.zeroize_privateparts();
self.message.zeroize();
self.nullifier.zeroize();
self.s.zeroize();
if let Some(v1) = self.v1specific.as_mut() {
v1.hashed_to_curve_r.zeroize();
v1.r_point.zeroize();
}
}
}

#[wasm_bindgen(skip_jsdoc)]
/// @throws a "crypto error" in case of a problem with the secret key
/// @param {boolean} v1 - is the flag to choose between V1 and V2 output.
/// @param {Uint8Array} sk - must be exactly 32 bytes, and strictly represent a non-zero scalar of `secp256` in big-endian.
/// @param {Uint8Array} sk - secret key in SEC1 DER format.
/// @param {Uint8Array} msg
/// @returns {PlumeSignature}
pub fn sign(v1: bool, sk: &mut [u8], msg: &[u8]) -> Result<PlumeSignature, JsError> {
let sk_z =
plume_rustcrypto::SecretKey
::from(plume_rustcrypto::NonZeroScalar::try_from(sk.as_ref())?);
let sk_z = plume_rustcrypto::SecretKey::from_sec1_der(sk)?;
sk.zeroize();
let signer = plume_rustcrypto::randomizedsigner::PlumeSigner::new(&sk_z, v1);

Expand Down Expand Up @@ -136,7 +141,7 @@ impl TryInto<plume_rustcrypto::PlumeSignature> for PlumeSignature {
}
}

impl From <plume_rustcrypto::PlumeSignature> for PlumeSignature {
impl From<plume_rustcrypto::PlumeSignature> for PlumeSignature {
fn from(value: plume_rustcrypto::PlumeSignature) -> Self {
PlumeSignature {
message: value.message,
Expand Down

0 comments on commit 533bf84

Please sign in to comment.