From eb046e6263481e0e4e6390074cf843bc0f3d1ab8 Mon Sep 17 00:00:00 2001 From: kata Date: Wed, 11 Dec 2024 17:55:34 +0800 Subject: [PATCH] fix potential uint overflow --- src/stdlib/native/int/lib.no | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/stdlib/native/int/lib.no b/src/stdlib/native/int/lib.no index 09bdbf16a..315c252cd 100644 --- a/src/stdlib/native/int/lib.no +++ b/src/stdlib/native/int/lib.no @@ -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); // range check let ignore_ = bits::to_bits(bit_len, val); @@ -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); @@ -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); @@ -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);