Skip to content

Commit

Permalink
Implement trailing zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
atgrosso committed Oct 14, 2024
1 parent 339bf86 commit 786e61c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
48 changes: 48 additions & 0 deletions stwo_cairo_verifier/src/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ pub impl ChannelImpl of ChannelTrait {
};
bytes
}

fn trailing_zeros(self: Channel) -> u32 {
let digest: felt252 = self.digest;
let mut digest_as_u256: u256 = digest.try_into().unwrap();

return Self::count_trailing_zeros(@digest_as_u256);
}

fn count_trailing_zeros(n: @u256) -> u32 {
let mut count = 0;
let mut value = n.clone();
while value > 0 {
if value & 1_u256 == 1 {
break;
}
count += 1;
value = value / 2;
};

return count;
}
}

#[inline]
Expand Down Expand Up @@ -336,4 +357,31 @@ mod tests {
let second_result = channel.draw_random_bytes();
assert_ne!(first_result, second_result);
}

#[test]
pub fn test_can_return_trailing_zeros_of_digest() {
let initial_digest = 0xcafecafe;
let mut channel = ChannelTrait::new(initial_digest);
let trailing_zeros: u32 = channel.trailing_zeros();
assert_eq!(trailing_zeros, 1);
}

#[test]
pub fn test_can_return_trailing_zeros_of_integer() {
let mut value = 1024_u256;
let mut tz = ChannelTrait::count_trailing_zeros(@value);
assert_eq!(tz, 10);

value = 16;
let mut tz = ChannelTrait::count_trailing_zeros(@value);
assert_eq!(tz, 4);

value = 3405695742;
let mut tz = ChannelTrait::count_trailing_zeros(@value);
assert_eq!(tz, 1);

value = 11111;
let mut tz = ChannelTrait::count_trailing_zeros(@value);
assert_eq!(tz, 0);
}
}
6 changes: 6 additions & 0 deletions stwo_cairo_verifier/src/pcs/verifier.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ impl CommitmentSchemeVerifierImpl of CommitmentSchemeVerifierTrait {

channel.mix_nonce(proof.proof_of_work);

// TODO: implement
// let proof_of_work_bits: u32 = *self.config.pow_bits;
// if channel.trailing_zeros() < proof_of_work_bits {
// return Result::Err(VerificationError::ProofOfWork);
// }

// Verify merkle decommitments.
assert_eq!(self.trees.len(), proof.queried_values.len());
assert_eq!(self.trees.len(), proof.decommitments.len());
Expand Down

0 comments on commit 786e61c

Please sign in to comment.