Skip to content

Commit

Permalink
read_felt_vector_from_prover
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Dec 21, 2023
1 parent e5be56c commit 386fbed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
28 changes: 24 additions & 4 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const C_PRIME_AS_UINT256_HIGH: u128 =
329648542954659146201578277794459156480; // 31 * 0x8000000000000110000000000000000;
const STARK_PRIME: u256 =
3618502788666131213697322783095070105623107215331596699973092056135872020481;
const INVERSE_2_TO_256_MOD_STARK_PRIME: felt252 =
const MONTGOMERY_R: felt252 = 3618502788666127798953978732740734578953660990361066340291730267701097005025; // 2**256 % STARK_PRIME
const MONTGOMERY_R_INVERSE_MOD_STARK_PRIME: felt252 =
113078212145816603762751633895895194930089271709401121343797004406777446400;

#[derive(Drop)]
Expand Down Expand Up @@ -44,7 +45,7 @@ impl ChannelImpl of ChannelTrait {
if (rand < u256 { low: C_PRIME_AS_UINT256_LOW, high: C_PRIME_AS_UINT256_HIGH }) {
n -= 1;
let to_append = (rand % STARK_PRIME).try_into().unwrap();
res.append(to_append * INVERSE_2_TO_256_MOD_STARK_PRIME);
res.append(to_append * MONTGOMERY_R_INVERSE_MOD_STARK_PRIME);
}
} else {
break;
Expand All @@ -64,8 +65,27 @@ impl ChannelImpl of ChannelTrait {
self.digest = blake2s(hash_data).flip_endiannes();
}

fn read_felts_from_prover(ref self: Channel, values: Array<felt252>) {
let hashed = poseidon_hash_span(values.span());
fn read_felts_from_prover(ref self: Channel, values: Span<felt252>) {
let hashed = poseidon_hash_span(values);
self.read_felt_from_prover(hashed);
}

fn read_felt_vector_from_prover(ref self: Channel, values: Span<felt252>) {
let mut hash_data = ArrayTrait::<u32>::new();

assert(self.digest.low != 0xffffffffffffffffffffffffffffffff, 'digest low is 2^128-1');
(self.digest + 1).to_array_be(ref hash_data);

let mut i = 0;
loop {
if i == values.len() {
break;
};
let value_u256: u256 = (*values[i] * MONTGOMERY_R).into();
value_u256.to_array_be(ref hash_data);
i += 1;
};

self.digest = blake2s(hash_data).flip_endiannes();
}
}
7 changes: 6 additions & 1 deletion src/channel/tests/test_channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ fn test_read_from_prover() {
arr.append(2);
arr.append(3);
arr.append(-1);
channel.read_felts_from_prover(arr);
channel.read_felts_from_prover(arr.span());

assert(channel.counter == 0, 'invalid read felts');
assert(channel.digest == 0x135bc3291210bb6248a09cea1a97b0023c5602b18a9e0786aeed16352972504, 'invalid read felts');

channel.read_felt_vector_from_prover(arr.span());

assert(channel.counter == 0, 'invalid read felts');
assert(channel.digest == 0x413b1e08fe14f181acc48007a89e4d044a9edb54523e8eae5829fde606d4074d, 'invalid read felts');
}

0 comments on commit 386fbed

Please sign in to comment.