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

NonZero (rotate_left & rotate_right) Proof #202

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

lang280
Copy link

@lang280 lang280 commented Dec 1, 2024

Working on #71 (Safety of NonZero)

We are looking for feedback on our proof for rotate_left & rotate_right.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@lang280 lang280 requested a review from a team as a code owner December 1, 2024 22:31
Comment on lines 2314 to 2341
mod macro_nonzero_check_rotate_right {
use super::*;
macro_rules! nonzero_check_rotate_right {
($t:ty, $nonzero_type:ty, $nonzero_check_rotate_right_for:ident) => {
#[kani::proof]
pub fn $nonzero_check_rotate_right_for() {
let int_x: $t = kani::any();
let n: u32 = kani::any();
kani::assume(int_x != 0); // x must be non-zero

// Ensure that n is within a valid range for rotating
kani::assume(n < (core::mem::size_of::<$t>() as u32 * 8));
kani::assume(n >= 0);

unsafe {
let x = <$nonzero_type>::new_unchecked(int_x);

// Perform rotate_right
let result = x.rotate_right(n);

// Ensure the result is still non-zero
assert!(result.get() != 0);
}


}
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you perhaps just use a single macro with one extra parameter to cover both rotate_left and rotate_right? Or, possibly even simpler: just make the macro call both as all requirements are the same for both functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have merge the 2 rotate function into 1 macro

kani::assume(int_x != 0); // x must be non-zero

// Ensure that n is within a valid range for rotating
// kani::assume(n < (std::mem::size_of::<$t>() as u32 * 8));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented out (yet still left in here)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this comment

// kani::assume(n < (std::mem::size_of::<$t>() as u32 * 8));
// need to use core::mem instead of std::mem
kani::assume(n < (core::mem::size_of::<$t>() as u32 * 8));
kani::assume(n >= 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is trivially true given n is unsigned.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this assume

// Ensure that n is within a valid range for rotating
// kani::assume(n < (std::mem::size_of::<$t>() as u32 * 8));
// need to use core::mem instead of std::mem
kani::assume(n < (core::mem::size_of::<$t>() as u32 * 8));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this assumption required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After talked with Carolyn, I understand that there's no need to have assume here. Already removed it

Comment on lines 2291 to 2292
// Ensure the result is still non-zero
assert!(result.get() != 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about asserting that rotate_left(rotate_right(x)) == x?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have merged rotated_right and rotate_left with this logic

@lang280 lang280 changed the title NonZero (rotate_left & rotate_right) Proof for Contracts NonZero (rotate_left & rotate_right) Proof Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants