From daf212592e0a3c22fcf83101e9a6154415ef636c Mon Sep 17 00:00:00 2001 From: Cat McGee Date: Thu, 29 Feb 2024 12:45:07 +0900 Subject: [PATCH 1/6] initial doc --- .../getting_started/tooling/noir_codegen.md | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 docs/docs/getting_started/tooling/noir_codegen.md diff --git a/docs/docs/getting_started/tooling/noir_codegen.md b/docs/docs/getting_started/tooling/noir_codegen.md new file mode 100644 index 00000000000..7ba8bf0ae12 --- /dev/null +++ b/docs/docs/getting_started/tooling/noir_codegen.md @@ -0,0 +1,72 @@ +--- +title: Noir Codegen for Typescript +description: Learn how to use Noir codegen to generate Typescript interfaces +keywords: [Nargo, Noir, compile, TypeScript] +sidebar_position: 2 +--- + +You can generate TypeScript bindings with `noir_codegen`, allowing for easier and faster integration of your Noir circuits into TypeScript projects. + +It is used in conjuction with `nargo export` which generates JSON for specified functions. + +## Installation + +You can install `noir_codegen` into your project using `yarn` or `npm`: + +```bash +yarn add @noir-lang/noir_codegen +``` + +or + +```bash +npm install @noir-lang/noir_codegen +``` +## Usage + +Before generating TypeScript bindings, you need to export your Noir programs using `nargo export`: + +```bash +nargo export +``` + +You can run this in the directory where your `Nargo.toml` lives, or you can specify a path like so: + +```bash +nargo export --program-dir=./path/to/your/noir/program +``` + +Now you can run `noir-codegen`: + + + +## Example + +For example, if you have a Noir program like this: + +```rust +#[export] +fn exported_function_foo(x: u64, y: u64, array: [u8; 5], my_struct: NestedStruct, string: str<5>) -> (u64, u64, MyStruct) { + assert(array.len() == 5); + assert(my_struct.foo.foo); + assert(string == "12345"); + + print(x); + assert(x < y); + (x + y, 3, my_struct.foo) +} + +fn unexported_function(x: u64, y: u64) { + assert(x != y); +} +``` + +You can run `nargo export` in the directory with your `Nargo.toml` and + + +### Example Usage + +Here's an example command that demonstrates how to use `nargo export` with these arguments: + +```sh +nargo export --out-dir ./path/to/output '**/*.json' From ccfd83016c2c8c3a4a5d5fee75f5e57a5c4a906d Mon Sep 17 00:00:00 2001 From: Cat McGee Date: Mon, 18 Mar 2024 15:28:10 +0000 Subject: [PATCH 2/6] updated --- .../getting_started/tooling/noir_codegen.md | 72 ++++++++++++++++--- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/docs/docs/getting_started/tooling/noir_codegen.md b/docs/docs/getting_started/tooling/noir_codegen.md index 7ba8bf0ae12..f42951863e5 100644 --- a/docs/docs/getting_started/tooling/noir_codegen.md +++ b/docs/docs/getting_started/tooling/noir_codegen.md @@ -1,13 +1,13 @@ --- title: Noir Codegen for Typescript -description: Learn how to use Noir codegen to generate Typescript interfaces +description: Learn how to use Noir codegen to generate Typescript bindings keywords: [Nargo, Noir, compile, TypeScript] sidebar_position: 2 --- You can generate TypeScript bindings with `noir_codegen`, allowing for easier and faster integration of your Noir circuits into TypeScript projects. -It is used in conjuction with `nargo export` which generates JSON for specified functions. +It is used in conjunction with `nargo export` which generates JSON for specified functions. ## Installation @@ -24,6 +24,8 @@ npm install @noir-lang/noir_codegen ``` ## Usage +### Export circuit ABI + Before generating TypeScript bindings, you need to export your Noir programs using `nargo export`: ```bash @@ -36,15 +38,37 @@ You can run this in the directory where your `Nargo.toml` lives, or you can spec nargo export --program-dir=./path/to/your/noir/program ``` -Now you can run `noir-codegen`: +### Run noir-codegen + +To run `noir-codegen`, pass the path of your exported JSON. Here is an example of that that might look like: + +```bash +yarn noir-codegen ./export/main.json +``` + +You can optionally specify an output dir for your Typescript bindings to live with `--out-dir`: + +```bash +yarn noir-codegen ./export/main.json --out-dir ./path/to/output/ir +``` - ## Example For example, if you have a Noir program like this: ```rust +struct MyStruct { + foo: bool, + bar: [str<5>; 3], + baz: Field +} + +struct NestedStruct { + foo: MyStruct, + bar: [MyStruct; 3], + baz: u64 +} #[export] fn exported_function_foo(x: u64, y: u64, array: [u8; 5], my_struct: NestedStruct, string: str<5>) -> (u64, u64, MyStruct) { assert(array.len() == 5); @@ -61,12 +85,42 @@ fn unexported_function(x: u64, y: u64) { } ``` -You can run `nargo export` in the directory with your `Nargo.toml` and +After following the usage instructions, you will have an `index.ts` that looks like this: + +```typescript +/* Autogenerated file, do not edit! */ + +/* eslint-disable */ + +import { Noir, InputMap, CompiledCircuit, ForeignCallHandler } from "@noir-lang/noir_js" + +export { ForeignCallHandler } from "@noir-lang/noir_js" +export type u64 = string; +export type u8 = string | number; +export type Field = string; -### Example Usage +export type MyStruct = { + foo: boolean; + bar: string[]; + baz: Field; +}; -Here's an example command that demonstrates how to use `nargo export` with these arguments: +export type NestedStruct = { + foo: MyStruct; + bar: MyStruct[]; + baz: u64; +}; + + +export const exported_function_foo_circuit: CompiledCircuit = {"abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":64},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":64},"visibility":"private"},{"name":"array","type":{"kind":"array","length":5,"type":{"kind":"integer","sign":"unsigned","width":8}},"visibility":"private"},{"name":"my_struct","type":{"kind":"struct","path":"NestedStruct","fields":[{"name":"foo","type":{"kind":"struct","path":"MyStruct","fields":[{"name":"foo","type":{"kind":"boolean"}},{"name":"bar","type":{"kind":"array","length":3,"type":{"kind":"string","length":5}}},{"name":"baz","type":{"kind":"field"}}]}},{"name":"bar","type":{"kind":"array","length":3,"type":{"kind":"struct","path":"MyStruct","fields":[{"name":"foo","type":{"kind":"boolean"}},{"name":"bar","type":{"kind":"array","length":3,"type":{"kind":"string","length":5}}},{"name":"baz","type":{"kind":"field"}}]}}},{"name":"baz","type":{"kind":"integer","sign":"unsigned","width":64}}]},"visibility":"private"},{"name":"string","type":{"kind":"string","length":5},"visibility":"private"}],"param_witnesses":{"array":[{"start":2,"end":7}],"my_struct":[{"start":7,"end":76}],"string":[{"start":76,"end":81}],"x":[{"start":0,"end":1}],"y":[{"start":1,"end":2}]},"return_type":{"abi_type":{"kind":"tuple","fields":[{"kind":"integer","sign":"unsigned","width":64},{"kind":"integer","sign":"unsigned","width":64},{"kind":"struct","path":"MyStruct","fields":[{"name":"foo","type":{"kind":"boolean"}},{"name":"bar","type":{"kind":"array","length":3,"type":{"kind":"string","length":5}}},{"name":"baz","type":{"kind":"field"}}]}]},"visibility":"private"},"return_witnesses":[83,84,85,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]},"bytecode":"H4sIAAAAAAAA/9Wbe3PbRBTFV3Gabiw7cZynQwFT2vIsSJZt2byahlLKo9CkhP+T2B46A6TT8dBPxvcjVw+4OV0sdXy1I++MR3skZ+9PR2tptdl7opT6VcXFufwsJfUDph3Q9NFMV0Avg74GegW0Br0KugraBV0DXQe9BnoddAP0Bugm6E3QW6C3Qe+A3kvqqW7B8X3Qb4C+AfpN0G+Bfht0G/Q7oG+Cfhf0LdC3Qd8B/R7oD+D8P4TjH4H+GPRd0J+A/hS0B9oH3QEdgO6C7oHugw5BD0B/Buf/ORz/AvSXoL8CfQ/0Aej7oA9Bfw36AehvQD8E/S3oR6C/A/1DwsiLk2zT/d58xb/O2gq8frc7DjtjP/BPvc7wbNDzur2z/sAf+L1Bb9QZBMF40B2Ew7Nh6A39bjD2J71hMEkaK5LzRwHO8YTKqFDOx2Kc55MiOX+S4xwXyfmzHOeoSM4ncpznFcZXJLNgW//+pv5Otppxt6XidD2fxkTRwMng0RI7lm4prpseY/vpvreS7KJxjbN8tS3irzDfDyT4L4tmcdti7XZ6Ljt1xbgVXIe0rLDzvCbO4/nFnGfs33XGLtNueJa2l5Zl8KnOjvP4ugDvHBY3bZtfL4wt50OHhnjRe8UsH1YNLKuWfVg1xJbzoR/1h2qGD1UDS9WyD1VDbEEfhtSGm+GDa2BxLfvgGmIL+hCNcWoZPtQMLDXLPtQMsQV96KbnOsuHuoGlbtkHzpiXV5eAV0NdJnZwSm2sZfiwZmBZs+wDZ8zLq0vAq6EuEzukKZRoHm6WD+sGlnXLPnDGvLxuCXg11GVihzQ1Fr1XzPKhYWBpWPaBM+blrZaAV0NdJnafpiSjee1ZPmwYWDYs+8AZ8/K6JeDVUJeJ3Y/uk80MH5oGlqZlHzhjXt7agvFWF4zXLQGvhrpM7DAar29m+LBpYNm07ANnzMu7sWC8jQXjLYO/GuoyscNonmsrw4ctA8uWZR84Y15eXQJeDXWZ2J1zamM7w4dtA8u2ZR84Y15eXQJeDXWZ2GE0rt7J8GHHwLJj2QfOmJe3umC8tQXjbZaAV0NdJnaflryo3Qwfdg0su5Z94Ix5efWC8dZLwKuhLhM7oKVX0Xq6WT7sGVj2LPuwZ4gt6EP0PtTK8KFlYGlZ9qFliC34PI7Wwuxn+LBvYNm37EMaj56x6TqF5y+e/TlVUPjihwqrpws/HPad24bvmhZPOEzzv1lVhgU6B8nWm6/4FQatDCciFYe3Ne/KvzlWyPi4w3DqYm1zX4+S7bH6r7etGPxOi8P2UWc5hGPp1mF/f6iudkD8jvM/7bgGjoZhX2GdMD3ZY/XqEtRl4Vi8LcmO+JptedCWfyTIdayK+YE4cC3m5TySu65XOCsF9hlvvuLP0Wde8e+poH/pb5C3WdR1/0WOOyiS80S4Dx0B61LSV+keR8MNeiDQcr90mRkNQ+jmTK/Q6RIE+vct3ZxpupReVWm6kKaeaBqDXtloOE1DSRpG0XCG0lQoNYXSUSgFhdJO2ipOL7mp4jSSWyoecdxRcVrI+ypOB6EUEEr7oFSPuypO6aA0Dvqx0QnRvCZdABrn0v/EaNxPcy/0nklruSilgtIoKHWC0iUoReJe4ul9FT+MKM3hgYrTGR6qOG3hkYrTE75XcVoCLcd/fPmhZeS0RPsJ85FPl87jGfV76pcn6uqD7mmyvZFsT6fT8R/Pp+3pRft0NGq/fDb9rX3x1/jF5PeLl+ofXhoRQLg1AAA="}; + +export async function exported_function_foo(x: u64, y: u64, array: u8[], my_struct: NestedStruct, string: string, foreignCallHandler?: ForeignCallHandler): Promise<[u64, u64, MyStruct]> { + const program = new Noir(exported_function_foo_circuit); + const args: InputMap = { x, y, array, my_struct, string }; + const { returnValue } = await program.execute(args, foreignCallHandler); + return returnValue as [u64, u64, MyStruct]; +} +``` -```sh -nargo export --out-dir ./path/to/output '**/*.json' +As you can see, `exported_fcuntion_foo()` is now available to simulate in Typescript, and `unexported_function()` is not. \ No newline at end of file From d61cdf2d088684162a3e5ca7d319ad4372289ad9 Mon Sep 17 00:00:00 2001 From: James Zaki Date: Wed, 27 Mar 2024 16:41:20 +0000 Subject: [PATCH 3/6] Flesh out some details in noir_codegen.md --- .../getting_started/tooling/noir_codegen.md | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/docs/docs/getting_started/tooling/noir_codegen.md b/docs/docs/getting_started/tooling/noir_codegen.md index f42951863e5..a9d1f5c1a33 100644 --- a/docs/docs/getting_started/tooling/noir_codegen.md +++ b/docs/docs/getting_started/tooling/noir_codegen.md @@ -7,7 +7,7 @@ sidebar_position: 2 You can generate TypeScript bindings with `noir_codegen`, allowing for easier and faster integration of your Noir circuits into TypeScript projects. -It is used in conjunction with `nargo export` which generates JSON for specified functions. +It is used in conjunction with `nargo export` which generates JSON for specified functions in a Noir library (not binary or contract circuits). ## Installation @@ -24,15 +24,22 @@ npm install @noir-lang/noir_codegen ``` ## Usage -### Export circuit ABI +### Export ABI of specified functions -Before generating TypeScript bindings, you need to export your Noir programs using `nargo export`: +Use the `#[export]` macro to indicate which functions in the noir library that you'd like to export. +```rust +#[export] +fn your_function(... +``` + +The following command, when run from the directory with `Nargo.toml`, will create a folder with a .json file per exported function: ```bash nargo export ``` +The .json files live in a new `exports` directory. -You can run this in the directory where your `Nargo.toml` lives, or you can specify a path like so: +You can also specify the directory of Noir programs using `--program-dir` as follows: ```bash nargo export --program-dir=./path/to/your/noir/program @@ -40,16 +47,18 @@ nargo export --program-dir=./path/to/your/noir/program ### Run noir-codegen -To run `noir-codegen`, pass the path of your exported JSON. Here is an example of that that might look like: +The `noir-codegen` command, pass the path of your exported JSON file(s) (wildcard characters accepted too, `*.json`). : ```bash -yarn noir-codegen ./export/main.json +yarn noir-codegen ./export/your_function.json ``` +An `exports` directory is created with an index.ts file containing all exported functions. + You can optionally specify an output dir for your Typescript bindings to live with `--out-dir`: ```bash -yarn noir-codegen ./export/main.json --out-dir ./path/to/output/ir +yarn noir-codegen ./export/*.json --out-dir ./path/to/output/dir ``` @@ -123,4 +132,4 @@ export async function exported_function_foo(x: u64, y: u64, array: u8[], my_stru } ``` -As you can see, `exported_fcuntion_foo()` is now available to simulate in Typescript, and `unexported_function()` is not. \ No newline at end of file +As you can see, `exported_function_foo()` is now readily available to us in Typescript, and `unexported_function()` is not. From 7398f6f3c87356e2eecca7b3fb9dcef6a54718bb Mon Sep 17 00:00:00 2001 From: James Zaki Date: Thu, 28 Mar 2024 12:20:03 +0000 Subject: [PATCH 4/6] Clarify sentence noir_codegen.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Pedro Sousa --- docs/docs/getting_started/tooling/noir_codegen.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/docs/getting_started/tooling/noir_codegen.md b/docs/docs/getting_started/tooling/noir_codegen.md index a9d1f5c1a33..de91533c5ca 100644 --- a/docs/docs/getting_started/tooling/noir_codegen.md +++ b/docs/docs/getting_started/tooling/noir_codegen.md @@ -47,7 +47,9 @@ nargo export --program-dir=./path/to/your/noir/program ### Run noir-codegen -The `noir-codegen` command, pass the path of your exported JSON file(s) (wildcard characters accepted too, `*.json`). : +You should now have a folder "exports" with your compiled function. To turn it into a TS binding, we will use the `noir-codegen` package we installed. + +If you installed it globally, just pass the path of your exported JSON file(s) (you can use wildcards too, ex. `*.json`). : ```bash yarn noir-codegen ./export/your_function.json From 3147d67a94e2ea0ac9d0b39cafdab68bb9b71166 Mon Sep 17 00:00:00 2001 From: James Zaki Date: Thu, 28 Mar 2024 17:35:00 +0000 Subject: [PATCH 5/6] Refine codegen doc --- .../getting_started/tooling/noir_codegen.md | 120 +++++++----------- 1 file changed, 48 insertions(+), 72 deletions(-) diff --git a/docs/docs/getting_started/tooling/noir_codegen.md b/docs/docs/getting_started/tooling/noir_codegen.md index de91533c5ca..ef933f38105 100644 --- a/docs/docs/getting_started/tooling/noir_codegen.md +++ b/docs/docs/getting_started/tooling/noir_codegen.md @@ -5,133 +5,109 @@ keywords: [Nargo, Noir, compile, TypeScript] sidebar_position: 2 --- -You can generate TypeScript bindings with `noir_codegen`, allowing for easier and faster integration of your Noir circuits into TypeScript projects. +When using typescript, it is extra work to interpret Noir program outputs in a type-safe way. Third party libraries may exist for popular Noir programs, but they are either hard to find or unmaintained. -It is used in conjunction with `nargo export` which generates JSON for specified functions in a Noir library (not binary or contract circuits). +Now you can generate typescript bindings for your Noir programs in two steps: +1. Exporting Noir functions using `nargo export` +2. Using the typescript module `noir_codegen` to generate typescript binding + +**Note:** you can only export functions from a Noir *library* (not binary or contract program types). ## Installation -You can install `noir_codegen` into your project using `yarn` or `npm`: +### Your TypeScript project + +If you don't already have a TypeScript project you can add the module with `yarn` (or `npm`), then initialize it: + +```bash +yarn add typescript -D +npx tsc --init +``` + +### Add TypeScript module - `noir_codegen` + +The following command will add the module to your project's devDependencies: ```bash -yarn add @noir-lang/noir_codegen +yarn add @noir-lang/noir_codegen -D ``` -or +### Nargo library +Make sure you have Nargo, v0.25.0 or greater, installed. If you don't, follow the [installation guide](../installation/index.md). + +If you're in a new project, make a `circuits` folder and create a new Noir library: ```bash -npm install @noir-lang/noir_codegen +mkdir circuits && cd circuits +nargo new --lib myNoirLib ``` + ## Usage ### Export ABI of specified functions -Use the `#[export]` macro to indicate which functions in the noir library that you'd like to export. +First go to the `.nr` files in your Noir library, and add the `#[export]` macro to each function that you want to use in TypeScript. + ```rust #[export] fn your_function(... ``` -The following command, when run from the directory with `Nargo.toml`, will create a folder with a .json file per exported function: +From your Noir library (where `Nargo.toml` is), run the following command: ```bash nargo export ``` -The .json files live in a new `exports` directory. -You can also specify the directory of Noir programs using `--program-dir` as follows: +You will now have an `export` directory with a .json file per exported function. + +You can also specify the directory of Noir programs using `--program-dir`, for example: ```bash -nargo export --program-dir=./path/to/your/noir/program +nargo export --program-dir=./circuits/myNoirLib ``` -### Run noir-codegen - -You should now have a folder "exports" with your compiled function. To turn it into a TS binding, we will use the `noir-codegen` package we installed. +### Generate TypeScript bindings from exported functions -If you installed it globally, just pass the path of your exported JSON file(s) (you can use wildcards too, ex. `*.json`). : +To use the `noir-codegen` package we added to the TypeScript project: ```bash yarn noir-codegen ./export/your_function.json ``` -An `exports` directory is created with an index.ts file containing all exported functions. +This creates an `exports` directory with an `index.ts` file containing all exported functions. -You can optionally specify an output dir for your Typescript bindings to live with `--out-dir`: +**Note:** adding `--out-dir` allows you to specify an output dir for your Typescript bindings to go. Eg: ```bash yarn noir-codegen ./export/*.json --out-dir ./path/to/output/dir ``` +## Example .nr function to .ts output -## Example - -For example, if you have a Noir program like this: +Consider a Noir library with this function: ```rust -struct MyStruct { - foo: bool, - bar: [str<5>; 3], - baz: Field -} - -struct NestedStruct { - foo: MyStruct, - bar: [MyStruct; 3], - baz: u64 -} #[export] -fn exported_function_foo(x: u64, y: u64, array: [u8; 5], my_struct: NestedStruct, string: str<5>) -> (u64, u64, MyStruct) { - assert(array.len() == 5); - assert(my_struct.foo.foo); - assert(string == "12345"); - - print(x); - assert(x < y); - (x + y, 3, my_struct.foo) -} - -fn unexported_function(x: u64, y: u64) { - assert(x != y); +fn not_equal(x: Field, y: Field) -> bool { + x != y } ``` -After following the usage instructions, you will have an `index.ts` that looks like this: +After the export and codegen steps, you should have an `index.ts` like: ```typescript -/* Autogenerated file, do not edit! */ - -/* eslint-disable */ - -import { Noir, InputMap, CompiledCircuit, ForeignCallHandler } from "@noir-lang/noir_js" - -export { ForeignCallHandler } from "@noir-lang/noir_js" - -export type u64 = string; -export type u8 = string | number; export type Field = string; -export type MyStruct = { - foo: boolean; - bar: string[]; - baz: Field; -}; - -export type NestedStruct = { - foo: MyStruct; - bar: MyStruct[]; - baz: u64; -}; - -export const exported_function_foo_circuit: CompiledCircuit = {"abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":64},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":64},"visibility":"private"},{"name":"array","type":{"kind":"array","length":5,"type":{"kind":"integer","sign":"unsigned","width":8}},"visibility":"private"},{"name":"my_struct","type":{"kind":"struct","path":"NestedStruct","fields":[{"name":"foo","type":{"kind":"struct","path":"MyStruct","fields":[{"name":"foo","type":{"kind":"boolean"}},{"name":"bar","type":{"kind":"array","length":3,"type":{"kind":"string","length":5}}},{"name":"baz","type":{"kind":"field"}}]}},{"name":"bar","type":{"kind":"array","length":3,"type":{"kind":"struct","path":"MyStruct","fields":[{"name":"foo","type":{"kind":"boolean"}},{"name":"bar","type":{"kind":"array","length":3,"type":{"kind":"string","length":5}}},{"name":"baz","type":{"kind":"field"}}]}}},{"name":"baz","type":{"kind":"integer","sign":"unsigned","width":64}}]},"visibility":"private"},{"name":"string","type":{"kind":"string","length":5},"visibility":"private"}],"param_witnesses":{"array":[{"start":2,"end":7}],"my_struct":[{"start":7,"end":76}],"string":[{"start":76,"end":81}],"x":[{"start":0,"end":1}],"y":[{"start":1,"end":2}]},"return_type":{"abi_type":{"kind":"tuple","fields":[{"kind":"integer","sign":"unsigned","width":64},{"kind":"integer","sign":"unsigned","width":64},{"kind":"struct","path":"MyStruct","fields":[{"name":"foo","type":{"kind":"boolean"}},{"name":"bar","type":{"kind":"array","length":3,"type":{"kind":"string","length":5}}},{"name":"baz","type":{"kind":"field"}}]}]},"visibility":"private"},"return_witnesses":[83,84,85,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]},"bytecode":"H4sIAAAAAAAA/9Wbe3PbRBTFV3Gabiw7cZynQwFT2vIsSJZt2byahlLKo9CkhP+T2B46A6TT8dBPxvcjVw+4OV0sdXy1I++MR3skZ+9PR2tptdl7opT6VcXFufwsJfUDph3Q9NFMV0Avg74GegW0Br0KugraBV0DXQe9BnoddAP0Bugm6E3QW6C3Qe+A3kvqqW7B8X3Qb4C+AfpN0G+Bfht0G/Q7oG+Cfhf0LdC3Qd8B/R7oD+D8P4TjH4H+GPRd0J+A/hS0B9oH3QEdgO6C7oHugw5BD0B/Buf/ORz/AvSXoL8CfQ/0Aej7oA9Bfw36AehvQD8E/S3oR6C/A/1DwsiLk2zT/d58xb/O2gq8frc7DjtjP/BPvc7wbNDzur2z/sAf+L1Bb9QZBMF40B2Ew7Nh6A39bjD2J71hMEkaK5LzRwHO8YTKqFDOx2Kc55MiOX+S4xwXyfmzHOeoSM4ncpznFcZXJLNgW//+pv5Otppxt6XidD2fxkTRwMng0RI7lm4prpseY/vpvreS7KJxjbN8tS3irzDfDyT4L4tmcdti7XZ6Ljt1xbgVXIe0rLDzvCbO4/nFnGfs33XGLtNueJa2l5Zl8KnOjvP4ugDvHBY3bZtfL4wt50OHhnjRe8UsH1YNLKuWfVg1xJbzoR/1h2qGD1UDS9WyD1VDbEEfhtSGm+GDa2BxLfvgGmIL+hCNcWoZPtQMLDXLPtQMsQV96KbnOsuHuoGlbtkHzpiXV5eAV0NdJnZwSm2sZfiwZmBZs+wDZ8zLq0vAq6EuEzukKZRoHm6WD+sGlnXLPnDGvLxuCXg11GVihzQ1Fr1XzPKhYWBpWPaBM+blrZaAV0NdJnafpiSjee1ZPmwYWDYs+8AZ8/K6JeDVUJeJ3Y/uk80MH5oGlqZlHzhjXt7agvFWF4zXLQGvhrpM7DAar29m+LBpYNm07ANnzMu7sWC8jQXjLYO/GuoyscNonmsrw4ctA8uWZR84Y15eXQJeDXWZ2J1zamM7w4dtA8u2ZR84Y15eXQJeDXWZ2GE0rt7J8GHHwLJj2QfOmJe3umC8tQXjbZaAV0NdJnaflryo3Qwfdg0su5Z94Ix5efWC8dZLwKuhLhM7oKVX0Xq6WT7sGVj2LPuwZ4gt6EP0PtTK8KFlYGlZ9qFliC34PI7Wwuxn+LBvYNm37EMaj56x6TqF5y+e/TlVUPjihwqrpws/HPad24bvmhZPOEzzv1lVhgU6B8nWm6/4FQatDCciFYe3Ne/KvzlWyPi4w3DqYm1zX4+S7bH6r7etGPxOi8P2UWc5hGPp1mF/f6iudkD8jvM/7bgGjoZhX2GdMD3ZY/XqEtRl4Vi8LcmO+JptedCWfyTIdayK+YE4cC3m5TySu65XOCsF9hlvvuLP0Wde8e+poH/pb5C3WdR1/0WOOyiS80S4Dx0B61LSV+keR8MNeiDQcr90mRkNQ+jmTK/Q6RIE+vct3ZxpupReVWm6kKaeaBqDXtloOE1DSRpG0XCG0lQoNYXSUSgFhdJO2ipOL7mp4jSSWyoecdxRcVrI+ypOB6EUEEr7oFSPuypO6aA0Dvqx0QnRvCZdABrn0v/EaNxPcy/0nklruSilgtIoKHWC0iUoReJe4ul9FT+MKM3hgYrTGR6qOG3hkYrTE75XcVoCLcd/fPmhZeS0RPsJ85FPl87jGfV76pcn6uqD7mmyvZFsT6fT8R/Pp+3pRft0NGq/fDb9rX3x1/jF5PeLl+ofXhoRQLg1AAA="}; +export const is_equal_circuit: CompiledCircuit = {"abi":{"parameters":[{"name":"x","type":{"kind":"field"},"visibility":"private"},{"name":"y","type":{"kind":"field"},"visibility":"private"}],"param_witnesses":{"x":[{"start":0,"end":1}],"y":[{"start":1,"end":2}]},"return_type":{"abi_type":{"kind":"boolean"},"visibility":"private"},"return_witnesses":[4]},"bytecode":"H4sIAAAAAAAA/7WUMQ7DIAxFQ0Krrr2JjSGYLVcpKrn/CaqqDQN12WK+hPBgmWd/wEyHbF1SS923uhOs3pfoChI+wKXMAXzIKyNj4PB0TFTYc0w5RUjoqeAeEu1wqK0F54RGkWvW44LPzExnlkbMEs4JNZmN8PxS42uHv82T8a3Jeyn2Ks+VLPcO558HmyLMCDOXAXXtpPt4R/Rt9T36ss6dS9HGPx/eG17nGegKBQAA"}; -export async function exported_function_foo(x: u64, y: u64, array: u8[], my_struct: NestedStruct, string: string, foreignCallHandler?: ForeignCallHandler): Promise<[u64, u64, MyStruct]> { - const program = new Noir(exported_function_foo_circuit); - const args: InputMap = { x, y, array, my_struct, string }; +export async function is_equal(x: Field, y: Field, foreignCallHandler?: ForeignCallHandler): Promise { + const program = new Noir(is_equal_circuit); + const args: InputMap = { x, y }; const { returnValue } = await program.execute(args, foreignCallHandler); - return returnValue as [u64, u64, MyStruct]; + return returnValue as boolean; } ``` -As you can see, `exported_function_foo()` is now readily available to us in Typescript, and `unexported_function()` is not. +Now the `is_equal()` function and relevant types are readily available for use in Typescript. From 8a0fd05c3b0719056ea389b2a927611f0699393f Mon Sep 17 00:00:00 2001 From: James Zaki Date: Thu, 28 Mar 2024 17:41:32 +0000 Subject: [PATCH 6/6] Chore camel case TypeScript --- docs/docs/getting_started/tooling/noir_codegen.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/docs/getting_started/tooling/noir_codegen.md b/docs/docs/getting_started/tooling/noir_codegen.md index ef933f38105..d65151da0ab 100644 --- a/docs/docs/getting_started/tooling/noir_codegen.md +++ b/docs/docs/getting_started/tooling/noir_codegen.md @@ -1,15 +1,15 @@ --- -title: Noir Codegen for Typescript -description: Learn how to use Noir codegen to generate Typescript bindings +title: Noir Codegen for TypeScript +description: Learn how to use Noir codegen to generate TypeScript bindings keywords: [Nargo, Noir, compile, TypeScript] sidebar_position: 2 --- -When using typescript, it is extra work to interpret Noir program outputs in a type-safe way. Third party libraries may exist for popular Noir programs, but they are either hard to find or unmaintained. +When using TypeScript, it is extra work to interpret Noir program outputs in a type-safe way. Third party libraries may exist for popular Noir programs, but they are either hard to find or unmaintained. -Now you can generate typescript bindings for your Noir programs in two steps: +Now you can generate TypeScript bindings for your Noir programs in two steps: 1. Exporting Noir functions using `nargo export` -2. Using the typescript module `noir_codegen` to generate typescript binding +2. Using the TypeScript module `noir_codegen` to generate TypeScript binding **Note:** you can only export functions from a Noir *library* (not binary or contract program types). @@ -77,7 +77,7 @@ yarn noir-codegen ./export/your_function.json This creates an `exports` directory with an `index.ts` file containing all exported functions. -**Note:** adding `--out-dir` allows you to specify an output dir for your Typescript bindings to go. Eg: +**Note:** adding `--out-dir` allows you to specify an output dir for your TypeScript bindings to go. Eg: ```bash yarn noir-codegen ./export/*.json --out-dir ./path/to/output/dir @@ -110,4 +110,4 @@ export async function is_equal(x: Field, y: Field, foreignCallHandler?: ForeignC } ``` -Now the `is_equal()` function and relevant types are readily available for use in Typescript. +Now the `is_equal()` function and relevant types are readily available for use in TypeScript.