Repository: dusk-network/bls12_381 (language: rust)

Issue:
Implement Scalar::pow_of_2()
Since I start to see few places where we use this, what about having:

```rust
self.variables[&a] & Scalar::pow_of_2(num_bits) - Scalar::one(),
```

instead?
Not only it's more concise and readable, but the implementation can remove the unnecessary iteration and the array of four `u64` creation in the stack since we just pass a single value (`usize` I assume).

_Originally posted by @ZER0 in https://github.com/dusk-network/plonk/pull/258_


Discussion / hints:

Fix patch:
diff --git a/src/scalar.rs b/src/scalar.rs
index 40b16448..34221340 100644
--- a/src/scalar.rs
+++ b/src/scalar.rs
@@ -605,6 +605,20 @@ impl Scalar {
         res
     }
 
+    /// Computes `2^X` where X is a `u64` without the need to generate
+    // an array in the stack as `pow` & `pow_vartime` require.
+    pub fn pow_of_2(by: u64) -> Self {
+        let two = Scalar::from(2u64);
+        let mut res = Self::one();
+        for i in (0..64).rev() {
+            res = res.square();
+            let mut tmp = res;
+            tmp *= two;
+            res.conditional_assign(&tmp, (((by >> i) & 0x1) as u8).into());
+        }
+        res
+    }
+
     /// Computes the multiplicative inverse of this element,
     /// failing if the element is zero.
     pub fn invert(&self) -> CtOption<Self> {
@@ -1405,3 +1419,11 @@ fn bit_repr() {
         &two_pow_128_bits[..]
     )
 }
+
+#[test]
+fn pow_of_two_test() {
+    let two = Scalar::from(2u64);
+    for i in 0..1000 {
+        assert_eq!(Scalar::pow_of_2(i as u64), two.pow(&[i as u64, 0, 0, 0]));
+    }
+}


Test patch: