Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add draw_random_bytes to the channel #21

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions stwo_cairo_verifier/src/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ pub impl ChannelImpl of ChannelTrait {
};
res
}

/// Returns 31 random bytes computed as the first 31 bytes of the representative of
/// `self.draw_felt252()` in little endinan.
fn draw_random_bytes(ref self: Channel) -> Array<u8> {
let shift: felt252 = 256;
let shift_representative: u256 = shift.into();
let mut cur = self.draw_felt252();
let mut bytes = array![];
let mut i: usize = 0;
while i < 31 {
let cur_representative: u256 = cur.into();
let next_representative = cur_representative / shift_representative;
let next: felt252 = next_representative.try_into().unwrap();
let res = cur - next * shift;
let res_representative: u256 = res.into();
cur = next;
bytes.append((res_representative & 0xff).try_into().unwrap());
i += 1;
};
bytes
}
}

#[inline]
Expand Down Expand Up @@ -228,4 +249,86 @@ mod tests {

assert_ne!(initial_digest, channel.digest);
}

#[test]
pub fn test_draw_random_bytes_1() {
let initial_digest = 0;
let mut channel = ChannelTrait::new(initial_digest);
let result = channel.draw_random_bytes();
let expected_result = array![
197,
20,
139,
143,
49,
135,
207,
202,
93,
167,
20,
244,
184,
186,
20,
136,
204,
43,
46,
147,
213,
253,
175,
170,
13,
64,
15,
168,
232,
211,
147
];
assert_eq!(expected_result, result);
}

#[test]
pub fn test_draw_random_bytes_2() {
let initial_digest = 0xdeadbeef;
let mut channel = ChannelTrait::new(initial_digest);
let result = channel.draw_random_bytes();
let expected_result = array![
168,
175,
85,
209,
218,
65,
155,
212,
165,
88,
130,
167,
44,
242,
17,
127,
75,
251,
142,
180,
157,
176,
27,
167,
179,
247,
27,
113,
149,
41,
12
];
assert_eq!(expected_result, result);
}
}
Loading