From eb68471db8bb210d068c35b5d201f8f3ff5eca3d Mon Sep 17 00:00:00 2001 From: Aaron Feickert <66188213+AaronFeickert@users.noreply.github.com> Date: Mon, 20 Nov 2023 15:14:26 -0600 Subject: [PATCH] Update `ToRistrettoPoint` comments and test --- infrastructure/tari_script/src/op_codes.rs | 8 +++++--- infrastructure/tari_script/src/script.rs | 14 +++++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/infrastructure/tari_script/src/op_codes.rs b/infrastructure/tari_script/src/op_codes.rs index e8454bd0bf..5e91c529fd 100644 --- a/infrastructure/tari_script/src/op_codes.rs +++ b/infrastructure/tari_script/src/op_codes.rs @@ -262,9 +262,11 @@ pub enum Opcode { /// Identical to CheckMultiSig, except that the aggregate of the public keys is pushed to the stack if multiple /// signature validation succeeds. Fails with `VerifyFailed` if any signature is invalid. CheckMultiSigVerifyAggregatePubKey(u8, u8, Vec, Box), - /// Pops the top element from the stack, either a scalar or a hash, calculates the corresponding Ristretto point, - /// and pushes the result to the stack. Fails with `StackUnderflow` if the stack is empty. Fails with - /// `IncompatibleTypes` if the stack item is not a valid 32 byte sequence. + /// Pops the top element from the stack (either a scalar or a hash), parses it canonically as a Ristretto secret + /// key if possible, computes the corresponding Ristretto public key, and pushes this value to the stack. + /// Fails with `StackUnderflow` if the stack is empty. + /// Fails with `IncompatibleTypes` if the stack item is not either a scalar or a hash. + /// Fails with `InvalidInput` if the stack item cannot be canonically parsed as a Ristretto secret key. ToRistrettoPoint, // Miscellaneous diff --git a/infrastructure/tari_script/src/script.rs b/infrastructure/tari_script/src/script.rs index 6f05aa91f0..9f90961541 100644 --- a/infrastructure/tari_script/src/script.rs +++ b/infrastructure/tari_script/src/script.rs @@ -1739,10 +1739,13 @@ mod test { #[test] fn to_ristretto_point() { use crate::StackItem::PublicKey; + use crate::Opcode::ToRistrettoPoint; + + // Generate a key pair let mut rng = rand::thread_rng(); let (k_1, p_1) = RistrettoPublicKey::random_keypair(&mut rng); - use crate::Opcode::ToRistrettoPoint; + // Generate a test script let ops = vec![ToRistrettoPoint]; let script = TariScript::new(ops); @@ -1751,17 +1754,22 @@ mod test { let err = script.execute(&inputs).unwrap_err(); assert!(matches!(err, ScriptError::IncompatibleTypes)); - // scalar + // Valid scalar let mut scalar = [0u8; 32]; scalar.copy_from_slice(k_1.as_bytes()); let inputs = inputs!(scalar); let result = script.execute(&inputs).unwrap(); assert_eq!(result, PublicKey(p_1.clone())); - // hash + // Valid hash let inputs = ExecutionStack::new(vec![Hash(scalar)]); let result = script.execute(&inputs).unwrap(); assert_eq!(result, PublicKey(p_1)); + + // Invalid bytes + let invalid = [u8::MAX; 32]; // not a canonical scalar encoding! + let inputs = inputs!(invalid); + assert!(matches!(script.execute(&inputs), Err(ScriptError::InvalidInput))); } #[test]