interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / curve25519-dalek / src / backend / serial / fiat_u32 / field.rs

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