kindelia / Kindelia

An efficient, secure cryptocomputer
https://kindelia.org/
603 stars 39 forks source link

Bits of `LEN(tx)` are inverted but bits of `TX_COUNT` are not #191

Closed steinerkelvin closed 1 year ago

steinerkelvin commented 1 year ago

Block serialization is specfied as the following:

body ::= TX_COUNT | LEN(tx_0) | tx_0 | LEN(tx_1) | tx_1 | ...

On the implementation, TX_COUNT is written on the result buffer directly:

let mut tx_count = 0;
for transaction in transactions.into_iter() {
  ...
  tx_count += 1;
}
data[0] = tx_count as u8;

But, LEN(tx) bits are inverted before they are written to the buffer:

let len_bytes = transaction.encode_length();
data.push(len_bytes.0);
data.push(len_bytes.1);

pub fn encode_length(&self) -> (u8, u8) {
  let len = self.data.len() as u16;
  let num = (len as u16).reverse_bits();
  (((num >> 8) & 0xFF) as u8, (num & 0xFF) as u8)
}
steinerkelvin commented 1 year ago

fixed by c1346b9