81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
|
// crypto types
|
||
|
import * as Types from 'types/sdk/crypto'
|
||
|
|
||
|
// External crypto
|
||
|
import { Groth16 } from 'src/groth16'
|
||
|
import circomlib from 'circomlib'
|
||
|
import { buildGroth16 } from 'websnark'
|
||
|
|
||
|
// Some utils to work with hex numbers
|
||
|
import { HexUtils, NumberUtils } from 'lib/utils'
|
||
|
|
||
|
// Parse some files
|
||
|
import { Files } from 'lib/data'
|
||
|
|
||
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SETUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
/**
|
||
|
* Several objects have to be set up, like the groth16 prover. All related types are
|
||
|
* (will be) contained within this namespace.
|
||
|
*/
|
||
|
export namespace Setup {
|
||
|
export async function provingKey(): Promise<ArrayBufferLike> {
|
||
|
return (await Files.loadRaw('circuits/tornadoProvingKey.bin')).buffer
|
||
|
}
|
||
|
|
||
|
export async function tornadoCircuit(): Promise<Buffer> {
|
||
|
return await Files.loadRaw('circuits/tornado.json')
|
||
|
}
|
||
|
|
||
|
export function groth16(): Promise<Groth16> {
|
||
|
const defaultParams = { wasmInitialMemory: 5000 }
|
||
|
return buildGroth16(defaultParams)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export namespace Primitives {
|
||
|
export function calcPedersenHash(
|
||
|
pedersenHashData: Types.InputFor.PedersenHash
|
||
|
): Types.OutputOf.PedersenHash {
|
||
|
return circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(pedersenHashData.msg))[0]
|
||
|
}
|
||
|
|
||
|
export function createNote(msg: Buffer): string {
|
||
|
return HexUtils.bufferToHex(msg, 62)
|
||
|
}
|
||
|
|
||
|
export function createDeposit(depositData?: Types.InputFor.CreateDeposit): Types.TornadoDeposit {
|
||
|
if (!depositData?.nullifier || !depositData?.secret)
|
||
|
depositData = {
|
||
|
nullifier: NumberUtils.randomBigInteger(31),
|
||
|
secret: NumberUtils.randomBigInteger(31)
|
||
|
}
|
||
|
// @ts-ignore
|
||
|
let preimage = Buffer.concat([depositData.nullifier.leInt2Buff(31), depositData.secret.leInt2Buff(31)])
|
||
|
let commitment = calcPedersenHash({ msg: preimage })
|
||
|
let commitmentHex = HexUtils.bigIntToHex(commitment)
|
||
|
// @ts-ignore
|
||
|
let nullifierHash = calcPedersenHash({ msg: depositData.nullifier.leInt2Buff(31) })
|
||
|
let nullifierHex = HexUtils.bigIntToHex(nullifierHash)
|
||
|
return {
|
||
|
nullifier: depositData.nullifier!,
|
||
|
secret: depositData.secret!,
|
||
|
preimage: preimage,
|
||
|
commitment: commitment,
|
||
|
commitmentHex: commitmentHex,
|
||
|
nullifierHash: nullifierHash,
|
||
|
nullifierHex: nullifierHex
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TODO: implement and decide whether to add in declarations an ambient namespace and merge it here
|
||
|
|
||
|
// export function buildMerkleTree(deposit: Crypto.TornadoDeposit): Crypto.MerkleTree {}
|
||
|
|
||
|
// export function calcMerkleProof(tree: Crypto.MerkleTree): Crypto.MerkleProof {}
|
||
|
|
||
|
// export function calcDepositProof(merkleProof: Crypto.InputFor.DepositProof): Crypto.OutputOf.DepositProof {}
|
||
|
|
||
|
// Namespace exports
|
||
|
export { Types }
|