-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: document big integers (#4487)
# Description ## Problem\* Resolves #4205 ## Summary\* Add a section on standard library documentation ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [X] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [X] I have tested the changes locally. - [X] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: José Pedro Sousa <[email protected]> Co-authored-by: Savio <[email protected]>
- Loading branch information
1 parent
b94adb9
commit c6e6efd
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,8 @@ | |
"sdiv", | ||
"secp256k1", | ||
"secp256r1", | ||
"Secpk", | ||
"Secpr", | ||
"signedness", | ||
"signorecello", | ||
"smol", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"no-missing-space-atx": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
--- | ||
title: Big Integers | ||
description: How to use big integers from Noir standard library | ||
keywords: | ||
[ | ||
Big Integer, | ||
Noir programming language, | ||
Noir libraries, | ||
] | ||
--- | ||
|
||
The BigInt module in the standard library exposes some class of integers which do not fit (well) into a Noir native field. It implements modulo arithmetic, modulo a 'big' prime number. | ||
|
||
:::note | ||
|
||
The module can currently be considered as `Field`s with fixed modulo sizes used by a set of elliptic curves, in addition to just the native curve. [More work](https://github.com/noir-lang/noir/issues/510) is needed to achieve arbitrarily sized big integers. | ||
|
||
::: | ||
|
||
Currently 6 classes of integers (i.e 'big' prime numbers) are available in the module, namely: | ||
|
||
- BN254 Fq: Bn254Fq | ||
- BN254 Fr: Bn254Fr | ||
- Secp256k1 Fq: Secpk1Fq | ||
- Secp256k1 Fr: Secpk1Fr | ||
- Secp256r1 Fr: Secpr1Fr | ||
- Secp256r1 Fq: Secpr1Fq | ||
|
||
Where XXX Fq and XXX Fr denote respectively the order of the base and scalar field of the (usual) elliptic curve XXX. | ||
For instance the big integer 'Secpk1Fq' in the standard library refers to integers modulo $2^{256}-2^{32}-977$. | ||
|
||
Feel free to explore the source code for the other primes: | ||
|
||
#include_code curve_order_base noir_stdlib/src/bigint.nr rust | ||
|
||
## Example usage | ||
|
||
A common use-case is when constructing a big integer from its bytes representation, and performing arithmetic operations on it: | ||
|
||
#include_code big_int_example test_programs/execution_success/bigint/src/main.nr rust | ||
|
||
## Methods | ||
|
||
The available operations for each big integer are: | ||
|
||
### from_le_bytes | ||
|
||
Construct a big integer from its little-endian bytes representation. Example: | ||
|
||
```rust | ||
let a = Secpk1Fq::from_le_bytes([x, y, 0, 45, 2]); | ||
``` | ||
|
||
Sure, here's the formatted version of the remaining methods: | ||
|
||
### to_le_bytes | ||
|
||
Return the little-endian bytes representation of a big integer. Example: | ||
|
||
```rust | ||
let bytes = a.to_le_bytes(); | ||
``` | ||
|
||
### add | ||
|
||
Add two big integers. Example: | ||
|
||
```rust | ||
let sum = a + b; | ||
``` | ||
|
||
### sub | ||
|
||
Subtract two big integers. Example: | ||
|
||
```rust | ||
let difference = a - b; | ||
``` | ||
|
||
### mul | ||
|
||
Multiply two big integers. Example: | ||
|
||
```rust | ||
let product = a * b; | ||
``` | ||
|
||
### div | ||
|
||
Divide two big integers. Note that division is field division and not euclidean division. Example: | ||
|
||
```rust | ||
let quotient = a / b; | ||
``` | ||
|
||
### eq | ||
|
||
Compare two big integers. Example: | ||
|
||
```rust | ||
let are_equal = a == b; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters