51 Commits

Author SHA1 Message Date
skaunov
03e132597f improve plume_arkworks::sec1_affine to be used downstream 2025-06-06 17:42:10 +03:00
Sergey Kaunov
2bca61f809 Pr (#130)
* fix `zeroize` minimal version

* fix `dev-dependencies` minimal versions

* re-exports `rand` from `arkworks`
2025-06-05 23:25:36 +03:00
skaunov
c281e3223b plume_arkworks improvement 2025-05-28 19:21:59 +03:00
skaunov
aa341b06e2 implements separate public and private inputs in API #113 for the package 2025-05-24 14:27:47 +03:00
skaunov
1b8af975e1 promote rc 2024-08-10 15:19:17 +03:00
Sergey Kaunov
92315e24f4 Wasm readability (#115)
reorg code for better reading through
2024-07-17 12:43:15 +03:00
Sergey Kaunov
83e3ed91f0 Replaces NPM package with Wasm wrapper
Resolves #114

removes respective GA
this one should be tested like a package I guess,
ideas for such tests are welcome as issues

Couple of things left out of the committed code.

# Subtle
I was really late to understand that Subtle crypto supports the different curve `secp256r`, *and* it doesn't provide a facility to store secret values. So implementation for `web_sys::SecretKey` turned out to be just extra miles leading nowhere.
```toml
web-sys = { version = "0.3", features = ["CryptoKey", "SubtleCrypto", "Crypto", "EcKeyImportParams"] }
wasm-bindgen-futures = "0.4"
```
```rust
#[wasm_bindgen]
extern "C" {
    // Return type of js_sys::global()
    type Global;
    // // Web Crypto API: Crypto interface (https://www.w3.org/TR/WebCryptoAPI/)
    // type WebCrypto;
    // Getters for the WebCrypto API
    #[wasm_bindgen(method, getter)]
    fn crypto(this: &Global) -> web_sys::Crypto;
}

// `fn sign`
if sk.type_() != "secret" {return Err(JsError::new("`sk` must be secret key"))}
if !js_sys::Object::values(&sk.algorithm().map_err(
    |er|
        JsError::new(er.as_string().expect("TODO check this failing").as_str())
)?).includes(&JsValue::from_str("P-256"), 0) {return Err(JsError::new("`sk` must be from `secp256`"))}

// this was my approach, but seems I got what they did at <https://github.com/rust-random/getrandom/blob/master/src/js.rs>
// js_sys::global().entries().find(); // TODO throw if no Crypto in global

let global_the: Global = js_sys::global().unchecked_into();
let crypto_the: web_sys::Crypto = global_the.crypto();
let subtle_the = crypto_the.subtle();
let sk = JsFuture::from(subtle_the.export_key("pkcs8", &sk)?).await?;

// ...
::from_pkcs8_der(js_sys::ArrayBuffer::from(sk).try_into()?)?;
    zeroize::Zeroizing::new(js_sys::Uint8Array::from(JsFuture::from(subtle_the.export_key("pkcs8", &sk).map_err(
        |er|
            Err(JsError::new(er.as_string().expect("TODO check this failing").as_str()))
        )?).await?).to_vec());

// ...

// `fn try_into`

// ...

// zeroization protection ommitted here due to deprecation // <https://github.com/plume-sig/zk-nullifier-sig/issues/112>
// mostly boilerplate from signing; also some excessive ops left for the same reason
// TODO align error-handling in this part
if self.c.type_() != "secret" {return Err(JsError::new("`c` must be secret key"))}
if !js_sys::Object::values(&self.c.algorithm()?).includes(js_sys::JsString::from("P-256").into(), 0) {return Err(JsError::new("`c` must be from `secp256`"))}
this was my approach, but seems I got what they did at <https://github.com/rust-random/getrandom/blob/master/src/js.rs>
js_sys::global().entries().find(); // TODO throw if no Crypto in global
let global_the: Global = js_sys::global().unchecked_into();
let crypto_the: web_sys::Crypto = global_the.crypto();
let subtle_the = crypto_the.subtle();
let c_pkcs = //zeroize::Zeroizing::new(
    js_sys::Uint8Array::from(JsFuture::from(subtle_the.export_key("pkcs8", &self.c)?).await?).to_vec();
// );
let c_scalar = &plume_rustcrypto::SecretKey::from_pkcs8_der(&c_pkcs)?.to_nonzero_scalar();
sk_z.zeroize();

// ...
```

# randomness
Somehow I thought Wasm doesn't have access to RNG, so I used a seedable one and required the seed. Here's how `sign` `fn` was different.
```rust
// Wasm environment doesn't have a suitable way to get randomness for the signing process, so this instantiates ChaCha20 RNG with the provided seed.
// @throws a "crypto error" in case of a problem with the secret key, and a verbal error on a problem with `seed`
// @param {Uint8Array} seed - must be exactly 32 bytes.
pub fn sign(seed: &mut [u8], v1: bool, sk: &mut [u8], msg: &[u8]) -> Result<PlumeSignature, JsError> {
    // ...

    let seed_z: zeroize::Zeroizing<[u8; 32]> = zeroize::Zeroizing::new(seed.try_into()?);
    seed.zeroize();

    // TODO switch to `wasi-random` when that will be ready for crypto
    let sig = match v1 {
        true => plume_rustcrypto::PlumeSignature::sign_v1(
            &sk_z, msg, &mut rand_chacha::ChaCha20Rng::from_seed(seed_z)
        ),
        false => plume_rustcrypto::PlumeSignature::sign_v2(
            &sk_z, msg, &mut rand_chacha::ChaCha20Rng::from_seed(seed_z)
        ),
    };

    let sig = signer.sign_with_rng(
        &mut rand_chacha::ChaCha20Rng::from_seed(*seed_z), msg
    );

    // ...
}
```

# `BigInt` conversion
It was appealing to leave `s` as `BigInt` (see the comments), but that seems to be confusing and hinder downstream code reusage. There's an util function left for anybody who would want to have it as `BigInt`, but leaving the contraty function makes less sense and also makes the thing larger. So let me left it here for reference.
```rust
let scalar_from_bigint =
    |n: js_sys::BigInt| -> Result<plume_rustcrypto::NonZeroScalar, anyhow::Error> {
        let result = plume_rustcrypto::NonZeroScalar::from_repr(k256::FieldBytes::from_slice(
            hex::decode({
                let hexstring_freelen = n.to_string(16).map_err(
                    |er|
                        anyhow::Error::msg(er.as_string().expect("`RangeError` can be printed out"))
                )?.as_string().expect("on `JsString` this always produce a `String`");
                let l = hexstring_freelen.len();
                if l > 32*2 {return Err(anyhow::Error::msg("too many digits"))}
                else {["0".repeat(64-l), hexstring_freelen].concat()}
            })?.as_slice()
        ).to_owned());
        if result.is_none().into() {Err(anyhow::Error::msg("isn't valid `secp256` non-zero scalar"))}
        else {Ok(result.expect(EXPECT_NONEALREADYCHECKED))}
    };
```
2024-07-17 03:15:22 +03:00
Sergey Kaunov
7893200dc9 Copy keywords from the crate (#108)
> ...
so, I will take the tags from a create, btw
>> and the npm js package has no keywords, if we can add some for indexing, that would be great
2024-04-17 21:34:21 +03:00
Sergey Kaunov
d0ceebc44d Readme (#107)
* usage example correction

* version bump for publishing
2024-03-10 16:43:38 +03:00
Sergey Kaunov
83dc5ff82b Readme (#106)
* just some readme stuff for @Divide-by-0

* some requested edits

* some requested edits

* some requested edits

* version bump for publishing
2024-03-10 16:28:40 +03:00
Sergey Kaunov
e956b27ee1 Draft of <README.MD> for NPM (#103)
I believe these should be organized other way (divide the monorepo?). But @Divide-by-0 seems to really need something to start with rn
2024-02-29 23:46:39 +03:00
Sergey Kaunov
a44d60c78e Npm repository link (#102)
* add the link to the repo for NPM

* version bump / <npmjs> sync
2024-02-29 18:52:09 +03:00
Anton
79c602670b chore: clean up (#74)
* chore: clean up

- [x] Add checks for ci actions
- [x] Run prettier, clippy, fmt commands for all the files
- [x] Move circom circuits to a circom folder
- [x] Get rid of js var statements

* chore: add resolver version for cargo.toml

* chore: add circom tests

* chore: optimize check triggers

* chore: remove `check` command

* chore: use only `pnpm`

* chore: update readme

---------

Co-authored-by: 0xmad <0xmad@users.noreply.github.com>
2023-11-18 20:48:24 +03:00
Sergey Kaunov
1b11bdd4a9 Update .gitignore
https://github.com/plume-sig/zk-nullifier-sig/pull/66/#issuecomment-1780924701
2023-10-26 18:40:05 +03:00
Sergey Kaunov
e1ca66e26f Delete javascript/yarn.lock
Sad way to merge #66
2023-10-26 18:35:49 +03:00
skaunov
6c487c0fb9 "README" files update 2023-10-20 20:31:21 +03:00
skaunov
d8fb1b8b08 Fix merging, JS lock files update 2023-10-20 20:26:26 +03:00
skaunov
da9f5fb288 All modules naming is aligned. + refactoring&tests
[Discussion] results are incorporated. Naming groomed repository wide. \
Fun fact: I already was confused by that "hash2" naming, lol.

Note that `fn verify_signals` barely [benefit]
from renaming at all currently.

[Discussion]: 251fba6902 (commitcomment-130400727)
[benefit]: https://github.com/plume-sig/zk-nullifier-sig/issues/61
2023-10-20 17:59:39 +03:00
Sergey Kaunov
f4b53bc183 Fix: array signals (#65)
Co-authored-by: RajeshRk18 <kannar432@gmail.com>
2023-10-20 14:55:14 +03:00
Oren
acf76e1fd5 v2.0.3 2023-10-06 23:38:36 +03:00
Oren
76da9687cc Set the default version to V2 (JavaScript) 2023-10-06 23:27:35 +03:00
Oren
82c7c265ae lintomg: fix spacing 2023-10-06 23:23:27 +03:00
Oren
1935b0d320 v2.0.2 2023-10-06 22:23:07 +03:00
Oren
0ddb004f3d Replace node:crypto with js-sha256 for browser compatibility 2023-10-06 22:18:14 +03:00
Oren
f7cccd0d80 v2.0.0 2023-10-05 20:21:09 +03:00
Oren
512159a91c Align PlumeVersion.V1 to be equal to 1 2023-10-05 20:17:44 +03:00
Oren
19ae71bc31 Export PlumeVersion 2023-10-05 20:15:33 +03:00
Oren
7018be9b66 Move jest to devDependencies 2023-10-05 20:14:35 +03:00
RajeshRk18
91088140fd Add: Ref comment 2023-09-27 15:40:12 +05:30
Vu Vo
837e28bae1 remove js-sha512 2023-05-12 21:21:13 +07:00
Vu Vo
9ac8f68880 modified javascript lib 2023-04-17 18:37:41 +07:00
blakemscurr
3ba74797c4 Merge branch 'main' of https://github.com/zk-nullifier-sig/zk-nullifier-sig into main 2023-03-23 13:34:42 +13:00
Richard Liu
154aba52d6 1.5.0 - add tests, remove Buffer (bad for browsers) 2023-03-20 17:29:47 +07:00
blakemscurr
6d4c645c6f Merge branch 'main' of https://github.com/zk-nullifier-sig/zk-nullifier-sig into main
Reintroduce curve.P vs curve.N fix, and merge testing styles.
2023-03-16 11:33:36 +13:00
Richard Liu
7781afa7cc update js version 2023-03-13 16:19:27 +07:00
blakemscurr
baee84e770 Move from deprecated crypto package to built in node:crypto. 2023-02-14 13:19:16 +13:00
blakemscurr
94e1b6d46a Tidy up EOF new lines. 2023-02-14 13:11:20 +13:00
Richard Liu
e82f74c751 add computeAllInputs to javascript 2023-02-13 00:51:23 -08:00
blakemscurr
2c8182f87b Split test constants into a separate file. 2023-02-13 16:07:33 +13:00
Richard Liu
ecac4e7395 1.0.2 2023-02-12 14:09:23 -08:00
Richard Liu
fea535557c fix 2023-02-12 14:08:54 -08:00
Richard Liu
0cdde5445d publish 2023-02-12 05:47:47 -08:00
Richard Liu
5cce16343d add npmignore 2023-02-12 05:46:33 -08:00
blakemscurr
b4139edb2c Correctly calculate sha256 of 6 compressed ec points in the circuit. 2023-02-10 16:43:43 +13:00
blakemscurr
ee5809915b Add whole circuit test. 2023-02-04 16:42:57 +13:00
Richard Liu
bd7b3b1023 change package name 2023-02-02 10:59:47 -08:00
blakemscurr
a680f19e08 Test circuit WIP.
First equation not verifying, even outside circuit.
2023-02-02 17:13:00 +13:00
Richard Liu
c10e8b032f use published amcl-js package 2023-02-01 01:25:06 -08:00
Richard Liu
546e45c611 remove zero-installs 2022-12-18 02:12:07 -08:00
Richard Liu
4a06e51d38 prepare for publish 2022-12-12 02:26:36 -08:00