Skip to content

Replace lc_map: BTreeMap -> lc_map: Vec #385

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

Merged
merged 11 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 1 addition & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install Rust
uses: actions-rs/toolchain@v1
Expand All @@ -99,12 +99,6 @@ jobs:
target: aarch64-unknown-none
override: true

- name: Check
uses: actions-rs/cargo@v1
with:
command: check
args: --examples --workspace --target aarch64-unknown-none

- name: Build
uses: actions-rs/cargo@v1
with:
Expand Down
2 changes: 2 additions & 0 deletions relations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ ark-poly.workspace = true
ark-serialize.workspace = true
tracing = { workspace = true, features = ["attributes"] }
tracing-subscriber = { workspace = true, default-features = true, optional = true }
hashbrown = { version = "0.15", default-features = false , features = [ "default-hasher"] }
rayon = { workspace = true, optional = true }

[dev-dependencies]
ark-test-curves = { workspace = true, default-features = false, features = [ "bls12_381_scalar_field" ] }
jemallocator = { version = "0.5" }

[features]
default = []
Expand Down
3 changes: 2 additions & 1 deletion relations/examples/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ To run the examples, use the following commands:
cargo run --example satisfiable

# Run the non-satisfiable example with the `std` feature enabled to see the output trace
cargo run --example non-satisfiable --features std
cargo run --example non-satisfiable --features std
```
102 changes: 102 additions & 0 deletions relations/examples/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

use ark_ff::{Field, UniformRand};
use ark_relations::gr1cs::{
ConstraintSynthesizer, ConstraintSystem, ConstraintSystemRef, LinearCombination,
OptimizationGoal, SynthesisMode,
};
use ark_std::rand::{Rng, SeedableRng};
use ark_test_curves::bls12_381::Fr;
use jemallocator::Jemalloc;

pub const NUM_COEFFS_IN_LC: usize = 10;

struct BenchCircuit {
a: Fr,
b: Fr,
c: Fr,
num_constraints: usize,
}

impl ConstraintSynthesizer<Fr> for BenchCircuit {
fn generate_constraints(self, cs: ConstraintSystemRef<Fr>) -> ark_relations::gr1cs::Result<()> {
let a = cs.new_witness_variable(|| Ok(self.a)).unwrap();
let b = cs.new_witness_variable(|| Ok(self.b)).unwrap();
let c = cs.new_witness_variable(|| Ok(self.c)).unwrap();
let mut variables = vec![a, b, c];
variables.reserve(3 * self.num_constraints);
let mut lcs = Vec::with_capacity(self.num_constraints);

let mut rng = ark_std::rand::rngs::StdRng::seed_from_u64(0u64);

for i in 0..self.num_constraints {
let cur_num_vars = ark_std::cmp::min(variables.len(), 10);
let lower = variables.len().checked_sub(cur_num_vars).unwrap_or(0);
let upper = variables.len();

let a_i_lc_size = rng.gen_range(1..=NUM_COEFFS_IN_LC);
let a_i = LinearCombination(
(0..a_i_lc_size)
.map(|_| (Fr::ONE, variables[rng.gen_range(lower..upper)]))
.collect::<Vec<_>>(),
);

let b_i_lc_size = rng.gen_range(1..=NUM_COEFFS_IN_LC);
let b_i = LinearCombination(
(0..b_i_lc_size)
.map(|_| (Fr::ONE, variables[rng.gen_range(lower..upper)]))
.collect::<Vec<_>>(),
);

let c_i = variables[rng.gen_range(lower..upper)];

if i % 2 == 0 {
let extra_lc = LinearCombination(
(0..a_i_lc_size)
.map(|_| (Fr::ONE, variables[rng.gen_range(lower..upper)]))
.collect::<Vec<_>>(),
);
let extra_lc = cs.new_lc(extra_lc)?;
lcs.push(extra_lc);
cs.enforce_r1cs_constraint(a_i + extra_lc, b_i, c_i.into())?;
} else {
cs.enforce_r1cs_constraint(a_i, b_i, c_i.into())?;
};
let v1 = cs.new_witness_variable(|| Ok(self.a))?;
let v2 = cs.new_witness_variable(|| Ok(self.a))?;
let v3 = cs.new_witness_variable(|| Ok(self.a))?;
variables.push(v1);
variables.push(v2);
variables.push(v3);
}

Ok(())
}
}

fn main() {
for log_num_constraints in 23..24 {
let circuit = BenchCircuit {
a: Fr::rand(&mut ark_std::test_rng()),
b: Fr::rand(&mut ark_std::test_rng()),
c: Fr::rand(&mut ark_std::test_rng()),
num_constraints: 2usize.pow(log_num_constraints),
};
let cs = ConstraintSystem::<Fr>::new_ref();
cs.set_optimization_goal(OptimizationGoal::Constraints);
cs.set_mode(SynthesisMode::Prove {
construct_matrices: true,
generate_lc_assignments: false,
});
let start = std::time::Instant::now();
circuit.generate_constraints(cs.clone()).unwrap();
cs.finalize();
let elapsed = start.elapsed();
println!(
"Synthesizing 2^{} constraints took {:?}",
log_num_constraints,
elapsed.as_secs_f32()
);
}
}
3 changes: 1 addition & 2 deletions relations/examples/non_satisfiable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ fn main() {
generate_constraints(cs.clone(), p1, p2, p3, p4, w1, w2, w3, w4, expected_result).unwrap();
let trace = cs.which_is_unsatisfied().unwrap().unwrap();
println!(
"This is the trace of non-satisfied scenario, Check out the trace:\n{}",
trace
"The constraint system was not satisfied; here is a trace indicating which constraint was unsatisfied: \n{trace}",
)
}
// Function to enforce the overall constraints by combining subcircuits
Expand Down
Loading
Loading