Skip to content

Commit 770e7c4

Browse files
committed
Refactor tests for proof generation
Move all binaries into `rampup4`, and avoid creating multiple provers
1 parent 4236dd8 commit 770e7c4

File tree

1 file changed

+51
-44
lines changed

1 file changed

+51
-44
lines changed

ledger/src/proofs/transaction.rs

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3906,6 +3906,7 @@ pub fn create_proof<C: ProofConstants, F: FieldWitness>(
39063906
proof
39073907
}
39083908

3909+
#[derive(Clone)]
39093910
pub struct Prover<F: FieldWitness> {
39103911
/// Constants to each kind of proof
39113912
pub internal_vars: InternalVars<F>,
@@ -4048,6 +4049,7 @@ mod tests {
40484049
self,
40494050
macros::{BinProtRead, BinProtWrite},
40504051
};
4052+
use once_cell::sync::Lazy;
40514053

40524054
use crate::{
40534055
proofs::{
@@ -4285,11 +4287,14 @@ mod tests {
42854287

42864288
fn read_gates() -> Gates {
42874289
let base_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
4290+
let base_dir = base_dir.join("rampup4");
42884291

4289-
fn read_gates_file<F: FieldWitness>(filepath: &impl AsRef<Path>) -> Vec<CircuitGate<F>> {
4290-
let file = std::fs::File::open(filepath).unwrap();
4292+
fn read_gates_file<F: FieldWitness>(
4293+
filepath: &impl AsRef<Path>,
4294+
) -> std::io::Result<Vec<CircuitGate<F>>> {
4295+
let file = std::fs::File::open(filepath)?;
42914296
let reader = std::io::BufReader::new(file);
4292-
serde_json::from_reader(reader).unwrap()
4297+
serde_json::from_reader(reader).map_err(Into::into)
42934298
}
42944299

42954300
let internal_vars_path = base_dir.join("internal_vars_rampup4.bin");
@@ -4302,63 +4307,55 @@ mod tests {
43024307
let (internal_vars_wrap, rows_rev_wrap) =
43034308
read_constraints_data::<Fq>(&internal_vars_path, &rows_rev_path).unwrap();
43044309

4305-
let internal_vars_path = base_dir.join("rampup4").join("merge_internal_vars.bin");
4306-
let rows_rev_path = base_dir.join("rampup4").join("merge_rows_rev.bin");
4310+
let internal_vars_path = base_dir.join("merge_internal_vars.bin");
4311+
let rows_rev_path = base_dir.join("merge_rows_rev.bin");
43074312
let (merge_internal_vars, merge_rows_rev) =
43084313
read_constraints_data::<Fp>(&internal_vars_path, &rows_rev_path).unwrap();
43094314

4310-
let internal_vars_path = base_dir.join("rampup4").join("block_internal_vars.bin");
4311-
let rows_rev_path = base_dir.join("rampup4").join("block_rows_rev.bin");
4315+
let internal_vars_path = base_dir.join("block_internal_vars.bin");
4316+
let rows_rev_path = base_dir.join("block_rows_rev.bin");
43124317
let (block_internal_vars, block_rows_rev) =
43134318
read_constraints_data::<Fp>(&internal_vars_path, &rows_rev_path).unwrap();
43144319

4315-
let internal_vars_path = base_dir
4316-
.join("rampup4")
4317-
.join("block_wrap_internal_vars.bin");
4318-
let rows_rev_path = base_dir.join("rampup4").join("block_wrap_rows_rev.bin");
4320+
let internal_vars_path = base_dir.join("block_wrap_internal_vars.bin");
4321+
let rows_rev_path = base_dir.join("block_wrap_rows_rev.bin");
43194322
let (block_wrap_internal_vars, block_wrap_rows_rev) =
43204323
read_constraints_data::<Fq>(&internal_vars_path, &rows_rev_path).unwrap();
43214324

4322-
let internal_vars_path = base_dir
4323-
.join("rampup4")
4324-
.join("zkapp_step_internal_vars.bin");
4325-
let rows_rev_path = base_dir.join("rampup4").join("zkapp_step_rows_rev.bin");
4325+
let internal_vars_path = base_dir.join("zkapp_step_internal_vars.bin");
4326+
let rows_rev_path = base_dir.join("zkapp_step_rows_rev.bin");
43264327
let (
43274328
zkapp_step_opt_signed_opt_signed_internal_vars,
43284329
zkapp_step_opt_signed_opt_signed_rows_rev,
43294330
) = read_constraints_data::<Fp>(&internal_vars_path, &rows_rev_path).unwrap();
43304331

4331-
let internal_vars_path = base_dir
4332-
.join("rampup4")
4333-
.join("zkapp_step_opt_signed_internal_vars.bin");
4334-
let rows_rev_path = base_dir
4335-
.join("rampup4")
4336-
.join("zkapp_step_opt_signed_rows_rev.bin");
4332+
let internal_vars_path = base_dir.join("zkapp_step_opt_signed_internal_vars.bin");
4333+
let rows_rev_path = base_dir.join("zkapp_step_opt_signed_rows_rev.bin");
43374334
let (zkapp_step_opt_signed_internal_vars, zkapp_step_opt_signed_rows_rev) =
43384335
read_constraints_data::<Fp>(&internal_vars_path, &rows_rev_path).unwrap();
43394336

43404337
let gates: Vec<CircuitGate<Fp>> =
4341-
read_gates_file(&base_dir.join("gates_step_rampup4.json"));
4338+
read_gates_file(&base_dir.join("gates_step_rampup4.json")).unwrap();
43424339
let wrap_gates: Vec<CircuitGate<Fq>> =
4343-
read_gates_file(&base_dir.join("gates_wrap_rampup4.json"));
4340+
read_gates_file(&base_dir.join("gates_wrap_rampup4.json")).unwrap();
43444341
let merge_gates: Vec<CircuitGate<Fp>> =
4345-
read_gates_file(&base_dir.join("gates_merge_rampup4.json"));
4342+
read_gates_file(&base_dir.join("gates_merge_rampup4.json")).unwrap();
43464343

4347-
let base_dir = base_dir.join("rampup4");
4348-
let block_gates: Vec<CircuitGate<Fp>> = read_gates_file(&base_dir.join("block_gates.json"));
4344+
let block_gates: Vec<CircuitGate<Fp>> =
4345+
read_gates_file(&base_dir.join("block_gates.json")).unwrap();
43494346
let block_wrap_gates: Vec<CircuitGate<Fq>> =
4350-
read_gates_file(&base_dir.join("block_wrap_gates.json"));
4347+
read_gates_file(&base_dir.join("block_wrap_gates.json")).unwrap();
43514348
let zkapp_step_opt_signed_opt_signed_gates: Vec<CircuitGate<Fp>> =
4352-
read_gates_file(&base_dir.join("zkapp_step_gates.json"));
4349+
read_gates_file(&base_dir.join("zkapp_step_gates.json")).unwrap();
43534350
let zkapp_step_opt_signed_gates: Vec<CircuitGate<Fp>> =
4354-
read_gates_file(&base_dir.join("zkapp_step_opt_signed_gates.json"));
4351+
read_gates_file(&base_dir.join("zkapp_step_opt_signed_gates.json")).unwrap();
43554352

43564353
let internal_vars_path = base_dir.join("zkapp_step_proof_internal_vars.bin");
43574354
let rows_rev_path = base_dir.join("zkapp_step_proof_rows_rev.bin");
43584355
let (zkapp_step_proof_internal_vars, zkapp_step_proof_rows_rev) =
43594356
read_constraints_data::<Fp>(&internal_vars_path, &rows_rev_path).unwrap();
43604357
let zkapp_step_proof_gates: Vec<CircuitGate<Fp>> =
4361-
read_gates_file(&base_dir.join("zkapp_step_proof_gates.json"));
4358+
read_gates_file(&base_dir.join("zkapp_step_proof_gates.json")).unwrap();
43624359

43634360
Gates {
43644361
gates,
@@ -4388,6 +4385,9 @@ mod tests {
43884385
}
43894386
}
43904387

4388+
static PROVERS: Lazy<Arc<Provers>> = Lazy::new(|| Arc::new(make_provers()));
4389+
4390+
#[derive(Clone)]
43914391
struct Provers {
43924392
tx_step_prover: Prover<Fp>,
43934393
tx_wrap_prover: Prover<Fq>,
@@ -4399,6 +4399,11 @@ mod tests {
43994399
zkapp_step_proof_prover: Prover<Fp>,
44004400
}
44014401

4402+
fn get_provers() -> Provers {
4403+
(&**PROVERS).clone()
4404+
}
4405+
4406+
/// Slow, use `get_provers` instead
44024407
fn make_provers() -> Provers {
44034408
let Gates {
44044409
gates,
@@ -4426,6 +4431,7 @@ mod tests {
44264431
zkapp_step_proof_internal_vars,
44274432
zkapp_step_proof_rows_rev,
44284433
} = read_gates();
4434+
44294435
let tx_prover_index = make_prover_index::<StepTransactionProof, _>(gates);
44304436
let merge_prover_index = make_prover_index::<StepMergeProof, _>(merge_gates);
44314437
let wrap_prover_index = make_prover_index::<WrapTransactionProof, _>(wrap_gates);
@@ -4512,7 +4518,8 @@ mod tests {
45124518
zkapp_step_opt_signed_opt_signed_prover,
45134519
zkapp_step_opt_signed_prover,
45144520
zkapp_step_proof_prover,
4515-
} = make_provers();
4521+
} = get_provers();
4522+
45164523
let v = &tx_wrap_prover.index.verifier_index.as_ref().unwrap();
45174524
let v_json = serde_json::to_string(&v).unwrap();
45184525
// std::fs::write("/tmp/tx.json", &v_json).unwrap();
@@ -4555,14 +4562,14 @@ mod tests {
45554562
zkapp_step_opt_signed_opt_signed_prover: _,
45564563
zkapp_step_opt_signed_prover: _,
45574564
zkapp_step_proof_prover: _,
4558-
} = make_provers();
4565+
} = get_provers();
45594566

45604567
let mut witnesses: Witness<Fp> = Witness::new::<StepTransactionProof>();
45614568

45624569
fn read_witnesses(filename: &str) -> Vec<Fp> {
45634570
let f = std::fs::read_to_string(
45644571
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
4565-
// .join("rampup4")
4572+
.join("rampup4")
45664573
.join(filename),
45674574
)
45684575
.unwrap();
@@ -4617,7 +4624,7 @@ mod tests {
46174624
zkapp_step_opt_signed_opt_signed_prover: _,
46184625
zkapp_step_opt_signed_prover: _,
46194626
zkapp_step_proof_prover: _,
4620-
} = make_provers();
4627+
} = get_provers();
46214628

46224629
let mut witnesses: Witness<Fp> = Witness::new::<StepMergeProof>();
46234630
witnesses.ocaml_aux = crate::proofs::merge::read_witnesses().unwrap();
@@ -4667,7 +4674,7 @@ mod tests {
46674674
zkapp_step_opt_signed_opt_signed_prover,
46684675
zkapp_step_opt_signed_prover,
46694676
zkapp_step_proof_prover,
4670-
} = make_provers();
4677+
} = get_provers();
46714678

46724679
let LedgerProof { proof, .. } = generate_zkapp_proof(ZkappParams {
46734680
statement: &statement,
@@ -4713,7 +4720,7 @@ mod tests {
47134720
zkapp_step_opt_signed_opt_signed_prover,
47144721
zkapp_step_opt_signed_prover,
47154722
zkapp_step_proof_prover,
4716-
} = make_provers();
4723+
} = get_provers();
47174724

47184725
let LedgerProof { proof, .. } = generate_zkapp_proof(ZkappParams {
47194726
statement: &statement,
@@ -4775,7 +4782,7 @@ mod tests {
47754782
zkapp_step_opt_signed_opt_signed_prover: _,
47764783
zkapp_step_opt_signed_prover: _,
47774784
zkapp_step_proof_prover: _,
4778-
} = make_provers();
4785+
} = get_provers();
47794786
let mut witnesses: Witness<Fp> = Witness::new::<StepBlockProof>();
47804787

47814788
let WrapProof { proof, .. } = generate_block_proof(
@@ -4805,6 +4812,11 @@ mod tests {
48054812
fn test_proofs() {
48064813
let base_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("rampup4");
48074814

4815+
if !base_dir.exists() {
4816+
eprintln!("{:?} not found", base_dir);
4817+
return;
4818+
}
4819+
48084820
let Provers {
48094821
tx_step_prover,
48104822
tx_wrap_prover,
@@ -4814,7 +4826,7 @@ mod tests {
48144826
zkapp_step_opt_signed_opt_signed_prover,
48154827
zkapp_step_opt_signed_prover,
48164828
zkapp_step_proof_prover,
4817-
} = make_provers();
4829+
} = get_provers();
48184830

48194831
#[rustfmt::skip]
48204832
let zkapp_cases = [
@@ -4913,11 +4925,6 @@ mod tests {
49134925
);
49144926
}
49154927

4916-
if !base_dir.exists() {
4917-
eprintln!("{:?} not found", base_dir);
4918-
return;
4919-
}
4920-
49214928
// Same values than OCaml
49224929
#[rustfmt::skip]
49234930
let requests = [
@@ -4942,7 +4949,7 @@ mod tests {
49424949
// ("fee_transfer_6_rampup4.bin", "6b95aa737e1c8351bbb7a141108a73c808cb92aae9e266ecce13f679d6f6b2df"),
49434950
// ("fee_transfer_7_rampup4.bin", "5d97141c3adf576503381e485f5ab20ed856448880658a0a56fb23567225875c"),
49444951
// ("fee_transfer_8_rampup4.bin", "e1fa6b5a88b184428a0918cd4bd56952b54f05a5dc175b17e154204533167a78"),
4945-
("fee_transfer_9_rampup4.bin", "087a07eddedf5de18b2f2bd7ded3cd474d00a0030e9c13d7a5fd2433c72fc7d5"),
4952+
// ("fee_transfer_9_rampup4.bin", "087a07eddedf5de18b2f2bd7ded3cd474d00a0030e9c13d7a5fd2433c72fc7d5"),
49464953
];
49474954

49484955
fn read_witnesses(filename: &str) -> Vec<Fp> {

0 commit comments

Comments
 (0)