Skip to content

Commit

Permalink
feat(applying): use pallas-crypto functions for witness verification
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicoLeberle committed Nov 8, 2023
1 parent 7fff85a commit d74b148
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
1 change: 0 additions & 1 deletion pallas-applying/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ authors = ["Maico Leberle <[email protected]>"]
doctest = false

[dependencies]
cryptoxide = "0.1"
cbor_event = "^2.1.1"
pallas-addresses = { path = "../pallas-addresses" }
pallas-codec = { path = "../pallas-codec" }
Expand Down
68 changes: 43 additions & 25 deletions pallas-applying/src/byron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ use crate::types::{
};

use cbor_event::se::Serializer;
use cryptoxide::ed25519;
use pallas_addresses::byron::{
AddrAttrs, AddrType, AddressId, AddressPayload, ByronAddress, SpendingData,
};
use pallas_codec::{minicbor::encode, utils::CborWrap};
use pallas_crypto::hash::Hash;
use pallas_crypto::{
hash::Hash,
key::ed25519::{PublicKey, Signature},
};
use pallas_primitives::byron::{
Address, MintedTxPayload, PubKey, Signature, Twit, Tx, TxIn, TxOut,
Address, MintedTxPayload, PubKey, Signature as ByronSignature, Twit, Tx, TxIn, TxOut,
};
use pallas_traverse::OriginalHash;

Expand Down Expand Up @@ -107,17 +109,8 @@ fn get_tx_size(tx: &Tx) -> Result<u64, ValidationError> {
}

pub enum TaggedSignature<'a> {
PkWitness(&'a Signature),
RedeemWitness(&'a Signature),
}

impl<'a> TaggedSignature<'a> {
fn get_raw_signature(&'a self) -> &'a Signature {
match self {
TaggedSignature::PkWitness(sign) => sign,
TaggedSignature::RedeemWitness(sign) => sign,
}
}
PkWitness(&'a ByronSignature),
RedeemWitness(&'a ByronSignature),
}

fn check_witnesses(mtxp: &MintedTxPayload, utxos: &UTxOs, prot_magic: &u32) -> ValidationResult {
Expand All @@ -127,16 +120,11 @@ fn check_witnesses(mtxp: &MintedTxPayload, utxos: &UTxOs, prot_magic: &u32) -> V
let tx_inputs: &Vec<TxIn> = &tx.inputs;
for input in tx_inputs {
let tx_out: &TxOut = find_tx_out(input, utxos)?;
let (pub_key, sign): (&PubKey, &TaggedSignature) = find_witness(tx_out, &witnesses)?;
let signature: &Vec<u8> = sign.get_raw_signature();
let mut se = Serializer::new_vec();
serialize_tag(&mut se, sign)?;
se.serialize(&prot_magic)
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
se.serialize(&tx_hash.as_ref())
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
let data_to_verify: Vec<u8> = se.finalize();
if !ed25519::verify(&data_to_verify, &pub_key.as_slice()[0..32], signature) {
let (pub_key, sign): (&PubKey, &TaggedSignature) = find_raw_witness(tx_out, &witnesses)?;
let public_key: PublicKey = get_verification_key(pub_key);
let data_to_verify: Vec<u8> = get_data_to_verify(sign, prot_magic, &tx_hash)?;
let signature: Signature = get_signature(sign);
if !public_key.verify(data_to_verify, &signature) {
return Err(ValidationError::WrongSignature);
}
}
Expand Down Expand Up @@ -168,7 +156,7 @@ fn find_tx_out<'a>(input: &'a TxIn, utxos: &'a UTxOs) -> Result<&'a TxOut, Valid
.ok_or(ValidationError::InputMissingInUTxO)
}

fn find_witness<'a>(
fn find_raw_witness<'a>(
tx_out: &TxOut,
witnesses: &'a Vec<(&'a PubKey, TaggedSignature<'a>)>,
) -> Result<(&'a PubKey, &'a TaggedSignature<'a>), ValidationError> {
Expand Down Expand Up @@ -238,3 +226,33 @@ fn serialize_tag(
}
Ok(())
}

fn get_verification_key(pk: &PubKey) -> PublicKey {
let mut trunc_len: [u8; PublicKey::SIZE] = [0; PublicKey::SIZE];
trunc_len.copy_from_slice(&pk.as_slice()[0..PublicKey::SIZE]);
From::<[u8; PublicKey::SIZE]>::from(trunc_len)
}

fn get_data_to_verify(
sign: &TaggedSignature,
prot_magic: &u32,
tx_hash: &Hash<32>,
) -> Result<Vec<u8>, ValidationError> {
let mut se = Serializer::new_vec();
serialize_tag(&mut se, sign)?;
se.serialize(&prot_magic)
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
se.serialize(&tx_hash.as_ref())
.map_err(|_| ValidationError::UnableToProcessWitnesses)?;
Ok(se.finalize())
}

fn get_signature(tagged_signature: &TaggedSignature<'_>) -> Signature {
let inner_sig = match tagged_signature {
TaggedSignature::PkWitness(sign) => sign,
TaggedSignature::RedeemWitness(sign) => sign,
};
let mut trunc_len: [u8; Signature::SIZE] = [0; Signature::SIZE];
trunc_len.copy_from_slice(inner_sig.as_slice());
From::<[u8; Signature::SIZE]>::from(trunc_len)
}

0 comments on commit d74b148

Please sign in to comment.