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

fix potential uint overflow #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
12 changes: 8 additions & 4 deletions src/stdlib/native/int/lib.no
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ struct Uint8 {
fn Uint8.new(val: Field) -> Uint8 {
let bit_len = 8;

bits::check_field_size(bit_len);
// has to ensure multiply won't overflow prime field
bits::check_field_size(bit_len * 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't it off by one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Inside check_field_size, it checks the prime field is strictly greater than bit_len * 2:

if to_cmp >= bit_len {
    return Err(Error::new(
        "constraint-generation",
        ErrorKind::AssertionFailed,
        span,
    ));
}

So the prime field bit len is ensured to have at least one more bit than the test value.


// range check
let ignore_ = bits::to_bits(bit_len, val);
Expand All @@ -33,7 +34,8 @@ struct Uint16 {
fn Uint16.new(val: Field) -> Uint16 {
let bit_len = 16;

bits::check_field_size(bit_len);
// has to ensure multiply won't overflow prime field
bits::check_field_size(bit_len * 2);

// range check
let ignore_ = bits::to_bits(bit_len, val);
Expand All @@ -52,7 +54,8 @@ struct Uint32 {
fn Uint32.new(val: Field) -> Uint32 {
let bit_len = 32;

bits::check_field_size(bit_len);
// has to ensure multiply won't overflow prime field
bits::check_field_size(bit_len * 2);

// range check
let ignore_ = bits::to_bits(bit_len, val);
Expand All @@ -71,7 +74,8 @@ struct Uint64 {
fn Uint64.new(val: Field) -> Uint64 {
let bit_len = 64;

bits::check_field_size(bit_len);
// has to ensure multiply won't overflow prime field
bits::check_field_size(bit_len * 2);

// range check
let ignore_ = bits::to_bits(bit_len, val);
Expand Down
Loading