rust-crypto icon indicating copy to clipboard operation
rust-crypto copied to clipboard

Use standard rotate_left func in sha3

Open tvladyslav opened this issue 8 years ago • 2 comments

tvladyslav avatar Mar 26 '17 23:03 tvladyslav

@crypto-universe I believe the custom rotate left may be used to ensure constant time operations.

YetAnotherMinion avatar Apr 26 '17 21:04 YetAnotherMinion

@YetAnotherMinion I don't think this is the case. I've checked generated assembler code in both cases (lines 184 and 245). There are no compares and jumps, so functions will execute in constant time. To prove that I made some simple benchmark,

#[bench] fn bench_rotate_custom_9(b: &mut Bencher) { b.iter(|| { (0..1000).fold(0, |old, new| rotl64_1(old ^ new, 9)) }); }

#[bench] fn bench_rotate_standard_9(b: &mut Bencher) { b.iter(|| { (0..1000).fold(0, |old, new| rotl64_2(old ^ new, 9)) }); } // And 4 similar benchmarks for another rotation number

and here are the results:

test bench_rotate_custom_20 ... bench: 763 ns/iter (+/- 46) test bench_rotate_custom_45 ... bench: 757 ns/iter (+/- 33) test bench_rotate_custom_9 ... bench: 765 ns/iter (+/- 44) test bench_rotate_standard_20 ... bench: 763 ns/iter (+/- 28) test bench_rotate_standard_45 ... bench: 764 ns/iter (+/- 28) test bench_rotate_standard_9 ... bench: 768 ns/iter (+/- 37)

Both functions has almost equal computation time for arbitrary n (on my Core i7-2630QM CPU @ 2.00GHz).

tvladyslav avatar Apr 30 '17 01:04 tvladyslav