-
Notifications
You must be signed in to change notification settings - Fork 61
Add segwit API #120
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
Merged
Add segwit API #120
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,103 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
//! Segregated Witness functionality - useful for enforcing parts of [`BIP-173`] and [`BIP-350`]. | ||
//! | ||
//! [BIP-173]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki> | ||
//! [BIP-350]: <https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki> | ||
|
||
use core::fmt; | ||
|
||
use crate::primitives::gf32::Fe32; | ||
|
||
/// The field element representing segwit version 0. | ||
pub const VERSION_0: Fe32 = Fe32::Q; | ||
/// The field element representing segwit version 1 (taproot). | ||
pub const VERSION_1: Fe32 = Fe32::P; | ||
|
||
/// Returns true if given field element represents a valid segwit version. | ||
pub fn is_valid_witness_version(witness_version: Fe32) -> bool { | ||
validate_witness_version(witness_version).is_ok() | ||
} | ||
|
||
/// Returns true if `length` represents a valid witness program length for `witness_version`. | ||
pub fn is_valid_witness_program_length(length: usize, witness_version: Fe32) -> bool { | ||
validate_witness_program_length(length, witness_version).is_ok() | ||
} | ||
|
||
/// Checks that the given field element represents a valid segwit witness version. | ||
pub fn validate_witness_version(witness_version: Fe32) -> Result<(), InvalidWitnessVersionError> { | ||
if witness_version.to_u8() > 16 { | ||
Err(InvalidWitnessVersionError(witness_version)) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Validates the segwit witness program `length` rules for witness `version`. | ||
pub fn validate_witness_program_length( | ||
length: usize, | ||
version: Fe32, | ||
) -> Result<(), WitnessLengthError> { | ||
use WitnessLengthError::*; | ||
|
||
if length < 2 { | ||
return Err(TooShort); | ||
} | ||
if length > 40 { | ||
return Err(TooLong); | ||
} | ||
if version == VERSION_0 && length != 20 && length != 32 { | ||
return Err(InvalidSegwitV0); | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Field element does not represent a valid witness version. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct InvalidWitnessVersionError(Fe32); | ||
|
||
impl fmt::Display for InvalidWitnessVersionError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "field element does not represent a valid witness version") | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for InvalidWitnessVersionError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } | ||
} | ||
|
||
/// Witness program invalid because of incorrect length. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
pub enum WitnessLengthError { | ||
/// The witness data is too short. | ||
TooShort, | ||
/// The witness data is too long. | ||
TooLong, | ||
/// The segwit v0 witness is not 20 or 32 bytes long. | ||
InvalidSegwitV0, | ||
} | ||
|
||
impl fmt::Display for WitnessLengthError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
use WitnessLengthError::*; | ||
|
||
match *self { | ||
TooShort => write!(f, "witness program is less than 2 bytes long"), | ||
TooLong => write!(f, "witness program is more than 40 bytes long"), | ||
InvalidSegwitV0 => write!(f, "the segwit v0 witness is not 20 or 32 bytes long"), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for WitnessLengthError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
use WitnessLengthError::*; | ||
|
||
match *self { | ||
TooShort | TooLong | InvalidSegwitV0 => None, | ||
} | ||
} | ||
} |
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.