Skip to content

Commit

Permalink
Merge pull request #7 from arkworks-rs/no_std
Browse files Browse the repository at this point in the history
Add support for `no_std`, plus some `clippy` fixes
  • Loading branch information
weikengchen committed Oct 29, 2020
2 parents ad395fa + ad92c3d commit f17309b
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 119 deletions.
120 changes: 120 additions & 0 deletions src/fields/nonnative/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name: CI
on:
pull_request:
push:
branches:
- master
env:
RUST_BACKTRACE: 1

jobs:
style:
name: Check Style
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v1
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt

- name: cargo fmt --check
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

test:
name: Test
runs-on: ubuntu-latest
env:
RUSTFLAGS: -Dwarnings
strategy:
matrix:
rust:
- stable
- nightly
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install Rust (${{ matrix.rust }})
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true

- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Check examples
uses: actions-rs/cargo@v1
with:
command: check
args: --examples --all

- name: Check examples with all features on stable
uses: actions-rs/cargo@v1
with:
command: check
args: --examples --all-features --all
if: matrix.rust == 'stable'

- name: Check benchmarks on nightly
uses: actions-rs/cargo@v1
with:
command: check
args: --all-features --examples --all --benches
if: matrix.rust == 'nightly'

- name: Test
uses: actions-rs/cargo@v1
with:
command: test
args: "--all \
--all-features"

check_no_std:
name: Check no_std
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install Rust (${{ matrix.rust }})
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: thumbv6m-none-eabi
override: true

- name: Install Rust ARM64 (${{ matrix.rust }})
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: aarch64-unknown-none
override: true

- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: ark-nonnative-field
run: |
cargo build --no-default-features --target aarch64-unknown-none
cargo check --examples --no-default-features --target aarch64-unknown-none
15 changes: 5 additions & 10 deletions src/fields/nonnative/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,21 @@ debug-assertions = false
################################# Dependencies ################################

[dependencies]
rand = "0.7"
derivative = "2"
rand = { version = "0.7", default-features = false }
derivative = { version = "2", features = [ "use_core" ] }

tracing-subscriber = { version = "0.2" }
tracing = { version = "0.1", default-features = false }
tracing = { version = "0.1", default-features = false, features = [ "attributes" ] }

ark-ff = { git = "https://github.com/arkworks-rs/algebra", default-features = false }
ark-ec = { git = "https://github.com/arkworks-rs/algebra", default-features = false }
ark-std = { git = "https://github.com/arkworks-rs/utils", default-features = false }
ark-relations = { git = "https://github.com/arkworks-rs/snark", default-features = false }
ark-r1cs-std = { git = "https://github.com/arkworks-rs/r1cs-std", default-features = false }

rand_core = { version = "0.5" }
num-traits = { version = "0.2", default-features = false }
num-bigint = "0.3.0"
num-bigint = { version = "0.3.0", default-features = false }

[dev-dependencies]
criterion = "0.3"
rand_xorshift = { version = "0.2" }
paste = "1.0"
ark-bls12-381 = { git = "https://github.com/arkworks-rs/curves", features = ["curve"], default-features = false }
ark-mnt4-298 = { git = "https://github.com/arkworks-rs/curves", features = ["curve"], default-features = false }
Expand All @@ -64,8 +60,7 @@ ark-mnt6-753 = { git = "https://github.com/arkworks-rs/curves", default-features

[features]
default = []
std = [ "ark-std/std", "ark-ff/std", "ark-ec/std", "ark-relations/std", "ark-r1cs-std/std" ]
parallel = [ "std", "ark-ff/parallel", "ark-std/parallel", "ark-ec/parallel", "ark-r1cs-std/parallel" ]
std = [ "ark-std/std", "ark-ff/std", "ark-ec/std", "ark-relations/std", "ark-r1cs-std/std", "num-traits/std", "num-bigint/std" ]

[[bench]]
name = "nonnative-bench"
Expand Down
24 changes: 11 additions & 13 deletions src/fields/nonnative/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::NonNativeFieldVar;
use ark_ff::test_rng as thread_rng;
use ark_ff::PrimeField;
use ark_r1cs_std::eq::EqGadget;
use ark_r1cs_std::fields::FieldVar;
use ark_nonnative_field::NonNativeFieldVar;
use ark_r1cs_std::{alloc::AllocVar, eq::EqGadget, fields::FieldVar};
use ark_relations::r1cs::{ConstraintSystem, ConstraintSystemRef};
use curves::{bls12_381, mnt4_298, mnt4_753, mnt6_298, mnt6_753};
use rand::thread_rng;
use rand_core::RngCore;
use rand::RngCore;

const NUM_REPETITIONS: usize = 100;

Expand Down Expand Up @@ -173,11 +171,11 @@ macro_rules! nonnative_bench {
}

fn main() {
nonnative_bench!(MNT46Small, mnt4_298::Fr, mnt6_298::Fr);
nonnative_bench!(MNT64Small, mnt6_298::Fr, mnt4_298::Fr);
nonnative_bench!(MNT46Big, mnt4_753::Fr, mnt6_753::Fr);
nonnative_bench!(MNT64Big, mnt6_753::Fr, mnt4_753::Fr);
nonnative_bench!(BLS12MNT4Small, bls12_381::Fr, mnt4_298::Fr);
nonnative_bench!(BLS12, bls12_381::Fq, bls12_381::Fr);
nonnative_bench!(MNT6BigMNT4Small, mnt6_753::Fr, mnt4_298::Fr);
nonnative_bench!(MNT46Small, ark_mnt4_298::Fr, ark_mnt6_298::Fr);
nonnative_bench!(MNT64Small, ark_mnt6_298::Fr, ark_mnt4_298::Fr);
nonnative_bench!(MNT46Big, ark_mnt4_753::Fr, ark_mnt6_753::Fr);
nonnative_bench!(MNT64Big, ark_mnt6_753::Fr, ark_mnt4_753::Fr);
nonnative_bench!(BLS12MNT4Small, ark_bls12_381::Fr, ark_mnt4_298::Fr);
nonnative_bench!(BLS12, ark_bls12_381::Fq, ark_bls12_381::Fr);
nonnative_bench!(MNT6BigMNT4Small, ark_mnt6_753::Fr, ark_mnt4_298::Fr);
}
27 changes: 12 additions & 15 deletions src/fields/nonnative/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use ark_mnt6_753::MNT6_753;
use ark_nonnative_field::NonNativeFieldVar;
use ark_r1cs_std::{alloc::AllocVar, eq::EqGadget, fields::FieldVar, R1CSVar};
use ark_relations::r1cs::{ConstraintSystem, ConstraintSystemRef};
use rand::thread_rng;
use rand_core::RngCore;
use rand::RngCore;

const NUM_REPETITIONS: usize = 100;
const TEST_COUNT: usize = 100;
Expand Down Expand Up @@ -336,7 +335,7 @@ fn addition_stress_test<TargetField: PrimeField, BaseField: PrimeField, R: RngCo
for _ in 0..TEST_COUNT {
let next_native = TargetField::rand(rng);
let next = NonNativeFieldVar::<TargetField, BaseField>::new_witness(
ark_relations::ns!(cs, format!("next num for repetition {}", rep)),
ark_relations::ns!(cs, "next num for repetition"),
|| Ok(next_native),
)
.unwrap();
Expand All @@ -360,7 +359,7 @@ fn multiplication_stress_test<TargetField: PrimeField, BaseField: PrimeField, R:
for _ in 0..TEST_COUNT {
let next_native = TargetField::rand(rng);
let next = NonNativeFieldVar::<TargetField, BaseField>::new_witness(
ark_relations::ns!(cs, format!("next num for repetition {}", rep)),
ark_relations::ns!(cs, "next num for repetition"),
|| Ok(next_native),
)
.unwrap();
Expand All @@ -384,13 +383,13 @@ fn mul_and_add_stress_test<TargetField: PrimeField, BaseField: PrimeField, R: Rn
for _ in 0..TEST_COUNT {
let next_add_native = TargetField::rand(rng);
let next_add = NonNativeFieldVar::<TargetField, BaseField>::new_witness(
ark_relations::ns!(cs, format!("next to add num for repetition {}", rep)),
ark_relations::ns!(cs, "next to add num for repetition"),
|| Ok(next_add_native),
)
.unwrap();
let next_mul_native = TargetField::rand(rng);
let next_mul = NonNativeFieldVar::<TargetField, BaseField>::new_witness(
ark_relations::ns!(cs, format!("next to mul num for repetition {}", rep)),
ark_relations::ns!(cs, "next to mul num for repetition"),
|| Ok(next_mul_native),
)
.unwrap();
Expand All @@ -415,13 +414,13 @@ fn square_mul_add_stress_test<TargetField: PrimeField, BaseField: PrimeField, R:
for _ in 0..TEST_COUNT {
let next_add_native = TargetField::rand(rng);
let next_add = NonNativeFieldVar::<TargetField, BaseField>::new_witness(
ark_relations::ns!(cs, format!("next to add num for repetition {}", rep)),
ark_relations::ns!(cs, "next to add num for repetition"),
|| Ok(next_add_native),
)
.unwrap();
let next_mul_native = TargetField::rand(rng);
let next_mul = NonNativeFieldVar::<TargetField, BaseField>::new_witness(
ark_relations::ns!(cs, format!("next to mul num for repetition {}", rep)),
ark_relations::ns!(cs, "next to mul num for repetition"),
|| Ok(next_mul_native),
)
.unwrap();
Expand Down Expand Up @@ -498,7 +497,7 @@ fn double_stress_test_3<TargetField: PrimeField, BaseField: PrimeField, R: RngCo
let num_square_native = num_native * &num_native;
let num_square = &num * &num;
let num_square_native_gadget = NonNativeFieldVar::<TargetField, BaseField>::new_witness(
ark_relations::ns!(cs, format!("repetition {}: alloc_native num", rep)),
ark_relations::ns!(cs, "repetition: alloc_native num"),
|| Ok(num_square_native),
)
.unwrap();
Expand Down Expand Up @@ -535,8 +534,7 @@ macro_rules! nonnative_test_individual {
paste::item! {
#[test]
fn [<$test_method _ $test_name:lower>]() {
let rng = &mut thread_rng();

let rng = &mut ark_ff::test_rng();
/*{
let cs = ConstraintSystem::<$test_base_field>::new();
let cs_ref = ConstraintSystemRef::new(cs);
Expand All @@ -548,10 +546,9 @@ macro_rules! nonnative_test_individual {
}*/

for _ in 0..NUM_REPETITIONS {
let cs = ConstraintSystem::<$test_base_field>::new();
let cs_ref = ConstraintSystemRef::new(cs);
$test_method::<$test_target_field, $test_base_field, _>(cs_ref.clone(), rng);
assert!(cs_ref.is_satisfied().unwrap());
let cs = ConstraintSystem::<$test_base_field>::new_ref();
$test_method::<$test_target_field, $test_base_field, _>(cs.clone(), rng);
assert!(cs.is_satisfied().unwrap());
}
}
}
Expand Down
Loading

0 comments on commit f17309b

Please sign in to comment.