Skip to content

Commit aecd7b0

Browse files
committed
Add base64 example for gate counts
1 parent f97b2f6 commit aecd7b0

File tree

10 files changed

+90
-0
lines changed

10 files changed

+90
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

lib_examples/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

lib_examples/Nargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = [
3+
"base64_example",
4+
]
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "base64_example"
3+
type = "bin"
4+
authors = [""]
5+
compiler_version = ">=0.36.0"
6+
7+
[dependencies]
8+
base64 = {tag = "v0.3.0", git = "https://github.com/noir-lang/noir_base64.git"}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
base64_encoded = ""
2+
input = ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
input = "The quick brown fox jumps over the lazy dog, while 42 ravens perch atop a rusty mailbox."
2+
base64_encoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZywgd2hpbGUgNDIgcmF2ZW5zIHBlcmNoIGF0b3AgYSBydXN0eSBtYWlsYm94Lg=="
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
input = "The quick br"
2+
base64_encoded = "VGhlIHF1aWNrIGJy"

lib_examples/base64_example/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Base64 example use and bb benchmarking
2+
3+
This makes use of [this](https://github.com/noir-lang/noir_base64.git) base64 library (see Nargo.toml for version)
4+
5+
The lib covers several encode/decode tests, whereas this program has some extra code to facilitate benchmarking the gate counts of a proving backend. Comparisons can be made against other proving backends if desired.
6+
7+
## Test setups for gate counts
8+
9+
Assuming you have `nargo` and a compatible proving backend (eg nargo v0.36.0 and barretenberg bb v0.58.0):
10+
11+
- Choose the desired number of encode/decode runs by setting the corresponding `global` variables in src/main.nr
12+
- Choose the input strings via the `comptime global` variables (the lengths are managed automatically at compile time)
13+
- To ensure the test values are expected lengths:
14+
- `nargo test`
15+
- Ensure these test values are in a corresponding Prover toml file eg (`Prover_SHORT.toml`)
16+
- Execute the program referring to the prover toml file for inputs:
17+
- `nargo execute -p Prover_SHORT.toml`
18+
- Note there's an optional param that you can use to specify the name of the witness file generated
19+
- Optional: `nargo execute -p Prover_SHORT.toml base64_example_SHORT.gz`
20+
- If using barretenberg, check the gate count referring to the newly created program:
21+
- `bb gates -b ../target/base64_example.json`
22+
- Note: you can similarly specify a non-default witness file to use with the program
23+
- Optional: `bb gates -b ../target/base64_example.json -w ../target/base64_example_SHORT`
24+
- Repeat this for different runs of encoding and decoding, of different length inputs to collect results
25+
- (Automating this is left as an exercise for the reader ;)
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
mod test_inputs;
2+
use base64::{BASE64_ENCODER, BASE64_DECODER};
3+
4+
// Choose number of encode and/or decode runs
5+
pub global ENCODE_RUNS: u32 = 3;
6+
pub global DECODE_RUNS: u32 = 0;
7+
8+
// Choose size, and use corresponding Prover.toml during execution
9+
comptime global TEST_INPUT = test_inputs::TEST_INPUT_SHORT;
10+
comptime global TEST_BASE64_ENCODED = test_inputs::TEST_BASE64_ENCODED_SHORT;
11+
12+
comptime global U: u32 = comptime { TEST_INPUT.as_bytes().len() };
13+
comptime global B: u32 = comptime {
14+
let mut q = U / 3;
15+
let r = U - q * 3;
16+
if (r > 0) { q += 1; }; // round up since encoded base64 gets padded
17+
q * 4
18+
};
19+
20+
fn main(input: str<U>, base64_encoded: str<B>) {
21+
for _ in 0..ENCODE_RUNS {
22+
let _encoded: [u8; B] = BASE64_ENCODER.encode(input.as_bytes());
23+
}
24+
for _ in 0..DECODE_RUNS {
25+
let _decoded: [u8; U] = BASE64_DECODER.decode(base64_encoded.as_bytes());
26+
}
27+
}
28+
29+
#[test]
30+
fn test() {
31+
main(TEST_INPUT, TEST_BASE64_ENCODED);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//SHORT
2+
pub comptime global TEST_INPUT_SHORT = "The quick br";
3+
pub comptime global TEST_BASE64_ENCODED_SHORT = "VGhlIHF1aWNrIGJy";
4+
5+
//LONG
6+
pub comptime global TEST_INPUT_LONG =
7+
"The quick brown fox jumps over the lazy dog, while 42 ravens perch atop a rusty mailbox.";
8+
pub comptime global TEST_BASE64_ENCODED_LONG = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZywgd2hpbGUgNDIgcmF2ZW5zIHBlcmNoIGF0b3AgYSBydXN0eSBtYWlsYm94Lg==";
9+
10+
//LONG LONG
11+
// pub comptime global TEST_INPUT_LONG_LONG =
12+
// "The quick brown fox jumps over the lazy dog, while 42 ravens perch atop a rusty mailbox.";
13+
// pub comptime global TEST_BASE64_ENCODED_LONG_LONG = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZywgd2hpbGUgNDIgcmF2ZW5zIHBlcmNoIGF0b3AgYSBydXN0eSBtYWlsYm94Lg==";

0 commit comments

Comments
 (0)