Open tvladyslav opened 7 years ago
@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).
@crypto-universe I believe the custom rotate left may be used to ensure constant time operations.