Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
therustmonk committed Jun 28, 2022
1 parent 955e17f commit b4cd57a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 56 deletions.
3 changes: 1 addition & 2 deletions applications/test_faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ fn create_utxo(
if !factories.range_proof.verify(&proof, &commitment) {
panic!("Range proof does not verify");
};
let encryption_keys = generate_keys();
let encrypted_value = EncryptedValue::encrypt_value(&encryption_keys.k, &commitment, value).unwrap();
let encrypted_value = EncryptedValue::default();
let metadata_sig = TransactionOutput::create_final_metadata_signature(
TransactionOutputVersion::get_current_version(),
value,
Expand Down
11 changes: 4 additions & 7 deletions base_layer/core/src/transactions/coinbase_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,16 @@ mod test {
.with_fees(145 * uT)
.with_nonce(p.nonce.clone())
.with_spend_key(p.spend_key.clone())
.with_rewind_data(rewind_data);
.with_rewind_data(rewind_data.clone());
let (tx, _) = builder
.build(rules.consensus_constants(42), rules.emission_schedule())
.unwrap();
let block_reward = rules.emission_schedule().block_reward(42) + 145 * uT;

let output = &tx.body.outputs()[0];
let committed_value = EncryptedValue::decrypt_value(
&p.rewind_data.encryption_key,
&output.commitment,
&output.encrypted_value,
)
.unwrap();
let committed_value =
EncryptedValue::decrypt_value(&rewind_data.encryption_key, &output.commitment, &output.encrypted_value)
.unwrap();
assert_eq!(committed_value, block_reward);
let blinding_factor = output
.recover_mask(&factories.range_proof, &rewind_blinding_key)
Expand Down
16 changes: 10 additions & 6 deletions base_layer/core/src/transactions/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ pub struct TestParams {
pub sender_private_commitment_nonce: PrivateKey,
pub sender_public_commitment_nonce: PublicKey,
pub commitment_factory: CommitmentFactory,
pub encryption_key: PrivateKey,
pub transaction_weight: TransactionWeight,
pub rewind_data: RewindData,
}
Expand Down Expand Up @@ -151,7 +150,6 @@ impl TestParams {
sender_private_commitment_nonce: sender_sig_pvt_nonce.clone(),
sender_public_commitment_nonce: PublicKey::from_secret_key(&sender_sig_pvt_nonce),
commitment_factory: CommitmentFactory::default(),
encryption_key: PrivateKey::random(&mut OsRng),
transaction_weight: TransactionWeight::v2(),
rewind_data: RewindData {
rewind_blinding_key: PrivateKey::random(&mut OsRng),
Expand Down Expand Up @@ -180,7 +178,8 @@ impl TestParams {
let updated_features =
OutputFeatures::features_with_updated_recovery_byte(&commitment, rewind_data, &params.features);

let encrypted_value = EncryptedValue::encrypt_value(&self.encryption_key, &commitment, params.value).unwrap();
let encrypted_value =
EncryptedValue::encrypt_value(&self.rewind_data.encryption_key, &commitment, params.value).unwrap();
let metadata_signature = TransactionOutput::create_final_metadata_signature(
TransactionOutputVersion::get_current_version(),
params.value,
Expand Down Expand Up @@ -681,6 +680,7 @@ pub fn create_stx_protocol(schema: TransactionSchema) -> (SenderTransactionProto
.with_offset(test_params_change_and_txn.offset.clone())
.with_private_nonce(test_params_change_and_txn.nonce.clone())
.with_change_secret(test_params_change_and_txn.change_spend_key.clone())
.with_rewindable_outputs(test_params_change_and_txn.rewind_data.clone())
.with_change_script(
script!(Nop),
inputs!(PublicKey::from_secret_key(
Expand All @@ -702,7 +702,7 @@ pub fn create_stx_protocol(schema: TransactionSchema) -> (SenderTransactionProto
let mut outputs = Vec::with_capacity(schema.to.len());
for val in schema.to {
let test_params = TestParams::new();
let utxo = test_params.create_unblinded_output(UtxoTestParams {
let utxo = test_params.create_unblinded_output_with_rewind_data(UtxoTestParams {
value: val,
features: schema.features.clone(),
script: schema.script.clone(),
Expand Down Expand Up @@ -756,8 +756,12 @@ pub fn create_stx_protocol(schema: TransactionSchema) -> (SenderTransactionProto
..Default::default()
};

let encrypted_value =
EncryptedValue::encrypt_value(&test_params_change_and_txn.encryption_key, &commitment, change).unwrap();
let encrypted_value = EncryptedValue::encrypt_value(
&test_params_change_and_txn.rewind_data.encryption_key,
&commitment,
change,
)
.unwrap();

let change_metadata_sig = TransactionOutput::create_final_metadata_signature(
output_version,
Expand Down
83 changes: 47 additions & 36 deletions base_layer/core/src/transactions/transaction_protocol/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,6 @@ mod test {
transaction_protocol::{
sender::{SenderTransactionProtocol, TransactionSenderMessage},
single_receiver::SingleReceiverTransactionProtocol,
RewindData,
TransactionProtocolError,
TransactionProtocolError::RangeProofError,
},
Expand Down Expand Up @@ -1240,31 +1239,22 @@ mod test {
fn single_recipient_with_rewindable_change_and_receiver_outputs_bulletproofs() {
let factories = CryptoFactories::default();
// Alice's parameters
let a = TestParams::new();
let alice_test_params = TestParams::new();
// Bob's parameters
let b = TestParams::new();
let bob_test_params = TestParams::new();
let alice_value = MicroTari(25000);
let (utxo, input) = create_test_input(alice_value, 0, &factories.commitment);

// Rewind params
let rewind_blinding_key = PrivateKey::random(&mut OsRng);

let rewind_data = RewindData {
rewind_blinding_key: rewind_blinding_key.clone(),
recovery_byte_key: PrivateKey::random(&mut OsRng),
encryption_key: PrivateKey::random(&mut OsRng),
};

let script = script!(Nop);

let mut builder = SenderTransactionProtocol::builder(1, create_consensus_constants(0));
builder
.with_lock_height(0)
.with_fee_per_gram(MicroTari(20))
.with_offset(a.offset.clone())
.with_private_nonce(a.nonce.clone())
.with_change_secret(a.change_spend_key.clone())
.with_rewindable_outputs(rewind_data)
.with_offset(alice_test_params.offset.clone())
.with_private_nonce(alice_test_params.nonce.clone())
.with_change_secret(alice_test_params.change_spend_key.clone())
.with_rewindable_outputs(alice_test_params.rewind_data.clone())
.with_input(utxo, input)
.with_amount(0, MicroTari(5000))
.with_recipient_data(
Expand Down Expand Up @@ -1295,7 +1285,14 @@ mod test {
assert!(alice.is_collecting_single_signature());

// Receiver gets message, deserializes it etc, and creates his response
let bob_info = SingleReceiverTransactionProtocol::create(&msg, b.nonce, b.spend_key, &factories, None).unwrap();
let bob_info = SingleReceiverTransactionProtocol::create(
&msg,
bob_test_params.nonce,
bob_test_params.spend_key,
&factories,
None,
)
.unwrap();

// Alice gets message back, deserializes it, etc
alice
Expand All @@ -1317,28 +1314,42 @@ mod test {
let output_0 = &tx.body.outputs()[0];
let output_1 = &tx.body.outputs()[1];

let committed_value_0 = EncryptedValue::decrypt_value(
&a.rewind_data.encryption_key,
if let Ok(committed_value) = EncryptedValue::decrypt_value(
&alice_test_params.rewind_data.encryption_key,
&output_0.commitment,
&output_0.encrypted_value,
)
.unwrap();
let committed_value_1 = EncryptedValue::decrypt_value(
&a.rewind_data.encryption_key,
) {
let blinding_factor = output_0
.recover_mask(
&factories.range_proof,
&alice_test_params.rewind_data.rewind_blinding_key,
)
.unwrap();
assert_eq!(
factories
.commitment
.commit_value(&blinding_factor, committed_value.as_u64()),
output_0.commitment
);
} else if let Ok(committed_value) = EncryptedValue::decrypt_value(
&alice_test_params.rewind_data.encryption_key,
&output_1.commitment,
&output_1.encrypted_value,
)
.unwrap();
// TODO: Fix this logic when 'encrypted_value.todo_decrypt()' is fixed only one of these will be possible
let blinding_factor_0 = output_0
.recover_mask(&factories.range_proof, &rewind_blinding_key)
.unwrap();
let blinding_factor_1 = output_1
.recover_mask(&factories.range_proof, &rewind_blinding_key)
.unwrap();
// TODO: Fix this logic when 'encrypted_value.todo_decrypt()' is fixed only one of these will be possible
assert!(committed_value_0 == change || committed_value_1 == change);
// TODO: Fix this logic when 'encrypted_value.todo_decrypt()' is fixed only one of these will be possible
assert!(blinding_factor_0 == a.change_spend_key || blinding_factor_1 == a.change_spend_key);
) {
let blinding_factor = output_1
.recover_mask(
&factories.range_proof,
&alice_test_params.rewind_data.rewind_blinding_key,
)
.unwrap();
assert_eq!(
factories
.commitment
.commit_value(&blinding_factor, committed_value.as_u64()),
output_1.commitment
);
} else {
panic!("Could not recover Alice's output");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ where
if output.script != script!(Nop) {
continue;
}
// TODO: Fix this logic when 'todo_decrypt' - only commence if the tag is recognized
let committed_value = EncryptedValue::decrypt_value(
&self.rewind_data.encryption_key,
&output.commitment,
Expand Down
4 changes: 0 additions & 4 deletions base_layer/wallet/src/output_manager_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1755,11 +1755,8 @@ where
.as_bytes(),
)?;
let blinding_key = PrivateKey::from_bytes(&hash_secret_key(&spending_key))?;
// TODO: This may be usefull for the encrypted value decryption since this is an atomic swap -
// TODO: when fixing 'todo_decrypt'
let rewind_key = PrivateKey::from_bytes(&hash_secret_key(&blinding_key))?;
let encryption_key = PrivateKey::from_bytes(&hash_secret_key(&rewind_key))?;
// TODO: Fix this logic when 'todo_decrypt' - only commence if the tag is recognized
if let Ok(amount) = EncryptedValue::decrypt_value(&encryption_key, &output.commitment, &output.encrypted_value)
{
let blinding_factor = output.recover_mask(&self.resources.factories.range_proof, &blinding_key)?;
Expand Down Expand Up @@ -1990,7 +1987,6 @@ where
let rewind_blinding_key = PrivateKey::from_bytes(&hash_secret_key(&spending_key))?;
let recovery_byte_key = PrivateKey::from_bytes(&hash_secret_key(&rewind_blinding_key))?;
let encryption_key = PrivateKey::from_bytes(&hash_secret_key(&recovery_byte_key))?;
// TODO: Fix this logic when 'todo_decrypt' - only commence if the tag is recognized
let committed_value =
EncryptedValue::decrypt_value(&encryption_key, &output.commitment, &output.encrypted_value);
if let Ok(committed_value) = committed_value {
Expand Down

0 comments on commit b4cd57a

Please sign in to comment.