Skip to content

This repository offers a straightforward way to encode/decode ASCII input bytes. It's designed to be easy to use and reliable tool for encoding/decoding base64 data in various projects using o1js.

License

Notifications You must be signed in to change notification settings

Shigoto-dev19/o1js-base64

Repository files navigation

O1JS Base64 Encode/Decode

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.

How to use the package

Install the package

npm install o1js-base64

Import the provable type Bytes from o1js

import { Bytes } from 'o1js';

Base64 Decode

  1. Import the base64Decode function
import { base64Decode } from 'o1js-base64';
  1. For the example of a string input:
const encodedB64 = Bytes.fromString('7xQM+vU=');
const decodedB64 = base64Decode(encodedB64, 5);

Base64 Encode

  1. Import the base64Encode function
import { base64Encode } from 'o1js-base64';
  1. For the example of a string input:
const inputBytes = Bytes.fromString('Childish Monderino');
const encodedB64 = base64Encode(inputBytes);

Notes

  • The base64Encode and base64Decode functions are interchangeable, but base64Encode is slightly more efficient.

  • The base64Decode function will throw an error if the encodedB64 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 appropriate provable 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.

How to build

npm run build

How to run tests

npm run test
npm run testw # watch mode

How to run coverage

npm run coverage

How to benchmark

npm run benchmark

Preview

Base64 Decode zkProgram

Summary
Total rows 2138
Generic 1522
EndoMulScalar 616

Action Time (s)
Compile 1.104
Prove 11.219
Verify 0.844

Base64 Encode zkProgram

Summary
Total rows 1697
Generic 1203
EndoMulScalar 494

Action Time (s)
Compile 0.667
Prove 9.383
Verify 1.047

Acknowledgement

  • 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, the base64Decode circuit rows were reduced by around 75% from 8081 to 2138.

License

Apache-2.0

About

This repository offers a straightforward way to encode/decode ASCII input bytes. It's designed to be easy to use and reliable tool for encoding/decoding base64 data in various projects using o1js.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published