diff --git a/src/channel/channel.cairo b/src/channel/channel.cairo index 256c47fab..ddbb16dc9 100644 --- a/src/channel/channel.cairo +++ b/src/channel/channel.cairo @@ -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)] @@ -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; @@ -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) { - let hashed = poseidon_hash_span(values.span()); + fn read_felts_from_prover(ref self: Channel, values: Span) { + let hashed = poseidon_hash_span(values); self.read_felt_from_prover(hashed); } + + fn read_felt_vector_from_prover(ref self: Channel, values: Span) { + let mut hash_data = ArrayTrait::::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(); + } } diff --git a/src/channel/tests/test_channel.cairo b/src/channel/tests/test_channel.cairo index 2cd562695..dfe30ce55 100644 --- a/src/channel/tests/test_channel.cairo +++ b/src/channel/tests/test_channel.cairo @@ -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'); }