From 88c2d69e32249e447a4b568fa492afaa99a1236d Mon Sep 17 00:00:00 2001 From: Filip Krawczyk Date: Thu, 21 Dec 2023 10:04:10 +0100 Subject: [PATCH] Implement read_felt_from_prover --- src/channel/channel.cairo | 10 ++++++++++ src/channel/tests/test_channel.cairo | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/channel/channel.cairo b/src/channel/channel.cairo index 1a4997130..452b03bcf 100644 --- a/src/channel/channel.cairo +++ b/src/channel/channel.cairo @@ -51,4 +51,14 @@ impl ChannelImpl of ChannelTrait { }; res } + + fn read_felt_from_prover(ref self: Channel, value: felt252) { + let value_u256: u256 = value.into(); + let mut hash_data = ArrayTrait::::new(); + + (self.digest + 1).to_array_be(ref hash_data); + value_u256.to_array_be(ref hash_data); + + self.digest = blake2s(hash_data).flip_endiannes(); + } } diff --git a/src/channel/tests/test_channel.cairo b/src/channel/tests/test_channel.cairo index 273ba3a9c..4f919109c 100644 --- a/src/channel/tests/test_channel.cairo +++ b/src/channel/tests/test_channel.cairo @@ -31,3 +31,13 @@ fn test_random_uint256_to_prover() { 'invalid random uint256' ); } + +#[test] +#[available_gas(9999999999)] +fn test_read_felt_from_prover() { + let mut channel = ChannelTrait::new(0); + + channel.read_felt_from_prover(0xffffffffffffffffffffffffffffffffffffffff); + + assert(channel.digest == 0xb056692f5fc4f27dedd1fb6269b02c542a415f1d84555708a354ffb25cf97ad5, 'invalid read felt'); +}