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 / scalar_mul / precomputed_straus.rs
| 1 | // -*- mode: rust; -*- |
| 2 | // |
| 3 | // This file is part of curve25519-dalek. |
| 4 | // Copyright (c) 2019 Henry de Valence. |
| 5 | // See LICENSE for licensing information. |
| 6 | // |
| 7 | // Authors: |
| 8 | // - Henry de Valence <hdevalence@hdevalence.ca> |
| 9 | |
| 10 | //! Precomputation for Straus's method. |
| 11 | |
| 12 | #![allow(non_snake_case)] |
| 13 | |
| 14 | use alloc::vec::Vec; |
| 15 | |
| 16 | use core::borrow::Borrow; |
| 17 | use core::cmp::Ordering; |
| 18 | |
| 19 | use crate::backend::serial::curve_models::{ |
| 20 | AffineNielsPoint, CompletedPoint, ProjectiveNielsPoint, ProjectivePoint, |
| 21 | }; |
| 22 | use crate::edwards::EdwardsPoint; |
| 23 | use crate::scalar::Scalar; |
| 24 | use crate::traits::Identity; |
| 25 | use crate::traits::VartimePrecomputedMultiscalarMul; |
| 26 | use crate::window::{NafLookupTable5, NafLookupTable8}; |
| 27 | |
| 28 | #[allow(missing_docs)] |
| 29 | pub struct VartimePrecomputedStraus { |
| 30 | static_lookup_tables: Vec<NafLookupTable8<AffineNielsPoint>>, |
| 31 | } |
| 32 | |
| 33 | impl VartimePrecomputedMultiscalarMul for VartimePrecomputedStraus { |
| 34 | type Point = EdwardsPoint; |
| 35 | |
| 36 | fn new<I>(static_points: I) -> Self |
| 37 | where |
| 38 | I: IntoIterator, |
| 39 | I::Item: Borrow<Self::Point>, |
| 40 | { |
| 41 | Self { |
| 42 | static_lookup_tables: static_points |
| 43 | .into_iter() |
| 44 | .map(|P| NafLookupTable8::<AffineNielsPoint>::from(P.borrow())) |
| 45 | .collect(), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn optional_mixed_multiscalar_mul<I, J, K>( |
| 50 | &self, |
| 51 | static_scalars: I, |
| 52 | dynamic_scalars: J, |
| 53 | dynamic_points: K, |
| 54 | ) -> Option<Self::Point> |
| 55 | where |
| 56 | I: IntoIterator, |
| 57 | I::Item: Borrow<Scalar>, |
| 58 | J: IntoIterator, |
| 59 | J::Item: Borrow<Scalar>, |
| 60 | K: IntoIterator<Item = Option<Self::Point>>, |
| 61 | { |
| 62 | let static_nafs = static_scalars |
| 63 | .into_iter() |
| 64 | .map(|c| c.borrow().non_adjacent_form(5)) |
| 65 | .collect::<Vec<_>>(); |
| 66 | let dynamic_nafs: Vec<_> = dynamic_scalars |
| 67 | .into_iter() |
| 68 | .map(|c| c.borrow().non_adjacent_form(5)) |
| 69 | .collect::<Vec<_>>(); |
| 70 | |
| 71 | let dynamic_lookup_tables = dynamic_points |
| 72 | .into_iter() |
| 73 | .map(|P_opt| P_opt.map(|P| NafLookupTable5::<ProjectiveNielsPoint>::from(&P))) |
| 74 | .collect::<Option<Vec<_>>>()?; |
| 75 | |
| 76 | let sp = self.static_lookup_tables.len(); |
| 77 | let dp = dynamic_lookup_tables.len(); |
| 78 | assert_eq!(sp, static_nafs.len()); |
| 79 | assert_eq!(dp, dynamic_nafs.len()); |
| 80 | |
| 81 | // We could save some doublings by looking for the highest |
| 82 | // nonzero NAF coefficient, but since we might have a lot of |
| 83 | // them to search, it's not clear it's worthwhile to check. |
| 84 | let mut S = ProjectivePoint::identity(); |
| 85 | for j in (0..256).rev() { |
| 86 | let mut R: CompletedPoint = S.double(); |
| 87 | |
| 88 | for i in 0..dp { |
| 89 | let t_ij = dynamic_nafs[i][j]; |
| 90 | match t_ij.cmp(&0) { |
| 91 | Ordering::Greater => { |
| 92 | R = &R.as_extended() + &dynamic_lookup_tables[i].select(t_ij as usize) |
| 93 | } |
| 94 | Ordering::Less => { |
| 95 | R = &R.as_extended() - &dynamic_lookup_tables[i].select(-t_ij as usize) |
| 96 | } |
| 97 | Ordering::Equal => {} |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[allow(clippy::needless_range_loop)] |
| 102 | for i in 0..sp { |
| 103 | let t_ij = static_nafs[i][j]; |
| 104 | match t_ij.cmp(&0) { |
| 105 | Ordering::Greater => { |
| 106 | R = &R.as_extended() + &self.static_lookup_tables[i].select(t_ij as usize) |
| 107 | } |
| 108 | Ordering::Less => { |
| 109 | R = &R.as_extended() - &self.static_lookup_tables[i].select(-t_ij as usize) |
| 110 | } |
| 111 | Ordering::Equal => {} |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | S = R.as_projective(); |
| 116 | } |
| 117 | |
| 118 | Some(S.as_extended()) |
| 119 | } |
| 120 | } |