Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ark-r1cs-std"
version = "0.5.0"
authors = [ "arkworks contributors" ]
authors = ["arkworks contributors"]
description = "A standard library for constraint system gadgets"
homepage = "https://arkworks.rs"
repository = "https://github.com/arkworks-rs/r1cs-std"
Expand All @@ -17,31 +17,50 @@ resolver = "2"
ark-ff = { version = "0.5.0", default-features = false }
ark-ec = { version = "0.5.0", default-features = false }
ark-std = { version = "0.5.0", default-features = false }
ark-relations = { version = "0.5.0", default-features = false }
ark-relations = { version = "0.5.0", default-features = true }

educe = "0.6.0"
tracing = { version = "0.1", default-features = false, features = [ "attributes" ] }
tracing = { version = "0.1", default-features = false, features = [
"attributes",
] }
num-bigint = { version = "0.4", default-features = false }
num-traits = { version = "0.2", default-features = false }
num-integer = { version = "0.1.44", default-features = false }

[dev-dependencies]
ark-test-curves = { version = "0.5.0", default-features = false, features = ["bls12_381_scalar_field", "bls12_381_curve", "mnt4_753_scalar_field"] }
ark-test-curves = { version = "0.5.0", default-features = false, features = [
"bls12_381_scalar_field",
"bls12_381_curve",
"mnt4_753_scalar_field",
] }
ark-poly = { version = "0.5.0", default-features = false }
paste = "1.0"
ark-bls12-377 = { version = "0.5.0", features = ["curve"], default-features = false }
ark-bls12-381 = { version = "0.5.0", features = ["curve"], default-features = false }
ark-mnt4-298 = { version = "0.5.0", features = ["curve"], default-features = false }
ark-mnt4-753 = { version = "0.5.0", features = ["curve"], default-features = false }
ark-mnt6-298 = { version = "0.5.0", default-features = false }
ark-mnt6-753 = { version = "0.5.0", default-features = false }
ark-pallas = { version = "0.5.0", features = ["curve"], default-features = false }
ark-bn254 = { version = "0.5.0", features = ["curve"], default-features = false }
ark-bls12-377 = { version = "0.5.0", features = [
"curve",
], default-features = false }
ark-bls12-381 = { version = "0.5.0", features = [
"curve",
], default-features = false }
ark-mnt4-298 = { version = "0.5.0", features = [
"curve",
], default-features = false }
ark-mnt4-753 = { version = "0.5.0", features = [
"curve",
], default-features = false }
ark-mnt6-298 = { version = "0.5.0", default-features = false }
ark-mnt6-753 = { version = "0.5.0", default-features = false }
ark-pallas = { version = "0.5.0", features = [
"curve",
], default-features = false }
ark-bn254 = { version = "0.5.0", features = [
"curve",
], default-features = false }
tracing-subscriber = { version = "0.3", default-features = true }

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

[[bench]]
name = "emulated-bench"
Expand Down Expand Up @@ -76,3 +95,7 @@ debug = true
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(ci)'] }


# patch
[patch.crates-io]
ark-relations = { git = "https://github.com/arkworks-rs/snark.git", default-features = true }
19 changes: 15 additions & 4 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use ark_r1cs_std::{
fields::{emulated_fp::EmulatedFpVar, FieldVar},
};
use ark_relations::{
gr1cs::{ConstraintSystem, ConstraintSystemRef, OptimizationGoal},
ns,
r1cs::{ConstraintSystem, ConstraintSystemRef, OptimizationGoal},
};
use ark_std::rand::RngCore;

Expand All @@ -19,9 +19,20 @@ fn get_density<BaseField: PrimeField>(cs: &ConstraintSystemRef<BaseField>) -> us
let mut cs_bak = r.borrow().clone();

cs_bak.finalize();
let matrices = cs_bak.to_matrices().unwrap();

matrices.a_num_non_zero + matrices.b_num_non_zero + matrices.c_num_non_zero
let matrices_map = cs_bak.to_matrices().unwrap();
match matrices_map.get(ark_relations::gr1cs::R1CS_PREDICATE_LABEL) {
None => 0,
Some(matrices) => {
let a = &matrices[0];
let b = &matrices[1];
let c = &matrices[2];
let a_num_non_zero: usize = a.iter().map(|lc| lc.len()).sum();
let b_num_non_zero: usize = b.iter().map(|lc| lc.len()).sum();
let c_num_non_zero: usize = c.iter().map(|lc| lc.len()).sum();

a_num_non_zero + b_num_non_zero + c_num_non_zero
},
}
},
}
}
Expand Down
72 changes: 72 additions & 0 deletions scripts/migrate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bash

# -----------------------------------------------------------------------------
# rename_r1cs.sh
#
# Recursively:
# 1. Renames .rs filenames: R1CS->GR1CS, r1cs->gr1cs (except those with r1cs_std).
# 2. In .rs file contents, replaces R1CS->GR1CS, r1cs->gr1cs (except lines with r1cs_std).
# 3. Finally replaces enforce_constraint->enforce_r1cs_constraint (except lines with r1cs_std).
# -----------------------------------------------------------------------------

# --- Detect which 'sed' in-place flag to use (GNU vs BSD/macOS) ---
# On macOS, 'sed -i' requires a backup extension parameter (can be empty '').
# On GNU sed, 'sed -i' is fine without extra args.
if sed --version 2>/dev/null | grep -q "GNU"; then
# GNU sed
SED_INPLACE=(-i)
else
# BSD/macOS sed
SED_INPLACE=(-i '')
fi

echo "###############################################"
echo "# ⚠️ Cautious: RUN THIS SCRIPT ONLY ONCE! DO NOT RUN IT TWICE! #"
echo "# Make sure you have a backup before running. #"
echo "###############################################"
echo
read -p "Are you sure you want to continue? (yes/no) " choice

if [[ "$choice" != "yes" ]]; then
echo "Aborting script execution."
exit 1
fi

# 1) Rename files that contain 'r1cs' or 'R1CS' in their name, skipping any with 'r1cs_std'.
echo "Renaming files that contain 'r1cs' or 'R1CS' (but not 'r1cs_std')..."
export LC_ALL=C # Ensure consistent behavior for case conversions

# Use 'find' + 'while IFS= read -r -d ""' to handle spaces & special chars in filenames.
while IFS= read -r -d '' file; do
# If filename includes r1cs_std, skip
if [[ "$file" =~ r1cs_std ]]; then
continue
fi

# Compute new filename by substituting:
newfile="$(echo "$file" | sed 's/R1CS/GR1CS/g; s/r1cs/gr1cs/g')"

if [[ "$newfile" != "$file" ]]; then
echo " -> Renaming: $file -> $newfile"
mv "$file" "$newfile"
fi
done < <(find . -type f -name "*.rs" -print0)

# 2) Replace R1CS->GR1CS and r1cs->gr1cs in file contents,
# skipping lines that have 'r1cs_std'.
echo "Replacing (R1CS->GR1CS, r1cs->gr1cs) in .rs file contents..."
while IFS= read -r -d '' file; do
# Replace R1CS -> GR1CS on lines NOT containing 'r1cs_std'
sed "${SED_INPLACE[@]}" '/r1cs_std/! s/R1CS/GR1CS/g' "$file"
# Replace r1cs -> gr1cs on lines NOT containing 'r1cs_std'
sed "${SED_INPLACE[@]}" '/r1cs_std/! s/r1cs/gr1cs/g' "$file"
done < <(find . -type f -name "*.rs" ! -name "*r1cs_std*" -print0)

# 3) Finally, replace 'enforce_constraint' -> 'enforce_r1cs_constraint',
# skipping lines that have 'r1cs_std'.
echo "Replacing enforce_constraint->enforce_r1cs_constraint..."
while IFS= read -r -d '' file; do
sed "${SED_INPLACE[@]}" '/r1cs_std/! s/enforce_constraint/enforce_r1cs_constraint/g' "$file"
done < <(find . -type f -name "*.rs" ! -name "*r1cs_std*" -print0)

echo "Done!"
12 changes: 6 additions & 6 deletions src/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Vec;
use ark_ff::Field;
use ark_relations::r1cs::{Namespace, SynthesisError};
use ark_relations::gr1cs::{Namespace, SynthesisError};
use core::borrow::Borrow;

/// Describes the mode that a variable should be allocated in within
Expand Down Expand Up @@ -49,7 +49,7 @@ pub trait AllocVar<V: ?Sized, F: Field>: Sized {
/// Allocates a new constant of type `Self` in the `ConstraintSystem` `cs`.
///
/// This should *not* allocate any new variables or constraints in `cs`.
#[tracing::instrument(target = "r1cs", skip(cs, t))]
#[tracing::instrument(target = "gr1cs", skip(cs, t))]
fn new_constant(
cs: impl Into<Namespace<F>>,
t: impl Borrow<V>,
Expand All @@ -59,7 +59,7 @@ pub trait AllocVar<V: ?Sized, F: Field>: Sized {

/// Allocates a new public input of type `Self` in the `ConstraintSystem`
/// `cs`.
#[tracing::instrument(target = "r1cs", skip(cs, f))]
#[tracing::instrument(target = "gr1cs", skip(cs, f))]
fn new_input<T: Borrow<V>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
Expand All @@ -69,7 +69,7 @@ pub trait AllocVar<V: ?Sized, F: Field>: Sized {

/// Allocates a new private witness of type `Self` in the `ConstraintSystem`
/// `cs`.
#[tracing::instrument(target = "r1cs", skip(cs, f))]
#[tracing::instrument(target = "gr1cs", skip(cs, f))]
fn new_witness<T: Borrow<V>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
Expand All @@ -90,8 +90,8 @@ pub trait AllocVar<V: ?Sized, F: Field>: Sized {
/// variable, one can write:
/// ```
/// use ark_ff::PrimeField;
/// use ark_r1cs_std::{alloc::AllocVar, fields::{fp::FpVar, FieldVar}, R1CSVar};
/// use ark_relations::r1cs::SynthesisError;
/// use ark_r1cs_std::{alloc::AllocVar, fields::{fp::FpVar, FieldVar}, GR1CSVar};
/// use ark_relations::gr1cs::SynthesisError;
///
/// fn div<F: PrimeField>(x_var: &FpVar<F>, y_var: &FpVar<F>) -> Result<FpVar<F>, SynthesisError> {
/// let cs = x_var.cs().or(y_var.cs());
Expand Down
30 changes: 15 additions & 15 deletions src/boolean/allocated.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::borrow::Borrow;

use ark_ff::{Field, PrimeField};
use ark_relations::r1cs::{ConstraintSystemRef, Namespace, SynthesisError, Variable};
use ark_relations::gr1cs::{ConstraintSystemRef, Namespace, SynthesisError, Variable};

use crate::{
alloc::{AllocVar, AllocationMode},
Expand Down Expand Up @@ -58,7 +58,7 @@ impl<F: Field> AllocatedBool<F> {

/// Performs an XOR operation over the two operands, returning
/// an `AllocatedBool`.
#[tracing::instrument(target = "r1cs")]
#[tracing::instrument(target = "gr1cs")]
pub fn not(&self) -> Result<Self, SynthesisError> {
let variable = self.cs.new_lc(lc!() + Variable::One - self.variable)?;
Ok(Self {
Expand All @@ -69,7 +69,7 @@ impl<F: Field> AllocatedBool<F> {

/// Performs an XOR operation over the two operands, returning
/// an `AllocatedBool`.
#[tracing::instrument(target = "r1cs")]
#[tracing::instrument(target = "gr1cs")]
pub fn xor(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(self.value()? ^ b.value()?)
Expand All @@ -90,7 +90,7 @@ impl<F: Field> AllocatedBool<F> {
// -2a * b = c - a - b
// 2a * b = a + b - c
// (a + a) * b = a + b - c
self.cs.enforce_constraint(
self.cs.enforce_r1cs_constraint(
lc!() + self.variable + self.variable,
lc!() + b.variable,
lc!() + self.variable + b.variable - result.variable,
Expand All @@ -101,15 +101,15 @@ impl<F: Field> AllocatedBool<F> {

/// Performs an AND operation over the two operands, returning
/// an `AllocatedBool`.
#[tracing::instrument(target = "r1cs")]
#[tracing::instrument(target = "gr1cs")]
pub fn and(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(self.value()? & b.value()?)
})?;

// Constrain (a) * (b) = (c), ensuring c is 1 iff
// a AND b are both 1.
self.cs.enforce_constraint(
self.cs.enforce_r1cs_constraint(
lc!() + self.variable,
lc!() + b.variable,
lc!() + result.variable,
Expand All @@ -120,15 +120,15 @@ impl<F: Field> AllocatedBool<F> {

/// Performs an OR operation over the two operands, returning
/// an `AllocatedBool`.
#[tracing::instrument(target = "r1cs")]
#[tracing::instrument(target = "gr1cs")]
pub fn or(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(self.value()? | b.value()?)
})?;

// Constrain (1 - a) * (1 - b) = (1 - c), ensuring c is 0 iff
// a and b are both false, and otherwise c is 1.
self.cs.enforce_constraint(
self.cs.enforce_r1cs_constraint(
lc!() + Variable::One - self.variable,
lc!() + Variable::One - b.variable,
lc!() + Variable::One - result.variable,
Expand All @@ -138,15 +138,15 @@ impl<F: Field> AllocatedBool<F> {
}

/// Calculates `a AND (NOT b)`.
#[tracing::instrument(target = "r1cs")]
#[tracing::instrument(target = "gr1cs")]
pub fn and_not(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(self.value()? & !b.value()?)
})?;

// Constrain (a) * (1 - b) = (c), ensuring c is 1 iff
// a is true and b is false, and otherwise c is 0.
self.cs.enforce_constraint(
self.cs.enforce_r1cs_constraint(
lc!() + self.variable,
lc!() + Variable::One - b.variable,
lc!() + result.variable,
Expand All @@ -156,15 +156,15 @@ impl<F: Field> AllocatedBool<F> {
}

/// Calculates `(NOT a) AND (NOT b)`.
#[tracing::instrument(target = "r1cs")]
#[tracing::instrument(target = "gr1cs")]
pub fn nor(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(!(self.value()? | b.value()?))
})?;

// Constrain (1 - a) * (1 - b) = (c), ensuring c is 1 iff
// a and b are both false, and otherwise c is 0.
self.cs.enforce_constraint(
self.cs.enforce_r1cs_constraint(
lc!() + Variable::One - self.variable,
lc!() + Variable::One - b.variable,
lc!() + result.variable,
Expand Down Expand Up @@ -205,15 +205,15 @@ impl<F: Field> AllocVar<bool, F> for AllocatedBool<F> {
// Constrain: (1 - a) * a = 0
// This constrains a to be either 0 or 1.

cs.enforce_constraint(lc!() + Variable::One - variable, lc!() + variable, lc!())?;
cs.enforce_r1cs_constraint(lc!() + Variable::One - variable, lc!() + variable, lc!())?;

Ok(Self { variable, cs })
}
}
}

impl<F: PrimeField> CondSelectGadget<F> for AllocatedBool<F> {
#[tracing::instrument(target = "r1cs")]
#[tracing::instrument(target = "gr1cs")]
fn conditionally_select(
cond: &Boolean<F>,
true_val: &Self,
Expand All @@ -235,7 +235,7 @@ impl<F: PrimeField> CondSelectGadget<F> for AllocatedBool<F> {
mod test {
use super::*;

use ark_relations::r1cs::ConstraintSystem;
use ark_relations::gr1cs::ConstraintSystem;
use ark_test_curves::bls12_381::Fr;
#[test]
fn allocated_xor() -> Result<(), SynthesisError> {
Expand Down
Loading
Loading