11//! AES related functionality.
22
3- #[ cfg( all ( feature = "std" , target_arch = "x86_64" ) ) ]
3+ #[ cfg( target_arch = "x86_64" ) ]
44mod x86_64;
55
6- #[ cfg( not( feature = "std" ) ) ]
7- extern crate alloc;
8-
96use aes:: cipher:: array:: Array ;
107use aes:: cipher:: { BlockCipherDecrypt , BlockCipherEncrypt , KeyInit } ;
118use aes:: Aes128 ;
12- #[ cfg( not( feature = "std" ) ) ]
13- use alloc:: vec:: Vec ;
149use subspace_core_primitives:: pot:: { PotCheckpoints , PotKey , PotOutput , PotSeed } ;
1510
1611/// Creates the AES based proof.
1712#[ inline( always) ]
1813pub ( crate ) fn create ( seed : PotSeed , key : PotKey , checkpoint_iterations : u32 ) -> PotCheckpoints {
19- #[ cfg( all( feature = "std" , target_arch = "x86_64" ) ) ]
20- if std:: is_x86_feature_detected!( "aes" ) {
21- return unsafe { x86_64:: create ( seed. as_ref ( ) , key. as_ref ( ) , checkpoint_iterations) } ;
14+ #[ cfg( target_arch = "x86_64" ) ]
15+ {
16+ cpufeatures:: new!( has_aes, "aes" ) ;
17+ if has_aes:: get ( ) {
18+ return unsafe { x86_64:: create ( seed. as_ref ( ) , key. as_ref ( ) , checkpoint_iterations) } ;
19+ }
2220 }
2321
2422 create_generic ( seed, key, checkpoint_iterations)
@@ -48,27 +46,26 @@ fn create_generic(seed: PotSeed, key: PotKey, checkpoint_iterations: u32) -> Pot
4846pub ( crate ) fn verify_sequential (
4947 seed : PotSeed ,
5048 key : PotKey ,
51- checkpoints : & [ PotOutput ] ,
49+ checkpoints : & PotCheckpoints ,
5250 checkpoint_iterations : u32 ,
5351) -> bool {
5452 assert_eq ! ( checkpoint_iterations % 2 , 0 ) ;
5553
5654 let key = Array :: from ( * key) ;
5755 let cipher = Aes128 :: new ( & key) ;
5856
59- let mut inputs = Vec :: with_capacity ( checkpoints. len ( ) ) ;
60- inputs. push ( Array :: from ( * seed) ) ;
61- for & checkpoint in checkpoints. iter ( ) . rev ( ) . skip ( 1 ) . rev ( ) {
62- inputs. push ( Array :: from ( * checkpoint) ) ;
63- }
64- let mut outputs = checkpoints
65- . iter ( )
66- . map ( |& checkpoint| Array :: from ( * checkpoint) )
67- . collect :: < Vec < _ > > ( ) ;
57+ let mut inputs = [ [ 0u8 ; 16 ] ; PotCheckpoints :: NUM_CHECKPOINTS . get ( ) as usize ] ;
58+ inputs[ 0 ] = * seed;
59+ inputs[ 1 ..] . copy_from_slice ( PotOutput :: repr_from_slice (
60+ & checkpoints[ ..PotCheckpoints :: NUM_CHECKPOINTS . get ( ) as usize - 1 ] ,
61+ ) ) ;
62+
63+ let mut outputs = [ [ 0u8 ; 16 ] ; PotCheckpoints :: NUM_CHECKPOINTS . get ( ) as usize ] ;
64+ outputs. copy_from_slice ( PotOutput :: repr_from_slice ( checkpoints. as_slice ( ) ) ) ;
6865
6966 for _ in 0 ..checkpoint_iterations / 2 {
70- cipher. encrypt_blocks ( & mut inputs) ;
71- cipher. decrypt_blocks ( & mut outputs) ;
67+ cipher. encrypt_blocks ( Array :: cast_slice_from_core_mut ( & mut inputs) ) ;
68+ cipher. decrypt_blocks ( Array :: cast_slice_from_core_mut ( & mut outputs) ) ;
7269 }
7370
7471 inputs == outputs
@@ -77,6 +74,7 @@ pub(crate) fn verify_sequential(
7774#[ cfg( test) ]
7875mod tests {
7976 use super :: * ;
77+ use subspace_core_primitives:: pot:: PotOutput ;
8078
8179 const SEED : [ u8 ; 16 ] = [
8280 0xd6 , 0x66 , 0xcc , 0xd8 , 0xd5 , 0x93 , 0xc2 , 0x3d , 0xa8 , 0xdb , 0x6b , 0x5b , 0x14 , 0x13 , 0xb1 ,
@@ -100,7 +98,7 @@ mod tests {
10098 fn test_create_verify ( ) {
10199 let seed = PotSeed :: from ( SEED ) ;
102100 let key = PotKey :: from ( KEY ) ;
103- let checkpoint_iterations = 100 ;
101+ let checkpoint_iterations = 20 ;
104102
105103 // Can encrypt/decrypt.
106104 let checkpoints = create ( seed, key, checkpoint_iterations) ;
@@ -112,7 +110,7 @@ mod tests {
112110 assert ! ( verify_sequential(
113111 seed,
114112 key,
115- & * checkpoints,
113+ & checkpoints,
116114 checkpoint_iterations,
117115 ) ) ;
118116
@@ -122,37 +120,37 @@ mod tests {
122120 assert ! ( !verify_sequential(
123121 seed,
124122 key,
125- & * checkpoints_1,
123+ & checkpoints_1,
126124 checkpoint_iterations,
127125 ) ) ;
128126
129127 // Decryption with wrong number of iterations fails.
130128 assert ! ( !verify_sequential(
131129 seed,
132130 key,
133- & * checkpoints,
131+ & checkpoints,
134132 checkpoint_iterations + 2 ,
135133 ) ) ;
136134 assert ! ( !verify_sequential(
137135 seed,
138136 key,
139- & * checkpoints,
137+ & checkpoints,
140138 checkpoint_iterations - 2 ,
141139 ) ) ;
142140
143141 // Decryption with wrong seed fails.
144142 assert ! ( !verify_sequential(
145143 PotSeed :: from( SEED_1 ) ,
146144 key,
147- & * checkpoints,
145+ & checkpoints,
148146 checkpoint_iterations,
149147 ) ) ;
150148
151149 // Decryption with wrong key fails.
152150 assert ! ( !verify_sequential(
153151 seed,
154152 PotKey :: from( KEY_1 ) ,
155- & * checkpoints,
153+ & checkpoints,
156154 checkpoint_iterations,
157155 ) ) ;
158156 }
0 commit comments