Skip to content

Commit

Permalink
Rename NonNativeFieldVar -> EmulatedFpVar (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush authored Dec 28, 2023
1 parent 1ff3a90 commit ed2d55e
Show file tree
Hide file tree
Showing 18 changed files with 669 additions and 740 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# CHANGELOG

## Pending

### Breaking changes

- [\#134](https://github.com/arkworks-rs/r1cs-std/pull/134) Add `Mul<NonnativeFieldVar>` bounds and impls for `CurveVar`.
- [\#135](https://github.com/arkworks-rs/r1cs-std/pull/135)
- Rename `NonNativeFieldVar` to `EmulatedFpVar`.
- Rename `fields::nonnative` to `fields::emulated_fp`.
- Rename `fields::nonnative::{Allocated}NonNativeMulResultVar` to `fields::emulated_fp::{Allocated}MulResultVar`.

### Features

### Improvements

### Bug Fixes

## 0.4.0

- [\#117](https://github.com/arkworks-rs/r1cs-std/pull/117) Fix result of `precomputed_base_scalar_mul_le` to not discard previous value.
- [\#124](https://github.com/arkworks-rs/r1cs-std/pull/124) Fix `scalar_mul_le` constraints unsatisfiability when short Weierstrass point is zero.
- [\#127](https://github.com/arkworks-rs/r1cs-std/pull/127) Convert `NonNativeFieldVar` constants to little-endian bytes instead of big-endian (`ToBytesGadget`).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ std = [ "ark-ff/std", "ark-relations/std", "ark-std/std", "num-bigint/std" ]
parallel = [ "std", "ark-ff/parallel", "ark-std/parallel"]

[[bench]]
name = "nonnative-bench"
name = "emulated-bench"
path = "benches/bench.rs"
harness = false

Expand Down
78 changes: 31 additions & 47 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ark_ff::PrimeField;
use ark_r1cs_std::{
alloc::AllocVar,
eq::EqGadget,
fields::{nonnative::NonNativeFieldVar, FieldVar},
fields::{emulated_fp::EmulatedFpVar, FieldVar},
};
use ark_relations::{
ns,
Expand All @@ -26,20 +26,18 @@ fn get_density<BaseField: PrimeField>(cs: &ConstraintSystemRef<BaseField>) -> us
}
}

fn allocation<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
fn allocation<TargetF: PrimeField, BaseField: PrimeField, R: RngCore>(
cs: ConstraintSystemRef<BaseField>,
rng: &mut R,
) -> (usize, usize) {
let a_native = TargetField::rand(rng);
let a_native = TargetF::rand(rng);

let constraints_before = cs.num_constraints();
let nonzeros_before = get_density(&cs);

// There will be a check that ensures it has the reasonable number of bits
let _ = NonNativeFieldVar::<TargetField, BaseField>::new_witness(ns!(cs, "alloc a"), || {
Ok(a_native)
})
.unwrap();
let _ = EmulatedFpVar::<TargetF, BaseField>::new_witness(ns!(cs, "alloc a"), || Ok(a_native))
.unwrap();

let constraints_after = cs.num_constraints();
let nonzeros_after = get_density(&cs);
Expand All @@ -50,21 +48,17 @@ fn allocation<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
);
}

fn addition<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
fn addition<TargetF: PrimeField, BaseField: PrimeField, R: RngCore>(
cs: ConstraintSystemRef<BaseField>,
rng: &mut R,
) -> (usize, usize) {
let a_native = TargetField::rand(rng);
let a = NonNativeFieldVar::<TargetField, BaseField>::new_witness(ns!(cs, "alloc a"), || {
Ok(a_native)
})
.unwrap();

let b_native = TargetField::rand(rng);
let b = NonNativeFieldVar::<TargetField, BaseField>::new_witness(ns!(cs, "alloc b"), || {
Ok(b_native)
})
.unwrap();
let a_native = TargetF::rand(rng);
let a = EmulatedFpVar::<TargetF, BaseField>::new_witness(ns!(cs, "alloc a"), || Ok(a_native))
.unwrap();

let b_native = TargetF::rand(rng);
let b = EmulatedFpVar::<TargetF, BaseField>::new_witness(ns!(cs, "alloc b"), || Ok(b_native))
.unwrap();

let constraints_before = cs.num_constraints();
let nonzeros_before = get_density(&cs);
Expand All @@ -80,19 +74,15 @@ fn addition<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
);
}

fn equality<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
fn equality<TargetF: PrimeField, BaseField: PrimeField, R: RngCore>(
cs: ConstraintSystemRef<BaseField>,
rng: &mut R,
) -> (usize, usize) {
let a_native = TargetField::rand(rng);
let a1 = NonNativeFieldVar::<TargetField, BaseField>::new_witness(ns!(cs, "alloc a1"), || {
Ok(a_native)
})
.unwrap();
let a2 = NonNativeFieldVar::<TargetField, BaseField>::new_witness(ns!(cs, "alloc a2"), || {
Ok(a_native)
})
.unwrap();
let a_native = TargetF::rand(rng);
let a1 = EmulatedFpVar::<TargetF, BaseField>::new_witness(ns!(cs, "alloc a1"), || Ok(a_native))
.unwrap();
let a2 = EmulatedFpVar::<TargetF, BaseField>::new_witness(ns!(cs, "alloc a2"), || Ok(a_native))
.unwrap();

let constraints_before = cs.num_constraints();
let nonzeros_before = get_density(&cs);
Expand All @@ -108,21 +98,17 @@ fn equality<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
);
}

fn multiplication<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
fn multiplication<TargetF: PrimeField, BaseField: PrimeField, R: RngCore>(
cs: ConstraintSystemRef<BaseField>,
rng: &mut R,
) -> (usize, usize) {
let a_native = TargetField::rand(rng);
let a = NonNativeFieldVar::<TargetField, BaseField>::new_witness(ns!(cs, "initial a"), || {
Ok(a_native)
})
.unwrap();

let b_native = TargetField::rand(rng);
let b = NonNativeFieldVar::<TargetField, BaseField>::new_witness(ns!(cs, "initial b"), || {
Ok(b_native)
})
.unwrap();
let a_native = TargetF::rand(rng);
let a = EmulatedFpVar::<TargetF, BaseField>::new_witness(ns!(cs, "initial a"), || Ok(a_native))
.unwrap();

let b_native = TargetF::rand(rng);
let b = EmulatedFpVar::<TargetF, BaseField>::new_witness(ns!(cs, "initial b"), || Ok(b_native))
.unwrap();

let constraints_before = cs.num_constraints();
let nonzeros_before = get_density(&cs);
Expand All @@ -138,15 +124,13 @@ fn multiplication<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
);
}

fn inverse<TargetField: PrimeField, BaseField: PrimeField, R: RngCore>(
fn inverse<TargetF: PrimeField, BaseField: PrimeField, R: RngCore>(
cs: ConstraintSystemRef<BaseField>,
rng: &mut R,
) -> (usize, usize) {
let num_native = TargetField::rand(rng);
let num = NonNativeFieldVar::<TargetField, BaseField>::new_witness(ns!(cs, "alloc"), || {
Ok(num_native)
})
.unwrap();
let num_native = TargetF::rand(rng);
let num = EmulatedFpVar::<TargetF, BaseField>::new_witness(ns!(cs, "alloc"), || Ok(num_native))
.unwrap();

let constraints_before = cs.num_constraints();
let nonzeros_before = get_density(&cs);
Expand Down
Loading

0 comments on commit ed2d55e

Please sign in to comment.