From ee75928be46c1774b1a5ea04caabdf3c0564e394 Mon Sep 17 00:00:00 2001 From: Arthur Paulino Date: Wed, 1 Nov 2023 11:30:16 -0300 Subject: [PATCH] remove Alpha-specific examples, making LEM the norm (#826) --- examples/sha256_ivc.rs | 22 +++---- examples/sha256_ivc_lem.rs | 117 --------------------------------- examples/sha256_nivc.rs | 28 ++++---- examples/sha256_nivc_lem.rs | 127 ------------------------------------ tests/lurk-nivc-test.rs | 10 --- 5 files changed, 25 insertions(+), 279 deletions(-) delete mode 100644 examples/sha256_ivc_lem.rs delete mode 100644 examples/sha256_nivc_lem.rs diff --git a/examples/sha256_ivc.rs b/examples/sha256_ivc.rs index b5fd4ea28d..eaea353d6f 100644 --- a/examples/sha256_ivc.rs +++ b/examples/sha256_ivc.rs @@ -4,18 +4,16 @@ use tracing_subscriber::{fmt, prelude::*, EnvFilter, Registry}; use tracing_texray::TeXRayLayer; use lurk::{ - circuit::circuit_frame::MultiFrame, coprocessor::sha256::{Sha256Coproc, Sha256Coprocessor}, - eval::{empty_sym_env, lang::Lang}, + eval::lang::Lang, field::LurkField, + lem::{multiframe::MultiFrame, pointers::Ptr, store::Store}, proof::{nova::NovaProver, Prover}, - ptr::Ptr, public_parameters::{ instance::{Instance, Kind}, public_params, }, state::user_sym, - store::Store, }; const REDUCTION_COUNT: usize = 10; @@ -48,7 +46,7 @@ fn sha256_ivc(store: &Store, n: usize, input: &[usize]) -> Ptr< "# ); - store.read(&program).unwrap() + store.read_with_default_state(&program).unwrap() } /// Run the example in this file with @@ -62,17 +60,15 @@ fn main() { tracing::subscriber::set_global_default(subscriber).unwrap(); let args = std::env::args().collect::>(); - let n = args[1].parse().unwrap(); + let n = args.get(1).unwrap_or(&"1".into()).parse().unwrap(); - let store = &mut Store::::new(); + let store = &Store::::default(); let cproc_sym = user_sym(&format!("sha256_ivc_{n}")); let call = sha256_ivc(store, n, &(0..n).collect::>()); - let lang = Lang::>::new_with_bindings( - store, - vec![(cproc_sym, Sha256Coprocessor::new(n).into())], - ); + let mut lang = Lang::>::new(); + lang.add_coprocessor_lem(cproc_sym, Sha256Coprocessor::new(n), store); let lang_rc = Arc::new(lang.clone()); let nova_prover = @@ -97,7 +93,7 @@ fn main() { let (proof, z0, zi, num_steps) = tracing_texray::examine(tracing::info_span!("bang!")) .in_scope(|| { nova_prover - .evaluate_and_prove(&pp, call, empty_sym_env(store), store, 10000, &lang_rc) + .evaluate_and_prove(&pp, call, store.intern_nil(), store, 10000, &lang_rc) .unwrap() }); let proof_end = proof_start.elapsed(); @@ -114,7 +110,7 @@ fn main() { if res { println!( - "Congratulations! You proved and verified a SHA256 hash calculation in {:?} time!", + "Congratulations! You proved and verified a IVC SHA256 hash calculation in {:?} time!", pp_end + proof_end + verify_end ); } diff --git a/examples/sha256_ivc_lem.rs b/examples/sha256_ivc_lem.rs deleted file mode 100644 index 85df409d10..0000000000 --- a/examples/sha256_ivc_lem.rs +++ /dev/null @@ -1,117 +0,0 @@ -use pasta_curves::pallas::Scalar as Fr; -use std::{sync::Arc, time::Instant}; -use tracing_subscriber::{fmt, prelude::*, EnvFilter, Registry}; -use tracing_texray::TeXRayLayer; - -use lurk::{ - coprocessor::sha256::{Sha256Coproc, Sha256Coprocessor}, - eval::lang::Lang, - field::LurkField, - lem::{multiframe::MultiFrame, pointers::Ptr, store::Store}, - proof::{nova::NovaProver, Prover}, - public_parameters::{ - instance::{Instance, Kind}, - public_params, - }, - state::user_sym, -}; - -const REDUCTION_COUNT: usize = 10; - -fn sha256_ivc(store: &Store, n: usize, input: &[usize]) -> Ptr { - assert_eq!(n, input.len()); - let input = input - .iter() - .map(|i| i.to_string()) - .collect::>() - .join(" "); - let input = format!("'({input})"); - let program = format!( - r#" -(letrec ((encode-1 (lambda (term) - (let ((type (car term)) - (value (cdr term))) - (if (eq 'sha256 type) - (eval (cons 'sha256_ivc_{n} value)) - (if (eq 'lurk type) - (commit value) - (if (eq 'id type) - value)))))) - (encode (lambda (input) - (if input - (cons - (encode-1 (car input)) - (encode (cdr input))))))) - (encode '((sha256 . {input})))) -"# - ); - - store.read_with_default_state(&program).unwrap() -} - -/// Run the example in this file with -/// `cargo run --release --example sha256_ivc_lem ` -/// where `n` is the needed arity -fn main() { - let subscriber = Registry::default() - .with(fmt::layer().pretty()) - .with(EnvFilter::from_default_env()) - .with(TeXRayLayer::new()); - tracing::subscriber::set_global_default(subscriber).unwrap(); - - let args = std::env::args().collect::>(); - let n = args.get(1).unwrap_or(&"1".into()).parse().unwrap(); - - let store = &Store::::default(); - let cproc_sym = user_sym(&format!("sha256_ivc_{n}")); - - let call = sha256_ivc(store, n, &(0..n).collect::>()); - - let mut lang = Lang::>::new(); - lang.add_coprocessor_lem(cproc_sym, Sha256Coprocessor::new(n), store); - let lang_rc = Arc::new(lang.clone()); - - let nova_prover = - NovaProver::, MultiFrame<'_, _, _>>::new(REDUCTION_COUNT, lang); - - println!("Setting up public parameters (rc = {REDUCTION_COUNT})..."); - - let pp_start = Instant::now(); - let instance = Instance::new( - REDUCTION_COUNT, - lang_rc.clone(), - true, - Kind::NovaPublicParams, - ); - // see the documentation on `with_public_params` - let pp = public_params(&instance).unwrap(); - let pp_end = pp_start.elapsed(); - println!("Public parameters took {:?}", pp_end); - - println!("Beginning proof step..."); - let proof_start = Instant::now(); - let (proof, z0, zi, num_steps) = tracing_texray::examine(tracing::info_span!("bang!")) - .in_scope(|| { - nova_prover - .evaluate_and_prove(&pp, call, store.intern_nil(), store, 10000, &lang_rc) - .unwrap() - }); - let proof_end = proof_start.elapsed(); - - println!("Proofs took {:?}", proof_end); - - println!("Verifying proof..."); - - let verify_start = Instant::now(); - let res = proof.verify(&pp, num_steps, &z0, &zi).unwrap(); - let verify_end = verify_start.elapsed(); - - println!("Verify took {:?}", verify_end); - - if res { - println!( - "Congratulations! You proved and verified a LEM IVC SHA256 hash calculation in {:?} time!", - pp_end + proof_end + verify_end - ); - } -} diff --git a/examples/sha256_nivc.rs b/examples/sha256_nivc.rs index d259def011..b764c0ddfd 100644 --- a/examples/sha256_nivc.rs +++ b/examples/sha256_nivc.rs @@ -4,23 +4,26 @@ use tracing_subscriber::{fmt, prelude::*, EnvFilter, Registry}; use tracing_texray::TeXRayLayer; use lurk::{ - circuit::circuit_frame::MultiFrame, coprocessor::sha256::{Sha256Coproc, Sha256Coprocessor}, - eval::{empty_sym_env, lang::Lang}, + eval::lang::Lang, field::LurkField, + lem::{ + eval::{evaluate, make_eval_step_from_lang}, + multiframe::MultiFrame, + pointers::Ptr, + store::Store, + }, proof::{supernova::SuperNovaProver, Prover}, - ptr::Ptr, public_parameters::{ instance::{Instance, Kind}, supernova_public_params, }, state::user_sym, - store::Store, }; const REDUCTION_COUNT: usize = 10; -fn sha256_nivc(store: &mut Store, n: usize, input: &[usize]) -> Ptr { +fn sha256_nivc(store: &Store, n: usize, input: &[usize]) -> Ptr { assert_eq!(n, input.len()); let input = input .iter() @@ -48,7 +51,7 @@ fn sha256_nivc(store: &mut Store, n: usize, input: &[usize]) -> "# ); - store.read(&program).unwrap() + store.read_with_default_state(&program).unwrap() } /// Run the example in this file with @@ -64,17 +67,18 @@ fn main() { let args = std::env::args().collect::>(); let n = args.get(1).unwrap_or(&"1".into()).parse().unwrap(); - let store = &mut Store::::new(); + let store = &Store::::default(); let cproc_sym = user_sym(&format!("sha256_nivc_{n}")); let call = sha256_nivc(store, n, &(0..n).collect::>()); - let lang = Lang::>::new_with_bindings( - store, - vec![(cproc_sym, Sha256Coprocessor::new(n).into())], - ); + let mut lang = Lang::>::new(); + lang.add_coprocessor_lem(cproc_sym, Sha256Coprocessor::new(n), store); let lang_rc = Arc::new(lang.clone()); + let lurk_step = make_eval_step_from_lang(&lang, false); + let (frames, _) = evaluate(Some((&lurk_step, &lang)), call, store, 1000).unwrap(); + let supernova_prover = SuperNovaProver::, MultiFrame<'_, _, _>>::new(REDUCTION_COUNT, lang); @@ -97,7 +101,7 @@ fn main() { let (proof, z0, zi, num_steps, last_circuit_index) = tracing_texray::examine(tracing::info_span!("bang!")).in_scope(|| { supernova_prover - .evaluate_and_prove(&pp, call, empty_sym_env(store), store, 10000, lang_rc) + .prove(&pp, &frames, store, lang_rc) .unwrap() }); let proof_end = proof_start.elapsed(); diff --git a/examples/sha256_nivc_lem.rs b/examples/sha256_nivc_lem.rs deleted file mode 100644 index c6fa5b1a1b..0000000000 --- a/examples/sha256_nivc_lem.rs +++ /dev/null @@ -1,127 +0,0 @@ -use pasta_curves::pallas::Scalar as Fr; -use std::{sync::Arc, time::Instant}; -use tracing_subscriber::{fmt, prelude::*, EnvFilter, Registry}; -use tracing_texray::TeXRayLayer; - -use lurk::{ - coprocessor::sha256::{Sha256Coproc, Sha256Coprocessor}, - eval::lang::Lang, - field::LurkField, - lem::{ - eval::{evaluate, make_eval_step_from_lang}, - multiframe::MultiFrame, - pointers::Ptr, - store::Store, - }, - proof::{supernova::SuperNovaProver, Prover}, - public_parameters::{ - instance::{Instance, Kind}, - supernova_public_params, - }, - state::user_sym, -}; - -const REDUCTION_COUNT: usize = 10; - -fn sha256_nivc(store: &Store, n: usize, input: &[usize]) -> Ptr { - assert_eq!(n, input.len()); - let input = input - .iter() - .map(|i| i.to_string()) - .collect::>() - .join(" "); - let input = format!("({})", input); - let program = format!( - r#" -(letrec ((encode-1 (lambda (term) - (let ((type (car term)) - (value (cdr term))) - (if (eq 'sha256 type) - (eval (cons 'sha256_nivc_{n} value)) - (if (eq 'lurk type) - (commit value) - (if (eq 'id type) - value)))))) - (encode (lambda (input) - (if input - (cons - (encode-1 (car input)) - (encode (cdr input))))))) - (encode '((sha256 . {input})))) -"# - ); - - store.read_with_default_state(&program).unwrap() -} - -/// Run the example in this file with -/// `cargo run --release --example sha256_nivc_lem ` -/// where `n` is the needed arity (default is 1) -fn main() { - let subscriber = Registry::default() - .with(fmt::layer().pretty()) - .with(EnvFilter::from_default_env()) - .with(TeXRayLayer::new()); - tracing::subscriber::set_global_default(subscriber).unwrap(); - - let args = std::env::args().collect::>(); - let n = args.get(1).unwrap_or(&"1".into()).parse().unwrap(); - - let store = &Store::::default(); - let cproc_sym = user_sym(&format!("sha256_nivc_{n}")); - - let call = sha256_nivc(store, n, &(0..n).collect::>()); - - let mut lang = Lang::>::new(); - lang.add_coprocessor_lem(cproc_sym, Sha256Coprocessor::new(n), store); - let lang_rc = Arc::new(lang.clone()); - - let lurk_step = make_eval_step_from_lang(&lang, false); - let (frames, _) = evaluate(Some((&lurk_step, &lang)), call, store, 1000).unwrap(); - - let supernova_prover = - SuperNovaProver::, MultiFrame<'_, _, _>>::new(REDUCTION_COUNT, lang); - - println!("Setting up running claim parameters (rc = {REDUCTION_COUNT})..."); - let pp_start = Instant::now(); - - let instance_primary = Instance::new( - REDUCTION_COUNT, - lang_rc.clone(), - true, - Kind::SuperNovaAuxParams, - ); - let pp = supernova_public_params::<_, _, MultiFrame<'_, _, _>>(&instance_primary).unwrap(); - - let pp_end = pp_start.elapsed(); - println!("Running claim parameters took {:?}", pp_end); - - println!("Beginning proof step..."); - let proof_start = Instant::now(); - let (proof, z0, zi, num_steps, last_circuit_index) = - tracing_texray::examine(tracing::info_span!("bang!")).in_scope(|| { - supernova_prover - .prove(&pp, &frames, store, lang_rc) - .unwrap() - }); - let proof_end = proof_start.elapsed(); - - println!("Proofs took {:?}", proof_end); - - println!("Verifying proof..."); - - let verify_start = Instant::now(); - let res = proof - .verify(&pp, last_circuit_index, num_steps, &z0, &zi) - .unwrap(); - let verify_end = verify_start.elapsed(); - - println!("Verify took {:?}", verify_end); - - if res { - println!( - "Congratulations! You proved and verified a LEM NIVC SHA256 hash calculation in {:?} time!", - pp_end + proof_end + verify_end - ); - } -} diff --git a/tests/lurk-nivc-test.rs b/tests/lurk-nivc-test.rs index f6cd24380a..83112d2615 100644 --- a/tests/lurk-nivc-test.rs +++ b/tests/lurk-nivc-test.rs @@ -10,13 +10,3 @@ fn test_sha256_nivc() { cmd.args(["run", "--release", "--example", "sha256_nivc"]); cmd.assert().success(); } - -/// TODO: replace this test for more granular ones, specific for the NIVC -/// pipeline steps -#[test] -#[ignore] -fn test_sha256_nivc_lem() { - let mut cmd = Command::new("cargo"); - cmd.args(["run", "--release", "--example", "sha256_nivc_lem"]); - cmd.assert().success(); -}