Skip to content

Commit

Permalink
Implement append_little_endian for felt252
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Dec 29, 2023
1 parent c02f915 commit ba5982d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ impl ChannelImpl of ChannelTrait {
}

fn read_felt_from_prover(ref self: Channel, value: felt252) {
let value_u256: u256 = value.into();
let mut hash_data = ArrayTrait::<u32>::new();

assert(self.digest.low != BoundedU128::max(), 'digest low is 2^128-1');
hash_data.append_big_endian(self.digest + 1);
hash_data.append_big_endian(value_u256);
hash_data.append_big_endian(value);

self.digest = blake2s(hash_data).flip_endianness();
self.counter = 0;
Expand All @@ -84,8 +83,7 @@ impl ChannelImpl of ChannelTrait {
if i == values.len() {
break;
};
let value_u256: u256 = (*values[i] * MONTGOMERY_R).into();
hash_data.append_big_endian(value_u256);
hash_data.append_big_endian(*values[i] * MONTGOMERY_R);
i += 1;
};

Expand Down
12 changes: 12 additions & 0 deletions src/common/array_append.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ impl ArrayU32AppendU256 of ArrayAppendTrait<u32, u256> {
}
}

impl ArrayU32AppendFelt of ArrayAppendTrait<u32, felt252> {
fn append_little_endian(ref self: Array<u32>, element: felt252) {
let as_u256: u256 = element.into();
self.append_little_endian(as_u256);
}

fn append_big_endian(ref self: Array<u32>, element: felt252) {
let as_u256: u256 = element.into();
self.append_big_endian(as_u256);
}
}

impl ArrayU32AppendU128 of ArrayAppendTrait<u32, u128> {
fn append_little_endian(ref self: Array<u32>, mut element: u128) {
let mut i = 4;
Expand Down

0 comments on commit ba5982d

Please sign in to comment.