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-base64Import the provable type Bytes from o1js
import { Bytes } from 'o1js';- Import the
base64Decodefunction
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
base64Encodefunction
import { base64Encode } from 'o1js-base64';- For the example of a string input:
const inputBytes = Bytes.fromString('Childish Monderino');
const encodedB64 = base64Encode(inputBytes);-
The
base64Encodeandbase64Decodefunctions are interchangeable, butbase64Encodeis slightly more efficient. -
The
base64Decodefunction will throw an error if theencodedB64length is not a multiple of 4 or contains non-base64 characters. -
Ensure to provide the accurate decoded byte length parameter when invoking the
base64Decodefunction. -
Utilize the
calculateB64DecodedBytesLengthfunction 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
base64Decodefunction within a zkProgram or zkApp, it's crucial to use the appropriateprovable Bytetypes 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 buildnpm run test
npm run testw # watch modenpm run coveragenpm 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
UInt8instead of full field elements, thebase64Decodecircuit rows were reduced by around 75% from 8081 to 2138.
- By operating on