From 9e3e9241345d683ae4d6ba509061f3ff079f667b Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Wed, 1 Nov 2023 14:29:41 -0400 Subject: [PATCH 01/19] Improve readability --- .../data_types/01_integers.md | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 3a115f2fc23..a91ef710363 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -1,28 +1,19 @@ --- title: Integers -description: - Explore the Integer data type in Noir. Learn about its methods, see real-world examples, and grasp how to efficiently use Integers in your Noir code. -keywords: - [ - noir, - integer types, - methods, - examples, - arithmetic, - ] +description: Explore the Integer data type in Noir. Learn about its methods, see real-world examples, and grasp how to efficiently use Integers in your Noir code. +keywords: [noir, integer types, methods, examples, arithmetic] --- An integer type is a range constrained field type. The Noir frontend currently supports unsigned, arbitrary-sized integer types. -> **Note:** When an integer is defined in Noir without a specific type, it will default to `Field`. The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. +When an integer is defined in Noir without a specific type, it will default to `Field`. The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. An integer type is specified first with the letter `u`, indicating its unsigned nature, followed by its length in bits (e.g. `32`). For example, a `u32` variable can store a value in the range of $\\([0,2^{32}-1]\\)$. -> **Note:** The default proving backend supports both even (e.g. `u16`, `u48`) and odd (e.g. `u5`, `u3`) -> sized integer types. +> **Note:** The default proving backend supports both even (e.g. `u16`, `u48`) and odd (e.g. `u5`, `u3`) sized integer types. Taking a look of how the type is used: @@ -32,6 +23,8 @@ fn main(x : Field, y : u32) { } ``` -`x`, `y` and `z` are all private values in this example. However, `x` is a field while `y` and `z` -are unsigned 32-bit integers. If `y` or `z` exceeds the range $\\([0,2^{32}-1]\\)$, proofs created +Note that `x`, `y` and `z` are all private values in this example, where `x` is a field while `y` and `z` +are unsigned 32-bit integers. + +If `y` or `z` exceeds the range $\\([0,2^{32}-1]\\)$, proofs created will be rejected by the verifier. From c9afb191f10f04f899ddf0ceb1560d75d99bdb83 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Wed, 1 Nov 2023 14:31:14 -0400 Subject: [PATCH 02/19] Improve readability --- docs/docs/language_concepts/data_types/01_integers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index a91ef710363..72de24d7dbf 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -13,7 +13,7 @@ An integer type is specified first with the letter `u`, indicating its unsigned its length in bits (e.g. `32`). For example, a `u32` variable can store a value in the range of $\\([0,2^{32}-1]\\)$. -> **Note:** The default proving backend supports both even (e.g. `u16`, `u48`) and odd (e.g. `u5`, `u3`) sized integer types. +> **Note:** The default proving backend supports both even (e.g. _u8_, _u48_) and odd (e.g. _u3_, _u61_) sized integer types. Taking a look of how the type is used: From e869722997cf320f9e4310ee7ee27d3598819820 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Wed, 1 Nov 2023 14:36:19 -0400 Subject: [PATCH 03/19] Change even and odd uint examples --- docs/docs/language_concepts/data_types/01_integers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 72de24d7dbf..efe171113d9 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -13,7 +13,7 @@ An integer type is specified first with the letter `u`, indicating its unsigned its length in bits (e.g. `32`). For example, a `u32` variable can store a value in the range of $\\([0,2^{32}-1]\\)$. -> **Note:** The default proving backend supports both even (e.g. _u8_, _u48_) and odd (e.g. _u3_, _u61_) sized integer types. +> **Note:** The default proving backend supports both even (e.g. _u2_, _u52_) and odd (e.g. _u3_, _u127_) sized integer types. Taking a look of how the type is used: From 711ffc7fe329a9d4423b50057b6245fb6fefde12 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Wed, 1 Nov 2023 14:43:20 -0400 Subject: [PATCH 04/19] Add overflow example --- .../data_types/01_integers.md | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index efe171113d9..8ab53aaede0 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -10,21 +10,36 @@ arbitrary-sized integer types. When an integer is defined in Noir without a specific type, it will default to `Field`. The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. An integer type is specified first with the letter `u`, indicating its unsigned nature, followed by -its length in bits (e.g. `32`). For example, a `u32` variable can store a value in the range of -$\\([0,2^{32}-1]\\)$. +its length in bits (e.g. `8`). For example, a `u8` variable can store a value in the range of +$\\([0,2^{8}-1]\\)$. -> **Note:** The default proving backend supports both even (e.g. _u2_, _u52_) and odd (e.g. _u3_, _u127_) sized integer types. +> **Note:** The default proving backend supports both even (e.g. _u2_, _u32_) and odd (e.g. _u3_, _u127_) sized integer types. Taking a look of how the type is used: ```rust -fn main(x : Field, y : u32) { - let z = x as u32 + y; +fn main(x : Field, y : u8) { + let z = x as u8 + y; + assert (z > 0); } ``` Note that `x`, `y` and `z` are all private values in this example, where `x` is a field while `y` and `z` are unsigned 32-bit integers. -If `y` or `z` exceeds the range $\\([0,2^{32}-1]\\)$, proofs created +If `y` or `z` exceeds the range $\\([0,2^{8}-1]\\)$, proofs created will be rejected by the verifier. + +For example, attempting to prove the above code with the following inputs: + +```toml +x = "1" +y = "256" +``` + +Would result in: + +``` +$ nargo prove +The parameter y is expected to be a Integer { sign: Unsigned, width: 8 } but found incompatible value Field(2⁸) +``` From aee18e57fb5ca02b28b35702ec4006dfedc8b4d1 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Wed, 1 Nov 2023 14:45:55 -0400 Subject: [PATCH 05/19] Improve readibility --- docs/docs/language_concepts/data_types/01_integers.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 8ab53aaede0..8d036b543b4 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -24,10 +24,10 @@ fn main(x : Field, y : u8) { } ``` -Note that `x`, `y` and `z` are all private values in this example, where `x` is a field while `y` and `z` -are unsigned 32-bit integers. +Note that _x_, _y_ and _z_ are all private values in this example, where _x_ is a field while _y_ and _z_ +are unsigned 8-bit integers. -If `y` or `z` exceeds the range $\\([0,2^{8}-1]\\)$, proofs created +If _y_ or _z_ exceeds the range $\\([0,2^{8}-1]\\)$, proofs created will be rejected by the verifier. For example, attempting to prove the above code with the following inputs: From 6756c2b96a4bb66831550cc35546cb3eeec98415 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Wed, 1 Nov 2023 14:52:29 -0400 Subject: [PATCH 06/19] Update overflow error example --- .../language_concepts/data_types/01_integers.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 8d036b543b4..e96c1021ea3 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -9,7 +9,7 @@ arbitrary-sized integer types. When an integer is defined in Noir without a specific type, it will default to `Field`. The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. -An integer type is specified first with the letter `u`, indicating its unsigned nature, followed by +An unsigned integer type is specified first with the letter `u`, indicating its unsigned nature, followed by its length in bits (e.g. `8`). For example, a `u8` variable can store a value in the range of $\\([0,2^{8}-1]\\)$. @@ -34,12 +34,19 @@ For example, attempting to prove the above code with the following inputs: ```toml x = "1" -y = "256" +y = "255" ``` Would result in: ``` $ nargo prove -The parameter y is expected to be a Integer { sign: Unsigned, width: 8 } but found incompatible value Field(2⁸) +error: Assertion failed: 'attempt to add with overflow' + ┌─ ~/src/main.nr:2:13 + │ +2 │ let z = x as u8 + y; + │ ----------- + │ + = Call stack: + ... ``` From 269c1ceb78babc59db3263675b36b5decfb31ff4 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Wed, 1 Nov 2023 15:15:21 -0400 Subject: [PATCH 07/19] Add signed integer section --- .../data_types/01_integers.md | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index e96c1021ea3..bc51781e269 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -4,11 +4,12 @@ description: Explore the Integer data type in Noir. Learn about its methods, see keywords: [noir, integer types, methods, examples, arithmetic] --- -An integer type is a range constrained field type. The Noir frontend currently supports unsigned, -arbitrary-sized integer types. +An integer type is a range constrained field type. The Noir frontend currently supports arbitrary-sized, both unsigned and signed integer types. When an integer is defined in Noir without a specific type, it will default to `Field`. The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. +## Unsigned Integers + An unsigned integer type is specified first with the letter `u`, indicating its unsigned nature, followed by its length in bits (e.g. `8`). For example, a `u8` variable can store a value in the range of $\\([0,2^{8}-1]\\)$. @@ -42,11 +43,54 @@ Would result in: ``` $ nargo prove error: Assertion failed: 'attempt to add with overflow' - ┌─ ~/src/main.nr:2:13 - │ -2 │ let z = x as u8 + y; - │ ----------- - │ - = Call stack: - ... +┌─ ~/src/main.nr:2:13 +│ +│ let z = x as u8 + y; +│ ----------- +│ += Call stack: + ... +``` + +## Signed Integers + +A signed integer type is specified first with the letter `i`, followed by +its length in bits (e.g. `8`). For example, a `i8` variable can store a value in the range of +$\\([-2^{7}+1,2^{7}-1]\\)$. + +An example of how the type is used: + +```rust +fn main() { + let x : i8 = -1; + let y : i8 = -1; + let z = x + y; + assert (z == -2); +} +``` + +Similar to unsigned integers, signed integers are also range constrained. + +For example, attempting to prove: + +```rust +fn main() { + let x : i8 = -127; + let y : i8 = -10; + let z = x + y; +} +``` + +Would result in: + +``` +$ nargo prove +error: Assertion failed: 'attempt to add with overflow' +┌─ ~/src/main.nr:4:13 +│ +│ let z = x + y; +│ ----- +│ += Call stack: + ... ``` From 9484ca369b1ca79ecacd06e12139efd1e7d83366 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Wed, 1 Nov 2023 15:53:01 -0400 Subject: [PATCH 08/19] Simplify descriptions and examples --- .../data_types/01_integers.md | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index bc51781e269..5164c26f39f 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -4,38 +4,38 @@ description: Explore the Integer data type in Noir. Learn about its methods, see keywords: [noir, integer types, methods, examples, arithmetic] --- -An integer type is a range constrained field type. The Noir frontend currently supports arbitrary-sized, both unsigned and signed integer types. +An integer type is a range constrained field type. The Noir frontend supports arbitrarily-sized, both unsigned and signed integer types. -When an integer is defined in Noir without a specific type, it will default to `Field`. The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. +> **Note:** When an integer is defined in Noir without a specific type, it will default to `Field`. The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. ## Unsigned Integers -An unsigned integer type is specified first with the letter `u`, indicating its unsigned nature, followed by -its length in bits (e.g. `8`). For example, a `u8` variable can store a value in the range of -$\\([0,2^{8}-1]\\)$. - -> **Note:** The default proving backend supports both even (e.g. _u2_, _u32_) and odd (e.g. _u3_, _u127_) sized integer types. - -Taking a look of how the type is used: +An unsigned integer type is specified first with the letter `u`, indicating its unsigned nature, followed by its length in bits (e.g. `8`): ```rust -fn main(x : Field, y : u8) { - let z = x as u8 + y; - assert (z > 0); +fn main() { + let x : u8 = 1; + let y : u8 = 1; + let z = x + y; + assert (z == 2); } ``` -Note that _x_, _y_ and _z_ are all private values in this example, where _x_ is a field while _y_ and _z_ -are unsigned 8-bit integers. +The length in bits determines the boundaries the integer type can store. For example, a `u8` variable can store a value in the range of 0 to 255 (i.e. $\\2^{8}-1\\$). -If _y_ or _z_ exceeds the range $\\([0,2^{8}-1]\\)$, proofs created -will be rejected by the verifier. +Computations that exceed the type boundaries would result in overflow errors. For example, attempting to prove: + +```rust +fn main(x : u8, y : u8) { + let z = x + y; +} +``` -For example, attempting to prove the above code with the following inputs: +With: ```toml -x = "1" -y = "255" +x = "255" +y = "1" ``` Would result in: @@ -43,22 +43,20 @@ Would result in: ``` $ nargo prove error: Assertion failed: 'attempt to add with overflow' -┌─ ~/src/main.nr:2:13 +┌─ ~/src/main.nr:9:13 │ -│ let z = x as u8 + y; -│ ----------- +│ let z = x + y; +│ ----- │ = Call stack: ... ``` -## Signed Integers +> **Note:** The default proving backend supports both even (e.g. _u2_) and odd (e.g. _u3_) arbitrarily-sized integer types up to _u127_. -A signed integer type is specified first with the letter `i`, followed by -its length in bits (e.g. `8`). For example, a `i8` variable can store a value in the range of -$\\([-2^{7}+1,2^{7}-1]\\)$. +## Signed Integers -An example of how the type is used: +A signed integer type is specified first with the letter `i`, stands for integer, followed by its length in bits (e.g. `8`): ```rust fn main() { @@ -69,14 +67,14 @@ fn main() { } ``` -Similar to unsigned integers, signed integers are also range constrained. +The length in bits determines the boundaries the integer type can store. For example, a `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). -For example, attempting to prove: +Computations that exceed the type boundaries would result in overflow errors. For example, attempting to prove: ```rust fn main() { - let x : i8 = -127; - let y : i8 = -10; + let x : i8 = -118; + let y : i8 = -11; let z = x + y; } ``` @@ -94,3 +92,5 @@ error: Assertion failed: 'attempt to add with overflow' = Call stack: ... ``` + +> **Note:** The default proving backend supports both even (e.g. _i2_) and odd (e.g. _i3_) arbitrarily-sized integer types up to _i127_. From 857fd25f7271ce53557359c4fea1db761a336ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Sousa?= Date: Thu, 2 Nov 2023 11:24:44 +0000 Subject: [PATCH 09/19] adding wrapping methods documentation --- .../data_types/01_integers.md | 66 +++++++++++-------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 5164c26f39f..c7ee3c919ca 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -23,7 +23,24 @@ fn main() { The length in bits determines the boundaries the integer type can store. For example, a `u8` variable can store a value in the range of 0 to 255 (i.e. $\\2^{8}-1\\$). -Computations that exceed the type boundaries would result in overflow errors. For example, attempting to prove: +## Signed Integers + +A signed integer type is specified first with the letter `i`, stands for integer, followed by its length in bits (e.g. `8`): + +```rust +fn main() { + let x : i8 = -1; + let y : i8 = -1; + let z = x + y; + assert (z == -2); +} +``` + +The length in bits determines the boundaries the integer type can store. For example, a `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). + +## Overflows + +Computations that exceed the type boundaries would result in overflow errors. This happens with both signed and unsigned integers. For example, attempting to prove: ```rust fn main(x : u8, y : u8) { @@ -52,45 +69,36 @@ error: Assertion failed: 'attempt to add with overflow' ... ``` -> **Note:** The default proving backend supports both even (e.g. _u2_) and odd (e.g. _u3_) arbitrarily-sized integer types up to _u127_. - -## Signed Integers - -A signed integer type is specified first with the letter `i`, stands for integer, followed by its length in bits (e.g. `8`): +A similar error would happen with unsigned integers, for example while trying to prove: ```rust fn main() { - let x : i8 = -1; - let y : i8 = -1; + let x : i8 = -118; + let y : i8 = -11; let z = x + y; - assert (z == -2); } ``` -The length in bits determines the boundaries the integer type can store. For example, a `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). +> **Note:** The default proving backend supports both even (e.g. _u2_) and odd (e.g. _u3_) arbitrarily-sized integer types up to _u127_. + +### Wrapping methods -Computations that exceed the type boundaries would result in overflow errors. For example, attempting to prove: +Although integer overflow is expected to error, it is understood that some use-cases may actually want to rely on wrapping. + +For that, you can import and use these `wrapping` functions from the standard library, which are defined as: ```rust -fn main() { - let x : i8 = -118; - let y : i8 = -11; - let z = x + y; -} +wrapping_add(x : T, y: T) -> T +wrapping_sub(x : T, y: T) -> T +wrapping_mul(x : T, y: T) -> T ``` -Would result in: +example usage: -``` -$ nargo prove -error: Assertion failed: 'attempt to add with overflow' -┌─ ~/src/main.nr:4:13 -│ -│ let z = x + y; -│ ----- -│ -= Call stack: - ... -``` +```rust +use dep::std; -> **Note:** The default proving backend supports both even (e.g. _i2_) and odd (e.g. _i3_) arbitrarily-sized integer types up to _i127_. +fn main(x : u8, y : u8) { + let z = std::wrapping_add(x + y); +} +``` From 245699dbae67eca677fa27ebb20ff8d21dcf0271 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Thu, 2 Nov 2023 12:15:45 -0400 Subject: [PATCH 10/19] Incorporate Jake's suggestions --- .../data_types/01_integers.md | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index c7ee3c919ca..96ef421e5ef 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -10,7 +10,7 @@ An integer type is a range constrained field type. The Noir frontend supports ar ## Unsigned Integers -An unsigned integer type is specified first with the letter `u`, indicating its unsigned nature, followed by its length in bits (e.g. `8`): +An unsigned integer type is specified first with the letter `u` (indicating its unsigned nature) followed by its length in bits (e.g. `8`): ```rust fn main() { @@ -25,22 +25,22 @@ The length in bits determines the boundaries the integer type can store. For exa ## Signed Integers -A signed integer type is specified first with the letter `i`, stands for integer, followed by its length in bits (e.g. `8`): +A signed integer type is specified first with the letter `i` (which stands for integer) followed by its length in bits (e.g. `8`): ```rust fn main() { - let x : i8 = -1; - let y : i8 = -1; + let x: i8 = -1; + let y: i8 = -1; let z = x + y; assert (z == -2); } ``` -The length in bits determines the boundaries the integer type can store. For example, a `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). +The bit count of the integer determines the maximum and minimum integers the type can store. For example, an `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). ## Overflows -Computations that exceed the type boundaries would result in overflow errors. This happens with both signed and unsigned integers. For example, attempting to prove: +Computations that exceed the type boundaries will result in overflow errors. This happens with both signed and unsigned integers. For example, attempting to prove: ```rust fn main(x : u8, y : u8) { @@ -69,12 +69,12 @@ error: Assertion failed: 'attempt to add with overflow' ... ``` -A similar error would happen with unsigned integers, for example while trying to prove: +A similar error would happen with signed integers, for example while trying to prove: ```rust fn main() { - let x : i8 = -118; - let y : i8 = -11; + let x: i8 = -118; + let y: i8 = -11; let z = x + y; } ``` @@ -83,15 +83,13 @@ fn main() { ### Wrapping methods -Although integer overflow is expected to error, it is understood that some use-cases may actually want to rely on wrapping. +Although integer overflow is expected to error, some use-cases rely on wrapping. For these use-cases, the standard library provides `wrapping` variants of certain common operations: -For that, you can import and use these `wrapping` functions from the standard library, which are defined as: - -```rust +````rust wrapping_add(x : T, y: T) -> T -wrapping_sub(x : T, y: T) -> T -wrapping_mul(x : T, y: T) -> T -``` +fn wrapping_add(x : T, y: T) -> T; +fn wrapping_sub(x : T, y: T) -> T; +fn wrapping_mul(x : T, y: T) -> T; example usage: @@ -99,6 +97,7 @@ example usage: use dep::std; fn main(x : u8, y : u8) { - let z = std::wrapping_add(x + y); +fn main(x : u8, y : u8) -> pub u8 { + std::wrapping_add(x + y) } -``` +```` From 916b2a087c6bbf73230316337ecf769cee81e20c Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 2 Nov 2023 11:39:11 -0500 Subject: [PATCH 11/19] Update docs/docs/language_concepts/data_types/01_integers.md --- docs/docs/language_concepts/data_types/01_integers.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 96ef421e5ef..b04be931bbc 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -14,8 +14,8 @@ An unsigned integer type is specified first with the letter `u` (indicating its ```rust fn main() { - let x : u8 = 1; - let y : u8 = 1; + let x: u8 = 1; + let y: u8 = 1; let z = x + y; assert (z == 2); } From 12ffa953e1e49ce073050756137e538adb1fdd8c Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 2 Nov 2023 11:39:16 -0500 Subject: [PATCH 12/19] Update docs/docs/language_concepts/data_types/01_integers.md --- docs/docs/language_concepts/data_types/01_integers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index b04be931bbc..678481ba42f 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -43,7 +43,7 @@ The bit count of the integer determines the maximum and minimum integers the typ Computations that exceed the type boundaries will result in overflow errors. This happens with both signed and unsigned integers. For example, attempting to prove: ```rust -fn main(x : u8, y : u8) { +fn main(x: u8, y: u8) { let z = x + y; } ``` From dbcef566f8957c995240ee3c02ea120083b9f493 Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 2 Nov 2023 11:39:23 -0500 Subject: [PATCH 13/19] Update docs/docs/language_concepts/data_types/01_integers.md --- docs/docs/language_concepts/data_types/01_integers.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 678481ba42f..1c89df9062d 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -86,18 +86,16 @@ fn main() { Although integer overflow is expected to error, some use-cases rely on wrapping. For these use-cases, the standard library provides `wrapping` variants of certain common operations: ````rust -wrapping_add(x : T, y: T) -> T -fn wrapping_add(x : T, y: T) -> T; -fn wrapping_sub(x : T, y: T) -> T; -fn wrapping_mul(x : T, y: T) -> T; +fn wrapping_add(x: T, y: T) -> T; +fn wrapping_sub(x: T, y: T) -> T; +fn wrapping_mul(x: T, y: T) -> T; example usage: ```rust use dep::std; -fn main(x : u8, y : u8) { -fn main(x : u8, y : u8) -> pub u8 { +fn main(x: u8, y: u8) -> pub u8 { std::wrapping_add(x + y) } ```` From bc8c8d99579634360cc5d21e14a9f0b6b351732f Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Thu, 2 Nov 2023 13:02:34 -0400 Subject: [PATCH 14/19] Fix code block --- docs/docs/language_concepts/data_types/01_integers.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 1c89df9062d..6c088be8589 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -85,10 +85,11 @@ fn main() { Although integer overflow is expected to error, some use-cases rely on wrapping. For these use-cases, the standard library provides `wrapping` variants of certain common operations: -````rust +```rust fn wrapping_add(x: T, y: T) -> T; fn wrapping_sub(x: T, y: T) -> T; fn wrapping_mul(x: T, y: T) -> T; +``` example usage: @@ -98,4 +99,4 @@ use dep::std; fn main(x: u8, y: u8) -> pub u8 { std::wrapping_add(x + y) } -```` +``` From 9e8020983bb5cdd3e891b17375d6d814417e014f Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Thu, 2 Nov 2023 13:03:43 -0400 Subject: [PATCH 15/19] Minor word change --- docs/docs/language_concepts/data_types/01_integers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 6c088be8589..03591fa71ea 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -91,7 +91,7 @@ fn wrapping_sub(x: T, y: T) -> T; fn wrapping_mul(x: T, y: T) -> T; ``` -example usage: +Example of how it is used: ```rust use dep::std; From 2806aa31f8631ed25f7b64e349f66424e1c7e348 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Thu, 2 Nov 2023 13:06:42 -0400 Subject: [PATCH 16/19] Align bit size descriptions --- docs/docs/language_concepts/data_types/01_integers.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 03591fa71ea..015b9edc32b 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -10,7 +10,7 @@ An integer type is a range constrained field type. The Noir frontend supports ar ## Unsigned Integers -An unsigned integer type is specified first with the letter `u` (indicating its unsigned nature) followed by its length in bits (e.g. `8`): +An unsigned integer type is specified first with the letter `u` (indicating its unsigned nature) followed by its bit size (e.g. `8`): ```rust fn main() { @@ -21,11 +21,11 @@ fn main() { } ``` -The length in bits determines the boundaries the integer type can store. For example, a `u8` variable can store a value in the range of 0 to 255 (i.e. $\\2^{8}-1\\$). +The bit size determines the maximum value the integer type can store. For example, a `u8` variable can store a value in the range of 0 to 255 (i.e. $\\2^{8}-1\\$). ## Signed Integers -A signed integer type is specified first with the letter `i` (which stands for integer) followed by its length in bits (e.g. `8`): +A signed integer type is specified first with the letter `i` (which stands for integer) followed by its bit size (e.g. `8`): ```rust fn main() { @@ -36,7 +36,7 @@ fn main() { } ``` -The bit count of the integer determines the maximum and minimum integers the type can store. For example, an `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). +The bit size determines the maximum and minimum range of value the integer type can store. For example, an `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). ## Overflows From 971b12eb9d079b76b9e8c45ad2836f670d16520c Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Thu, 2 Nov 2023 13:07:01 -0400 Subject: [PATCH 17/19] Simplify example description --- docs/docs/language_concepts/data_types/01_integers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index 015b9edc32b..e057f39d5cf 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -69,7 +69,7 @@ error: Assertion failed: 'attempt to add with overflow' ... ``` -A similar error would happen with signed integers, for example while trying to prove: +A similar error would happen with signed integers: ```rust fn main() { From aefce77e48e33a507c4c8b0918adf8001e7fe8d7 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Thu, 2 Nov 2023 13:12:07 -0400 Subject: [PATCH 18/19] Reposition max bit size note --- docs/docs/language_concepts/data_types/01_integers.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index e057f39d5cf..f8f49a64d4b 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -38,6 +38,8 @@ fn main() { The bit size determines the maximum and minimum range of value the integer type can store. For example, an `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). +> **Note:** If you are using the default proving backend with Noir, both even (e.g. _u2_, _i2_) and odd (e.g. _u3_, _i3_) arbitrarily-sized integer types up to 127 bits (i.e. _u127_ and _i127_) are supported. + ## Overflows Computations that exceed the type boundaries will result in overflow errors. This happens with both signed and unsigned integers. For example, attempting to prove: @@ -79,8 +81,6 @@ fn main() { } ``` -> **Note:** The default proving backend supports both even (e.g. _u2_) and odd (e.g. _u3_) arbitrarily-sized integer types up to _u127_. - ### Wrapping methods Although integer overflow is expected to error, some use-cases rely on wrapping. For these use-cases, the standard library provides `wrapping` variants of certain common operations: From 0f7f0d857e88fcdaaa52df35e26523f14dcd35c9 Mon Sep 17 00:00:00 2001 From: Savio-Sou <72797635+Savio-Sou@users.noreply.github.com> Date: Thu, 2 Nov 2023 13:15:15 -0400 Subject: [PATCH 19/19] Update notes to admonitions --- .../language_concepts/data_types/01_integers.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/docs/language_concepts/data_types/01_integers.md b/docs/docs/language_concepts/data_types/01_integers.md index f8f49a64d4b..b1e7ad11bfd 100644 --- a/docs/docs/language_concepts/data_types/01_integers.md +++ b/docs/docs/language_concepts/data_types/01_integers.md @@ -6,7 +6,13 @@ keywords: [noir, integer types, methods, examples, arithmetic] An integer type is a range constrained field type. The Noir frontend supports arbitrarily-sized, both unsigned and signed integer types. -> **Note:** When an integer is defined in Noir without a specific type, it will default to `Field`. The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. +:::info + +When an integer is defined in Noir without a specific type, it will default to `Field`. + +The one exception is for loop indices which default to `u64` since comparisons on `Field`s are not possible. + +::: ## Unsigned Integers @@ -38,7 +44,11 @@ fn main() { The bit size determines the maximum and minimum range of value the integer type can store. For example, an `i8` variable can store a value in the range of -128 to 127 (i.e. $\\-2^{7}\\$ to $\\2^{7}-1\\$). -> **Note:** If you are using the default proving backend with Noir, both even (e.g. _u2_, _i2_) and odd (e.g. _u3_, _i3_) arbitrarily-sized integer types up to 127 bits (i.e. _u127_ and _i127_) are supported. +:::tip + +If you are using the default proving backend with Noir, both even (e.g. _u2_, _i2_) and odd (e.g. _u3_, _i3_) arbitrarily-sized integer types up to 127 bits (i.e. _u127_ and _i127_) are supported. + +::: ## Overflows