Skip to content

Commit

Permalink
fix: move resources outside rln package
Browse files Browse the repository at this point in the history
- Put default resources to an external storage and access it through URL
- Add `RLN.create` which downloads the default resources if devs
didn't give resources
- Fix tests
  • Loading branch information
mhchia committed Sep 1, 2023
1 parent 403e9c3 commit f874de9
Show file tree
Hide file tree
Showing 11 changed files with 568 additions and 401 deletions.
275 changes: 107 additions & 168 deletions README.md

Large diffs are not rendered by default.

145 changes: 136 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@semaphore-protocol/group": "^3.10.1",
"@semaphore-protocol/identity": "^3.10.1",
"@zk-kit/incremental-merkle-tree": "^0.4.3",
"base64-js": "^1.5.1",
"axios": "^1.5.0",
"ethers": "^6.4.0",
"ffjavascript": "0.2.55",
"poseidon-lite": "^0.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export { IMessageIDCounter } from './message-id-counter'

export * from './types'
// Expose helpers
export { calculateExternalNullifier, calculateRateCommitment, calculateSignalHash, shamirRecovery } from './common'
export { calculateExternalNullifier, calculateRateCommitment, calculateSignalHash, shamirRecovery, DEFAULT_MERKLE_TREE_DEPTH } from './common'

export { RLNFullProof, RLNSNARKProof, RLNWitness, RLNPublicSignals, RLNProver, RLNVerifier, WithdrawProver } from './circuit-wrapper'
2 changes: 1 addition & 1 deletion src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class MemoryRLNRegistry implements IRLNRegistry {

constructor(
readonly rlnIdentifier: bigint,
readonly treeDepth?: number | undefined,
readonly treeDepth: number = DEFAULT_MERKLE_TREE_DEPTH,
) {
this.mapIsWithdrawing = new Map<string, boolean>()
this.mapMessageLimit = new Map<string, bigint>()
Expand Down
78 changes: 78 additions & 0 deletions src/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { VerificationKey } from './types'

import axios from 'axios'

type ICircuitParams = {
wasmFile: string | Uint8Array,
finalZkey: string | Uint8Array,
}

type IRLNParams = ICircuitParams & { verificationKey: VerificationKey }
type IWithdrawParams = ICircuitParams

// TODO: Change to a more permanent URL after trusted setup is complete
const resourcesURL = 'https://rln-resources-temp.s3.us-west-1.amazonaws.com/resources'
const rln20URL = `${resourcesURL}/rln-20`
const withdrawURL = `${resourcesURL}/withdraw`
const treeDepthToDefaultRLNParamsURL = {
'20': rln20URL,
}

async function downloadBinary(url: string): Promise<Uint8Array> {
const resp = await axios.get(url, { responseType: 'arraybuffer' })
return new Uint8Array(resp.data)
}

function parseVerificationKeyJSON(o: any): VerificationKey {

Check warning on line 26 in src/resources.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
// NOTE: This is not a complete check, to do better we can check values are of the correct type
if (!o.protocol) throw new Error('Verification key has no protocol')
if (!o.curve) throw new Error('Verification key has no curve')
if (!o.nPublic) throw new Error('Verification key has no nPublic')
if (!o.vk_alpha_1) throw new Error('Verification key has no vk_alpha_1')
if (!o.vk_beta_2) throw new Error('Verification key has no vk_beta_2')
if (!o.vk_gamma_2) throw new Error('Verification key has no vk_gamma_2')
if (!o.vk_delta_2) throw new Error('Verification key has no vk_delta_2')
if (!o.vk_alphabeta_12) throw new Error('Verification key has no vk_alphabeta_12')
if (!o.IC) throw new Error('Verification key has no IC')
return o
}

async function downloadVerificationKey(url: string): Promise<VerificationKey> {
const resp = await axios.get(url)
return parseVerificationKeyJSON(resp.data)
}

export async function getDefaultRLNParams(treeDepth: number): Promise<IRLNParams | undefined> {
const url = treeDepthToDefaultRLNParamsURL[treeDepth.toString()]
if (!url) {
return undefined
}
const wasmFileURL = `${url}/circuit.wasm`
const finalZkeyURL = `${url}/final.zkey`
const verificationKeyURL = `${url}/verification_key.json`
const verificationKey = await downloadVerificationKey(verificationKeyURL)
const [wasmFile, finalZkey] = await Promise.all([
downloadBinary(wasmFileURL),
downloadBinary(finalZkeyURL),
])
return {
wasmFile,
finalZkey,
verificationKey,
}
}

export async function getDefaultWithdrawParams(): Promise<IWithdrawParams> {
const wasmFileURL = `${withdrawURL}/circuit.wasm`
const finalZkeyURL = `${withdrawURL}/final.zkey`
const [wasmFile, finalZkey] = await Promise.all([
downloadBinary(wasmFileURL),
downloadBinary(finalZkeyURL),
])
return {
wasmFile,
finalZkey,
}
}


Loading

0 comments on commit f874de9

Please sign in to comment.