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

Initial WASM Support #254

Merged
merged 13 commits into from
May 13, 2023
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
override: true
default: true
components: rustfmt, clippy
target: |
wasm32-unknown-unknown

- name: Install nightly toolchain
uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -64,8 +66,10 @@ jobs:
- name: Check all tests and binaries compilation
run: cargo check --workspace --tests --lib --bins

- name: Check no_std compilation
run: cargo check --workspace --lib --no-default-features
- name: Check no_std support and WASM compilation
env:
RUSTFLAGS: -C target-cpu=generic
run: cargo build --target wasm32-unknown-unknown --no-default-features

- name: Test
run: bash ./scripts/run_tests.sh
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
cargo-system-config.toml
Cargo.lock
*.org
.pre-commit-config.yaml

# Test coverage (grcov)
default.profraw
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and follow [semantic versioning](https://semver.org/) for our releases.
- [#233](https://github.com/EspressoSystems/jellyfish/pull/233) BLS aggregation APIs
- [#234](https://github.com/EspressoSystems/jellyfish/pull/234) New `bytes_from_field_elements` util
- [#231](https://github.com/EspressoSystems/jellyfish/pull/231) Implemented FK23 for fast amortized opening for univariate PCS
- [#254](https://github.com/EspressoSystems/jellyfish/pull/254) Ensure `no_std` and target WASM support

### Changed

Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ cargo build
Run an example:

```
cargo run --release --example proof_of_exp
cargo run --release --example proof_of_exp --features test-srs
```

This is a simple example to prove and verify knowledge of exponent.
It shows how one may compose a circuit, and then build a proof for the circuit.

### WASM target

Jellyfish is `no_std` compliant and compilable to WASM target environment, just run:

```
./scripts/build_wasm.sh
```

### Tests

```
Expand Down
42 changes: 30 additions & 12 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 30 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,32 @@
outputs = { self, nixpkgs, flake-utils, flake-compat, rust-overlay, pre-commit-hooks, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
overlays = [
(import rust-overlay)
(self: super: {
rustc = (super.rustc.override {
stdenv = self.stdenv.override {
targetPlatform = super.stdenv.targetPlatform // {
parsed = {
cpu = { name = "wasm32"; };
vendor = {name = "unknown";};
kernel = {name = "unknown";};
abi = {name = "unknown";};
};
};
};
}).overrideAttrs (attrs: {
configureFlags = attrs.configureFlags ++ ["--set=build.docs=false"];
});
})
];
pkgs = import nixpkgs { inherit system overlays; };
nightlyToolchain = pkgs.rust-bin.selectLatestNightlyWith
(toolchain: toolchain.minimal.override { extensions = [ "rustfmt" ]; });

stableToolchain = pkgs.rust-bin.stable.latest.minimal.override {
extensions = [ "clippy" "llvm-tools-preview" "rust-src" ];
targets = ["wasm32-unknown-unknown"];
};
in with pkgs;
{
Expand Down Expand Up @@ -62,7 +81,8 @@
};
};
};
devShell = mkShell {
devShell = clang15Stdenv.mkDerivation {
name = "clang15-nix-shell";
buildInputs = [
argbash
openssl
Expand All @@ -72,7 +92,9 @@
stableToolchain
nightlyToolchain
cargo-sort

clang-tools_15
clangStdenv
llvm_15
] ++ lib.optionals stdenv.isDarwin [ darwin.apple_sdk.frameworks.Security ];

shellHook = ''
Expand All @@ -81,6 +103,11 @@

# Ensure `cargo fmt` uses `rustfmt` from nightly.
export RUSTFMT="${nightlyToolchain}/bin/rustfmt"

export C_INCLUDE_PATH="${llvmPackages_15.libclang.lib}/lib/clang/15.0.7/include"
export CC=$(which clang)
export AR=$(which llvm-ar)
export CFLAGS="-mcpu=generic"
''
# install pre-commit hooks
+ self.check.${system}.pre-commit-check.shellHook;
Expand Down
10 changes: 5 additions & 5 deletions plonk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ license = { workspace = true }
rust-version = { workspace = true }

[dependencies]
ark-bls12-377 = "0.4.0"
ark-bls12-381 = "0.4.0"
ark-bn254 = "0.4.0"
ark-bw6-761 = "0.4.0"
ark-ec = "0.4.0"
ark-ff = { version = "0.4.0", features = [ "asm" ] }
ark-poly = "0.4.0"
Expand All @@ -31,13 +27,17 @@ jf-relation = { path = "../relation", default-features = false }
jf-utils = { path = "../utilities" }
merlin = { version = "3.0.0", default-features = false }
num-bigint = { version = "0.4", default-features = false }
rand_chacha = { version = "0.3.1" }
rand_chacha = { version = "0.3.1", default-features = false }
rayon = { version = "1.5.0", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"] }
sha3 = "^0.10"
tagged-base64 = { git = "https://github.com/espressosystems/tagged-base64", tag = "0.3.0" }

[dev-dependencies]
ark-bls12-377 = "0.4.0"
ark-bls12-381 = "0.4.0"
ark-bn254 = "0.4.0"
ark-bw6-761 = "0.4.0"
ark-ed-on-bls12-377 = "0.4.0"
ark-ed-on-bls12-381 = "0.4.0"
ark-ed-on-bn254 = "0.4.0"
Expand Down
9 changes: 4 additions & 5 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ark-bls12-377 = "0.4.0"
ark-bls12-381 = "0.4.0"
ark-bn254 = "0.4.0"
ark-bw6-761 = "0.4.0"
ark-crypto-primitives = { version = "0.4.0", features = ["sponge"] }
ark-crypto-primitives = { version = "0.4.0", default-features = false, features = ["sponge"] }
ark-ec = "0.4.0"
ark-ed-on-bls12-377 = "0.4.0"
ark-ed-on-bls12-381 = "0.4.0"
Expand All @@ -21,8 +21,8 @@ ark-ff = "0.4.0"
ark-poly = "0.4.0"
ark-serialize = "0.4.0"
ark-std = { version = "0.4.0", default-features = false }
blst = "0.3.10"
crypto_box = "0.8.1"
blst = { git = "https://github.com/EspressoSystems/blst.git", branch = "no-std", default-features = false } # TODO: pin to a tag or commit
crypto_box = { version = "0.8.1", default-features = false, features = ["alloc", "u32_backend"] }
derivative = { version = "2", features = ["use_core"] }
digest = { version = "0.10.1", default-features = false, features = ["alloc"] }
displaydoc = { version = "0.2.3", default-features = false }
Expand All @@ -40,7 +40,7 @@ serde = { version = "1.0", default-features = false, features = ["derive"] }
sha2 = { version = "0.10.1", default-features = false }
sha3 = { version = "0.10.5", default-features = false }
tagged-base64 = { git = "https://github.com/espressosystems/tagged-base64", tag = "0.3.0" }
typenum = { version = "1.15.0", default-features = false }
typenum = { version = "1.15.0", default-features = false, features = ["no_std"] }
zeroize = { version = "1.5", default-features = false }

[dev-dependencies]
Expand All @@ -54,7 +54,6 @@ ark-ed-on-bn254 = "0.4.0"
bincode = "1.3"
criterion = "0.4.0"
hashbrown = "0.13.1"
rand_core = { version = "^0.6.0", features = ["getrandom"] }

[[bench]]
name = "merkle_path"
Expand Down
4 changes: 2 additions & 2 deletions primitives/benches/pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod bench {
use ark_bls12_381::{Bls12_381, Fr};
use ark_ff::UniformRand;
use ark_poly::{DenseMultilinearExtension, MultilinearExtension};
use ark_std::sync::Arc;
use ark_std::rc::Rc;
use jf_primitives::pcs::{
prelude::{MultilinearKzgPCS, PCSError, PolynomialCommitmentScheme},
StructuredReferenceString,
Expand All @@ -36,7 +36,7 @@ mod bench {
10
};

let poly = Arc::new(DenseMultilinearExtension::rand(nv, &mut rng));
let poly = Rc::new(DenseMultilinearExtension::rand(nv, &mut rng));
Copy link

Choose a reason for hiding this comment

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

single threaded benchmarks only?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed back in d1e8c3b

let (ml_ck, ml_vk) = uni_params.0.trim(nv)?;
let (uni_ck, uni_vk) = uni_params.1.trim(nv)?;
let ck = (ml_ck, uni_ck);
Expand Down
13 changes: 3 additions & 10 deletions primitives/src/pcs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use ark_std::{
borrow::Borrow,
fmt::Debug,
hash::Hash,
path::Path,
rand::{CryptoRng, RngCore},
vec::Vec,
};
Expand All @@ -32,13 +31,7 @@ pub trait PolynomialCommitmentScheme {
/// Structured reference string
type SRS: Clone + Debug + StructuredReferenceString;
/// Polynomial and its associated types
type Polynomial: Clone
+ Debug
+ Hash
+ PartialEq
+ Eq
+ CanonicalSerialize
+ CanonicalDeserialize;
type Polynomial: Clone + Debug + Hash + PartialEq + Eq + CanonicalSerialize;
/// Polynomial input domain
type Point: Clone + Ord + Debug + Sync + Hash + PartialEq + Eq;
/// Polynomial Evaluation
Expand Down Expand Up @@ -76,7 +69,7 @@ pub trait PolynomialCommitmentScheme {
/// If `file=None`, we load the default choice of SRS.
fn load_srs_from_file(
supported_size: usize,
file: Option<&Path>,
file: Option<&str>,
) -> Result<Self::SRS, PCSError> {
Self::SRS::load_srs_from_file(supported_size, file)
}
Expand Down Expand Up @@ -218,7 +211,7 @@ pub trait StructuredReferenceString: Sized {
/// implemented else where. We only load them into memory here.
///
/// If `file=None`, we load the default choice of SRS.
fn load_srs_from_file(_supported_size: usize, _file: Option<&Path>) -> Result<Self, PCSError> {
fn load_srs_from_file(_supported_size: usize, _file: Option<&str>) -> Result<Self, PCSError> {
unimplemented!("TODO: implement loading SRS from files");
}
}
Expand Down
10 changes: 5 additions & 5 deletions primitives/src/pcs/multilinear_kzg/batching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::pcs::{
};
use ark_ec::pairing::Pairing;
use ark_poly::{DenseMultilinearExtension, EvaluationDomain, MultilinearExtension, Polynomial};
use ark_std::{end_timer, format, start_timer, string::ToString, sync::Arc, vec, vec::Vec};
use ark_std::{end_timer, format, rc::Rc, start_timer, string::ToString, vec, vec::Vec};

/// Input
/// - the prover parameters for univariate KZG,
Expand Down Expand Up @@ -57,7 +57,7 @@ use ark_std::{end_timer, format, start_timer, string::ToString, sync::Arc, vec,
pub(super) fn batch_open_internal<E: Pairing>(
uni_prover_param: &UnivariateProverParam<E>,
ml_prover_param: &MultilinearProverParam<E>,
polynomials: &[Arc<DenseMultilinearExtension<E::ScalarField>>],
polynomials: &[Rc<DenseMultilinearExtension<E::ScalarField>>],
batch_commitment: &Commitment<E>,
points: &[Vec<E::ScalarField>],
) -> Result<(MultilinearKzgBatchProof<E>, Vec<E::ScalarField>), PCSError> {
Expand Down Expand Up @@ -318,7 +318,7 @@ mod tests {
fn test_batch_commit_helper<R: RngCore + CryptoRng>(
uni_params: &UnivariateUniversalParams<E>,
ml_params: &MultilinearUniversalParams<E>,
polys: &[Arc<DenseMultilinearExtension<Fr>>],
polys: &[Rc<DenseMultilinearExtension<Fr>>],
rng: &mut R,
) -> Result<(), PCSError> {
let merged_nv = get_batched_nv(polys[0].num_vars(), polys.len());
Expand Down Expand Up @@ -422,13 +422,13 @@ mod tests {

// normal polynomials
let polys1: Vec<_> = (0..5)
.map(|_| Arc::new(DenseMultilinearExtension::rand(4, &mut rng)))
.map(|_| Rc::new(DenseMultilinearExtension::rand(4, &mut rng)))
.collect();
test_batch_commit_helper(&uni_params, &ml_params, &polys1, &mut rng)?;

// single-variate polynomials
let polys1: Vec<_> = (0..5)
.map(|_| Arc::new(DenseMultilinearExtension::rand(1, &mut rng)))
.map(|_| Rc::new(DenseMultilinearExtension::rand(1, &mut rng)))
.collect();
test_batch_commit_helper(&uni_params, &ml_params, &polys1, &mut rng)?;

Expand Down
Loading