Skip to content

Commit

Permalink
chore: migrate examples to the Bn256 field (#1095)
Browse files Browse the repository at this point in the history
* chore: migrate examples to the Bn256 field

* fix: properly pad sha256 input
  • Loading branch information
arthurpaulino authored Feb 7, 2024
1 parent 5fc5679 commit a8d7671
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
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

1 comment on commit a8d7671

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Fibonacci GPU benchmark.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
32 vCPUs
125 GB RAM
Workflow run: https://github.com/lurk-lab/lurk-rs/actions/runs/7819175208

Benchmark Results

LEM Fibonacci Prove - rc = 100

ref=5fc56791936257d229582512c7474d192ca612aa ref=a8d7671693305928b83958b8b5f7c4d0aaa1a1f4
num-100 1.47 s (✅ 1.00x) 1.46 s (✅ 1.01x faster)
num-200 2.82 s (✅ 1.00x) 2.78 s (✅ 1.01x faster)

LEM Fibonacci Prove - rc = 600

ref=5fc56791936257d229582512c7474d192ca612aa ref=a8d7671693305928b83958b8b5f7c4d0aaa1a1f4
num-100 1.83 s (✅ 1.00x) 1.82 s (✅ 1.00x faster)
num-200 3.05 s (✅ 1.00x) 3.05 s (✅ 1.00x slower)

Made with criterion-table

Please sign in to comment.