From ccada7e1a162d08d83db949880db17012d230973 Mon Sep 17 00:00:00 2001 From: moana Date: Fri, 9 Aug 2024 15:42:08 +0200 Subject: [PATCH] circuit: Fixes for #232 --- circuits/CHANGELOG.md | 2 ++ circuits/src/lib.rs | 11 +++++++---- circuits/tests/serialization.rs | 12 ++++++------ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/circuits/CHANGELOG.md b/circuits/CHANGELOG.md index a586b8a..00d7431 100644 --- a/circuits/CHANGELOG.md +++ b/circuits/CHANGELOG.md @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `Serializable` trait implementation for `OutputNoteInfo` [#232] - Add `Clone` and `PartialEq` derives for `TxCircuit` [#232] - Add `PartialEq` derive for `InputNoteInfo` [#232] +- Add associated const `TxCircuit::SIZE` +- Add associated const `InputNoteInfo::SIZE` - Add `PartialEq` derive for `OutputNoteInfo` [#232] - Add `dusk-bls12_381` dependency [#235] - Add `"plonk"` feature to add the `dusk-plonk` dependency [#235] diff --git a/circuits/src/lib.rs b/circuits/src/lib.rs index 2dae245..45c32b9 100644 --- a/circuits/src/lib.rs +++ b/circuits/src/lib.rs @@ -54,7 +54,8 @@ pub struct TxCircuit { } impl TxCircuit { - const SIZE: usize = I * InputNoteInfo::::SIZE + /// The size of a `TxCircuit`. + pub const SIZE: usize = I * InputNoteInfo::::SIZE + OUTPUT_NOTES * OutputNoteInfo::SIZE + 2 * BlsScalar::SIZE + 2 * u64::SIZE @@ -102,8 +103,9 @@ impl TxCircuit { } let mut input_notes_info = Vec::new(); - for _ in 0..I { - input_notes_info.push(InputNoteInfo::from_slice(bytes)?); + for i in 0..I { + let start = i * InputNoteInfo::::SIZE; + input_notes_info.push(InputNoteInfo::from_slice(&bytes[start..])?); } let mut reader = &bytes[I * InputNoteInfo::::SIZE..]; @@ -217,7 +219,8 @@ pub struct InputNoteInfo { } impl InputNoteInfo { - const SIZE: usize = (1 + H * ARITY) * Item::SIZE + /// The size of an `InputNoteInfo` + pub const SIZE: usize = (1 + H * ARITY) * Item::SIZE + H * (u32::BITS as usize / 8) + Note::SIZE + JubJubAffine::SIZE diff --git a/circuits/tests/serialization.rs b/circuits/tests/serialization.rs index 22a99bd..c7ba98c 100644 --- a/circuits/tests/serialization.rs +++ b/circuits/tests/serialization.rs @@ -39,12 +39,12 @@ fn tx_ciruit_1_2() -> Result<(), BytesError> { fn tx_ciruit_2_2() -> Result<(), BytesError> { let mut rng = StdRng::seed_from_u64(0xbeef); - let circuit = random_circuit::<1>(&mut rng); + let circuit = random_circuit::<2>(&mut rng); let circuit_bytes = circuit.to_var_bytes(); assert_eq!( circuit, - TxCircuit::::from_slice(&circuit_bytes[..])? + TxCircuit::::from_slice(&circuit_bytes[..])? ); Ok(()) @@ -54,12 +54,12 @@ fn tx_ciruit_2_2() -> Result<(), BytesError> { fn tx_ciruit_3_2() -> Result<(), BytesError> { let mut rng = StdRng::seed_from_u64(0xbeef); - let circuit = random_circuit::<1>(&mut rng); + let circuit = random_circuit::<3>(&mut rng); let circuit_bytes = circuit.to_var_bytes(); assert_eq!( circuit, - TxCircuit::::from_slice(&circuit_bytes[..])? + TxCircuit::::from_slice(&circuit_bytes[..])? ); Ok(()) @@ -69,12 +69,12 @@ fn tx_ciruit_3_2() -> Result<(), BytesError> { fn tx_ciruit_4_2() -> Result<(), BytesError> { let mut rng = StdRng::seed_from_u64(0xbeef); - let circuit = random_circuit::<1>(&mut rng); + let circuit = random_circuit::<4>(&mut rng); let circuit_bytes = circuit.to_var_bytes(); assert_eq!( circuit, - TxCircuit::::from_slice(&circuit_bytes[..])? + TxCircuit::::from_slice(&circuit_bytes[..])? ); Ok(())