-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from compiler-errors/do-not-rely-on-inference
Do not rely on inference behavior for `Scalar::from_*` and `Point::from_*`
- Loading branch information
Showing
9 changed files
with
32 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,52 +1,41 @@ | ||
use secp256kfun::{marker::*, Scalar}; | ||
/// An ECDSA signature | ||
#[derive(Clone, PartialEq)] | ||
pub struct Signature<S = Public> { | ||
pub R_x: Scalar<Public, NonZero>, | ||
pub s: Scalar<S, NonZero>, | ||
pub struct Signature { | ||
pub R_x: Scalar<Public>, | ||
pub s: Scalar<Public>, | ||
} | ||
|
||
impl<S> Signature<S> { | ||
impl Signature { | ||
pub fn to_bytes(&self) -> [u8; 64] { | ||
let mut bytes = [0u8; 64]; | ||
bytes[0..32].copy_from_slice(&self.R_x.to_bytes()[..]); | ||
bytes[32..64].copy_from_slice(&self.s.to_bytes()[..]); | ||
bytes | ||
} | ||
|
||
pub fn as_tuple(&self) -> (&Scalar<Public, NonZero>, &Scalar<S, NonZero>) { | ||
pub fn as_tuple(&self) -> (&Scalar<Public>, &Scalar<Public>) { | ||
(&self.R_x, &self.s) | ||
} | ||
|
||
pub fn set_secrecy<SigSec: Secrecy>(self) -> Signature<SigSec> { | ||
Signature { | ||
R_x: self.R_x, | ||
s: self.s.set_secrecy::<SigSec>(), | ||
} | ||
} | ||
} | ||
|
||
impl Signature<Public> { | ||
impl Signature { | ||
pub fn from_bytes(bytes: [u8; 64]) -> Option<Self> { | ||
Scalar::from_slice(&bytes[0..32]) | ||
.and_then(|R_x| R_x.public().non_zero()) | ||
.and_then(|R_x| { | ||
Scalar::from_slice(&bytes[32..64]) | ||
.and_then(|s| s.public().non_zero()) | ||
.map(|s| Self { R_x, s }) | ||
}) | ||
let R_x = Scalar::from_slice(&bytes[0..32])?.non_zero()?; | ||
let s = Scalar::from_slice(&bytes[32..64])?.non_zero()?; | ||
Some(Self { R_x, s }) | ||
} | ||
} | ||
|
||
secp256kfun::impl_fromstr_deserialize! { | ||
name => "secp256k1 ECDSA signature", | ||
fn from_bytes<S: Secrecy>(bytes: [u8;64]) -> Option<Signature<S>> { | ||
Signature::from_bytes(bytes).map(|signature| signature.set_secrecy::<S>()) | ||
fn from_bytes(bytes: [u8;64]) -> Option<Signature> { | ||
Signature::from_bytes(bytes) | ||
} | ||
} | ||
|
||
secp256kfun::impl_display_debug_serialize! { | ||
fn to_bytes<S>(sig: &Signature<S>) -> [u8;64] { | ||
fn to_bytes(sig: &Signature) -> [u8;64] { | ||
sig.to_bytes() | ||
} | ||
} |
This file contains 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 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 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 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 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