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 / fiat_u64 / field.rs
| 1 | // -*- mode: rust; coding: utf-8; -*- |
| 2 | // |
| 3 | // This file is part of curve25519-dalek. |
| 4 | // Copyright (c) 2016-2018 Isis Lovecruft, Henry de Valence |
| 5 | // See LICENSE for licensing information. |
| 6 | // |
| 7 | // Authors: |
| 8 | // - Isis Agora Lovecruft <isis@patternsinthevoid.net> |
| 9 | // - Henry de Valence <hdevalence@hdevalence.ca> |
| 10 | |
| 11 | //! Field arithmetic modulo \\(p = 2\^{255} - 19\\), using \\(64\\)-bit |
| 12 | //! limbs with \\(128\\)-bit products. |
| 13 | //! |
| 14 | //! This uses the formally-verified field arithmetic generated by the |
| 15 | //! [fiat-crypto project](https://github.com/mit-plv/fiat-crypto) |
| 16 | |
| 17 | use core::fmt::Debug; |
| 18 | use core::ops::Neg; |
| 19 | use core::ops::{Add, AddAssign}; |
| 20 | use core::ops::{Mul, MulAssign}; |
| 21 | use core::ops::{Sub, SubAssign}; |
| 22 | |
| 23 | use subtle::Choice; |
| 24 | use subtle::ConditionallySelectable; |
| 25 | |
| 26 | #[cfg(feature = "zeroize")] |
| 27 | use zeroize::Zeroize; |
| 28 | |
| 29 | use fiat_crypto::curve25519_64::*; |
| 30 | |
| 31 | /// A `FieldElement51` represents an element of the field |
| 32 | /// \\( \mathbb Z / (2\^{255} - 19)\\). |
| 33 | /// |
| 34 | /// In the 64-bit implementation, a `FieldElement` is represented in |
| 35 | /// radix \\(2\^{51}\\) as five `u64`s; the coefficients are allowed to |
| 36 | /// grow up to \\(2\^{54}\\) between reductions modulo \\(p\\). |
| 37 | /// |
| 38 | /// # Note |
| 39 | /// |
| 40 | /// The `curve25519_dalek::field` module provides a type alias |
| 41 | /// `curve25519_dalek::field::FieldElement` to either `FieldElement51` |
| 42 | /// or `FieldElement2625`. |
| 43 | /// |
| 44 | /// The backend-specific type `FieldElement51` should not be used |
| 45 | /// outside of the `curve25519_dalek::field` module. |
| 46 | #[derive(Copy, Clone)] |
| 47 | pub struct FieldElement51(pub(crate) fiat_25519_tight_field_element); |
| 48 | |
| 49 | impl Debug for FieldElement51 { |
| 50 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 51 | write!(f, "FieldElement51({:?})", &(self.0).0[..]) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[cfg(feature = "zeroize")] |
| 56 | impl Zeroize for FieldElement51 { |
| 57 | fn zeroize(&mut self) { |
| 58 | (self.0).0.zeroize(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl<'b> AddAssign<&'b FieldElement51> for FieldElement51 { |
| 63 | fn add_assign(&mut self, rhs: &'b FieldElement51) { |
| 64 | let mut result_loose = fiat_25519_loose_field_element([0; 5]); |
| 65 | fiat_25519_add(&mut result_loose, &self.0, &rhs.0); |
| 66 | fiat_25519_carry(&mut self.0, &result_loose); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl<'a, 'b> Add<&'b FieldElement51> for &'a FieldElement51 { |
| 71 | type Output = FieldElement51; |
| 72 | fn add(self, rhs: &'b FieldElement51) -> FieldElement51 { |
| 73 | let mut result_loose = fiat_25519_loose_field_element([0; 5]); |
| 74 | fiat_25519_add(&mut result_loose, &self.0, &rhs.0); |
| 75 | let mut output = FieldElement51::ZERO; |
| 76 | fiat_25519_carry(&mut output.0, &result_loose); |
| 77 | output |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl<'b> SubAssign<&'b FieldElement51> for FieldElement51 { |
| 82 | fn sub_assign(&mut self, rhs: &'b FieldElement51) { |
| 83 | let mut result_loose = fiat_25519_loose_field_element([0; 5]); |
| 84 | fiat_25519_sub(&mut result_loose, &self.0, &rhs.0); |
| 85 | fiat_25519_carry(&mut self.0, &result_loose); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl<'a, 'b> Sub<&'b FieldElement51> for &'a FieldElement51 { |
| 90 | type Output = FieldElement51; |
| 91 | fn sub(self, rhs: &'b FieldElement51) -> FieldElement51 { |
| 92 | let mut result_loose = fiat_25519_loose_field_element([0; 5]); |
| 93 | fiat_25519_sub(&mut result_loose, &self.0, &rhs.0); |
| 94 | let mut output = FieldElement51::ZERO; |
| 95 | fiat_25519_carry(&mut output.0, &result_loose); |
| 96 | output |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl<'b> MulAssign<&'b FieldElement51> for FieldElement51 { |
| 101 | fn mul_assign(&mut self, rhs: &'b FieldElement51) { |
| 102 | let mut self_loose = fiat_25519_loose_field_element([0; 5]); |
| 103 | fiat_25519_relax(&mut self_loose, &self.0); |
| 104 | let mut rhs_loose = fiat_25519_loose_field_element([0; 5]); |
| 105 | fiat_25519_relax(&mut rhs_loose, &rhs.0); |
| 106 | fiat_25519_carry_mul(&mut self.0, &self_loose, &rhs_loose); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl<'a, 'b> Mul<&'b FieldElement51> for &'a FieldElement51 { |
| 111 | type Output = FieldElement51; |
| 112 | fn mul(self, rhs: &'b FieldElement51) -> FieldElement51 { |
| 113 | let mut self_loose = fiat_25519_loose_field_element([0; 5]); |
| 114 | fiat_25519_relax(&mut self_loose, &self.0); |
| 115 | let mut rhs_loose = fiat_25519_loose_field_element([0; 5]); |
| 116 | fiat_25519_relax(&mut rhs_loose, &rhs.0); |
| 117 | let mut output = FieldElement51::ZERO; |
| 118 | fiat_25519_carry_mul(&mut output.0, &self_loose, &rhs_loose); |
| 119 | output |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | impl<'a> Neg for &'a FieldElement51 { |
| 124 | type Output = FieldElement51; |
| 125 | fn neg(self) -> FieldElement51 { |
| 126 | let mut output_loose = fiat_25519_loose_field_element([0; 5]); |
| 127 | fiat_25519_opp(&mut output_loose, &self.0); |
| 128 | let mut output = FieldElement51::ZERO; |
| 129 | fiat_25519_carry(&mut output.0, &output_loose); |
| 130 | output |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl ConditionallySelectable for FieldElement51 { |
| 135 | fn conditional_select( |
| 136 | a: &FieldElement51, |
| 137 | b: &FieldElement51, |
| 138 | choice: Choice, |
| 139 | ) -> FieldElement51 { |
| 140 | let mut output = fiat_25519_tight_field_element([0u64; 5]); |
| 141 | fiat_25519_selectznz( |
| 142 | &mut output.0, |
| 143 | choice.unwrap_u8() as fiat_25519_u1, |
| 144 | &(a.0).0, |
| 145 | &(b.0).0, |
| 146 | ); |
| 147 | FieldElement51(output) |
| 148 | } |
| 149 | |
| 150 | fn conditional_swap(a: &mut FieldElement51, b: &mut FieldElement51, choice: Choice) { |
| 151 | u64::conditional_swap(&mut a.0[0], &mut b.0[0], choice); |
| 152 | u64::conditional_swap(&mut a.0[1], &mut b.0[1], choice); |
| 153 | u64::conditional_swap(&mut a.0[2], &mut b.0[2], choice); |
| 154 | u64::conditional_swap(&mut a.0[3], &mut b.0[3], choice); |
| 155 | u64::conditional_swap(&mut a.0[4], &mut b.0[4], choice); |
| 156 | } |
| 157 | |
| 158 | fn conditional_assign(&mut self, rhs: &FieldElement51, choice: Choice) { |
| 159 | let mut output = [0u64; 5]; |
| 160 | let choicebit = choice.unwrap_u8() as fiat_25519_u1; |
| 161 | fiat_25519_cmovznz_u64(&mut output[0], choicebit, self.0[0], rhs.0[0]); |
| 162 | fiat_25519_cmovznz_u64(&mut output[1], choicebit, self.0[1], rhs.0[1]); |
| 163 | fiat_25519_cmovznz_u64(&mut output[2], choicebit, self.0[2], rhs.0[2]); |
| 164 | fiat_25519_cmovznz_u64(&mut output[3], choicebit, self.0[3], rhs.0[3]); |
| 165 | fiat_25519_cmovznz_u64(&mut output[4], choicebit, self.0[4], rhs.0[4]); |
| 166 | *self = FieldElement51::from_limbs(output); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl FieldElement51 { |
| 171 | pub(crate) const fn from_limbs(limbs: [u64; 5]) -> FieldElement51 { |
| 172 | FieldElement51(fiat_25519_tight_field_element(limbs)) |
| 173 | } |
| 174 | |
| 175 | /// The scalar \\( 0 \\). |
| 176 | pub const ZERO: FieldElement51 = FieldElement51::from_limbs([0, 0, 0, 0, 0]); |
| 177 | /// The scalar \\( 1 \\). |
| 178 | pub const ONE: FieldElement51 = FieldElement51::from_limbs([1, 0, 0, 0, 0]); |
| 179 | /// The scalar \\( -1 \\). |
| 180 | pub const MINUS_ONE: FieldElement51 = FieldElement51::from_limbs([ |
| 181 | 2251799813685228, |
| 182 | 2251799813685247, |
| 183 | 2251799813685247, |
| 184 | 2251799813685247, |
| 185 | 2251799813685247, |
| 186 | ]); |
| 187 | |
| 188 | /// Given 64-bit input limbs, reduce to enforce the bound 2^(51 + epsilon). |
| 189 | #[inline(always)] |
| 190 | #[allow(dead_code)] // Need this to not complain about reduce not being used |
| 191 | fn reduce(limbs: [u64; 5]) -> FieldElement51 { |
| 192 | let input = fiat_25519_loose_field_element(limbs); |
| 193 | let mut output = fiat_25519_tight_field_element([0; 5]); |
| 194 | fiat_25519_carry(&mut output, &input); |
| 195 | FieldElement51(output) |
| 196 | } |
| 197 | |
| 198 | /// Load a `FieldElement51` from the low 255 bits of a 256-bit |
| 199 | /// input. |
| 200 | /// |
| 201 | /// # Warning |
| 202 | /// |
| 203 | /// This function does not check that the input used the canonical |
| 204 | /// representative. It masks the high bit, but it will happily |
| 205 | /// decode 2^255 - 18 to 1. Applications that require a canonical |
| 206 | /// encoding of every field element should decode, re-encode to |
| 207 | /// the canonical encoding, and check that the input was |
| 208 | /// canonical. |
| 209 | /// |
| 210 | pub fn from_bytes(bytes: &[u8; 32]) -> FieldElement51 { |
| 211 | let mut temp = [0u8; 32]; |
| 212 | temp.copy_from_slice(bytes); |
| 213 | temp[31] &= 127u8; |
| 214 | let mut output = fiat_25519_tight_field_element([0u64; 5]); |
| 215 | fiat_25519_from_bytes(&mut output, &temp); |
| 216 | FieldElement51(output) |
| 217 | } |
| 218 | |
| 219 | /// Serialize this `FieldElement51` to a 32-byte array. The |
| 220 | /// encoding is canonical. |
| 221 | pub fn as_bytes(&self) -> [u8; 32] { |
| 222 | let mut bytes = [0u8; 32]; |
| 223 | fiat_25519_to_bytes(&mut bytes, &self.0); |
| 224 | bytes |
| 225 | } |
| 226 | |
| 227 | /// Given `k > 0`, return `self^(2^k)`. |
| 228 | pub fn pow2k(&self, mut k: u32) -> FieldElement51 { |
| 229 | let mut output = *self; |
| 230 | loop { |
| 231 | let mut input = fiat_25519_loose_field_element([0; 5]); |
| 232 | fiat_25519_relax(&mut input, &output.0); |
| 233 | fiat_25519_carry_square(&mut output.0, &input); |
| 234 | k -= 1; |
| 235 | if k == 0 { |
| 236 | return output; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// Returns the square of this field element. |
| 242 | pub fn square(&self) -> FieldElement51 { |
| 243 | let mut self_loose = fiat_25519_loose_field_element([0; 5]); |
| 244 | fiat_25519_relax(&mut self_loose, &self.0); |
| 245 | let mut output = FieldElement51::ZERO; |
| 246 | fiat_25519_carry_square(&mut output.0, &self_loose); |
| 247 | output |
| 248 | } |
| 249 | |
| 250 | /// Returns 2 times the square of this field element. |
| 251 | pub fn square2(&self) -> FieldElement51 { |
| 252 | let mut self_loose = fiat_25519_loose_field_element([0; 5]); |
| 253 | fiat_25519_relax(&mut self_loose, &self.0); |
| 254 | let mut square = fiat_25519_tight_field_element([0; 5]); |
| 255 | fiat_25519_carry_square(&mut square, &self_loose); |
| 256 | let mut output_loose = fiat_25519_loose_field_element([0; 5]); |
| 257 | fiat_25519_add(&mut output_loose, &square, &square); |
| 258 | let mut output = FieldElement51::ZERO; |
| 259 | fiat_25519_carry(&mut output.0, &output_loose); |
| 260 | output |
| 261 | } |
| 262 | } |