monero_ed25519/
unreduced_scalar.rs

1use core::cmp::Ordering;
2use std_shims::{
3  sync::LazyLock,
4  io::{self, *},
5};
6
7use subtle::{Choice, ConstantTimeEq};
8use zeroize::Zeroize;
9
10use monero_io::*;
11
12use crate::Scalar;
13
14/// An unreduced scalar.
15///
16/// While most of modern Monero enforces scalars be reduced, certain legacy parts of the code did
17/// not. These section can generally simply be read as a scalar/reduced into a scalar when the time
18/// comes, yet a couple have non-standard reductions performed.
19///
20/// This struct delays scalar conversions and offers the non-standard reduction.
21#[derive(Clone, Copy, Eq, Debug, Zeroize)]
22pub struct UnreducedScalar([u8; 32]);
23
24impl ConstantTimeEq for UnreducedScalar {
25  fn ct_eq(&self, other: &Self) -> Choice {
26    self.0.ct_eq(&other.0)
27  }
28}
29impl PartialEq for UnreducedScalar {
30  /// This defers to `ConstantTimeEq::ct_eq`.
31  fn eq(&self, other: &Self) -> bool {
32    bool::from(self.ct_eq(other))
33  }
34}
35
36impl UnreducedScalar {
37  /// Read an UnreducedScalar.
38  pub fn read<R: Read>(r: &mut R) -> io::Result<UnreducedScalar> {
39    Ok(UnreducedScalar(read_bytes(r)?))
40  }
41
42  /// Write an UnreducedScalar.
43  pub fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
44    w.write_all(&self.0)
45  }
46
47  fn as_bits(&self) -> [u8; 256] {
48    let mut bits = [0; 256];
49    for (i, bit) in bits.iter_mut().enumerate() {
50      // Using `Choice` here takes advantage of `subtle`'s internal `black_box` function, necessary
51      // as our MSRV doesn't allow us to use `core::hint::black_box`
52      *bit = Choice::from(1 & (self.0[i / 8] >> (i % 8))).unwrap_u8();
53    }
54
55    bits
56  }
57
58  // Computes the non-adjacent form of this scalar with width 5.
59  //
60  // This matches Monero's `slide` function and intentionally gives incorrect outputs under
61  // certain conditions in order to match Monero.
62  //
63  // This function does not execute in constant time and must only be used with public data.
64  fn non_adjacent_form(&self) -> [i8; 256] {
65    let bits = self.as_bits();
66    let mut naf = [0i8; 256];
67    for (b, bit) in bits.into_iter().enumerate() {
68      naf[b] = i8::try_from(bit).expect("bit didn't fit within an i8");
69    }
70
71    for i in 0 .. 256 {
72      if naf[i] != 0 {
73        // if the bit is a one, work our way up through the window
74        // combining the bits with this bit.
75        for b in 1 .. 6 {
76          if (i + b) >= 256 {
77            // if we are at the length of the array then break out
78            // the loop.
79            break;
80          }
81          // potential_carry - the value of the bit at i+b compared to the bit at i
82          let potential_carry = naf[i + b] << b;
83
84          if potential_carry != 0 {
85            if (naf[i] + potential_carry) <= 15 {
86              // if our current "bit" plus the potential carry is less than 16
87              // add it to our current "bit" and set the potential carry bit to 0.
88              naf[i] += potential_carry;
89              naf[i + b] = 0;
90            } else if (naf[i] - potential_carry) >= -15 {
91              // else if our current "bit" minus the potential carry is more than -16
92              // take it away from our current "bit".
93              // we then work our way up through the bits setting ones to zero, when
94              // we hit the first zero we change it to one then stop, this is to factor
95              // in the minus.
96              naf[i] -= potential_carry;
97              #[allow(clippy::needless_range_loop)]
98              for k in (i + b) .. 256 {
99                if naf[k] == 0 {
100                  naf[k] = 1;
101                  break;
102                }
103                naf[k] = 0;
104              }
105            } else {
106              break;
107            }
108          }
109        }
110      }
111    }
112
113    naf
114  }
115
116  /// Recover the scalar that an array of bytes was incorrectly interpreted as by ref10's `slide`
117  /// function (as used by the reference Monero implementation in C++).
118  ///
119  /// For Borromean range proofs, Monero did not check the scalars used were reduced. This led to
120  /// some scalars serialized being interpreted as distinct scalars. This function recovers these
121  /// distinct scalars, as required to verify Borromean range proofs within the Monero protocol.
122  ///
123  /// See <https://github.com/monero-project/monero/issues/8438> for more info.
124  //
125  /// This function does not execute in constant time and must only be used with public data.
126  pub fn ref10_slide_scalar_vartime(&self) -> Scalar {
127    use curve25519_dalek::Scalar as DScalar;
128
129    /// Precomputed scalars used to recover an incorrectly reduced scalar
130    static PRECOMPUTED_SCALARS: LazyLock<[DScalar; 8]> = LazyLock::new(|| {
131      let mut precomputed_scalars = [DScalar::ONE; 8];
132      for (i, scalar) in precomputed_scalars.iter_mut().enumerate().skip(1) {
133        *scalar = DScalar::from(
134          u64::try_from((i * 2) + 1).expect("enumerating more than u64::MAX / 2 items"),
135        );
136      }
137      precomputed_scalars
138    });
139
140    if self.0[31] & 128 == 0 {
141      // Computing the w-NAF of a number can only give an output with 1 more bit than
142      // the number, so even if the number isn't reduced, the `slide` function will be
143      // correct when the last bit isn't set.
144      return Scalar::from(DScalar::from_bytes_mod_order(self.0));
145    }
146
147    let mut recovered = DScalar::ZERO;
148    for &numb in self.non_adjacent_form().iter().rev() {
149      recovered += recovered;
150      match numb.cmp(&0) {
151        Ordering::Greater => {
152          recovered +=
153            PRECOMPUTED_SCALARS[usize::try_from(numb).expect("positive i8 -> usize") / 2];
154        }
155        Ordering::Less => {
156          recovered -=
157            PRECOMPUTED_SCALARS[usize::try_from(-numb).expect("negated negative i8 -> usize") / 2];
158        }
159        Ordering::Equal => (),
160      }
161    }
162    Scalar::from(recovered)
163  }
164}