From 057c84abd53cb79d75a0a63fbe08d642f04ad4b8 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Fri, 28 Apr 2017 22:51:32 -0700 Subject: [PATCH] Add a bits() function for scalars --- src/scalar.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/scalar.rs b/src/scalar.rs index 8606913..d97efc1 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -207,6 +207,17 @@ impl Scalar { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]) } + /// Get the bits of the scalar. + pub fn bits(&self) -> [i8;256] { + let mut bits = [0i8; 256]; + for i in 0..256 { + // As i runs from 0..256, the bottom 3 bits index the bit, + // while the upper bits index the byte. + bits[i] = ((self.0[i>>3] >> (i&7)) & 1u8) as i8; + } + bits + } + /// Compute a width-5 "Non-Adjacent Form" of this scalar. /// /// A width-`w` NAF of a positive integer `k` is an expression @@ -220,12 +231,7 @@ impl Scalar { /// nonzero coefficients are as sparse as possible. pub fn non_adjacent_form(&self) -> [i8;256] { // Step 1: write out bits of the scalar - let mut naf = [0i8; 256]; - for i in 0..256 { - // As i runs from 0..256, the bottom 3 bits index the bit, - // while the upper bits index the byte. - naf[i] = ((self.0[i>>3] >> (i&7)) & 1u8) as i8; - } + let mut naf = self.bits(); // Step 2: zero coefficients by carrying them upwards or downwards 'bits: for i in 0..256 {