Skip to content

Commit

Permalink
chore: integrate code snippets script into docs (#4164)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR sets up code snippets in the Noir docs.

## 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.
  • Loading branch information
TomAFrench authored Jan 25, 2024
1 parent 3f5bad3 commit ed14628
Show file tree
Hide file tree
Showing 18 changed files with 558 additions and 91 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/docs-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup toolchain
uses: dtolnay/rust-toolchain@1.71.1

- uses: Swatinem/rust-cache@v2
with:
key: x86_64-unknown-linux-gnu
cache-on-failure: false
save-if: false

- name: Install Yarn dependencies
uses: ./.github/actions/setup

Expand Down
2 changes: 2 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# Production
/build
processed-docs
processed-docs-cache

# Generated files
.docusaurus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,10 @@ fn pedersen_hash(_input : [Field]) -> Field

example:

```rust
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let hash = std::hash::pedersen_hash(x);
}
```
#include_code pedersen-hash test_programs/execution_success/pedersen_hash/src/main.nr rust

<BlackBoxInfo />

<BlackBoxInfo />

## pedersen_commitment

Expand All @@ -79,12 +73,7 @@ fn pedersen_commitment(_input : [Field]) -> [Field; 2]

example:

```rust
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let commitment = std::hash::pedersen_commitment(x);
}
```
#include_code pedersen-commitment test_programs/execution_success/pedersen_commitment/src/main.nr rust

<BlackBoxInfo />

Expand All @@ -100,13 +89,7 @@ fn keccak256<N>(_input : [u8; N], _message_size: u32) -> [u8; 32]

example:

```rust
fn main() {
let x = [163, 117, 178, 149]; // some random bytes
let message_size = 4;
let hash = std::hash::keccak256(x, message_size);
}
```
#include_code keccak256 test_programs/execution_success/keccak256/src/main.nr rust

<BlackBoxInfo />

Expand All @@ -122,13 +105,7 @@ fn hash_1(input: [Field; 1]) -> Field

example:

```rust
fn main()
{
let hash_2 = std::hash::poseidon::bn254::hash_2([1, 2]);
assert(hash2 == 0x115cc0f5e7d690413df64c6b9662e9cf2a3617f2743245519e19607a4417189a);
}
```
#include_code poseidon test_programs/execution_success/poseidon_bn254_hash/src/main.nr rust

## mimc_bn254 and mimc

Expand Down
69 changes: 14 additions & 55 deletions docs/docs/noir/standard_library/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ types such as arrays are filled with default values of their element type.

### `std::cmp::Eq`

```rust
trait Eq {
fn eq(self, other: Self) -> bool;
}
```
#include_code eq-trait noir_stdlib/src/cmp.nr rust

Returns `true` if `self` is equal to `other`. Implementing this trait on a type
allows the type to be used with `==` and `!=`.

Expand Down Expand Up @@ -97,13 +94,9 @@ impl<A, B, C, D, E> Eq for (A, B, C, D, E)
where A: Eq, B: Eq, C: Eq, D: Eq, E: Eq { .. }
```

### `std::cmp::Cmp`
### `std::cmp::Ord`

```rust
trait Cmp {
fn cmp(self, other: Self) -> Ordering;
}
```
#include_code ord-trait noir_stdlib/src/cmp.nr rust

`a.cmp(b)` compares two values returning `Ordering::less()` if `a < b`,
`Ordering::equal()` if `a == b`, or `Ordering::greater()` if `a > b`.
Expand Down Expand Up @@ -151,23 +144,10 @@ These traits abstract over addition, subtraction, multiplication, and division r
Implementing these traits for a given type will also allow that type to be used with the corresponding operator
for that trait (`+` for Add, etc) in addition to the normal method names.

```rust
trait Add {
fn add(self, other: Self) -> Self;
}

trait Sub {
fn sub(self, other: Self) -> Self;
}

trait Mul {
fn mul(self, other: Self) -> Self;
}

trait Div {
fn div(self, other: Self) -> Self;
}
```
#include_code add-trait noir_stdlib/src/ops.nr rust
#include_code sub-trait noir_stdlib/src/ops.nr rust
#include_code mul-trait noir_stdlib/src/ops.nr rust
#include_code div-trait noir_stdlib/src/ops.nr rust

The implementations block below is given for the `Add` trait, but the same types that implement
`Add` also implement `Sub`, `Mul`, and `Div`.
Expand All @@ -189,11 +169,7 @@ impl Add for u64 { .. }

### `std::ops::Rem`

```rust
trait Rem {
fn rem(self, other: Self) -> Self;
}
```
#include_code rem-trait noir_stdlib/src/ops.nr rust

`Rem::rem(a, b)` is the remainder function returning the result of what is
left after dividing `a` and `b`. Implementing `Rem` allows the `%` operator
Expand All @@ -216,19 +192,9 @@ impl Rem for i64 { fn rem(self, other: i64) -> i64 { self % other } }

### `std::ops::{ BitOr, BitAnd, BitXor }`

```rust
trait BitOr {
fn bitor(self, other: Self) -> Self;
}

trait BitAnd {
fn bitand(self, other: Self) -> Self;
}

trait BitXor {
fn bitxor(self, other: Self) -> Self;
}
```
#include_code bitor-trait noir_stdlib/src/ops.nr rust
#include_code bitand-trait noir_stdlib/src/ops.nr rust
#include_code bitxor-trait noir_stdlib/src/ops.nr rust

Traits for the bitwise operations `|`, `&`, and `^`.

Expand All @@ -255,15 +221,8 @@ impl BitOr for i64 { fn bitor(self, other: i64) -> i64 { self | other } }

### `std::ops::{ Shl, Shr }`

```rust
trait Shl {
fn shl(self, other: Self) -> Self;
}

trait Shr {
fn shr(self, other: Self) -> Self;
}
```
#include_code shl-trait noir_stdlib/src/ops.nr rust
#include_code shr-trait noir_stdlib/src/ops.nr rust

Traits for a bit shift left and bit shift right.

Expand Down
1 change: 1 addition & 0 deletions docs/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default {
'@docusaurus/preset-classic',
{
docs: {
path: "processed-docs",
sidebarPath: './sidebars.js',
routeBasePath: '/docs',
remarkPlugins: [math],
Expand Down
5 changes: 3 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "0.0.0",
"private": true,
"scripts": {
"start": "docusaurus start",
"build": "yarn version::stables && docusaurus build",
"preprocess": "yarn node ./scripts/preprocess/index.js",
"start": "yarn preprocess && docusaurus start",
"build": "yarn preprocess && yarn version::stables && docusaurus build",
"version::stables": "ts-node ./scripts/setStable.ts",
"serve": "serve build"
},
Expand Down
Loading

0 comments on commit ed14628

Please sign in to comment.