Skip to content

Commit 52df8f7

Browse files
committed
Add #[inline(always)] for -> Transcript methods
We declare methods `#[inline(always)]` if they return a 200 byte Transcript because rustc does not handle large returns as efficently as one might like. See rust-random/rand#817
1 parent 1419a1a commit 52df8f7

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/context.rs

+11
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,24 @@ impl SigningTranscript for Transcript {
175175
/// You should use `merlin::Transcript`s directly if you must do
176176
/// anything more complex, like use signatures in larger zero-knoweldge
177177
/// protocols or sign several components but only reveal one later.
178+
///
179+
/// We declare these methods `#[inline(always)]` because rustc does
180+
/// not handle large returns as efficently as one might like.
181+
/// https://github.com/rust-random/rand/issues/817
178182
#[derive(Clone)] // Debug
179183
pub struct SigningContext(Transcript);
180184

181185
/// Initialize a signing context from a static byte string that
182186
/// identifies the signature's role in the larger protocol.
187+
#[inline(always)]
183188
pub fn signing_context(context : &'static [u8]) -> SigningContext {
184189
SigningContext::new(context)
185190
}
186191

187192
impl SigningContext {
188193
/// Initialize a signing context from a static byte string that
189194
/// identifies the signature's role in the larger protocol.
195+
#[inline(always)]
190196
pub fn new(context : &'static [u8]) -> SigningContext {
191197
SigningContext(Transcript::new(context))
192198
}
@@ -196,13 +202,15 @@ impl SigningContext {
196202
/// Avoid this method when processing large slices because it
197203
/// calls `merlin::Transcript::append_message` directly and
198204
/// `merlin` is designed for domain seperation, not performance.
205+
#[inline(always)]
199206
pub fn bytes(&self, bytes: &[u8]) -> Transcript {
200207
let mut t = self.0.clone();
201208
t.append_message(b"sign-bytes", bytes);
202209
t
203210
}
204211

205212
/// Initalize an owned signing transcript on a message provided as a hash function with extensible output
213+
#[inline(always)]
206214
pub fn xof<D: ExtendableOutput>(&self, h: D) -> Transcript {
207215
let mut prehash = [0u8; 32];
208216
h.xof_result().read(&mut prehash);
@@ -213,6 +221,7 @@ impl SigningContext {
213221

214222
/// Initalize an owned signing transcript on a message provided as
215223
/// a hash function with 256 bit output.
224+
#[inline(always)]
216225
pub fn hash256<D: FixedOutput<OutputSize=U32>>(&self, h: D) -> Transcript {
217226
let mut prehash = [0u8; 32];
218227
prehash.copy_from_slice(h.fixed_result().as_slice());
@@ -223,6 +232,7 @@ impl SigningContext {
223232

224233
/// Initalize an owned signing transcript on a message provided as
225234
/// a hash function with 512 bit output, usually a gross over kill.
235+
#[inline(always)]
226236
pub fn hash512<D: FixedOutput<OutputSize=U64>>(&self, h: D) -> Transcript {
227237
let mut prehash = [0u8; 64];
228238
prehash.copy_from_slice(h.fixed_result().as_slice());
@@ -267,6 +277,7 @@ where H: Input + ExtendableOutput + Clone
267277
/// We intentionally consume and never reexpose the hash function
268278
/// provided, so that our domain seperation works correctly even
269279
/// when using `&mut SimpleTranscript : SigningTranscript`.
280+
#[inline(always)]
270281
pub fn new(h: H) -> SimpleTranscript<H> { SimpleTranscript(h) }
271282
}
272283

src/musig.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use crate::errors::MultiSignatureStage;
5757
/// run in the same sorted ordering as `BTreeMap::iter`/`keys`/etc.
5858
/// We avoided a context: &'static [u8] here and in callers becuase they
5959
/// seem irreevant to the security arguments in the MuSig paper.
60+
#[inline(always)]
6061
fn commit_public_keys<'a,I>(keys: I) -> Transcript
6162
where I: Iterator<Item=&'a PublicKey>
6263
{

src/vrf.rs

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub trait VRFSigningTranscript {
125125

126126
impl<T> VRFSigningTranscript for T where T: SigningTranscript {
127127
type T = T;
128+
#[inline(always)]
128129
fn transcript_with_malleability_addressed(mut self, publickey: &PublicKey) -> T {
129130
self.commit_point(b"vrf-nm-pk", publickey.as_compressed());
130131
// publickey.make_transcript_nonmalleable(&mut self);
@@ -146,6 +147,7 @@ impl<T> VRFSigningTranscript for T where T: SigningTranscript {
146147
pub struct Malleable<T: SigningTranscript>(pub T);
147148
impl<T> VRFSigningTranscript for Malleable<T> where T: SigningTranscript {
148149
type T = T;
150+
#[inline(always)]
149151
fn transcript_with_malleability_addressed(self, _publickey: &PublicKey) -> T { self.0 }
150152
}
151153

@@ -347,6 +349,7 @@ impl VRFInOut {
347349
/// when considerable output is required, but it should reduce
348350
/// the final linked binary size slightly, and improves domain
349351
/// separation.
352+
#[inline(always)]
350353
pub fn make_merlin_rng(&self, context: &'static [u8]) -> merlin::TranscriptRng {
351354
// Very insecure hack except for our commit_witness_bytes below
352355
struct ZeroFakeRng;

0 commit comments

Comments
 (0)