Skip to content

Commit

Permalink
chore(docs): Update v0.23.0 docs with retroactive doc updates (#4324)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #4200.

## Summary\*

Copy updates introduced by the PRs listed in the Issue linked into the
versioned v0.23.0 doc folder.

Majority of the changes in this PR are merely direct copy-paste of
updates from the individual PRs. Exception being minor path fixes.

## 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.
  • Loading branch information
Savio-Sou authored Feb 10, 2024
1 parent 3972ead commit a44fcf3
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 58 deletions.
2 changes: 1 addition & 1 deletion docs/docs/noir/standard_library/black_box_fns.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here is a list of the current black box functions:
- [SHA256](./cryptographic_primitives/hashes#sha256)
- [Schnorr signature verification](./cryptographic_primitives/schnorr)
- [Blake2s](./cryptographic_primitives/hashes#blake2s)
- [Blake3](./cryptographic_primitives/hashes#blake2s)
- [Blake3](./cryptographic_primitives/hashes#blake3)
- [Pedersen Hash](./cryptographic_primitives/hashes#pedersen_hash)
- [Pedersen Commitment](./cryptographic_primitives/hashes#pedersen_commitment)
- [ECDSA signature verification](./cryptographic_primitives/ecdsa_sig_verification)
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/tutorials/noirjs_app.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ At this point in the tutorial, your folder structure should look like this:

`npx create vite` is amazing but it creates a bunch of files we don't really need for our simple example. Actually, let's just delete everything except for `index.html`, `main.js` and `package.json`. I feel lighter already.

![my heart is ready for you, noir.js](../../static/img/memes/titanic.jpeg)
![my heart is ready for you, noir.js](@site/static/img/memes/titanic.jpeg)

## HTML

Expand Down Expand Up @@ -270,7 +270,7 @@ if (verification) display('logs', 'Verifying proof... ✅');
You have successfully generated a client-side Noir web app!
![coded app without math knowledge](../../static/img/memes/flextape.jpeg)
![coded app without math knowledge](@site/static/img/memes/flextape.jpeg)
## Further Reading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,36 @@ fn main() {
}
```

### assert_max_bit_size

Adds a constraint to specify that the field can be represented with `bit_size` number of bits

```rust
fn assert_max_bit_size(self, bit_size: u32)
```

example:

```rust
fn main() {
let field = 2
field.assert_max_bit_size(32);
}
```

### sgn0

Parity of (prime) Field element, i.e. sgn0(x mod p) = 0 if x ∈ \{0, ..., p-1\} is even, otherwise sgn0(x mod p) = 1.

```rust
fn sgn0(self) -> u1
```


### lt

Returns true if the field is less than the other field

```rust
pub fn lt(self, another: Field) -> bool
```
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,55 @@ If you are using the default proving backend with Noir, both even (e.g. _u2_, _i

:::


## 128 bits Unsigned Integers

The built-in structure `U128` allows you to use 128-bit unsigned integers almost like a native integer type. However, there are some differences to keep in mind:
- You cannot cast between a native integer and `U128`
- There is a higher performance cost when using `U128`, compared to a native type.

Conversion between unsigned integer types and U128 are done through the use of `from_integer` and `to_integer` functions.

```rust
fn main() {
let x = U128::from_integer(23);
let y = U128::from_hex("0x7");
let z = x + y;
assert(z.to_integer() == 30);
}
```

`U128` is implemented with two 64 bits limbs, representing the low and high bits, which explains the performance cost. You should expect `U128` to be twice more costly for addition and four times more costly for multiplication.
You can construct a U128 from its limbs:
```rust
fn main(x: u64, y: u64) {
let x = U128::from_u64s_be(x,y);
assert(z.hi == x as Field);
assert(z.lo == y as Field);
}
```

Note that the limbs are stored as Field elements in order to avoid unnecessary conversions.
Apart from this, most operations will work as usual:

```rust
fn main(x: U128, y: U128) {
// multiplication
let c = x * y;
// addition and subtraction
let c = c - x + y;
// division
let c = x / y;
// bit operation;
let c = x & y | y;
// bit shift
let c = x << y;
// comparisons;
let c = x < y;
let c = x == y;
}
```

## 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:
Expand Down Expand Up @@ -108,6 +157,6 @@ Example of how it is used:
use dep::std;

fn main(x: u8, y: u8) -> pub u8 {
std::wrapping_add(x + y)
std::wrapping_add(x, y)
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,26 @@ keywords: [noir, black box functions]

Black box functions are functions in Noir that rely on backends implementing support for specialized constraints. This makes certain zk-snark unfriendly computations cheaper than if they were implemented in Noir.

:::warning

It is likely that not all backends will support a particular black box function.

:::

Because it is not guaranteed that all backends will support black box functions, it is possible that certain Noir programs won't compile against a particular backend if they use an unsupported black box function. It is possible to fallback to less efficient implementations written in Noir/ACIR in some cases.

Black box functions are specified with the `#[foreign(black_box_fn)]` attribute. For example, the SHA256 function in the Noir [source code](https://github.com/noir-lang/noir/blob/v0.5.1/noir_stdlib/src/hash.nr) looks like:

```rust
#[foreign(sha256)]
fn sha256<N>(_input : [u8; N]) -> [u8; 32] {}
```
The ACVM spec defines a set of blackbox functions which backends will be expected to implement. This allows backends to use optimized implementations of these constraints if they have them, however they may also fallback to less efficient naive implementations if not.

## Function list

Here is a list of the current black box functions that are supported by UltraPlonk:
Here is a list of the current black box functions:

- AES
- [SHA256](./cryptographic_primitives/hashes#sha256)
- [Schnorr signature verification](./cryptographic_primitives/schnorr)
- [Blake2s](./cryptographic_primitives/hashes#blake2s)
- [Blake3](./cryptographic_primitives/hashes#blake3)
- [Pedersen Hash](./cryptographic_primitives/hashes#pedersen_hash)
- [Pedersen Commitment](./cryptographic_primitives/hashes#pedersen_commitment)
- [ECDSA signature verification](./cryptographic_primitives/ecdsa_sig_verification)
- [Fixed base scalar multiplication](./cryptographic_primitives/scalar)
- [Compute merkle root](./merkle_trees#compute_merkle_root)
- AND
- XOR
- RANGE
- [Keccak256](./cryptographic_primitives/hashes#keccak256)
- [Recursive proof verification](./recursion)

Most black box functions are included as part of the Noir standard library, however `AND`, `XOR` and `RANGE` are used as part of the Noir language syntax. For instance, using the bitwise operator `&` will invoke the `AND` black box function. To ensure compatibility across backends, the ACVM has fallback implementations of `AND`, `XOR` and `RANGE` defined in its standard library which it can seamlessly fallback to if the backend doesn't support them.
Most black box functions are included as part of the Noir standard library, however `AND`, `XOR` and `RANGE` are used as part of the Noir language syntax. For instance, using the bitwise operator `&` will invoke the `AND` black box function.

You can view the black box functions defined in the ACVM code [here](https://github.com/noir-lang/noir/blob/master/acvm-repo/acir/src/circuit/black_box_functions.rs).
46 changes: 46 additions & 0 deletions docs/versioned_docs/version-v0.23.0/noir/standard_library/bn254.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: Bn254 Field Library
---

Noir provides a module in standard library with some optimized functions for bn254 Fr in `std::field::bn254`.

## decompose

```rust
fn decompose(x: Field) -> (Field, Field) {}
```

Decomposes a single field into two fields, low and high. The low field contains the lower 16 bytes of the input field and the high field contains the upper 16 bytes of the input field. Both field results are range checked to 128 bits.


## assert_gt

```rust
fn assert_gt(a: Field, b: Field) {}
```

Asserts that a > b. This will generate less constraints than using `assert(gt(a, b))`.

## assert_lt

```rust
fn assert_lt(a: Field, b: Field) {}
```

Asserts that a < b. This will generate less constraints than using `assert(lt(a, b))`.

## gt

```rust
fn gt(a: Field, b: Field) -> bool {}
```

Returns true if a > b.

## lt

```rust
fn lt(a: Field, b: Field) -> bool {}
```

Returns true if a < b.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ Noir supports ECDSA signatures verification over the secp256k1 and secp256r1 cur

Verifier for ECDSA Secp256k1 signatures

```rust
fn verify_signature(_public_key_x : [u8; 32], _public_key_y : [u8; 32], _signature: [u8; 64], _message: [u8]) -> bool
```rust title="ecdsa_secp256k1" showLineNumbers
pub fn verify_signature<N>(
public_key_x: [u8; 32],
public_key_y: [u8; 32],
signature: [u8; 64],
message_hash: [u8; N]
) -> bool
```
> <sup><sub><a href="https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/ecdsa_secp256k1.nr#L2-L9" target="_blank" rel="noopener noreferrer">Source code: noir_stdlib/src/ecdsa_secp256k1.nr#L2-L9</a></sub></sup>

example:

Expand All @@ -30,9 +37,16 @@ fn main(hashed_message : [u8;32], pub_key_x : [u8;32], pub_key_y : [u8;32], sign

Verifier for ECDSA Secp256r1 signatures

```rust
fn verify_signature(_public_key_x : [u8; 32], _public_key_y : [u8; 32], _signature: [u8; 64], _message: [u8]) -> bool
```rust title="ecdsa_secp256r1" showLineNumbers
pub fn verify_signature<N>(
public_key_x: [u8; 32],
public_key_y: [u8; 32],
signature: [u8; 64],
message_hash: [u8; N]
) -> bool
```
> <sup><sub><a href="https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/ecdsa_secp256r1.nr#L2-L9" target="_blank" rel="noopener noreferrer">Source code: noir_stdlib/src/ecdsa_secp256r1.nr#L2-L9</a></sub></sup>

example:

Expand Down
Loading

0 comments on commit a44fcf3

Please sign in to comment.