@reclaimprotocol/zk-symmetric-crypto
Version:
JS Wrappers for Various ZK Snark Circuits
67 lines (66 loc) • 2.09 kB
JavaScript
import { crypto } from '@reclaimprotocol/tls';
import { bitsToUint8Array, bitsToUintArray, toUint8Array, toUintArray, uint8ArrayToBits, uintArrayToBits } from "./utils.js";
// commit hash for this repo
export const GIT_COMMIT_HASH = '7e9026fd556fb68e5268e0237b8d3d6c8ea9f4b1';
export const CONFIG = {
'chacha20': {
index: 0,
chunkSize: 32,
bitsPerWord: 32,
keySizeBytes: 32,
ivSizeBytes: 12,
startCounter: 1,
// num of blocks per chunk
blocksPerChunk: 2,
// chacha20 circuit uses LE encoding
isLittleEndian: true,
uint8ArrayToBits: (arr) => (uintArrayToBits(toUintArray(arr)).flat()),
bitsToUint8Array: (bits) => {
const arr = bitsToUintArray(bits);
return toUint8Array(arr);
},
encrypt: makeAuthenticatedEncrypt('CHACHA20-POLY1305')
},
'aes-256-ctr': {
index: 2,
chunkSize: 80,
bitsPerWord: 8,
keySizeBytes: 32,
ivSizeBytes: 12,
startCounter: 2,
// num of blocks per chunk
blocksPerChunk: 5,
// AES circuit uses BE encoding
isLittleEndian: false,
uint8ArrayToBits,
bitsToUint8Array,
encrypt: makeAuthenticatedEncrypt('AES-256-GCM')
},
'aes-128-ctr': {
index: 1,
chunkSize: 80,
bitsPerWord: 8,
keySizeBytes: 16,
ivSizeBytes: 12,
startCounter: 2,
// num of blocks per chunk
blocksPerChunk: 5,
// AES circuit uses BE encoding
isLittleEndian: false,
uint8ArrayToBits,
bitsToUint8Array,
encrypt: makeAuthenticatedEncrypt('AES-128-GCM')
},
};
function makeAuthenticatedEncrypt(alg) {
return async ({ key, iv, in: data }) => {
const impKey = await crypto.importKey(alg, key);
const { ciphertext } = await crypto.authenticatedEncrypt(alg, {
key: impKey,
iv,
data,
aead: new Uint8Array(0),
});
return ciphertext.slice(0, data.length);
};
}