From 25c90dda4b116ab7265b628d7461f644d8f62bdb Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Thu, 29 Apr 2021 16:14:16 -0600 Subject: [PATCH] Add fast generation for not-consensus-valid authorized bundles and actions. --- src/bundle.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++--- src/circuit.rs | 2 +- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/bundle.rs b/src/bundle.rs index faa092f4..9a9fa063 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -340,25 +340,30 @@ pub struct BundleAuthorizingCommitment; #[cfg(any(test, feature = "test-dependencies"))] pub mod testing { use nonempty::NonEmpty; + use rand_7::{rngs::StdRng, SeedableRng}; use proptest::collection::vec; use proptest::prelude::*; - //use pasta_curves::{pallas}; - use crate::{ + circuit::Proof, note::{ commitment::ExtractedNoteCommitment, nullifier::testing::arb_nullifier, testing::arb_note, TransmittedNoteCiphertext, }, - primitives::redpallas::testing::arb_spendauth_verification_key, + primitives::redpallas::{ + self, + testing::{ + arb_binding_signing_key, arb_spendauth_signing_key, arb_spendauth_verification_key, + }, + }, value::{ testing::arb_note_value, NoteValue, ValueCommitTrapdoor, ValueCommitment, ValueSum, }, Anchor, }; - use super::{Action, Authorization, Bundle, Flags}; + use super::{Action, Authorization, Authorized, Bundle, Flags}; /// Marker for an unauthorized bundle with no proofs or signatures. #[derive(Debug)] @@ -397,6 +402,41 @@ pub mod testing { } } + prop_compose! { + /// Generate an action with invalid (random) authorization data. + pub fn arb_action(value: NoteValue)( + nf in arb_nullifier(), + sk in arb_spendauth_signing_key(), + note in arb_note(value), + rng_seed in prop::array::uniform32(prop::num::u8::ANY), + fake_sighash in prop::array::uniform32(prop::num::u8::ANY), + ) -> Action> { + let cmx = ExtractedNoteCommitment::from(note.commitment()); + let cv_net = ValueCommitment::derive( + (note.value() - NoteValue::zero()).unwrap(), + ValueCommitTrapdoor::zero() + ); + + // FIXME: make a real one from the note. + let encrypted_note = TransmittedNoteCiphertext { + epk_bytes: [0u8; 32], + enc_ciphertext: [0u8; 580], + out_ciphertext: [0u8; 80] + }; + + let rng = StdRng::from_seed(rng_seed); + + Action { + nf, + rk: redpallas::VerificationKey::from(&sk), + cmx, + encrypted_note, + cv_net, + authorization: sk.sign(rng, &fake_sighash), + } + } + } + prop_compose! { /// Create an arbitrary set of flags. pub fn arb_flags()(spends_enabled in prop::bool::ANY, outputs_enabled in prop::bool::ANY) -> Flags { @@ -432,4 +472,41 @@ pub mod testing { ) } } + + prop_compose! { + /// Generate an arbitrary bundle with fake authorization data. This bundle does not + /// necessarily respect consensus rules; for that use + /// [`crate::builder::testing::arb_bundle`] + pub fn arb_bundle()( + acts in vec( + arb_note_value().prop_flat_map(|v| + arb_action(v).prop_map(move |a| (v, a)) + ), + 1..10 + ), + flags in arb_flags(), + anchor in prop::array::uniform32(prop::num::u8::ANY).prop_map(Anchor), + sk in arb_binding_signing_key(), + rng_seed in prop::array::uniform32(prop::num::u8::ANY), + fake_proof in vec(prop::num::u8::ANY, 1973), + fake_sighash in prop::array::uniform32(prop::num::u8::ANY), + ) -> Bundle { + let (values, actions): (Vec, Vec>>) = acts.into_iter().unzip(); + let rng = StdRng::from_seed(rng_seed); + + Bundle::from_parts( + NonEmpty::from_vec(actions).unwrap(), + flags, + values.into_iter().fold( + ValueSum::zero(), + |acc, cv| (acc + (cv - NoteValue::zero()).unwrap()).unwrap() + ), + anchor, + Authorized { + proof: Proof(fake_proof), + binding_signature: sk.sign(rng, &fake_sighash), + } + ) + } + } } diff --git a/src/circuit.rs b/src/circuit.rs index 0d0d5281..c4aa5960 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -118,7 +118,7 @@ impl Instance { /// /// [`Bundle`]: crate::bundle::Bundle #[derive(Debug)] -pub struct Proof(Vec); +pub struct Proof(pub(crate) Vec); impl<'a> From<&'a Proof> for &'a Vec { fn from(proof: &'a Proof) -> &'a Vec {