Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZK-643: General bindings crate #76

Merged
merged 5 commits into from
Jan 7, 2025
Merged

ZK-643: General bindings crate #76

merged 5 commits into from
Jan 7, 2025

Conversation

kroist
Copy link
Collaborator

@kroist kroist commented Jan 6, 2025

This pull request introduces a new crate, shielder_bindings, which provides WebAssembly (WASM) and mobile bindings (uniffi-rs) for the Shielder cryptographic operations.

Key changes include:

New Crate Setup:

  • [crates/shielder_bindings/Cargo.toml]: Added new crate shielder_bindings which is mostly a copy of shielder-wasm crate, but currently only with poseidon hash and private-key-to-scalar conversion.

Build and Configuration:

  • [crates/shielder_bindings/Makefile]: Added separate makefile targets for building WASM packages and iOS bindings.
  • [crates/shielder_bindings/Cargo.toml]: Added feature flags for binding generation options:
    • build-wasm - webassembly
    • multithreading-wasm - optional multithreaded webassembly
    • build-uniffi - mobile bindings through uniffi

Documentation:

  • [crates/shielder_bindings/README.md]: Added README with an overview, build instructions for WebAssembly and iOS, and prerequisites.

Copy link

github-actions bot commented Jan 6, 2025

Transaction NameMainCurrentDifference (%)
NewAccountNative20003472000263-0.00420%
DepositNative18279301827942+0.00066%
WithdrawNative18981021898090-0.00063%

@pmikolajczyk41 pmikolajczyk41 self-requested a review January 7, 2025 08:48
Copy link
Member

@pmikolajczyk41 pmikolajczyk41 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few small comments, but no blockers; feel free to resolve them on your own

good job with taking care of two completely different compilation targets!

@@ -0,0 +1,60 @@
[package]
name = "shielder_bindings"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all other crates in this repo (and crate directories) use dash instead of underscore; if you don't mind - please rename

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately uniffi breaks if we use dash :D

edition.workspace = true
authors = ["Cardinal", "Piotr Roslaniec <[email protected]>"]
homepage.workspace = true
license = "Apache-2.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

licence can be inherited as well

Comment on lines +19 to +21
rand = { workspace = true, features = ["small_rng"] }
rand_chacha = { workspace = true }
rand_core = { workspace = true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused deps

rand_chacha = { workspace = true }
rand_core = { workspace = true }
rayon = { workspace = true, optional = true }
sha2 = { workspace = true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused dep

wasm-bindgen = { workspace = true, optional = true }

[build-dependencies]
rand = { workspace = true, features = ["small_rng"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused dep

"description": "WASM bindings for Shielder cryptography. Intended for use in web applications.",
"license": "Apache-2.0",
"author": "Cardinal",
"contributors": [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we expand this section?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean to expand it with our names, it seems to me that "author: Cardinal" covers us sufficiently, and we can leave "contributors" just for any external contributors, otherwise we'd need to start putting our names everywhere.

use wasm_bindgen::prelude::wasm_bindgen;

#[cfg_attr(feature = "build-wasm", wasm_bindgen)]
pub fn private_key_to_f(hex: &str) -> Vec<u8> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you don't want to remove the source of this copy-paste (from shielder-wasm) in this PR, pls add at least deprecation

Comment on lines +1 to +9
#![cfg_attr(not(feature = "build-uniffi"), no_std)]

#[cfg(feature = "build-uniffi")]
uniffi::setup_scaffolding!();

#[cfg(feature = "multithreading-wasm")]
pub use wasm_bindgen_rayon::init_thread_pool;

extern crate alloc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should include building this crate with different feature flags in our CI (probably by just calling Makefile targets)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really want to do this later, when the critical part is written and working

else
SED_CMD := sed -i
endif

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might add help target as we do in other Makefiles

- Rust toolchain (see rust-toolchain.toml)
- wasm-pack (for WebAssembly builds)
- Rust target: aarch64-apple-ios-sim (for iOS simulator builds)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something more, because when I do make ios I get error: failed to get iphonesimulator SDK path: No such file or directory (os error 2)

Copy link
Contributor

@guspiel guspiel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool:)

@@ -0,0 +1,19 @@
{
"name": "shielder-wasm",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover name?

{
"name": "shielder-wasm",
"version": "0.0.1",
"description": "WASM bindings for Shielder cryptography. Intended for use in web applications.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe "zkOS cryptography" here in the description?

"description": "WASM bindings for Shielder cryptography. Intended for use in web applications.",
"license": "Apache-2.0",
"author": "Cardinal",
"contributors": [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean to expand it with our names, it seems to me that "author: Cardinal" covers us sufficiently, and we can leave "contributors" just for any external contributors, otherwise we'd need to start putting our names everywhere.

[package]
name = "shielder_bindings"
version = "0.1.0"
description = "WASM and mobile bindings for the shielder-circuits"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think language sounds better if you remove "the" from the description, because shielder-circuits is a crate name

@@ -0,0 +1,35 @@
use alloc::vec::Vec;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you name this module differently? This is not just hashing. Perhaps "primitives" (seen this in Poseidon repo)? This term seems to cover both important constants and the hash function.


#[cfg_attr(feature = "build-wasm", wasm_bindgen)]
#[cfg_attr(feature = "build-uniffi", uniffi::export)]
pub fn arity() -> u32 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage looks cool:) Nice that we have one interface for both binding libraries.

@kroist kroist merged commit 2bb3528 into main Jan 7, 2025
14 checks passed
@kroist kroist deleted the general-bindings-crate branch January 7, 2025 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants