-
Notifications
You must be signed in to change notification settings - Fork 57
feat(template_lib): adds engine schnorr signature verification #1574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sdbondi
merged 1 commit into
tari-project:development
from
sdbondi:template-lib-signature-verify-api
Sep 18, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright 2025 The Tari Project | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| use tari_engine::fees::FeeTable; | ||
| use tari_ootle_common_types::Network; | ||
|
|
||
| const TESTNET_FEE_TABLE: FeeTable = FeeTable { | ||
| per_transaction_weight_cost: 1, | ||
| per_module_call_cost: 1, | ||
| per_byte_storage_cost: 1, | ||
| per_event_cost: 1, | ||
| per_log_cost: 1, | ||
| per_signature_verification_cost: 10, | ||
| }; | ||
|
|
||
| // TODO: finalize these values | ||
| const MAINNET_FEE_TABLE: FeeTable = FeeTable { | ||
| per_transaction_weight_cost: 1, | ||
| per_module_call_cost: 1, | ||
| per_byte_storage_cost: 1, | ||
| per_event_cost: 1, | ||
| per_log_cost: 1, | ||
| per_signature_verification_cost: 10, | ||
| }; | ||
|
|
||
| pub const fn get_fee_table_by_network(network: Network) -> &'static FeeTable { | ||
| match network { | ||
| Network::LocalNet => &TESTNET_FEE_TABLE, | ||
| Network::Igor => &TESTNET_FEE_TABLE, | ||
| Network::Esmeralda => &TESTNET_FEE_TABLE, | ||
| Network::StageNet => &TESTNET_FEE_TABLE, | ||
| Network::NextNet => &TESTNET_FEE_TABLE, | ||
| Network::MainNet => &MAINNET_FEE_TABLE, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright 2025 The Tari Project | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| use blake2::{ | ||
| digest::{consts, generic_array::GenericArray}, | ||
| Blake2b, | ||
| Digest, | ||
| }; | ||
| use tari_crypto::ristretto::RistrettoSchnorr; | ||
| use tari_engine_types::{ConvertFromByteType, FromByteType, ToByteType}; | ||
| use tari_template_lib_types::crypto::{PublicKey, RistrettoPublicKeyBytes, SignaturePayload}; | ||
|
|
||
| pub trait GetVerifier { | ||
| fn get_verifier(&self) -> &dyn Verifier; | ||
| } | ||
|
|
||
| impl GetVerifier for SignaturePayload { | ||
| fn get_verifier(&self) -> &dyn Verifier { | ||
| match self { | ||
| SignaturePayload::RistrettoSchnorrBlake2b(_) => &RistrettoSchnorrBlake2bVerifier, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub trait Verifier { | ||
| fn verify(&self, domain: &[u8], message: &[u8], public_key: &PublicKey, signature: &SignaturePayload) -> bool; | ||
| } | ||
|
|
||
| /// A verifier for Ristretto Schnorr signatures with Blake2b hashing. | ||
| pub struct RistrettoSchnorrBlake2bVerifier; | ||
|
|
||
| impl RistrettoSchnorrBlake2bVerifier { | ||
| pub fn compute_challenge( | ||
| domain: &[u8], | ||
| message: &[u8], | ||
| public_key: &RistrettoPublicKeyBytes, | ||
| nonce: &RistrettoPublicKeyBytes, | ||
| ) -> GenericArray<u8, consts::U64> { | ||
| Blake2b::<consts::U64>::new() | ||
| // Domain | ||
| .chain_update(domain) | ||
| // Fiat-Shamir - note that we force this on the user to avoid security pitfalls. | ||
| .chain_update(nonce.as_bytes()) | ||
| .chain_update(public_key.as_bytes()) | ||
| // Message | ||
| .chain_update(message) | ||
| .finalize() | ||
| } | ||
| } | ||
|
|
||
| impl Verifier for RistrettoSchnorrBlake2bVerifier { | ||
| fn verify(&self, domain: &[u8], message: &[u8], public_key: &PublicKey, signature: &SignaturePayload) -> bool { | ||
| let sig = signature | ||
| .ristretto_schnorr_blake2b() | ||
| .expect("Expected Ristretto Schnorr signature"); | ||
| let ristretto_public_key = public_key.ristretto25519().expect("Expected Ristretto PublicKey"); | ||
|
|
||
| let Ok(pk) = ristretto_public_key.try_from_byte_type() else { | ||
| return false; | ||
| }; | ||
|
|
||
| let Ok(sig) = RistrettoSchnorr::convert_from_byte_type(sig) else { | ||
| return false; | ||
| }; | ||
|
|
||
| // NOTE: this is general purpose signature verification, and we want to user to specify a domain, without | ||
| // forcing tari-style domains to be mixed in. | ||
| let message = Self::compute_challenge( | ||
| domain, | ||
| message, | ||
| ristretto_public_key, | ||
| &sig.get_public_nonce().to_byte_type(), | ||
| ); | ||
| sig.verify_raw_uniform(&pk, &message) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use rand::rngs::OsRng; | ||
| use tari_crypto::{ | ||
| keys::PublicKey as _, | ||
| ristretto::{RistrettoPublicKey, RistrettoSchnorr}, | ||
| }; | ||
| use tari_engine_types::ToByteType; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn it_verifies_a_valid_signature() { | ||
| let (secret_key, public_key) = RistrettoPublicKey::random_keypair(&mut OsRng); | ||
|
|
||
| let message = b"Hello, world!"; | ||
| let domain = b"Test domain"; | ||
|
|
||
| let (nonce, public_nonce) = RistrettoPublicKey::random_keypair(&mut OsRng); | ||
|
|
||
| let hashed_message = RistrettoSchnorrBlake2bVerifier::compute_challenge( | ||
| domain, | ||
| message, | ||
| &public_key.to_byte_type(), | ||
| &public_nonce.to_byte_type(), | ||
| ); | ||
| let signature = RistrettoSchnorr::sign_raw_uniform(&secret_key, nonce, &hashed_message).unwrap(); | ||
|
|
||
| let signature_payload = SignaturePayload::RistrettoSchnorrBlake2b(signature.to_byte_type()); | ||
| let public_key = PublicKey::Ristretto25519(public_key.to_byte_type()); | ||
|
|
||
| let verifier = RistrettoSchnorrBlake2bVerifier; | ||
| assert!(verifier.verify(domain, message, &public_key, &signature_payload)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.