Skip to content

Commit

Permalink
feat: modify gamma calculation for TariScript
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler committed Jun 11, 2021
2 parents b19789d + f972d83 commit c88d789
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ keys.json
node_modules
/integration_tests/temp
/applications/utils/temp/
/integration_tests/cucumber_output
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ mod test {
.with_fee_per_gram(20.into())
.with_offset(Default::default())
.with_private_nonce(test_params.nonce.clone())
.with_change_secret(test_params.change_key.clone());
.with_change_secret(test_params.change_spend_key.clone());

// Double spend the input from tx2 in tx3
let double_spend_utxo = tx2.body.inputs().first().unwrap().clone();
Expand Down
7 changes: 2 additions & 5 deletions base_layer/core/src/transactions/aggregated_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use tari_crypto::{
commitment::HomomorphicCommitmentFactory,
keys::PublicKey as PublicKeyTrait,
ristretto::pedersen::PedersenCommitment,
tari_utilities::{hex::Hex, ByteArray, Hashable},
tari_utilities::hex::Hex,
};

pub const LOG_TARGET: &str = "c::tx::aggregated_body";
Expand Down Expand Up @@ -390,10 +390,7 @@ impl AggregateBody {
for output in &self.outputs {
// We should not count the coinbase tx here
if !output.is_coinbase() {
output_keys = output_keys +
PrivateKey::from_bytes(&output.hash())
.map_err(|e| TransactionError::ConversionError(e.to_string()))? *
output.script_offset_public_key.clone();
output_keys = output_keys + output.script_offset_public_key.clone();
}
}
let lhs = input_keys - output_keys;
Expand Down
63 changes: 33 additions & 30 deletions base_layer/core/src/transactions/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,28 @@ pub fn create_test_input(
#[derive(Default)]
pub struct TestParams {
pub spend_key: PrivateKey,
pub change_key: PrivateKey,
pub change_spend_key: PrivateKey,
pub offset: PrivateKey,
pub nonce: PrivateKey,
pub public_nonce: PublicKey,
pub script_private_key: PrivateKey,
pub script_offset: PublicKey,
pub script_offset_pvt: PrivateKey,
pub script_offset_pub_key: PublicKey,
pub script_offset_pvt_key: PrivateKey,
}

impl TestParams {
pub fn new() -> TestParams {
let r = PrivateKey::random(&mut OsRng);
let script_offset_pvt = PrivateKey::random(&mut OsRng);
let script_offset_pvt_key = PrivateKey::random(&mut OsRng);
TestParams {
spend_key: PrivateKey::random(&mut OsRng),
change_key: PrivateKey::random(&mut OsRng),
change_spend_key: PrivateKey::random(&mut OsRng),
offset: PrivateKey::random(&mut OsRng),
public_nonce: PublicKey::from_secret_key(&r),
nonce: r,
script_private_key: PrivateKey::random(&mut OsRng),
script_offset: PublicKey::from_secret_key(&script_offset_pvt),
script_offset_pvt,
script_offset_pub_key: PublicKey::from_secret_key(&script_offset_pvt_key),
script_offset_pvt_key,
}
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ pub fn create_tx(
.with_fee_per_gram(fee_per_gram)
.with_offset(test_params.offset.clone())
.with_private_nonce(test_params.nonce.clone())
.with_change_secret(test_params.change_key.clone());
.with_change_secret(test_params.change_spend_key);

let mut unblinded_inputs = Vec::with_capacity(input_count);
let mut unblinded_outputs = Vec::with_capacity(output_count);
Expand All @@ -286,18 +286,19 @@ pub fn create_tx(
} else {
amount_for_last_output
};
let test_params = TestParams::new();
let utxo = UnblindedOutput::new(
output_amount,
test_params.spend_key.clone(),
None,
script!(Nop),
inputs!(PublicKey::from_secret_key(&test_params.spend_key)),
inputs!(PublicKey::from_secret_key(&test_params.script_private_key)),
0,
test_params.spend_key.clone(),
PublicKey::from_secret_key(&test_params.spend_key),
test_params.script_private_key.clone(),
test_params.script_offset_pub_key,
);
unblinded_outputs.push(utxo.clone());
stx_builder.with_output(utxo, test_params.spend_key.clone());
stx_builder.with_output(utxo, test_params.script_offset_pvt_key.clone());
}

let mut stx_protocol = stx_builder.build::<Blake256>(&factories).unwrap();
Expand All @@ -318,18 +319,20 @@ pub fn create_tx(
/// The output features will be applied to every output
pub fn spend_utxos(schema: TransactionSchema) -> (Transaction, Vec<UnblindedOutput>, TestParams) {
let factories = CryptoFactories::default();
let test_params = TestParams::new();
let test_params_change_and_txn = TestParams::new();
let mut stx_builder = SenderTransactionProtocol::builder(0);
stx_builder
.with_lock_height(schema.lock_height)
.with_fee_per_gram(schema.fee)
.with_offset(test_params.offset.clone())
.with_private_nonce(test_params.nonce.clone())
.with_change_secret(test_params.change_key.clone())
.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_change_script(
script!(Nop),
inputs!(PublicKey::from_secret_key(&test_params.script_private_key)),
test_params.script_private_key.clone(),
inputs!(PublicKey::from_secret_key(
&test_params_change_and_txn.script_private_key
)),
test_params_change_and_txn.script_private_key.clone(),
);

for tx_input in &schema.from {
Expand All @@ -342,34 +345,34 @@ pub fn spend_utxos(schema: TransactionSchema) -> (Transaction, Vec<UnblindedOutp
}
let mut outputs = Vec::with_capacity(schema.to.len());
for val in schema.to {
let k = PrivateKey::random(&mut OsRng);
let script_private_key = PrivateKey::random(&mut OsRng);
let script_offset_private_key = PrivateKey::random(&mut OsRng);
let test_params = TestParams::new();
let utxo = UnblindedOutput::new(
val,
k.clone(),
test_params.spend_key.clone(),
Some(schema.features.clone()),
script!(Nop),
inputs!(PublicKey::from_secret_key(&script_private_key)),
inputs!(PublicKey::from_secret_key(&test_params.script_private_key)),
0,
script_private_key,
PublicKey::from_secret_key(&script_offset_private_key),
test_params.script_private_key.clone(),
test_params.script_offset_pub_key,
);
outputs.push(utxo.clone());
stx_builder.with_output(utxo, script_offset_private_key);
stx_builder.with_output(utxo, test_params.script_offset_pvt_key);
}

let mut stx_protocol = stx_builder.build::<Blake256>(&factories).unwrap();
let change = stx_protocol.get_change_amount().unwrap();
let change_script_offset_public_key = stx_protocol.get_change_script_offset_public_key().unwrap().unwrap();
let change_output = UnblindedOutput::new(
change,
test_params.change_key.clone(),
test_params_change_and_txn.change_spend_key.clone(),
Some(schema.features),
script!(Nop),
inputs!(PublicKey::from_secret_key(&test_params.script_private_key)),
inputs!(PublicKey::from_secret_key(
&test_params_change_and_txn.script_private_key
)),
0,
test_params.script_private_key.clone(),
test_params_change_and_txn.script_private_key.clone(),
change_script_offset_public_key,
);
outputs.push(change_output);
Expand All @@ -378,7 +381,7 @@ pub fn spend_utxos(schema: TransactionSchema) -> (Transaction, Vec<UnblindedOutp
Err(e) => panic!("{:?}", e),
}
let txn = stx_protocol.get_transaction().unwrap().clone();
(txn, outputs, test_params)
(txn, outputs, test_params_change_and_txn)
}

/// Create a transaction kernel with the given fee, using random keys to generate the signature
Expand Down
26 changes: 11 additions & 15 deletions base_layer/core/src/transactions/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,6 @@ impl Transaction {
impl Add for Transaction {
type Output = Self;

// Note this will also do cut-through
fn add(mut self, other: Self) -> Self {
self = self.add_no_cut_through(other);
self
Expand Down Expand Up @@ -1315,35 +1314,32 @@ mod test {
assert_eq!(tx2.body.outputs().len(), 3);
assert_eq!(tx2.body.kernels().len(), 1);

let mut tx3 = tx.clone().add_no_cut_through(tx2.clone());
let tx = tx + tx2;
let tx3 = tx + tx2;
let mut tx3_cut_through = tx3.clone();
// check that all inputs are as we expect them to be
assert_eq!(tx3.body.inputs().len(), 3);
assert_eq!(tx3.body.outputs().len(), 5);
assert_eq!(tx3.body.kernels().len(), 2);

let double_inputs: Vec<TransactionInput> = tx3
// Do manual cut-through on tx3
let double_inputs: Vec<TransactionInput> = tx3_cut_through
.body
.inputs()
.clone()
.iter()
.filter(|input| tx3.body.outputs_mut().iter().any(|o| o.is_equal_to(input)))
.filter(|input| tx3_cut_through.body.outputs_mut().iter().any(|o| o.is_equal_to(input)))
.cloned()
.collect();

for input in double_inputs {
tx3.body.outputs_mut().retain(|x| !input.is_equal_to(x));
tx3.body.inputs_mut().retain(|x| *x != input);
tx3_cut_through.body.outputs_mut().retain(|x| !input.is_equal_to(x));
tx3_cut_through.body.inputs_mut().retain(|x| *x != input);
}

// check that cut-through has been applied.
assert!(tx.validate_internal_consistency(&factories, None).is_ok());
assert_eq!(tx.body.inputs().len(), 3);
assert_eq!(tx.body.outputs().len(), 5);
assert_eq!(tx.body.kernels().len(), 2);
// Validate basis transaction where cut-through has not been applied.
assert!(tx3.validate_internal_consistency(&factories, None).is_ok());

// tx3 has manual cut-through, it should not be possible so this should fail
assert!(tx3.validate_internal_consistency(&factories, None).is_err());
// tx3_cut_through has manual cut-through, it should not be possible so this should fail
assert!(tx3_cut_through.validate_internal_consistency(&factories, None).is_err());
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ mod test {
tx_id: 15,
amount: MicroTari(500),
public_excess: PublicKey::from_secret_key(&p.spend_key), // any random key will do
public_nonce: PublicKey::from_secret_key(&p.change_key), // any random key will do
public_nonce: PublicKey::from_secret_key(&p.change_spend_key), // any random key will do
metadata: m.clone(),
message: "".to_string(),
script: TariScript::default(),
Expand Down Expand Up @@ -284,7 +284,7 @@ mod test {
tx_id: 15,
amount,
public_excess: PublicKey::from_secret_key(&p.spend_key), // any random key will do
public_nonce: PublicKey::from_secret_key(&p.change_key), // any random key will do
public_nonce: PublicKey::from_secret_key(&p.change_spend_key), // any random key will do
metadata: m,
message: "".to_string(),
script: TariScript::default(),
Expand Down
23 changes: 10 additions & 13 deletions base_layer/core/src/transactions/transaction_protocol/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use tari_crypto::{
keys::PublicKey as PublicKeyTrait,
ristretto::pedersen::PedersenCommitment,
script::TariScript,
tari_utilities::{ByteArray, Hashable},
tari_utilities::ByteArray,
};

//---------------------------------------- Local Data types ----------------------------------------------------//
Expand Down Expand Up @@ -350,10 +350,7 @@ impl SenderTransactionProtocol {
"For single recipient there should be one recipient script offset".to_string(),
)
})?;
info.gamma = info.gamma.clone() -
PrivateKey::from_bytes(rec.output.hash().as_slice())
.map_err(|e| TPE::ConversionError(e.to_string()))? *
recipient_script_offset_private_key.clone();
info.gamma = info.gamma.clone() - recipient_script_offset_private_key.clone();

// nonce is in the signature, so we'll add those together later
info.public_excess = &info.public_excess + &rec.public_spend_key;
Expand Down Expand Up @@ -647,7 +644,7 @@ mod test {
.with_fee_per_gram(MicroTari(10))
.with_offset(p.offset.clone())
.with_private_nonce(p.nonce.clone())
.with_change_secret(p.change_key.clone())
.with_change_secret(p.change_spend_key.clone())
.with_input(utxo, input)
.with_output(
UnblindedOutput::new(
Expand Down Expand Up @@ -762,7 +759,7 @@ mod test {
.with_fee_per_gram(MicroTari(20))
.with_offset(a.offset.clone())
.with_private_nonce(a.nonce.clone())
.with_change_secret(a.change_key.clone())
.with_change_secret(a.change_spend_key.clone())
.with_input(utxo.clone(), input)
.with_recipient_script(0, script.clone(), script_offset)
.with_change_script(script, ExecutionStack::default(), PrivateKey::default())
Expand Down Expand Up @@ -839,7 +836,7 @@ mod test {
.with_fee_per_gram(MicroTari(20))
.with_offset(a.offset.clone())
.with_private_nonce(a.nonce.clone())
.with_change_secret(a.change_key)
.with_change_secret(a.change_spend_key)
.with_input(utxo, input)
.with_recipient_script(0, script.clone(), script_offset)
.with_change_script(script, ExecutionStack::default(), PrivateKey::default())
Expand Down Expand Up @@ -887,7 +884,7 @@ mod test {
.with_fee_per_gram(fee_per_gram)
.with_offset(alice.offset.clone())
.with_private_nonce(alice.nonce.clone())
.with_change_secret(alice.change_key)
.with_change_secret(alice.change_spend_key)
.with_input(utxo, input)
.with_amount(0, amount)
.with_recipient_script(0, script.clone(), script_offset)
Expand All @@ -913,7 +910,7 @@ mod test {
.with_fee_per_gram(fee_per_gram)
.with_offset(alice.offset.clone())
.with_private_nonce(alice.nonce.clone())
.with_change_secret(alice.change_key)
.with_change_secret(alice.change_spend_key)
.with_input(utxo, input)
.with_amount(0, amount)
.with_prevent_fee_gt_amount(false)
Expand Down Expand Up @@ -957,7 +954,7 @@ mod test {
.with_fee_per_gram(MicroTari(20))
.with_offset(a.offset.clone())
.with_private_nonce(a.nonce.clone())
.with_change_secret(a.change_key.clone())
.with_change_secret(a.change_spend_key.clone())
.with_rewindable_outputs(rewind_data)
.with_input(utxo, input)
.with_amount(0, MicroTari(5000))
Expand Down Expand Up @@ -1021,7 +1018,7 @@ mod test {

assert_eq!(full_rewind_result.committed_value, change);
assert_eq!(&full_rewind_result.proof_message, proof_message);
assert_eq!(full_rewind_result.blinding_factor, a.change_key);
assert_eq!(full_rewind_result.blinding_factor, a.change_spend_key);
},
Err(_) => {
let rr = tx.body.outputs()[1]
Expand All @@ -1038,7 +1035,7 @@ mod test {
.unwrap();
assert_eq!(full_rewind_result.committed_value, change);
assert_eq!(&full_rewind_result.proof_message, proof_message);
assert_eq!(full_rewind_result.blinding_factor, a.change_key);
assert_eq!(full_rewind_result.blinding_factor, a.change_spend_key);
},
}
}
Expand Down
Loading

0 comments on commit c88d789

Please sign in to comment.