Skip to content

Commit

Permalink
Update ToRistrettoPoint comments and test
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Nov 20, 2023
1 parent 3886df4 commit eb68471
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 5 additions & 3 deletions infrastructure/tari_script/src/op_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RistrettoPublicKey>, Box<Message>),
/// 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
Expand Down
14 changes: 11 additions & 3 deletions infrastructure/tari_script/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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]
Expand Down

0 comments on commit eb68471

Please sign in to comment.