Skip to content

Commit adb407d

Browse files
authored
misc(core): simplify encoding logic (#781)
* perf(core): simplify encoding logic * make constant-time
1 parent 3e54119 commit adb407d

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

crates/core/src/transcript/encoding/encoder.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,16 @@ impl Encoder for ChaChaEncoder {
108108

109109
fn encode_subsequence(&self, direction: Direction, seq: &Subsequence) -> Vec<u8> {
110110
const ZERO: [u8; 16] = [0; BIT_ENCODING_SIZE];
111+
111112
let mut encoding = self.encode_idx(direction, seq.index());
112-
for (byte_idx, &byte) in seq.data().iter().enumerate() {
113-
let start = byte_idx * BYTE_ENCODING_SIZE;
114-
for (bit_idx, bit) in byte.iter_lsb0().enumerate() {
115-
let pos = start + (bit_idx * BIT_ENCODING_SIZE);
116-
let delta = if bit { &self.delta } else { &ZERO };
117-
118-
encoding[pos..pos + BIT_ENCODING_SIZE]
119-
.iter_mut()
120-
.zip(delta)
121-
.for_each(|(a, b)| *a ^= *b);
122-
}
113+
for (pos, bit) in seq.data().iter_lsb0().enumerate() {
114+
// Add the delta to the encoding whenever the encoded bit is 1,
115+
// otherwise add a zero.
116+
let summand = if bit { &self.delta } else { &ZERO };
117+
encoding[pos * BIT_ENCODING_SIZE..(pos + 1) * BIT_ENCODING_SIZE]
118+
.iter_mut()
119+
.zip(summand)
120+
.for_each(|(a, b)| *a ^= *b);
123121
}
124122

125123
encoding

0 commit comments

Comments
 (0)