-
Notifications
You must be signed in to change notification settings - Fork 136
dsa: reduce Box<[u8]>
allocation in SignatureEncoding::to_vec
#969
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
Open
dishmaker
wants to merge
2
commits into
RustCrypto:master
Choose a base branch
from
dishmaker:dishmaker/dsa_reduce_alloc_der_encode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use alloc::boxed::Box; | ||
use crypto_bigint::{BoxedUint, NonZero}; | ||
use pkcs8::der::{ | ||
self, Decode, DecodeValue, Encode, EncodeValue, FixedTag, Header, Length, Reader, Sequence, | ||
Writer, asn1::UintRef, | ||
}; | ||
|
||
use crate::Signature; | ||
|
||
pub(crate) struct SignatureBoxed { | ||
r: Box<[u8]>, | ||
s: Box<[u8]>, | ||
} | ||
impl SignatureBoxed { | ||
pub fn new(sig: &Signature) -> Self { | ||
Self { | ||
r: sig.r().to_be_bytes(), | ||
s: sig.s().to_be_bytes(), | ||
} | ||
} | ||
|
||
pub fn to_ref(&self) -> der::Result<SignatureRef<'_>> { | ||
Ok(SignatureRef { | ||
r: UintRef::new(&self.r)?, | ||
s: UintRef::new(&self.s)?, | ||
}) | ||
} | ||
} | ||
|
||
pub(crate) struct SignatureRef<'a> { | ||
r: UintRef<'a>, | ||
s: UintRef<'a>, | ||
} | ||
impl<'a> SignatureRef<'a> { | ||
pub fn to_owned(&self) -> der::Result<Signature> { | ||
let r = BoxedUint::from_be_slice(self.r.as_bytes(), self.r.as_bytes().len() as u32 * 8) | ||
.map_err(|_| UintRef::TAG.value_error())?; | ||
let s = BoxedUint::from_be_slice(self.s.as_bytes(), self.s.as_bytes().len() as u32 * 8) | ||
.map_err(|_| UintRef::TAG.value_error())?; | ||
|
||
let r = NonZero::new(r) | ||
.into_option() | ||
.ok_or(UintRef::TAG.value_error())?; | ||
let s = NonZero::new(s) | ||
.into_option() | ||
.ok_or(UintRef::TAG.value_error())?; | ||
|
||
Ok(Signature::from_components(r, s)) | ||
} | ||
|
||
fn decode_value_inner<R: Reader<'a>>(reader: &mut R) -> der::Result<Self> { | ||
Ok(SignatureRef { | ||
r: UintRef::decode(reader)?, | ||
s: UintRef::decode(reader)?, | ||
}) | ||
} | ||
} | ||
|
||
impl<'a> DecodeValue<'a> for SignatureRef<'a> { | ||
type Error = der::Error; | ||
|
||
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> { | ||
reader.read_nested(header.length, Self::decode_value_inner) | ||
} | ||
} | ||
|
||
impl EncodeValue for SignatureRef<'_> { | ||
fn value_len(&self) -> der::Result<Length> { | ||
self.r.encoded_len()? + self.s.encoded_len()? | ||
} | ||
|
||
fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> { | ||
self.r.encode(writer)?; | ||
self.s.encode(writer)?; | ||
Ok(()) | ||
} | ||
} | ||
impl<'a> Sequence<'a> for SignatureRef<'a> {} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is essentially what
dsa::Signature
is (dsa::Signature is composed ofNonZero<BoxedUint>
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't it be possible to get an iterator over the inner limbs? and get big endian from there? without allocating a new box.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
der
checks for leading zeros when encoding UIntRef, so it would be too trickyUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well that's just up to the consumer of the said iterator to strip leading zeroes? I don't know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah nevermind, you have to have a buffer for
SignatureRef
, that can't be an iterator of limbs.