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

feat(zink-codegen)/added bound checks for primitive numbers #282

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sobuur0
Copy link

@sobuur0 sobuur0 commented Nov 27, 2024

Resolves #154

0x0be5B36E45BA0246fb33e0f915ed459d9fcC249d

@clearloop
Copy link
Member

Hi @sobuur0 , thanks for the contributions! please include the tests of bound checks in examples/ btw when you finish the implementation, also, please add your wallet address in the details of the PR, we'll send you the budget via polygon once the PR getting merged ^ ^

@sobuur0
Copy link
Author

sobuur0 commented Dec 3, 2024

@clearloop for this is it only the example that remains

Copy link
Member

@clearloop clearloop left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Introducing a trait for the bound checks make sense!

However, we can not use rust library's check_add because the WASM IR output, instead, we need to append extra logic for checking them on our own.

Yes, we need to create example under /examples for testing our implementation, for example /examples/safe_math!

When you meet any questions while working on the PR, no hesitate to request for a review via github, you can find it on the top right of the github UI!

btw please add your polygon address in the PR description, once the PR get merged, will transfer the bounty to the address provided in the PR ^ ^

($t:ty) => {
impl SafeArithmetic for $t {
fn safe_add(self, rhs: Self) -> Self {
self.checked_add(rhs)
Copy link
Member

Choose a reason for hiding this comment

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

we can not use the checked_add from rust library because it will generate verbose bytecode in the WASM IR, instead, we need to append EVM assembly opcodes to operations, for example

fn safe_add(self, rhs: Self) -> Self {
   // the return value r is now on stack
   let r = self.add(rhs);
   if r > Self::MAX {
     zink::revert!("...");
   }
   r
}

Same for all of the other operations

Copy link
Author

@sobuur0 sobuur0 Dec 5, 2024

Choose a reason for hiding this comment

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

@clearloop i'm having some imports issue, where should i place the logic?

Copy link
Member

Choose a reason for hiding this comment

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

replace them into your current functions of the SafeArithmetic trait, could please elaborate which kind of import issues you are facing?

@sobuur0 sobuur0 changed the title added bound checks for primitive numbers feat(zink-codegen)/added bound checks for primitive numbers Dec 3, 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.

Bound checks for numbers
2 participants