aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / curve25519-dalek / src / backend / serial / u32 / field.rs
| 1 | // -*- mode: rust; -*- |
| 2 | // |
| 3 | // This file is part of curve25519-dalek. |
| 4 | // Copyright (c) 2016-2021 isis lovecruft |
| 5 | // Copyright (c) 2016-2019 Henry de Valence |
| 6 | // See LICENSE for licensing information. |
| 7 | // |
| 8 | // Authors: |
| 9 | // - isis agora lovecruft <isis@patternsinthevoid.net> |
| 10 | // - Henry de Valence <hdevalence@hdevalence.ca> |
| 11 | |
| 12 | //! Field arithmetic modulo \\(p = 2\^{255} - 19\\), using \\(32\\)-bit |
| 13 | //! limbs with \\(64\\)-bit products. |
| 14 | //! |
| 15 | //! This code was originally derived from Adam Langley's Golang ed25519 |
| 16 | //! implementation, and was then rewritten to use unsigned limbs instead |
| 17 | //! of signed limbs. |
| 18 | |
| 19 | use core::fmt::Debug; |
| 20 | use core::ops::Neg; |
| 21 | use core::ops::{Add, AddAssign}; |
| 22 | use core::ops::{Mul, MulAssign}; |
| 23 | use core::ops::{Sub, SubAssign}; |
| 24 | |
| 25 | use subtle::Choice; |
| 26 | use subtle::ConditionallySelectable; |
| 27 | |
| 28 | #[cfg(feature = "zeroize")] |
| 29 | use zeroize::Zeroize; |
| 30 | |
| 31 | /// A `FieldElement2625` represents an element of the field |
| 32 | /// \\( \mathbb Z / (2\^{255} - 19)\\). |
| 33 | /// |
| 34 | /// In the 32-bit implementation, a `FieldElement` is represented in |
| 35 | /// radix \\(2\^{25.5}\\) as ten `u32`s. This means that a field |
| 36 | /// element \\(x\\) is represented as |
| 37 | /// $$ |
| 38 | /// x = \sum\_{i=0}\^9 x\_i 2\^{\lceil i \frac {51} 2 \rceil} |
| 39 | /// = x\_0 + x\_1 2\^{26} + x\_2 2\^{51} + x\_3 2\^{77} + \cdots + x\_9 2\^{230}; |
| 40 | /// $$ |
| 41 | /// the coefficients are alternately bounded by \\(2\^{25}\\) and |
| 42 | /// \\(2\^{26}\\). The limbs are allowed to grow between reductions up |
| 43 | /// to \\(2\^{25+b}\\) or \\(2\^{26+b}\\), where \\(b = 1.75\\). |
| 44 | /// |
| 45 | /// # Note |
| 46 | /// |
| 47 | /// The `curve25519_dalek::field` module provides a type alias |
| 48 | /// `curve25519_dalek::field::FieldElement` to either `FieldElement51` |
| 49 | /// or `FieldElement2625`. |
| 50 | /// |
| 51 | /// The backend-specific type `FieldElement2625` should not be used |
| 52 | /// outside of the `curve25519_dalek::field` module. |
| 53 | #[derive(Copy, Clone)] |
| 54 | pub struct FieldElement2625(pub(crate) [u32; 10]); |
| 55 | |
| 56 | impl Debug for FieldElement2625 { |
| 57 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 58 | write!(f, "FieldElement2625({:?})", &self.0[..]) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #[cfg(feature = "zeroize")] |
| 63 | impl Zeroize for FieldElement2625 { |
| 64 | fn zeroize(&mut self) { |
| 65 | self.0.zeroize(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl<'b> AddAssign<&'b FieldElement2625> for FieldElement2625 { |
| 70 | fn add_assign(&mut self, _rhs: &'b FieldElement2625) { |
| 71 | for i in 0..10 { |
| 72 | self.0[i] += _rhs.0[i]; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl<'a, 'b> Add<&'b FieldElement2625> for &'a FieldElement2625 { |
| 78 | type Output = FieldElement2625; |
| 79 | fn add(self, _rhs: &'b FieldElement2625) -> FieldElement2625 { |
| 80 | let mut output = *self; |
| 81 | output += _rhs; |
| 82 | output |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl<'b> SubAssign<&'b FieldElement2625> for FieldElement2625 { |
| 87 | fn sub_assign(&mut self, _rhs: &'b FieldElement2625) { |
| 88 | // See comment in FieldElement51::Sub |
| 89 | // |
| 90 | // Compute a - b as ((a + 2^4 * p) - b) to avoid underflow. |
| 91 | let b = &_rhs.0; |
| 92 | self.0 = FieldElement2625::reduce([ |
| 93 | ((self.0[0] + (0x3ffffed << 4)) - b[0]) as u64, |
| 94 | ((self.0[1] + (0x1ffffff << 4)) - b[1]) as u64, |
| 95 | ((self.0[2] + (0x3ffffff << 4)) - b[2]) as u64, |
| 96 | ((self.0[3] + (0x1ffffff << 4)) - b[3]) as u64, |
| 97 | ((self.0[4] + (0x3ffffff << 4)) - b[4]) as u64, |
| 98 | ((self.0[5] + (0x1ffffff << 4)) - b[5]) as u64, |
| 99 | ((self.0[6] + (0x3ffffff << 4)) - b[6]) as u64, |
| 100 | ((self.0[7] + (0x1ffffff << 4)) - b[7]) as u64, |
| 101 | ((self.0[8] + (0x3ffffff << 4)) - b[8]) as u64, |
| 102 | ((self.0[9] + (0x1ffffff << 4)) - b[9]) as u64, |
| 103 | ]) |
| 104 | .0; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | impl<'a, 'b> Sub<&'b FieldElement2625> for &'a FieldElement2625 { |
| 109 | type Output = FieldElement2625; |
| 110 | fn sub(self, _rhs: &'b FieldElement2625) -> FieldElement2625 { |
| 111 | let mut output = *self; |
| 112 | output -= _rhs; |
| 113 | output |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | impl<'b> MulAssign<&'b FieldElement2625> for FieldElement2625 { |
| 118 | fn mul_assign(&mut self, _rhs: &'b FieldElement2625) { |
| 119 | let result = (self as &FieldElement2625) * _rhs; |
| 120 | self.0 = result.0; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | impl<'a, 'b> Mul<&'b FieldElement2625> for &'a FieldElement2625 { |
| 125 | type Output = FieldElement2625; |
| 126 | |
| 127 | #[rustfmt::skip] // keep alignment of z* calculations |
| 128 | fn mul(self, _rhs: &'b FieldElement2625) -> FieldElement2625 { |
| 129 | /// Helper function to multiply two 32-bit integers with 64 bits |
| 130 | /// of output. |
| 131 | #[inline(always)] |
| 132 | fn m(x: u32, y: u32) -> u64 { |
| 133 | (x as u64) * (y as u64) |
| 134 | } |
| 135 | |
| 136 | // Alias self, _rhs for more readable formulas |
| 137 | let x: &[u32; 10] = &self.0; |
| 138 | let y: &[u32; 10] = &_rhs.0; |
| 139 | |
| 140 | // We assume that the input limbs x[i], y[i] are bounded by: |
| 141 | // |
| 142 | // x[i], y[i] < 2^(26 + b) if i even |
| 143 | // x[i], y[i] < 2^(25 + b) if i odd |
| 144 | // |
| 145 | // where b is a (real) parameter representing the excess bits of |
| 146 | // the limbs. We track the bitsizes of all variables through |
| 147 | // the computation and solve at the end for the allowable |
| 148 | // headroom bitsize b (which determines how many additions we |
| 149 | // can perform between reductions or multiplications). |
| 150 | |
| 151 | let y1_19 = 19 * y[1]; // This fits in a u32 |
| 152 | let y2_19 = 19 * y[2]; // iff 26 + b + lg(19) < 32 |
| 153 | let y3_19 = 19 * y[3]; // if b < 32 - 26 - 4.248 = 1.752 |
| 154 | let y4_19 = 19 * y[4]; |
| 155 | let y5_19 = 19 * y[5]; // below, b<2.5: this is a bottleneck, |
| 156 | let y6_19 = 19 * y[6]; // could be avoided by promoting to |
| 157 | let y7_19 = 19 * y[7]; // u64 here instead of in m() |
| 158 | let y8_19 = 19 * y[8]; |
| 159 | let y9_19 = 19 * y[9]; |
| 160 | |
| 161 | // What happens when we multiply x[i] with y[j] and place the |
| 162 | // result into the (i+j)-th limb? |
| 163 | // |
| 164 | // x[i] represents the value x[i]*2^ceil(i*51/2) |
| 165 | // y[j] represents the value y[j]*2^ceil(j*51/2) |
| 166 | // z[i+j] represents the value z[i+j]*2^ceil((i+j)*51/2) |
| 167 | // x[i]*y[j] represents the value x[i]*y[i]*2^(ceil(i*51/2)+ceil(j*51/2)) |
| 168 | // |
| 169 | // Since the radix is already accounted for, the result placed |
| 170 | // into the (i+j)-th limb should be |
| 171 | // |
| 172 | // x[i]*y[i]*2^(ceil(i*51/2)+ceil(j*51/2) - ceil((i+j)*51/2)). |
| 173 | // |
| 174 | // The value of ceil(i*51/2)+ceil(j*51/2) - ceil((i+j)*51/2) is |
| 175 | // 1 when both i and j are odd, and 0 otherwise. So we add |
| 176 | // |
| 177 | // x[i]*y[j] if either i or j is even |
| 178 | // 2*x[i]*y[j] if i and j are both odd |
| 179 | // |
| 180 | // by using precomputed multiples of x[i] for odd i: |
| 181 | |
| 182 | let x1_2 = 2 * x[1]; // This fits in a u32 iff 25 + b + 1 < 32 |
| 183 | let x3_2 = 2 * x[3]; // iff b < 6 |
| 184 | let x5_2 = 2 * x[5]; |
| 185 | let x7_2 = 2 * x[7]; |
| 186 | let x9_2 = 2 * x[9]; |
| 187 | |
| 188 | let z0 = m(x[0], y[0]) + m(x1_2, y9_19) + m(x[2], y8_19) + m(x3_2, y7_19) + m(x[4], y6_19) + m(x5_2, y5_19) + m(x[6], y4_19) + m(x7_2, y3_19) + m(x[8], y2_19) + m(x9_2, y1_19); |
| 189 | let z1 = m(x[0], y[1]) + m(x[1], y[0]) + m(x[2], y9_19) + m(x[3], y8_19) + m(x[4], y7_19) + m(x[5], y6_19) + m(x[6], y5_19) + m(x[7], y4_19) + m(x[8], y3_19) + m(x[9], y2_19); |
| 190 | let z2 = m(x[0], y[2]) + m(x1_2, y[1]) + m(x[2], y[0]) + m(x3_2, y9_19) + m(x[4], y8_19) + m(x5_2, y7_19) + m(x[6], y6_19) + m(x7_2, y5_19) + m(x[8], y4_19) + m(x9_2, y3_19); |
| 191 | let z3 = m(x[0], y[3]) + m(x[1], y[2]) + m(x[2], y[1]) + m(x[3], y[0]) + m(x[4], y9_19) + m(x[5], y8_19) + m(x[6], y7_19) + m(x[7], y6_19) + m(x[8], y5_19) + m(x[9], y4_19); |
| 192 | let z4 = m(x[0], y[4]) + m(x1_2, y[3]) + m(x[2], y[2]) + m(x3_2, y[1]) + m(x[4], y[0]) + m(x5_2, y9_19) + m(x[6], y8_19) + m(x7_2, y7_19) + m(x[8], y6_19) + m(x9_2, y5_19); |
| 193 | let z5 = m(x[0], y[5]) + m(x[1], y[4]) + m(x[2], y[3]) + m(x[3], y[2]) + m(x[4], y[1]) + m(x[5], y[0]) + m(x[6], y9_19) + m(x[7], y8_19) + m(x[8], y7_19) + m(x[9], y6_19); |
| 194 | let z6 = m(x[0], y[6]) + m(x1_2, y[5]) + m(x[2], y[4]) + m(x3_2, y[3]) + m(x[4], y[2]) + m(x5_2, y[1]) + m(x[6], y[0]) + m(x7_2, y9_19) + m(x[8], y8_19) + m(x9_2, y7_19); |
| 195 | let z7 = m(x[0], y[7]) + m(x[1], y[6]) + m(x[2], y[5]) + m(x[3], y[4]) + m(x[4], y[3]) + m(x[5], y[2]) + m(x[6], y[1]) + m(x[7], y[0]) + m(x[8], y9_19) + m(x[9], y8_19); |
| 196 | let z8 = m(x[0], y[8]) + m(x1_2, y[7]) + m(x[2], y[6]) + m(x3_2, y[5]) + m(x[4], y[4]) + m(x5_2, y[3]) + m(x[6], y[2]) + m(x7_2, y[1]) + m(x[8], y[0]) + m(x9_2, y9_19); |
| 197 | let z9 = m(x[0], y[9]) + m(x[1], y[8]) + m(x[2], y[7]) + m(x[3], y[6]) + m(x[4], y[5]) + m(x[5], y[4]) + m(x[6], y[3]) + m(x[7], y[2]) + m(x[8], y[1]) + m(x[9], y[0]); |
| 198 | |
| 199 | // How big is the contribution to z[i+j] from x[i], y[j]? |
| 200 | // |
| 201 | // Using the bounds above, we get: |
| 202 | // |
| 203 | // i even, j even: x[i]*y[j] < 2^(26+b)*2^(26+b) = 2*2^(51+2*b) |
| 204 | // i odd, j even: x[i]*y[j] < 2^(25+b)*2^(26+b) = 1*2^(51+2*b) |
| 205 | // i even, j odd: x[i]*y[j] < 2^(26+b)*2^(25+b) = 1*2^(51+2*b) |
| 206 | // i odd, j odd: 2*x[i]*y[j] < 2*2^(25+b)*2^(25+b) = 1*2^(51+2*b) |
| 207 | // |
| 208 | // We perform inline reduction mod p by replacing 2^255 by 19 |
| 209 | // (since 2^255 - 19 = 0 mod p). This adds a factor of 19, so |
| 210 | // we get the bounds (z0 is the biggest one, but calculated for |
| 211 | // posterity here in case finer estimation is needed later): |
| 212 | // |
| 213 | // z0 < ( 2 + 1*19 + 2*19 + 1*19 + 2*19 + 1*19 + 2*19 + 1*19 + 2*19 + 1*19 )*2^(51 + 2b) = 249*2^(51 + 2*b) |
| 214 | // z1 < ( 1 + 1 + 1*19 + 1*19 + 1*19 + 1*19 + 1*19 + 1*19 + 1*19 + 1*19 )*2^(51 + 2b) = 154*2^(51 + 2*b) |
| 215 | // z2 < ( 2 + 1 + 2 + 1*19 + 2*19 + 1*19 + 2*19 + 1*19 + 2*19 + 1*19 )*2^(51 + 2b) = 195*2^(51 + 2*b) |
| 216 | // z3 < ( 1 + 1 + 1 + 1 + 1*19 + 1*19 + 1*19 + 1*19 + 1*19 + 1*19 )*2^(51 + 2b) = 118*2^(51 + 2*b) |
| 217 | // z4 < ( 2 + 1 + 2 + 1 + 2 + 1*19 + 2*19 + 1*19 + 2*19 + 1*19 )*2^(51 + 2b) = 141*2^(51 + 2*b) |
| 218 | // z5 < ( 1 + 1 + 1 + 1 + 1 + 1 + 1*19 + 1*19 + 1*19 + 1*19 )*2^(51 + 2b) = 82*2^(51 + 2*b) |
| 219 | // z6 < ( 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1*19 + 2*19 + 1*19 )*2^(51 + 2b) = 87*2^(51 + 2*b) |
| 220 | // z7 < ( 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1*19 + 1*19 )*2^(51 + 2b) = 46*2^(51 + 2*b) |
| 221 | // z6 < ( 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1*19 )*2^(51 + 2b) = 33*2^(51 + 2*b) |
| 222 | // z7 < ( 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 )*2^(51 + 2b) = 10*2^(51 + 2*b) |
| 223 | // |
| 224 | // So z[0] fits into a u64 if 51 + 2*b + lg(249) < 64 |
| 225 | // if b < 2.5. |
| 226 | FieldElement2625::reduce([z0, z1, z2, z3, z4, z5, z6, z7, z8, z9]) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | impl<'a> Neg for &'a FieldElement2625 { |
| 231 | type Output = FieldElement2625; |
| 232 | fn neg(self) -> FieldElement2625 { |
| 233 | let mut output = *self; |
| 234 | output.negate(); |
| 235 | output |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | impl ConditionallySelectable for FieldElement2625 { |
| 240 | fn conditional_select( |
| 241 | a: &FieldElement2625, |
| 242 | b: &FieldElement2625, |
| 243 | choice: Choice, |
| 244 | ) -> FieldElement2625 { |
| 245 | FieldElement2625([ |
| 246 | u32::conditional_select(&a.0[0], &b.0[0], choice), |
| 247 | u32::conditional_select(&a.0[1], &b.0[1], choice), |
| 248 | u32::conditional_select(&a.0[2], &b.0[2], choice), |
| 249 | u32::conditional_select(&a.0[3], &b.0[3], choice), |
| 250 | u32::conditional_select(&a.0[4], &b.0[4], choice), |
| 251 | u32::conditional_select(&a.0[5], &b.0[5], choice), |
| 252 | u32::conditional_select(&a.0[6], &b.0[6], choice), |
| 253 | u32::conditional_select(&a.0[7], &b.0[7], choice), |
| 254 | u32::conditional_select(&a.0[8], &b.0[8], choice), |
| 255 | u32::conditional_select(&a.0[9], &b.0[9], choice), |
| 256 | ]) |
| 257 | } |
| 258 | |
| 259 | fn conditional_assign(&mut self, other: &FieldElement2625, choice: Choice) { |
| 260 | self.0[0].conditional_assign(&other.0[0], choice); |
| 261 | self.0[1].conditional_assign(&other.0[1], choice); |
| 262 | self.0[2].conditional_assign(&other.0[2], choice); |
| 263 | self.0[3].conditional_assign(&other.0[3], choice); |
| 264 | self.0[4].conditional_assign(&other.0[4], choice); |
| 265 | self.0[5].conditional_assign(&other.0[5], choice); |
| 266 | self.0[6].conditional_assign(&other.0[6], choice); |
| 267 | self.0[7].conditional_assign(&other.0[7], choice); |
| 268 | self.0[8].conditional_assign(&other.0[8], choice); |
| 269 | self.0[9].conditional_assign(&other.0[9], choice); |
| 270 | } |
| 271 | |
| 272 | fn conditional_swap(a: &mut FieldElement2625, b: &mut FieldElement2625, choice: Choice) { |
| 273 | u32::conditional_swap(&mut a.0[0], &mut b.0[0], choice); |
| 274 | u32::conditional_swap(&mut a.0[1], &mut b.0[1], choice); |
| 275 | u32::conditional_swap(&mut a.0[2], &mut b.0[2], choice); |
| 276 | u32::conditional_swap(&mut a.0[3], &mut b.0[3], choice); |
| 277 | u32::conditional_swap(&mut a.0[4], &mut b.0[4], choice); |
| 278 | u32::conditional_swap(&mut a.0[5], &mut b.0[5], choice); |
| 279 | u32::conditional_swap(&mut a.0[6], &mut b.0[6], choice); |
| 280 | u32::conditional_swap(&mut a.0[7], &mut b.0[7], choice); |
| 281 | u32::conditional_swap(&mut a.0[8], &mut b.0[8], choice); |
| 282 | u32::conditional_swap(&mut a.0[9], &mut b.0[9], choice); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | impl FieldElement2625 { |
| 287 | pub(crate) const fn from_limbs(limbs: [u32; 10]) -> FieldElement2625 { |
| 288 | FieldElement2625(limbs) |
| 289 | } |
| 290 | |
| 291 | /// The scalar \\( 0 \\). |
| 292 | pub const ZERO: FieldElement2625 = FieldElement2625::from_limbs([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); |
| 293 | /// The scalar \\( 1 \\). |
| 294 | pub const ONE: FieldElement2625 = FieldElement2625::from_limbs([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]); |
| 295 | /// The scalar \\( -1 \\). |
| 296 | pub const MINUS_ONE: FieldElement2625 = FieldElement2625::from_limbs([ |
| 297 | 0x3ffffec, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, 0x3ffffff, 0x1ffffff, |
| 298 | 0x3ffffff, 0x1ffffff, |
| 299 | ]); |
| 300 | |
| 301 | /// Invert the sign of this field element |
| 302 | pub fn negate(&mut self) { |
| 303 | // Compute -b as ((2^4 * p) - b) to avoid underflow. |
| 304 | let neg = FieldElement2625::reduce([ |
| 305 | ((0x3ffffed << 4) - self.0[0]) as u64, |
| 306 | ((0x1ffffff << 4) - self.0[1]) as u64, |
| 307 | ((0x3ffffff << 4) - self.0[2]) as u64, |
| 308 | ((0x1ffffff << 4) - self.0[3]) as u64, |
| 309 | ((0x3ffffff << 4) - self.0[4]) as u64, |
| 310 | ((0x1ffffff << 4) - self.0[5]) as u64, |
| 311 | ((0x3ffffff << 4) - self.0[6]) as u64, |
| 312 | ((0x1ffffff << 4) - self.0[7]) as u64, |
| 313 | ((0x3ffffff << 4) - self.0[8]) as u64, |
| 314 | ((0x1ffffff << 4) - self.0[9]) as u64, |
| 315 | ]); |
| 316 | self.0 = neg.0; |
| 317 | } |
| 318 | |
| 319 | /// Given `k > 0`, return `self^(2^k)`. |
| 320 | pub fn pow2k(&self, k: u32) -> FieldElement2625 { |
| 321 | debug_assert!(k > 0); |
| 322 | let mut z = self.square(); |
| 323 | for _ in 1..k { |
| 324 | z = z.square(); |
| 325 | } |
| 326 | z |
| 327 | } |
| 328 | |
| 329 | /// Given unreduced coefficients `z[0], ..., z[9]` of any size, |
| 330 | /// carry and reduce them mod p to obtain a `FieldElement2625` |
| 331 | /// whose coefficients have excess `b < 0.007`. |
| 332 | /// |
| 333 | /// In other words, each coefficient of the result is bounded by |
| 334 | /// either `2^(25 + 0.007)` or `2^(26 + 0.007)`, as appropriate. |
| 335 | #[rustfmt::skip] // keep alignment of carry chain |
| 336 | fn reduce(mut z: [u64; 10]) -> FieldElement2625 { |
| 337 | |
| 338 | const LOW_25_BITS: u64 = (1 << 25) - 1; |
| 339 | const LOW_26_BITS: u64 = (1 << 26) - 1; |
| 340 | |
| 341 | /// Carry the value from limb i = 0..8 to limb i+1 |
| 342 | #[inline(always)] |
| 343 | fn carry(z: &mut [u64; 10], i: usize) { |
| 344 | debug_assert!(i < 9); |
| 345 | if i % 2 == 0 { |
| 346 | // Even limbs have 26 bits |
| 347 | z[i + 1] += z[i] >> 26; |
| 348 | z[i] &= LOW_26_BITS; |
| 349 | } else { |
| 350 | // Odd limbs have 25 bits |
| 351 | z[i + 1] += z[i] >> 25; |
| 352 | z[i] &= LOW_25_BITS; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Perform two halves of the carry chain in parallel. |
| 357 | carry(&mut z, 0); carry(&mut z, 4); |
| 358 | carry(&mut z, 1); carry(&mut z, 5); |
| 359 | carry(&mut z, 2); carry(&mut z, 6); |
| 360 | carry(&mut z, 3); carry(&mut z, 7); |
| 361 | // Since z[3] < 2^64, c < 2^(64-25) = 2^39, |
| 362 | // so z[4] < 2^26 + 2^39 < 2^39.0002 |
| 363 | carry(&mut z, 4); carry(&mut z, 8); |
| 364 | // Now z[4] < 2^26 |
| 365 | // and z[5] < 2^25 + 2^13.0002 < 2^25.0004 (good enough) |
| 366 | |
| 367 | // Last carry has a multiplication by 19: |
| 368 | z[0] += 19 * (z[9] >> 25); |
| 369 | z[9] &= LOW_25_BITS; |
| 370 | |
| 371 | // Since z[9] < 2^64, c < 2^(64-25) = 2^39, |
| 372 | // so z[0] + 19*c < 2^26 + 2^43.248 < 2^43.249. |
| 373 | carry(&mut z, 0); |
| 374 | // Now z[1] < 2^25 - 2^(43.249 - 26) |
| 375 | // < 2^25.007 (good enough) |
| 376 | // and we're done. |
| 377 | |
| 378 | FieldElement2625([ |
| 379 | z[0] as u32, |
| 380 | z[1] as u32, |
| 381 | z[2] as u32, |
| 382 | z[3] as u32, |
| 383 | z[4] as u32, |
| 384 | z[5] as u32, |
| 385 | z[6] as u32, |
| 386 | z[7] as u32, |
| 387 | z[8] as u32, |
| 388 | z[9] as u32, |
| 389 | ]) |
| 390 | } |
| 391 | |
| 392 | /// Load a `FieldElement51` from the low 255 bits of a 256-bit |
| 393 | /// input. |
| 394 | /// |
| 395 | /// # Warning |
| 396 | /// |
| 397 | /// This function does not check that the input used the canonical |
| 398 | /// representative. It masks the high bit, but it will happily |
| 399 | /// decode 2^255 - 18 to 1. Applications that require a canonical |
| 400 | /// encoding of every field element should decode, re-encode to |
| 401 | /// the canonical encoding, and check that the input was |
| 402 | /// canonical. |
| 403 | #[rustfmt::skip] // keep alignment of h[*] values |
| 404 | pub fn from_bytes(data: &[u8; 32]) -> FieldElement2625 { |
| 405 | #[inline] |
| 406 | fn load3(b: &[u8]) -> u64 { |
| 407 | (b[0] as u64) | ((b[1] as u64) << 8) | ((b[2] as u64) << 16) |
| 408 | } |
| 409 | |
| 410 | #[inline] |
| 411 | fn load4(b: &[u8]) -> u64 { |
| 412 | (b[0] as u64) | ((b[1] as u64) << 8) | ((b[2] as u64) << 16) | ((b[3] as u64) << 24) |
| 413 | } |
| 414 | |
| 415 | let mut h = [0u64;10]; |
| 416 | const LOW_23_BITS: u64 = (1 << 23) - 1; |
| 417 | h[0] = load4(&data[ 0..]); |
| 418 | h[1] = load3(&data[ 4..]) << 6; |
| 419 | h[2] = load3(&data[ 7..]) << 5; |
| 420 | h[3] = load3(&data[10..]) << 3; |
| 421 | h[4] = load3(&data[13..]) << 2; |
| 422 | h[5] = load4(&data[16..]); |
| 423 | h[6] = load3(&data[20..]) << 7; |
| 424 | h[7] = load3(&data[23..]) << 5; |
| 425 | h[8] = load3(&data[26..]) << 4; |
| 426 | h[9] = (load3(&data[29..]) & LOW_23_BITS) << 2; |
| 427 | |
| 428 | FieldElement2625::reduce(h) |
| 429 | } |
| 430 | |
| 431 | /// Serialize this `FieldElement51` to a 32-byte array. The |
| 432 | /// encoding is canonical. |
| 433 | #[allow(clippy::identity_op)] |
| 434 | pub fn as_bytes(&self) -> [u8; 32] { |
| 435 | let inp = &self.0; |
| 436 | // Reduce the value represented by `in` to the range [0,2*p) |
| 437 | let mut h: [u32; 10] = FieldElement2625::reduce([ |
| 438 | // XXX this cast is annoying |
| 439 | inp[0] as u64, |
| 440 | inp[1] as u64, |
| 441 | inp[2] as u64, |
| 442 | inp[3] as u64, |
| 443 | inp[4] as u64, |
| 444 | inp[5] as u64, |
| 445 | inp[6] as u64, |
| 446 | inp[7] as u64, |
| 447 | inp[8] as u64, |
| 448 | inp[9] as u64, |
| 449 | ]) |
| 450 | .0; |
| 451 | |
| 452 | // Let h be the value to encode. |
| 453 | // |
| 454 | // Write h = pq + r with 0 <= r < p. We want to compute r = h mod p. |
| 455 | // |
| 456 | // Since h < 2*p, q = 0 or 1, with q = 0 when h < p and q = 1 when h >= p. |
| 457 | // |
| 458 | // Notice that h >= p <==> h + 19 >= p + 19 <==> h + 19 >= 2^255. |
| 459 | // Therefore q can be computed as the carry bit of h + 19. |
| 460 | |
| 461 | let mut q: u32 = (h[0] + 19) >> 26; |
| 462 | q = (h[1] + q) >> 25; |
| 463 | q = (h[2] + q) >> 26; |
| 464 | q = (h[3] + q) >> 25; |
| 465 | q = (h[4] + q) >> 26; |
| 466 | q = (h[5] + q) >> 25; |
| 467 | q = (h[6] + q) >> 26; |
| 468 | q = (h[7] + q) >> 25; |
| 469 | q = (h[8] + q) >> 26; |
| 470 | q = (h[9] + q) >> 25; |
| 471 | |
| 472 | debug_assert!(q == 0 || q == 1); |
| 473 | |
| 474 | // Now we can compute r as r = h - pq = r - (2^255-19)q = r + 19q - 2^255q |
| 475 | |
| 476 | const LOW_25_BITS: u32 = (1 << 25) - 1; |
| 477 | const LOW_26_BITS: u32 = (1 << 26) - 1; |
| 478 | |
| 479 | h[0] += 19 * q; |
| 480 | |
| 481 | // Now carry the result to compute r + 19q... |
| 482 | h[1] += h[0] >> 26; |
| 483 | h[0] &= LOW_26_BITS; |
| 484 | h[2] += h[1] >> 25; |
| 485 | h[1] &= LOW_25_BITS; |
| 486 | h[3] += h[2] >> 26; |
| 487 | h[2] &= LOW_26_BITS; |
| 488 | h[4] += h[3] >> 25; |
| 489 | h[3] &= LOW_25_BITS; |
| 490 | h[5] += h[4] >> 26; |
| 491 | h[4] &= LOW_26_BITS; |
| 492 | h[6] += h[5] >> 25; |
| 493 | h[5] &= LOW_25_BITS; |
| 494 | h[7] += h[6] >> 26; |
| 495 | h[6] &= LOW_26_BITS; |
| 496 | h[8] += h[7] >> 25; |
| 497 | h[7] &= LOW_25_BITS; |
| 498 | h[9] += h[8] >> 26; |
| 499 | h[8] &= LOW_26_BITS; |
| 500 | |
| 501 | // ... but instead of carrying the value |
| 502 | // (h[9] >> 25) = q*2^255 into another limb, |
| 503 | // discard it, subtracting the value from h. |
| 504 | debug_assert!((h[9] >> 25) == 0 || (h[9] >> 25) == 1); |
| 505 | h[9] &= LOW_25_BITS; |
| 506 | |
| 507 | let mut s = [0u8; 32]; |
| 508 | s[0] = (h[0] >> 0) as u8; |
| 509 | s[1] = (h[0] >> 8) as u8; |
| 510 | s[2] = (h[0] >> 16) as u8; |
| 511 | s[3] = ((h[0] >> 24) | (h[1] << 2)) as u8; |
| 512 | s[4] = (h[1] >> 6) as u8; |
| 513 | s[5] = (h[1] >> 14) as u8; |
| 514 | s[6] = ((h[1] >> 22) | (h[2] << 3)) as u8; |
| 515 | s[7] = (h[2] >> 5) as u8; |
| 516 | s[8] = (h[2] >> 13) as u8; |
| 517 | s[9] = ((h[2] >> 21) | (h[3] << 5)) as u8; |
| 518 | s[10] = (h[3] >> 3) as u8; |
| 519 | s[11] = (h[3] >> 11) as u8; |
| 520 | s[12] = ((h[3] >> 19) | (h[4] << 6)) as u8; |
| 521 | s[13] = (h[4] >> 2) as u8; |
| 522 | s[14] = (h[4] >> 10) as u8; |
| 523 | s[15] = (h[4] >> 18) as u8; |
| 524 | s[16] = (h[5] >> 0) as u8; |
| 525 | s[17] = (h[5] >> 8) as u8; |
| 526 | s[18] = (h[5] >> 16) as u8; |
| 527 | s[19] = ((h[5] >> 24) | (h[6] << 1)) as u8; |
| 528 | s[20] = (h[6] >> 7) as u8; |
| 529 | s[21] = (h[6] >> 15) as u8; |
| 530 | s[22] = ((h[6] >> 23) | (h[7] << 3)) as u8; |
| 531 | s[23] = (h[7] >> 5) as u8; |
| 532 | s[24] = (h[7] >> 13) as u8; |
| 533 | s[25] = ((h[7] >> 21) | (h[8] << 4)) as u8; |
| 534 | s[26] = (h[8] >> 4) as u8; |
| 535 | s[27] = (h[8] >> 12) as u8; |
| 536 | s[28] = ((h[8] >> 20) | (h[9] << 6)) as u8; |
| 537 | s[29] = (h[9] >> 2) as u8; |
| 538 | s[30] = (h[9] >> 10) as u8; |
| 539 | s[31] = (h[9] >> 18) as u8; |
| 540 | |
| 541 | // Check that high bit is cleared |
| 542 | debug_assert!((s[31] & 0b1000_0000u8) == 0u8); |
| 543 | |
| 544 | s |
| 545 | } |
| 546 | |
| 547 | #[rustfmt::skip] // keep alignment of z* calculations |
| 548 | fn square_inner(&self) -> [u64; 10] { |
| 549 | // Optimized version of multiplication for the case of squaring. |
| 550 | // Pre- and post- conditions identical to multiplication function. |
| 551 | let x = &self.0; |
| 552 | let x0_2 = 2 * x[0]; |
| 553 | let x1_2 = 2 * x[1]; |
| 554 | let x2_2 = 2 * x[2]; |
| 555 | let x3_2 = 2 * x[3]; |
| 556 | let x4_2 = 2 * x[4]; |
| 557 | let x5_2 = 2 * x[5]; |
| 558 | let x6_2 = 2 * x[6]; |
| 559 | let x7_2 = 2 * x[7]; |
| 560 | let x5_19 = 19 * x[5]; |
| 561 | let x6_19 = 19 * x[6]; |
| 562 | let x7_19 = 19 * x[7]; |
| 563 | let x8_19 = 19 * x[8]; |
| 564 | let x9_19 = 19 * x[9]; |
| 565 | |
| 566 | /// Helper function to multiply two 32-bit integers with 64 bits |
| 567 | /// of output. |
| 568 | #[inline(always)] |
| 569 | fn m(x: u32, y: u32) -> u64 { |
| 570 | (x as u64) * (y as u64) |
| 571 | } |
| 572 | |
| 573 | // This block is rearranged so that instead of doing a 32-bit multiplication by 38, we do a |
| 574 | // 64-bit multiplication by 2 on the results. This is because lg(38) is too big: we would |
| 575 | // have less than 1 bit of headroom left, which is too little. |
| 576 | let mut z = [0u64; 10]; |
| 577 | z[0] = m(x[0], x[0]) + m(x2_2, x8_19) + m(x4_2, x6_19) + (m(x1_2, x9_19) + m(x3_2, x7_19) + m(x[5], x5_19)) * 2; |
| 578 | z[1] = m(x0_2, x[1]) + m(x3_2, x8_19) + m(x5_2, x6_19) + (m(x[2], x9_19) + m(x[4], x7_19) ) * 2; |
| 579 | z[2] = m(x0_2, x[2]) + m(x1_2, x[1]) + m(x4_2, x8_19) + m(x[6], x6_19) + (m(x3_2, x9_19) + m(x5_2, x7_19)) * 2; |
| 580 | z[3] = m(x0_2, x[3]) + m(x1_2, x[2]) + m(x5_2, x8_19) + (m(x[4], x9_19) + m(x[6], x7_19) ) * 2; |
| 581 | z[4] = m(x0_2, x[4]) + m(x1_2, x3_2) + m(x[2], x[2]) + m(x6_2, x8_19) + (m(x5_2, x9_19) + m(x[7], x7_19)) * 2; |
| 582 | z[5] = m(x0_2, x[5]) + m(x1_2, x[4]) + m(x2_2, x[3]) + m(x7_2, x8_19) + m(x[6], x9_19) * 2; |
| 583 | z[6] = m(x0_2, x[6]) + m(x1_2, x5_2) + m(x2_2, x[4]) + m(x3_2, x[3]) + m(x[8], x8_19) + m(x7_2, x9_19) * 2; |
| 584 | z[7] = m(x0_2, x[7]) + m(x1_2, x[6]) + m(x2_2, x[5]) + m(x3_2, x[4]) + m(x[8], x9_19) * 2; |
| 585 | z[8] = m(x0_2, x[8]) + m(x1_2, x7_2) + m(x2_2, x[6]) + m(x3_2, x5_2) + m(x[4], x[4]) + m(x[9], x9_19) * 2; |
| 586 | z[9] = m(x0_2, x[9]) + m(x1_2, x[8]) + m(x2_2, x[7]) + m(x3_2, x[6]) + m(x4_2, x[5]) ; |
| 587 | |
| 588 | z |
| 589 | } |
| 590 | |
| 591 | /// Compute `self^2`. |
| 592 | pub fn square(&self) -> FieldElement2625 { |
| 593 | FieldElement2625::reduce(self.square_inner()) |
| 594 | } |
| 595 | |
| 596 | /// Compute `2*self^2`. |
| 597 | pub fn square2(&self) -> FieldElement2625 { |
| 598 | let mut coeffs = self.square_inner(); |
| 599 | for coeff in &mut coeffs { |
| 600 | *coeff += *coeff; |
| 601 | } |
| 602 | FieldElement2625::reduce(coeffs) |
| 603 | } |
| 604 | } |