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

chore: migrate examples to the Bn256 field #1095

Merged
merged 2 commits into from
Feb 7, 2024
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
10 changes: 5 additions & 5 deletions examples/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use lurk::circuit::gadgets::pointer::AllocatedPtr;
#[cfg(not(target_arch = "wasm32"))]
use lurk::coprocessor::circom::non_wasm::CircomCoprocessor;

use halo2curves::bn256::Fr as Bn;
use lurk::eval::lang::Lang;
use lurk::field::LurkField;
use lurk::lem::{pointers::Ptr, store::Store};
Expand All @@ -48,7 +49,6 @@ use lurk::public_parameters::{
};
use lurk::Symbol;
use lurk_macros::Coproc;
use pasta_curves::pallas::Scalar as Fr;

const REDUCTION_COUNT: usize = 1;

Expand Down Expand Up @@ -99,17 +99,17 @@ enum Sha256Coproc<F: LurkField> {
/// `cargo run --release -- circom --name sha256_2 examples/sha256/`
/// `cargo run --release --example circom`
fn main() {
let store = &Store::<Fr>::default();
let store = &Store::default();
let sym_str = Symbol::new(&[".circom_sha256_2"], false); // two inputs
let circom_sha256: CircomSha256<Fr> = CircomSha256::new(0);
let mut lang = Lang::<Fr, Sha256Coproc<Fr>>::new();
let circom_sha256: CircomSha256<Bn> = CircomSha256::new(0);
let mut lang = Lang::<Bn, Sha256Coproc<Bn>>::new();
lang.add_coprocessor(sym_str, CircomCoprocessor::new(circom_sha256));
let lang_rc = Arc::new(lang);

let expr = "(.circom_sha256_2)".to_string();
let ptr = store.read_with_default_state(&expr).unwrap();

let nova_prover = NovaProver::<Fr, Sha256Coproc<Fr>>::new(REDUCTION_COUNT, lang_rc.clone());
let nova_prover = NovaProver::<Bn, Sha256Coproc<Bn>>::new(REDUCTION_COUNT, lang_rc.clone());

println!("Setting up public parameters...");

Expand Down
8 changes: 4 additions & 4 deletions examples/sha256_ivc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pasta_curves::pallas::Scalar as Fr;
use halo2curves::bn256::Fr as Bn;
use std::{sync::Arc, time::Instant};
use tracing_subscriber::{fmt, prelude::*, EnvFilter, Registry};
use tracing_texray::TeXRayLayer;
Expand Down Expand Up @@ -62,16 +62,16 @@ fn main() {
let args = std::env::args().collect::<Vec<_>>();
let n = args.get(1).unwrap_or(&"1".into()).parse().unwrap();

let store = &Store::<Fr>::default();
let store = &Store::default();
let cproc_sym = user_sym(&format!("sha256_ivc_{n}"));

let call = sha256_ivc(store, n, &(0..n).collect::<Vec<_>>());

let mut lang = Lang::<Fr, Sha256Coproc<Fr>>::new();
let mut lang = Lang::<Bn, Sha256Coproc<Bn>>::new();
lang.add_coprocessor(cproc_sym, Sha256Coprocessor::new(n));
let lang_rc = Arc::new(lang.clone());

let nova_prover = NovaProver::<Fr, Sha256Coproc<Fr>>::new(REDUCTION_COUNT, lang_rc.clone());
let nova_prover = NovaProver::<Bn, Sha256Coproc<Bn>>::new(REDUCTION_COUNT, lang_rc.clone());

println!("Setting up public parameters (rc = {REDUCTION_COUNT})...");

Expand Down
8 changes: 4 additions & 4 deletions examples/sha256_nivc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pasta_curves::pallas::Scalar as Fr;
use halo2curves::bn256::Fr as Bn;
use std::{sync::Arc, time::Instant};
use tracing_subscriber::{fmt, prelude::*, EnvFilter, Registry};
use tracing_texray::TeXRayLayer;
Expand Down Expand Up @@ -66,12 +66,12 @@ fn main() {
let args = std::env::args().collect::<Vec<_>>();
let n = args.get(1).unwrap_or(&"1".into()).parse().unwrap();

let store = &Store::<Fr>::default();
let store = &Store::default();
let cproc_sym = user_sym(&format!("sha256_nivc_{n}"));

let call = sha256_nivc(store, n, &(0..n).collect::<Vec<_>>());

let mut lang = Lang::<Fr, Sha256Coproc<Fr>>::new();
let mut lang = Lang::<Bn, Sha256Coproc<Bn>>::new();
lang.add_coprocessor(cproc_sym, Sha256Coprocessor::new(n));
let lang_rc = Arc::new(lang.clone());

Expand All @@ -80,7 +80,7 @@ fn main() {
let frames = evaluate(Some((&lurk_step, &cprocs, &lang)), call, store, 1000).unwrap();

let supernova_prover =
SuperNovaProver::<Fr, Sha256Coproc<Fr>>::new(REDUCTION_COUNT, lang_rc.clone());
SuperNovaProver::<Bn, Sha256Coproc<Bn>>::new(REDUCTION_COUNT, lang_rc.clone());

println!("Setting up running claim parameters (rc = {REDUCTION_COUNT})...");
let pp_start = Instant::now();
Expand Down
9 changes: 4 additions & 5 deletions examples/tp_table.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use anyhow::{anyhow, Result};
use ascii_table::{Align, AsciiTable};
use criterion::black_box;
use halo2curves::bn256::Fr as Bn;
use lurk::{
eval::lang::{Coproc, Lang},
lem::{eval::evaluate, store::Store},
proof::nova::{public_params, NovaProver, PublicParams},
};
use num_traits::ToPrimitive;
use pasta_curves::pallas::Scalar as Fr;
use statrs::statistics::Statistics;
use std::{cell::OnceCell, sync::Arc, time::Instant};

Expand Down Expand Up @@ -142,7 +142,6 @@ fn n_folds_env() -> Result<usize> {
/// │ 16 │ 59.38±0.83 │ 56.42±0.63 │
/// └────────────┴────────────┴────────────┘
/// ```
/// The first table
fn main() {
let rc_vec = rc_env().unwrap_or_else(|_| vec![100]);
let max_n_folds = n_folds_env().unwrap_or(3);
Expand All @@ -152,12 +151,12 @@ fn main() {

let limit = n_iters(max_n_folds, *max_rc);

let store = Store::<Fr>::default();
let store = Store::default();
let program = store.read_with_default_state(PROGRAM).unwrap();

let frames = evaluate::<Fr, Coproc<Fr>>(None, program, &store, limit).unwrap();
let frames = evaluate::<Bn, Coproc<Bn>>(None, program, &store, limit).unwrap();

let lang = Lang::<Fr>::new();
let lang = Lang::<Bn>::new();
let lang_arc = Arc::new(lang.clone());

let mut data = Vec::with_capacity(rc_vec.len());
Expand Down
8 changes: 6 additions & 2 deletions src/coprocessor/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ fn synthesize_sha256<F: LurkField, CS: ConstraintSystem<F>>(

let mut bits = vec![];

let pad_to_next_len_multiple_of_8 = |bits: &mut Vec<_>| {
bits.resize((bits.len() + 7) / 8 * 8, zero.clone());
};

for ptr in ptrs {
let tag_bits = ptr
.tag()
Expand All @@ -39,9 +43,9 @@ fn synthesize_sha256<F: LurkField, CS: ConstraintSystem<F>>(
.to_bits_le_strict(&mut cs.namespace(|| "preimage_hash_bits"))?;

bits.extend(tag_bits);
bits.push(zero.clone()); // need 256 bits (or some multiple of 8).
pad_to_next_len_multiple_of_8(&mut bits);
bits.extend(hash_bits);
bits.push(zero.clone()); // need 256 bits (or some multiple of 8).
pad_to_next_len_multiple_of_8(&mut bits);
}

bits.reverse();
Expand Down