polyval/backend/
intrinsics.rs1use crate::{Block, Key, ParBlocks, Tag, field_element::FieldElement};
4
5#[cfg_attr(target_arch = "aarch64", path = "intrinsics/armv8.rs")]
6#[cfg_attr(
7 any(target_arch = "x86", target_arch = "x86_64"),
8 path = "intrinsics/x86.rs"
9)]
10mod intrinsics_impl;
11use intrinsics_impl::InitToken;
12
13#[derive(Clone)]
15pub(crate) struct State {
16 expanded_key: ExpandedKey,
18
19 acc: FieldElement,
21
22 init_token: InitToken,
24}
25
26impl State {
27 pub(crate) fn new(h: &Key) -> Self {
28 let (init_token, has_intrinsics) = InitToken::init_get();
29
30 let expanded_key = if has_intrinsics {
31 unsafe { intrinsics_impl::expand_key(&h.0) }
33 } else {
34 ExpandedKey {
37 h1: FieldElement::from(*h),
38 ..Default::default()
39 }
40 };
41
42 let y = FieldElement::default();
43
44 Self {
45 expanded_key,
46 acc: y,
47 init_token,
48 }
49 }
50
51 pub(crate) fn proc_block(&mut self, block: &Block) {
52 self.acc = if self.has_intrinsics() {
53 unsafe { intrinsics_impl::proc_block(&self.expanded_key, self.acc, block) }
55 } else {
56 (self.acc + block.into()) * self.expanded_key.h1
57 };
58 }
59
60 pub(crate) fn proc_par_blocks(&mut self, par_blocks: &ParBlocks) {
61 if self.has_intrinsics() {
62 self.acc = unsafe {
64 intrinsics_impl::proc_par_blocks(&self.expanded_key, self.acc, par_blocks)
65 };
66 } else {
67 for block in par_blocks {
69 self.proc_block(block);
70 }
71 }
72 }
73
74 pub(crate) fn finalize(&self) -> Tag {
75 self.acc.into()
76 }
77
78 pub(crate) fn reset(&mut self) {
79 self.acc = FieldElement::default();
80 }
81
82 #[inline]
83 fn has_intrinsics(&self) -> bool {
84 self.init_token.get()
85 }
86}
87
88#[derive(Clone, Default)]
92pub(crate) struct ExpandedKey {
93 h1: FieldElement,
95 d1: FieldElement,
97 h2: FieldElement,
99 d2: FieldElement,
101 h3: FieldElement,
103 d3: FieldElement,
105 h4: FieldElement,
107 d4: FieldElement,
109}