Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor public announcement #97

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[
{
"name": "tasm_neptune_transaction_removal_records_integrity",
"clock_cycle_count": 29578,
"hash_table_height": 5615,
"u32_table_height": 13250,
"clock_cycle_count": 29594,
"hash_table_height": 5621,
"u32_table_height": 13245,
"case": "CommonCase"
}
]
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[
{
"name": "tasm_neptune_transaction_transaction_kernel_mast_hash",
"clock_cycle_count": 7822,
"hash_table_height": 3631,
"u32_table_height": 74,
"clock_cycle_count": 7754,
"hash_table_height": 3601,
"u32_table_height": 73,
"case": "CommonCase"
},
{
"name": "tasm_neptune_transaction_transaction_kernel_mast_hash",
"clock_cycle_count": 8484,
"clock_cycle_count": 8486,
"hash_table_height": 3967,
"u32_table_height": 74,
"case": "WorstCase"
Expand Down
28 changes: 14 additions & 14 deletions profiles/tasm_neptune_transaction_removal_records_integrity.profile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tasm_neptune_transaction_removal_records_integrity:

# call graph
tasm_neptune_transaction_transaction_kernel_mast_hash: 4642
tasm_neptune_transaction_transaction_kernel_mast_hash: 4658
tasm_list_unsafeimplu32_new___digest: 36
tasm_memory_dyn_malloc: 25
tasm_list_unsafeimplu32_set_length___digest: 5
Expand All @@ -17,11 +17,11 @@ tasm_neptune_transaction_removal_records_integrity:
tasm_hashing_absorb_pad_varnum_zeros: 93
tasm_hashing_absorb_read_remainder: 14
tasm_list_unsafeimplu32_set_element___digest: 8
tasm_hashing_hash_varlen: 196
tasm_hashing_absorb: 182
tasm_hashing_absorb_hash_all_full_chunks: 41
tasm_hashing_absorb_pad_varnum_zeros: 82
tasm_hashing_absorb_read_remainder: 23
tasm_hashing_hash_varlen: 212
tasm_hashing_absorb: 198
tasm_hashing_absorb_hash_all_full_chunks: 53
tasm_hashing_absorb_pad_varnum_zeros: 104
tasm_hashing_absorb_read_remainder: 5
tasm_list_unsafeimplu32_set_element___digest: 8
tasm_hashing_hash_varlen: 156
tasm_hashing_absorb: 142
Expand Down Expand Up @@ -1486,18 +1486,18 @@ tasm_neptune_transaction_removal_records_integrity:
tasm_arithmetic_u64_eq: 6
tasm_list_unsafeimplu32_get_element___digest: 8
tasm_hashing_eq_digest: 15
total: 29578
total: 29594

# aggregated
tasm_neptune_transaction_transaction_kernel_mast_hash: 4642
tasm_neptune_transaction_transaction_kernel_mast_hash: 4658
tasm_list_unsafeimplu32_new___digest: 144
tasm_memory_dyn_malloc: 350
tasm_list_unsafeimplu32_set_length___digest: 5
tasm_hashing_hash_varlen: 6298
tasm_hashing_absorb: 6088
tasm_hashing_absorb_hash_all_full_chunks: 3987
tasm_hashing_absorb_pad_varnum_zeros: 1153
tasm_hashing_absorb_read_remainder: 408
tasm_hashing_hash_varlen: 6314
tasm_hashing_absorb: 6104
tasm_hashing_absorb_hash_all_full_chunks: 3999
tasm_hashing_absorb_pad_varnum_zeros: 1175
tasm_hashing_absorb_read_remainder: 390
tasm_list_unsafeimplu32_set_element___digest: 168
tasm_list_unsafeimplu32_get_element___digest: 1480
tasm_memory_push_ram_to_stack___digest: 50
Expand Down Expand Up @@ -1586,4 +1586,4 @@ tasm_neptune_transaction_removal_records_integrity:
tasm_arithmetic_u64_div2: 1764
tasm_hashing_swap_digest: 784
tasm_hashing_eq_digest: 30
total: 29578
total: 29594
10 changes: 4 additions & 6 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ fn make_coinbase_transaction(
let kernel = TransactionKernel {
inputs: vec![],
outputs: vec![coinbase_addition_record],
pubscript_hashes_and_inputs: vec![],
public_announcements: vec![],
fee: Amount::zero(),
timestamp,
coinbase: Some(coinbase_amount),
Expand All @@ -219,7 +219,7 @@ fn make_coinbase_transaction(
lock_script_witnesses: vec![],
input_membership_proofs: vec![],
output_utxos: vec![coinbase_utxo.clone()],
pubscripts: vec![],
public_announcements: vec![],
mutator_set_accumulator,
};
let validity_logic =
Expand Down Expand Up @@ -493,8 +493,7 @@ mod mine_loop_tests {
let four_neptune_coins = Amount::from(4).to_native_coins();
let receiver_privacy_digest = Digest::default();
let sender_randomness = Digest::default();
let pubscript: PubScript = PubScript::default();
let pubscript_input: Vec<BFieldElement> = vec![];
let public_announcement = PublicAnnouncement::default();
let tx_output = Utxo {
coins: four_neptune_coins,
lock_script_hash: LockScript::anyone_can_spend().hash(),
Expand All @@ -506,8 +505,7 @@ mod mine_loop_tests {
utxo: tx_output,
sender_randomness,
receiver_privacy_digest,
pubscript,
pubscript_input,
public_announcement,
}),
],
1.into(),
Expand Down
7 changes: 3 additions & 4 deletions src/models/blockchain/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Block {
outputs: vec![],
fee: 0u32.into(),
timestamp,
pubscript_hashes_and_inputs: vec![],
public_announcements: vec![],
coinbase: Some(total_premine_amount),
mutator_set_hash: MutatorSetAccumulator::<Hash>::new().hash(),
},
Expand Down Expand Up @@ -477,7 +477,7 @@ mod block_tests {
use crate::{
config_models::network::Network,
models::{
blockchain::transaction::PubScript, state::wallet::WalletSecret,
blockchain::transaction::PublicAnnouncement, state::wallet::WalletSecret,
state::UtxoReceiverData,
},
tests::shared::{get_mock_global_state, make_mock_block},
Expand Down Expand Up @@ -518,8 +518,7 @@ mod block_tests {
// create a new transaction, merge it into block 1 and check that block 1 is still valid
let new_utxo = Utxo::new_native_coin(other_address.lock_script(), 10.into());
let reciever_data = UtxoReceiverData {
pubscript: PubScript::default(),
pubscript_input: vec![],
public_announcement: PublicAnnouncement::default(),
receiver_privacy_digest: other_address.privacy_digest,
sender_randomness: random(),
utxo: new_utxo,
Expand Down
81 changes: 17 additions & 64 deletions src/models/blockchain/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ use std::cmp::max;
use std::hash::{Hash as StdHash, Hasher as StdHasher};
use std::time::SystemTime;
use tracing::{debug, error, warn};
use triton_vm::instruction::LabelledInstruction;
use triton_vm::program::Program;
use triton_vm::proof::Proof;
use triton_vm::{triton_asm, NonDeterminism, PublicInput};
use triton_vm::NonDeterminism;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::shared_math::bfield_codec::BFieldCodec;
use twenty_first::util_types::algebraic_hasher::AlgebraicHasher;
use twenty_first::util_types::emojihash_trait::Emojihash;

use self::amount::Amount;
use self::native_coin::native_coin_program;
use self::transaction_kernel::{PubScriptHashAndInput, TransactionKernel};
use self::transaction_kernel::TransactionKernel;
use self::utxo::{LockScript, TypeScript, Utxo};
use self::validity::TransactionValidationLogic;
use super::block::Block;
Expand All @@ -36,32 +34,14 @@ use crate::util_types::mutator_set::mutator_set_accumulator::MutatorSetAccumulat
use crate::util_types::mutator_set::mutator_set_trait::MutatorSet;
use crate::util_types::mutator_set::removal_record::RemovalRecord;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, GetSize, BFieldCodec)]
pub struct PubScript {
pub program: Program,
}

impl Default for PubScript {
fn default() -> Self {
Self {
program: Program::new(&triton_asm!(halt)),
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, GetSize, BFieldCodec, Default)]
pub struct PublicAnnouncement {
pub message: Vec<BFieldElement>,
}

impl From<Vec<LabelledInstruction>> for PubScript {
fn from(instrs: Vec<LabelledInstruction>) -> Self {
Self {
program: Program::new(&instrs),
}
}
}

impl From<&[LabelledInstruction]> for PubScript {
fn from(instrs: &[LabelledInstruction]) -> Self {
Self {
program: Program::new(instrs),
}
impl PublicAnnouncement {
pub fn new(message: Vec<BFieldElement>) -> Self {
Self { message }
}
}

Expand All @@ -75,7 +55,7 @@ pub struct PrimitiveWitness {
pub lock_script_witnesses: Vec<Vec<BFieldElement>>,
pub input_membership_proofs: Vec<MsMembershipProof<Hash>>,
pub output_utxos: Vec<Utxo>,
pub pubscripts: Vec<PubScript>,
pub public_announcements: Vec<PublicAnnouncement>,
pub mutator_set_accumulator: MutatorSetAccumulator<Hash>,
}

Expand Down Expand Up @@ -277,9 +257,9 @@ impl Transaction {
let merged_kernel = TransactionKernel {
inputs: [self.kernel.inputs, other.kernel.inputs].concat(),
outputs: [self.kernel.outputs, other.kernel.outputs].concat(),
pubscript_hashes_and_inputs: [
self.kernel.pubscript_hashes_and_inputs,
other.kernel.pubscript_hashes_and_inputs,
public_announcements: [
self.kernel.public_announcements,
other.kernel.public_announcements,
]
.concat(),
fee: self.kernel.fee + other.kernel.fee,
Expand Down Expand Up @@ -323,9 +303,9 @@ impl Transaction {
other_witness.output_utxos.clone(),
]
.concat(),
pubscripts: [
self_witness.pubscripts.clone(),
other_witness.pubscripts.clone(),
public_announcements: [
self_witness.public_announcements.clone(),
other_witness.public_announcements.clone(),
]
.concat(),
mutator_set_accumulator: self_witness.mutator_set_accumulator.clone(),
Expand Down Expand Up @@ -527,34 +507,7 @@ impl Transaction {
return false;
}

// verify pubscripts
for (
PubScriptHashAndInput {
pubscript_hash,
pubscript_input,
},
pubscript,
) in self
.kernel
.pubscript_hashes_and_inputs
.iter()
.zip(primitive_witness.pubscripts.iter())
{
if *pubscript_hash != Hash::hash(pubscript) {
return false;
}

let secret_input: Vec<BFieldElement> = vec![];

// The pubscript is satisfied if it halts gracefully without crashing.
if let Err(err) = pubscript.program.run(
PublicInput::new(pubscript_input.to_vec()),
NonDeterminism::new(secret_input),
) {
warn!("Could not verify pubscript for transaction; got err: \"{err}\".");
return false;
}
}
// in regards to public announcements: there isn't anything to verify

true
}
Expand All @@ -573,7 +526,7 @@ mod witness_tests {
lock_script_witnesses: vec![],
input_membership_proofs: vec![],
output_utxos: vec![],
pubscripts: vec![],
public_announcements: vec![],
mutator_set_accumulator: MutatorSetAccumulator::new(),
};

Expand Down
6 changes: 3 additions & 3 deletions src/models/blockchain/transaction/native_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn native_coin_reference(
// Kernel mast hash is the Merkle root whose leafs are
// - hash_varlen(input_sequence)
// - hash_varlen(output_sequence)
// - hash_varlen(pubscript_hashes_and_inputs_sequence)
// - hash_varlen(public_announcements_sequence)
// - hash_varlen(fee_sequence)
// - hash_varlen(coinbase_sequence)
// - hash_varlen(timestamp_sequence)
Expand All @@ -61,7 +61,7 @@ pub fn native_coin_reference(
*Vec::<Vec<BFieldElement>>::decode(&read_secret_input)?;
let input_sequence = &sequences[0];
let output_sequence = &sequences[1];
let pubscript_sequence = &sequences[2];
let public_announcements_sequence = &sequences[2];
let fee_sequence = &sequences[3];
let coinbase_sequence = &sequences[4];
let timestamp_sequence = &sequences[5];
Expand Down Expand Up @@ -114,7 +114,7 @@ pub fn native_coin_reference(
let leafs = [
Hash::hash_varlen(input_sequence),
Hash::hash_varlen(output_sequence),
Hash::hash_varlen(pubscript_sequence),
Hash::hash_varlen(public_announcements_sequence),
Hash::hash_varlen(fee_sequence),
Hash::hash_varlen(coinbase_sequence),
Hash::hash_varlen(timestamp_sequence),
Expand Down
Loading
Loading