mirror of
https://github.com/logos-messaging/js-rln.git
synced 2026-01-04 06:33:09 +00:00
decouple proof related stuff
This commit is contained in:
parent
58435f74b5
commit
4b8a38c7f8
@ -7,12 +7,8 @@ import {
|
|||||||
import { RLNContract } from "./contract/index.js";
|
import { RLNContract } from "./contract/index.js";
|
||||||
import { createRLN } from "./create.js";
|
import { createRLN } from "./create.js";
|
||||||
import { Keystore } from "./keystore/index.js";
|
import { Keystore } from "./keystore/index.js";
|
||||||
import {
|
import { Proof } from "./proof.js";
|
||||||
IdentityCredential,
|
import { IdentityCredential, RLNInstance } from "./rln.js";
|
||||||
Proof,
|
|
||||||
ProofMetadata,
|
|
||||||
RLNInstance,
|
|
||||||
} from "./rln.js";
|
|
||||||
import { MerkleRootTracker } from "./root_tracker.js";
|
import { MerkleRootTracker } from "./root_tracker.js";
|
||||||
import { extractMetaMaskSigner } from "./utils/index.js";
|
import { extractMetaMaskSigner } from "./utils/index.js";
|
||||||
|
|
||||||
@ -22,7 +18,6 @@ export {
|
|||||||
RLNInstance,
|
RLNInstance,
|
||||||
IdentityCredential,
|
IdentityCredential,
|
||||||
Proof,
|
Proof,
|
||||||
ProofMetadata,
|
|
||||||
RLNEncoder,
|
RLNEncoder,
|
||||||
RLNDecoder,
|
RLNDecoder,
|
||||||
MerkleRootTracker,
|
MerkleRootTracker,
|
||||||
|
|||||||
67
src/proof.ts
Normal file
67
src/proof.ts
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
import type { IRateLimitProof } from "@waku/interfaces";
|
||||||
|
|
||||||
|
import { concatenate, poseidonHash } from "./utils/index.js";
|
||||||
|
|
||||||
|
const proofOffset = 128;
|
||||||
|
const rootOffset = proofOffset + 32;
|
||||||
|
const epochOffset = rootOffset + 32;
|
||||||
|
const shareXOffset = epochOffset + 32;
|
||||||
|
const shareYOffset = shareXOffset + 32;
|
||||||
|
const nullifierOffset = shareYOffset + 32;
|
||||||
|
const rlnIdentifierOffset = nullifierOffset + 32;
|
||||||
|
|
||||||
|
class ProofMetadata {
|
||||||
|
constructor(
|
||||||
|
public readonly nullifier: Uint8Array,
|
||||||
|
public readonly shareX: Uint8Array,
|
||||||
|
public readonly shareY: Uint8Array,
|
||||||
|
public readonly externalNullifier: Uint8Array
|
||||||
|
) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Proof implements IRateLimitProof {
|
||||||
|
readonly proof: Uint8Array;
|
||||||
|
readonly merkleRoot: Uint8Array;
|
||||||
|
readonly epoch: Uint8Array;
|
||||||
|
readonly shareX: Uint8Array;
|
||||||
|
readonly shareY: Uint8Array;
|
||||||
|
readonly nullifier: Uint8Array;
|
||||||
|
readonly rlnIdentifier: Uint8Array;
|
||||||
|
|
||||||
|
constructor(proofBytes: Uint8Array) {
|
||||||
|
if (proofBytes.length < rlnIdentifierOffset) throw "invalid proof";
|
||||||
|
// parse the proof as proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32>
|
||||||
|
this.proof = proofBytes.subarray(0, proofOffset);
|
||||||
|
this.merkleRoot = proofBytes.subarray(proofOffset, rootOffset);
|
||||||
|
this.epoch = proofBytes.subarray(rootOffset, epochOffset);
|
||||||
|
this.shareX = proofBytes.subarray(epochOffset, shareXOffset);
|
||||||
|
this.shareY = proofBytes.subarray(shareXOffset, shareYOffset);
|
||||||
|
this.nullifier = proofBytes.subarray(shareYOffset, nullifierOffset);
|
||||||
|
this.rlnIdentifier = proofBytes.subarray(
|
||||||
|
nullifierOffset,
|
||||||
|
rlnIdentifierOffset
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
extractMetadata(): ProofMetadata {
|
||||||
|
const externalNullifier = poseidonHash(this.epoch, this.rlnIdentifier);
|
||||||
|
return new ProofMetadata(
|
||||||
|
this.nullifier,
|
||||||
|
this.shareX,
|
||||||
|
this.shareY,
|
||||||
|
externalNullifier
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function proofToBytes(p: IRateLimitProof): Uint8Array {
|
||||||
|
return concatenate(
|
||||||
|
p.proof,
|
||||||
|
p.merkleRoot,
|
||||||
|
p.epoch,
|
||||||
|
p.shareX,
|
||||||
|
p.shareY,
|
||||||
|
p.nullifier,
|
||||||
|
p.rlnIdentifier
|
||||||
|
);
|
||||||
|
}
|
||||||
70
src/rln.ts
70
src/rln.ts
@ -1,8 +1,8 @@
|
|||||||
import { createDecoder, createEncoder } from "@waku/core";
|
import { createDecoder, createEncoder } from "@waku/core";
|
||||||
import type { IRateLimitProof } from "@waku/interfaces";
|
|
||||||
import type {
|
import type {
|
||||||
ContentTopic,
|
ContentTopic,
|
||||||
IDecodedMessage,
|
IDecodedMessage,
|
||||||
|
IRateLimitProof,
|
||||||
EncoderOptions as WakuEncoderOptions,
|
EncoderOptions as WakuEncoderOptions,
|
||||||
} from "@waku/interfaces";
|
} from "@waku/interfaces";
|
||||||
import init from "@waku/zerokit-rln-wasm";
|
import init from "@waku/zerokit-rln-wasm";
|
||||||
@ -20,13 +20,14 @@ import type {
|
|||||||
EncryptedCredentials,
|
EncryptedCredentials,
|
||||||
} from "./keystore/index.js";
|
} from "./keystore/index.js";
|
||||||
import { KeystoreEntity, Password } from "./keystore/types.js";
|
import { KeystoreEntity, Password } from "./keystore/types.js";
|
||||||
|
import { Proof, proofToBytes } from "./proof.js";
|
||||||
import verificationKey from "./resources/verification_key.js";
|
import verificationKey from "./resources/verification_key.js";
|
||||||
import {
|
import {
|
||||||
buildBigIntFromUint8Array,
|
buildBigIntFromUint8Array,
|
||||||
poseidonHash,
|
concatenate,
|
||||||
|
extractMetaMaskSigner,
|
||||||
writeUIntLE,
|
writeUIntLE,
|
||||||
} from "./utils/index.js";
|
} from "./utils/index.js";
|
||||||
import { concatenate, extractMetaMaskSigner } from "./utils/index.js";
|
|
||||||
import * as wc from "./witness_calculator.js";
|
import * as wc from "./witness_calculator.js";
|
||||||
import { WitnessCalculator } from "./witness_calculator.js";
|
import { WitnessCalculator } from "./witness_calculator.js";
|
||||||
|
|
||||||
@ -88,69 +89,6 @@ export class IdentityCredential {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const proofOffset = 128;
|
|
||||||
const rootOffset = proofOffset + 32;
|
|
||||||
const epochOffset = rootOffset + 32;
|
|
||||||
const shareXOffset = epochOffset + 32;
|
|
||||||
const shareYOffset = shareXOffset + 32;
|
|
||||||
const nullifierOffset = shareYOffset + 32;
|
|
||||||
const rlnIdentifierOffset = nullifierOffset + 32;
|
|
||||||
|
|
||||||
export class ProofMetadata {
|
|
||||||
constructor(
|
|
||||||
public readonly nullifier: Uint8Array,
|
|
||||||
public readonly shareX: Uint8Array,
|
|
||||||
public readonly shareY: Uint8Array,
|
|
||||||
public readonly externalNullifier: Uint8Array
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
export class Proof implements IRateLimitProof {
|
|
||||||
readonly proof: Uint8Array;
|
|
||||||
readonly merkleRoot: Uint8Array;
|
|
||||||
readonly epoch: Uint8Array;
|
|
||||||
readonly shareX: Uint8Array;
|
|
||||||
readonly shareY: Uint8Array;
|
|
||||||
readonly nullifier: Uint8Array;
|
|
||||||
readonly rlnIdentifier: Uint8Array;
|
|
||||||
|
|
||||||
constructor(proofBytes: Uint8Array) {
|
|
||||||
if (proofBytes.length < rlnIdentifierOffset) throw "invalid proof";
|
|
||||||
// parse the proof as proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32>
|
|
||||||
this.proof = proofBytes.subarray(0, proofOffset);
|
|
||||||
this.merkleRoot = proofBytes.subarray(proofOffset, rootOffset);
|
|
||||||
this.epoch = proofBytes.subarray(rootOffset, epochOffset);
|
|
||||||
this.shareX = proofBytes.subarray(epochOffset, shareXOffset);
|
|
||||||
this.shareY = proofBytes.subarray(shareXOffset, shareYOffset);
|
|
||||||
this.nullifier = proofBytes.subarray(shareYOffset, nullifierOffset);
|
|
||||||
this.rlnIdentifier = proofBytes.subarray(
|
|
||||||
nullifierOffset,
|
|
||||||
rlnIdentifierOffset
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
extractMetadata(): ProofMetadata {
|
|
||||||
const externalNullifier = poseidonHash(this.epoch, this.rlnIdentifier);
|
|
||||||
return new ProofMetadata(
|
|
||||||
this.nullifier,
|
|
||||||
this.shareX,
|
|
||||||
this.shareY,
|
|
||||||
externalNullifier
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function proofToBytes(p: IRateLimitProof): Uint8Array {
|
|
||||||
return concatenate(
|
|
||||||
p.proof,
|
|
||||||
p.merkleRoot,
|
|
||||||
p.epoch,
|
|
||||||
p.shareX,
|
|
||||||
p.shareY,
|
|
||||||
p.nullifier,
|
|
||||||
p.rlnIdentifier
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
type StartRLNOptions = {
|
type StartRLNOptions = {
|
||||||
/**
|
/**
|
||||||
* If not set - will extract MetaMask account and get signer from it.
|
* If not set - will extract MetaMask account and get signer from it.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user