|
| 1 | +use super::{ |
| 2 | + encoding::{decode, encode}, |
| 3 | + AsymEnc, |
| 4 | +}; |
| 5 | +use crate::{ |
| 6 | + algebra::{f3::Small, fq::Fq, r3::R3, rq::Rq}, |
| 7 | + core::streamlined, |
| 8 | + params::{NtruCommon, Streamlined, StreamlinedNtru}, |
| 9 | +}; |
| 10 | +use hybrid_array::Array; |
| 11 | +use hybrid_array::{typenum::Unsigned, ArraySize}; |
| 12 | +use rand_core::CryptoRngCore; |
| 13 | + |
| 14 | +#[allow(non_snake_case)] |
| 15 | +fn rq_encode<Params: NtruCommon>(r: &Rq<Params>, out: &mut [u8]) { |
| 16 | + let mut R: Array<u16, Params::P> = Array::default(); |
| 17 | + let mut M: Array<u16, Params::P> = Array::default(); |
| 18 | + for i in 0..Params::P::USIZE { |
| 19 | + R[i] = (*r.0[i] as u16).wrapping_add(Params::Q12); |
| 20 | + } |
| 21 | + for i in 0..Params::P::USIZE { |
| 22 | + M[i] = Params::Q; |
| 23 | + } |
| 24 | + encode(&R, &M, out); |
| 25 | +} |
| 26 | +#[allow(non_snake_case)] |
| 27 | +fn rq_decode<Params: NtruCommon + StreamlinedNtru>( |
| 28 | + s: &Array<u8, Params::PublicKeyBytes>, |
| 29 | +) -> Rq<Params> { |
| 30 | + let mut R: Array<u16, Params::P> = Array::default(); |
| 31 | + let mut M: Array<u16, Params::P> = Array::default(); |
| 32 | + for i in 0..Params::P::USIZE { |
| 33 | + M[i] = Params::Q; |
| 34 | + } |
| 35 | + decode(s, &M, &mut R); |
| 36 | + let mut r = Rq::<Params>::default(); |
| 37 | + for i in 0..Params::P::USIZE { |
| 38 | + r.0[i] = Fq::new_i16(R[i].wrapping_sub(Params::Q12) as i16); |
| 39 | + } |
| 40 | + r |
| 41 | +} |
| 42 | +#[allow(non_snake_case)] |
| 43 | +fn rounded_encode<Params: NtruCommon + StreamlinedNtru>( |
| 44 | + r: &Rq<Params>, |
| 45 | +) -> Array<u8, Params::CipherTextBytes> { |
| 46 | + let mut R: Array<u16, Params::P> = Array::default(); |
| 47 | + let mut M: Array<u16, Params::P> = Array::default(); |
| 48 | + let mut out = Array::default(); |
| 49 | + for i in 0..Params::P::USIZE { |
| 50 | + R[i] = ((*r.0[i] as u32) |
| 51 | + .wrapping_add(Params::Q12 as u32) |
| 52 | + .wrapping_mul(10923) |
| 53 | + >> 15) as u16; |
| 54 | + } |
| 55 | + for i in 0..Params::P::USIZE { |
| 56 | + M[i] = (Params::Q + 2) / 3; |
| 57 | + } |
| 58 | + encode(&R, &M, &mut out); |
| 59 | + out |
| 60 | +} |
| 61 | +#[allow(non_snake_case)] |
| 62 | +fn rounded_decode<Params: NtruCommon + StreamlinedNtru>( |
| 63 | + s: &Array<u8, Params::CipherTextBytes>, |
| 64 | +) -> Rq<Params> { |
| 65 | + let mut R: Array<u16, Params::P> = Array::default(); |
| 66 | + let mut M: Array<u16, Params::P> = Array::default(); |
| 67 | + for i in 0..Params::P::USIZE { |
| 68 | + M[i] = (Params::Q + 2) / 3; |
| 69 | + } |
| 70 | + decode(s, &M, &mut R); |
| 71 | + let mut r = Rq::<Params>::default(); |
| 72 | + for i in 0..Params::P::USIZE { |
| 73 | + r.0[i] = Fq::new_i16(R[i].wrapping_mul(3).wrapping_sub(Params::Q12) as i16); |
| 74 | + } |
| 75 | + r |
| 76 | +} |
| 77 | + |
| 78 | +fn small_encode<Params: NtruCommon>(f: &R3<Params>, out: &mut [u8]) { |
| 79 | + let mut i = 0; |
| 80 | + let mut x; |
| 81 | + for _ in 0..Params::P::USIZE / 4 { |
| 82 | + x = (*f.0[i] + 1) as u8; |
| 83 | + x += ((*f.0[i + 1] + 1) as u8) << 2; |
| 84 | + x += ((*f.0[i + 2] + 1) as u8) << 4; |
| 85 | + x += ((*f.0[i + 3] + 1) as u8) << 6; |
| 86 | + out[i / 4] = x; |
| 87 | + i += 4; |
| 88 | + } |
| 89 | + x = (*f.0[i] + 1) as u8; |
| 90 | + out[i / 4] = x; |
| 91 | +} |
| 92 | + |
| 93 | +fn small_decode<Params: NtruCommon>(input: &[u8]) -> R3<Params> { |
| 94 | + let mut r = R3::default(); |
| 95 | + for (i, x) in input.iter().enumerate().take(Params::P::USIZE / 4) { |
| 96 | + let mut x = *x; |
| 97 | + r.0[i * 4] = Small::new_i8((x & 3) as i8 - 1); |
| 98 | + x >>= 2; |
| 99 | + r.0[i * 4 + 1] = Small::new_i8((x & 3) as i8 - 1); |
| 100 | + x >>= 2; |
| 101 | + r.0[i * 4 + 2] = Small::new_i8((x & 3) as i8 - 1); |
| 102 | + x >>= 2; |
| 103 | + r.0[i * 4 + 3] = Small::new_i8((x & 3) as i8 - 1); |
| 104 | + } |
| 105 | + let x = input[Params::P::USIZE / 4]; |
| 106 | + r.0[Params::P::USIZE - 1] = Small::new_i8((x & 3) as i8 - 1); |
| 107 | + r |
| 108 | +} |
| 109 | + |
| 110 | +impl<P> AsymEnc for Streamlined<P> |
| 111 | +where |
| 112 | + P: ArraySize, |
| 113 | + Streamlined<P>: NtruCommon + StreamlinedNtru + Sized, |
| 114 | +{ |
| 115 | + type Inputs = R3<Self>; |
| 116 | + fn decrypt( |
| 117 | + c: &Array<u8, Self::CipherTextBytes>, |
| 118 | + sk: &Array<u8, Self::SecretKeyBytes>, |
| 119 | + ) -> R3<Self> { |
| 120 | + let f = small_decode(&sk[..Self::InputsBytes::USIZE]); |
| 121 | + let v = small_decode(&sk[Self::InputsBytes::USIZE..]); |
| 122 | + let c = rounded_decode(c); |
| 123 | + streamlined::decrypt(&c, &f, &v) |
| 124 | + } |
| 125 | + fn key_gen( |
| 126 | + rng: &mut impl CryptoRngCore, |
| 127 | + ) -> ( |
| 128 | + Array<u8, Self::SecretKeyBytes>, |
| 129 | + Array<u8, Self::PublicKeyBytes>, |
| 130 | + ) { |
| 131 | + let (h, f, v) = streamlined::key_gen::<Self>(rng); |
| 132 | + let mut pk: Array<u8, Self::PublicKeyBytes> = Array::default(); |
| 133 | + let mut sk: Array<u8, Self::SecretKeyBytes> = Array::default(); |
| 134 | + rq_encode(&h, &mut pk); |
| 135 | + small_encode(&f, &mut sk[..Self::InputsBytes::USIZE]); |
| 136 | + small_encode(&v, &mut sk[Self::InputsBytes::USIZE..]); |
| 137 | + (sk, pk) |
| 138 | + } |
| 139 | + fn encrypt( |
| 140 | + r: &R3<Self>, |
| 141 | + pk: &Array<u8, Self::PublicKeyBytes>, |
| 142 | + ) -> Array<u8, Self::CipherTextBytes> { |
| 143 | + let h = rq_decode(pk); |
| 144 | + let c = streamlined::encrypt(r, &h); |
| 145 | + rounded_encode(&c) |
| 146 | + } |
| 147 | + fn inputs_encode(f: &R3<Self>, out: &mut [u8]) { |
| 148 | + small_encode(f, out); |
| 149 | + } |
| 150 | + |
| 151 | + fn inputs_random(rng: &mut impl CryptoRngCore) -> R3<Self> { |
| 152 | + R3::short_random(rng) |
| 153 | + } |
| 154 | +} |
0 commit comments