diff --git a/README.md b/README.md index 58d7cae..2867562 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,55 @@ -# O1JS Base64 Decode +# O1JS Base64 Encode/Decode -This repository offers a straightforward way to decode base64-encoded input bytes. It's designed to be easy to use and reliable tool for decoding base64 data in various projects using [o1js](https://docs.minaprotocol.com/zkapps/o1js/). +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](https://docs.minaprotocol.com/zkapps/o1js/). ## How to use the package -1. Install the package +Install the package ```sh npm install o1js-base64 ``` -2. Import the `base64Decode` function +Import the provable type `Bytes` from `o1js` ```typescript -import { base64Decode } from 'o1js-base64'; +import { Bytes } from 'o1js'; ``` -3. Import the provable type `Bytes` from `o1js` +### Base64 Decode + +1. Import the `base64Decode` function ```typescript -import { Bytes } from 'o1js'; +import { base64Decode } from 'o1js-base64'; ``` -4. For the example of a **string** input: +2. For the example of a **string** input: ```typescript const encodedB64 = Bytes.fromString('7xQM+vU='); const decodedB64 = base64Decode(encodedB64, 5); ``` +### Base64 Encode + +1. Import the `base64Encode` function + +```typescript +import { base64Encode } from 'o1js-base64'; +``` + +2. For the example of a **string** input: + +```typescript +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. @@ -45,7 +64,8 @@ const decodedB64 = base64Decode(encodedB64, 5); - For reference, consider this [snippet](https://github.com/Shigoto-dev19/o1js-base64/blob/main/src/run.ts#L7-L20), 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. - - Therefore, ensure the provable Byte types are accurately and deterministically assigned to match the input and output lengths. + - 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 @@ -74,18 +94,38 @@ 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](https://github.com/zkemail/zk-email-verify/blob/main/packages/circuits/lib/base64.circom) diff --git a/package.json b/package.json index 472bf2d..1a65014 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "o1js-base64", - "version": "0.0.3", + "version": "0.1.0", "description": "", "author": "", "license": "Apache-2.0", @@ -12,6 +12,7 @@ "zkapp", "base64", "decode", + "encode", "zk", "zkSNARK", "zero knowledge" diff --git a/src/index.ts b/src/index.ts index baeb9ef..cd7c307 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export { base64Decode } from './base64.js'; +export { base64Decode, base64Encode } from './base64.js'; export { calculateB64DecodedBytesLength } from './utils.js';