|
| 1 | +use core::arch::aarch64::*; |
| 2 | +use core::simd::u8x16; |
| 3 | +use core::slice; |
| 4 | +use subspace_core_primitives::pot::{PotCheckpoints, PotOutput}; |
| 5 | + |
| 6 | +const NUM_ROUND_KEYS: usize = 11; |
| 7 | + |
| 8 | +/// Create PoT proof with checkpoints |
| 9 | +#[target_feature(enable = "aes")] |
| 10 | +#[inline] |
| 11 | +pub(super) fn create( |
| 12 | + seed: &[u8; 16], |
| 13 | + key: &[u8; 16], |
| 14 | + checkpoint_iterations: u32, |
| 15 | +) -> PotCheckpoints { |
| 16 | + let mut checkpoints = PotCheckpoints::default(); |
| 17 | + |
| 18 | + let keys = expand_key(key); |
| 19 | + let xor_key = veorq_u8(keys[10], keys[0]); |
| 20 | + let mut seed = uint8x16_t::from(u8x16::from(*seed)); |
| 21 | + seed = veorq_u8(seed, keys[10]); |
| 22 | + for checkpoint in checkpoints.iter_mut() { |
| 23 | + for _ in 0..checkpoint_iterations { |
| 24 | + seed = vaesmcq_u8(vaeseq_u8(seed, xor_key)); |
| 25 | + seed = vaesmcq_u8(vaeseq_u8(seed, keys[1])); |
| 26 | + seed = vaesmcq_u8(vaeseq_u8(seed, keys[2])); |
| 27 | + seed = vaesmcq_u8(vaeseq_u8(seed, keys[3])); |
| 28 | + seed = vaesmcq_u8(vaeseq_u8(seed, keys[4])); |
| 29 | + seed = vaesmcq_u8(vaeseq_u8(seed, keys[5])); |
| 30 | + seed = vaesmcq_u8(vaeseq_u8(seed, keys[6])); |
| 31 | + seed = vaesmcq_u8(vaeseq_u8(seed, keys[7])); |
| 32 | + seed = vaesmcq_u8(vaeseq_u8(seed, keys[8])); |
| 33 | + seed = vaeseq_u8(seed, keys[9]); |
| 34 | + } |
| 35 | + |
| 36 | + let checkpoint_reg = veorq_u8(seed, keys[10]); |
| 37 | + **checkpoint = u8x16::from(checkpoint_reg).to_array(); |
| 38 | + } |
| 39 | + |
| 40 | + checkpoints |
| 41 | +} |
| 42 | + |
| 43 | +/// Verification mimics `create` function, but also has decryption half for better performance |
| 44 | +#[target_feature(enable = "aes")] |
| 45 | +#[inline] |
| 46 | +pub(super) fn verify_sequential_aes( |
| 47 | + seed: &[u8; 16], |
| 48 | + key: &[u8; 16], |
| 49 | + checkpoints: &PotCheckpoints, |
| 50 | + checkpoint_iterations: u32, |
| 51 | +) -> bool { |
| 52 | + let checkpoints = PotOutput::repr_from_slice(checkpoints.as_slice()); |
| 53 | + |
| 54 | + let keys = expand_key(key); |
| 55 | + let xor_key = veorq_u8(keys[10], keys[0]); |
| 56 | + |
| 57 | + // Invert keys for decryption, the first and last element is not used below, hence they are |
| 58 | + // copied as is from encryption keys (otherwise the first and last element would need to be |
| 59 | + // swapped) |
| 60 | + let mut inv_keys = keys; |
| 61 | + for i in 1..10 { |
| 62 | + inv_keys[i] = vaesimcq_u8(keys[10 - i]); |
| 63 | + } |
| 64 | + |
| 65 | + let mut inputs: [uint8x16_t; PotCheckpoints::NUM_CHECKPOINTS.get() as usize] = [ |
| 66 | + uint8x16_t::from(u8x16::from(*seed)), |
| 67 | + uint8x16_t::from(u8x16::from(checkpoints[0])), |
| 68 | + uint8x16_t::from(u8x16::from(checkpoints[1])), |
| 69 | + uint8x16_t::from(u8x16::from(checkpoints[2])), |
| 70 | + uint8x16_t::from(u8x16::from(checkpoints[3])), |
| 71 | + uint8x16_t::from(u8x16::from(checkpoints[4])), |
| 72 | + uint8x16_t::from(u8x16::from(checkpoints[5])), |
| 73 | + uint8x16_t::from(u8x16::from(checkpoints[6])), |
| 74 | + ]; |
| 75 | + |
| 76 | + let mut outputs: [uint8x16_t; PotCheckpoints::NUM_CHECKPOINTS.get() as usize] = [ |
| 77 | + uint8x16_t::from(u8x16::from(checkpoints[0])), |
| 78 | + uint8x16_t::from(u8x16::from(checkpoints[1])), |
| 79 | + uint8x16_t::from(u8x16::from(checkpoints[2])), |
| 80 | + uint8x16_t::from(u8x16::from(checkpoints[3])), |
| 81 | + uint8x16_t::from(u8x16::from(checkpoints[4])), |
| 82 | + uint8x16_t::from(u8x16::from(checkpoints[5])), |
| 83 | + uint8x16_t::from(u8x16::from(checkpoints[6])), |
| 84 | + uint8x16_t::from(u8x16::from(checkpoints[7])), |
| 85 | + ]; |
| 86 | + |
| 87 | + inputs = inputs.map(|input| veorq_u8(input, keys[10])); |
| 88 | + outputs = outputs.map(|output| veorq_u8(output, keys[0])); |
| 89 | + |
| 90 | + for _ in 0..checkpoint_iterations / 2 { |
| 91 | + inputs = inputs.map(|input| vaesmcq_u8(vaeseq_u8(input, xor_key))); |
| 92 | + outputs = outputs.map(|output| vaesimcq_u8(vaesdq_u8(output, xor_key))); |
| 93 | + |
| 94 | + for i in 1..9 { |
| 95 | + inputs = inputs.map(|input| vaesmcq_u8(vaeseq_u8(input, keys[i]))); |
| 96 | + outputs = outputs.map(|output| vaesimcq_u8(vaesdq_u8(output, inv_keys[i]))); |
| 97 | + } |
| 98 | + |
| 99 | + inputs = inputs.map(|input| vaeseq_u8(input, keys[9])); |
| 100 | + outputs = outputs.map(|output| vaesdq_u8(output, inv_keys[9])); |
| 101 | + } |
| 102 | + |
| 103 | + inputs.into_iter().zip(outputs).all(|(input, output)| { |
| 104 | + let diff = veorq_u8(input, output); |
| 105 | + let cmp = vceqq_u8(diff, xor_key); |
| 106 | + vminvq_u8(cmp) == u8::MAX |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +// Below code copied with minor changes from the following place under MIT/Apache-2.0 license by |
| 111 | +// Artyom Pavlov: |
| 112 | +// https://github.com/RustCrypto/block-ciphers/blob/fbb68f40b122909d92e40ee8a50112b6e5d0af8f/aes/src/armv8/expand.rs |
| 113 | + |
| 114 | +/// There are 4 AES words in a block. |
| 115 | +const BLOCK_WORDS: usize = 4; |
| 116 | + |
| 117 | +/// The AES (nee Rijndael) notion of a word is always 32-bits, or 4-bytes. |
| 118 | +const WORD_SIZE: usize = 4; |
| 119 | + |
| 120 | +/// AES round constants. |
| 121 | +const ROUND_CONSTS: [u32; 10] = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]; |
| 122 | + |
| 123 | +/// AES key expansion. |
| 124 | +#[target_feature(enable = "aes")] |
| 125 | +fn expand_key(key: &[u8; 16]) -> [uint8x16_t; NUM_ROUND_KEYS] { |
| 126 | + let mut expanded_keys = [uint8x16_t::from(u8x16::default()); NUM_ROUND_KEYS]; |
| 127 | + |
| 128 | + // Sanity check, as this is required in order for the subsequent conversion to be sound. |
| 129 | + const _: () = assert!(align_of::<uint8x16_t>() >= align_of::<u32>()); |
| 130 | + let columns = unsafe { |
| 131 | + slice::from_raw_parts_mut( |
| 132 | + expanded_keys.as_mut_ptr().cast::<u32>(), |
| 133 | + NUM_ROUND_KEYS * BLOCK_WORDS, |
| 134 | + ) |
| 135 | + }; |
| 136 | + |
| 137 | + for (i, chunk) in key.array_chunks::<WORD_SIZE>().enumerate() { |
| 138 | + columns[i] = u32::from_ne_bytes(*chunk); |
| 139 | + } |
| 140 | + |
| 141 | + // From "The Rijndael Block Cipher" Section 4.1: |
| 142 | + // > The number of columns of the Cipher Key is denoted by `Nk` and is |
| 143 | + // > equal to the key length divided by 32 [bits]. |
| 144 | + let nk = 16 / WORD_SIZE; |
| 145 | + |
| 146 | + for i in nk..NUM_ROUND_KEYS * BLOCK_WORDS { |
| 147 | + let mut word = columns[i - 1]; |
| 148 | + |
| 149 | + if i % nk == 0 { |
| 150 | + word = sub_word(word).rotate_right(8) ^ ROUND_CONSTS[i / nk - 1]; |
| 151 | + } else if nk > 6 && i % nk == 4 { |
| 152 | + word = sub_word(word); |
| 153 | + } |
| 154 | + |
| 155 | + columns[i] = columns[i - nk] ^ word; |
| 156 | + } |
| 157 | + |
| 158 | + expanded_keys |
| 159 | +} |
| 160 | + |
| 161 | +/// Sub bytes for a single AES word: used for key expansion |
| 162 | +#[target_feature(enable = "aes")] |
| 163 | +fn sub_word(input: u32) -> u32 { |
| 164 | + let input = vreinterpretq_u8_u32(vdupq_n_u32(input)); |
| 165 | + |
| 166 | + // AES single round encryption (with a "round" key of all zeros) |
| 167 | + let sub_input = vaeseq_u8(input, vdupq_n_u8(0)); |
| 168 | + |
| 169 | + vgetq_lane_u32::<0>(vreinterpretq_u32_u8(sub_input)) |
| 170 | +} |
0 commit comments