This repository offers a straightforward API to encode & decode base64-encoded input bytes. It's designed to be an easy-to-use and reliable tool for encoding & decoding base64 data in various projects using o1js.
Install the package
npm install o1js-base64
Import the provable type Bytes
from o1js
import { Bytes } from 'o1js';
- Import the
base64Decode
function
import { base64Decode } from 'o1js-base64';
- For the example of a string input:
const encodedB64 = Bytes.fromString('7xQM+vU=');
const decodedB64 = base64Decode(encodedB64, 5);
- Import the
base64Encode
function
import { base64Encode } from 'o1js-base64';
- For the example of a string input:
const inputBytes = Bytes.fromString('Childish Monderino');
const encodedB64 = base64Encode(inputBytes);
-
The
base64Encode
andbase64Decode
functions are interchangeable, butbase64Encode
is slightly more efficient. -
The
base64Decode
function will throw an error if theencodedB64
length is not a multiple of 4 or contains non-base64 characters. -
Ensure to provide the accurate decoded byte length parameter when invoking the
base64Decode
function. -
Utilize the
calculateB64DecodedBytesLength
function available within the package.-
You can find an example of its usage in this code snippet.
-
If needed, you can refer to this gist for manual calculation guidance.
-
-
When employing the
base64Decode
function within a zkProgram or zkApp, it's crucial to use the appropriateprovable Byte
types for both input and output.- For reference, consider this snippet, which demonstrates a zkProgram designed to decode a base64-encoded SHA256 digest.
- The encoded input has a length of 44, while the expected decoded output is 32 bytes long.
- Hence, it's crucial to accurately and deterministically assign provable Byte types to match the input and output lengths.
- Similarly, for base64 Encode, ensure the same careful consideration of Byte types.
npm run build
npm run test
npm run testw # watch mode
npm run coverage
npm run benchmark
Summary | |
---|---|
Total rows | 2138 |
Generic | 1522 |
EndoMulScalar | 616 |
Action | Time (s) |
---|---|
Compile | 1.104 |
Prove | 11.219 |
Verify | 0.844 |
Summary | |
---|---|
Total rows | 1697 |
Generic | 1203 |
EndoMulScalar | 494 |
Action | Time (s) |
---|---|
Compile | 0.667 |
Prove | 9.383 |
Verify | 1.047 |
-
This repo is inspired by the circom base64 implementation.
-
Big thanks to Gregor Mitscha-Baude for highlighting the inefficiency in processing full field elements.
- By operating on
UInt8
instead of full field elements, thebase64Decode
circuit rows were reduced by around 75% from 8081 to 2138.
- By operating on