Skip to content

Commit

Permalink
PCD class instances as return value in deserialize functions (#687)
Browse files Browse the repository at this point in the history
Closes #308
  • Loading branch information
cedoor authored Sep 26, 2023
1 parent 1b572c3 commit 2b63c59
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 96 deletions.
10 changes: 8 additions & 2 deletions packages/eddsa-pcd/src/EdDSAPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
StringArgument,
StringArrayArgument
} from "@pcd/pcd-types";
import { fromHexString, toHexString } from "@pcd/util";
import { fromHexString, toHexString, requireDefinedParameter } from "@pcd/util";
import { buildEddsa, buildPoseidon, Eddsa, Point, Poseidon } from "circomlibjs";
import { v4 as uuid } from "uuid";
import { EdDSACardBody } from "./CardBody";
Expand Down Expand Up @@ -143,7 +143,13 @@ export async function serialize(
}

export async function deserialize(serialized: string): Promise<EdDSAPCD> {
return JSON.parse(serialized, reviver);
const { id, claim, proof } = JSON.parse(serialized, reviver);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new EdDSAPCD(id, claim, proof);
}

export function getDisplayOptions(pcd: EdDSAPCD): DisplayOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/ethereum-group-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@pcd/pcd-types": "0.7.0",
"@pcd/semaphore-identity-pcd": "0.7.0",
"@pcd/semaphore-signature-pcd": "0.7.0",
"@pcd/util": "0.1.0",
"@personaelabs/spartan-ecdsa": "^2.1.4",
"@semaphore-protocol/identity": "^3.11.0",
"ethers": "^5.7.2",
Expand Down
32 changes: 21 additions & 11 deletions packages/ethereum-group-pcd/src/EthereumGroupPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SemaphoreSignaturePCD,
SemaphoreSignaturePCDPackage
} from "@pcd/semaphore-signature-pcd";
import { requireDefinedParameter } from "@pcd/util";
import {
CircuitPubInput,
MembershipProver,
Expand Down Expand Up @@ -279,21 +280,30 @@ export async function serialize(
export async function deserialize(
serialized: string
): Promise<EthereumGroupPCD> {
const parsed = JSONBig({ useNativeBigInt: true }).parse(serialized);
const { id, claim, proof } = JSONBig({ useNativeBigInt: true }).parse(
serialized
);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

const publicInput = new PublicInput(
parsed.claim.publicInput.r,
parsed.claim.publicInput.rV,
Buffer.from(parsed.claim.publicInput.msgHash),
claim.publicInput.r,
claim.publicInput.rV,
Buffer.from(claim.publicInput.msgHash),
new CircuitPubInput(
parsed.claim.publicInput.circuitPubInput.merkleRoot,
parsed.claim.publicInput.circuitPubInput.Tx,
parsed.claim.publicInput.circuitPubInput.Ty,
parsed.claim.publicInput.circuitPubInput.Ux,
parsed.claim.publicInput.circuitPubInput.Uy
claim.publicInput.circuitPubInput.merkleRoot,
claim.publicInput.circuitPubInput.Tx,
claim.publicInput.circuitPubInput.Ty,
claim.publicInput.circuitPubInput.Ux,
claim.publicInput.circuitPubInput.Uy
)
);
parsed.claim.publicInput = publicInput;
return new EthereumGroupPCD(parsed.id, parsed.claim, parsed.proof);

claim.publicInput = publicInput;

return new EthereumGroupPCD(id, claim, proof);
}

export function getDisplayOptions(pcd: EthereumGroupPCD): DisplayOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/ethereum-ownership-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@pcd/pcd-types": "0.7.0",
"@pcd/semaphore-identity-pcd": "0.7.0",
"@pcd/semaphore-signature-pcd": "0.7.0",
"@pcd/util": "0.1.0",
"@semaphore-protocol/identity": "^3.11.0",
"ethers": "^5.7.2",
"json-bigint": "^1.0.0",
Expand Down
9 changes: 8 additions & 1 deletion packages/ethereum-ownership-pcd/src/EthereumOwnershipPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SemaphoreSignaturePCD,
SemaphoreSignaturePCDPackage
} from "@pcd/semaphore-signature-pcd";
import { requireDefinedParameter } from "@pcd/util";
import { ethers } from "ethers";
import JSONBig from "json-bigint";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -200,7 +201,13 @@ export async function serialize(
export async function deserialize(
serialized: string
): Promise<EthereumOwnershipPCD> {
return JSONBig().parse(serialized);
const { id, claim, proof } = JSONBig().parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new EthereumOwnershipPCD(id, claim, proof);
}

export function getDisplayOptions(pcd: EthereumOwnershipPCD): DisplayOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/halo-nonce-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@pcd/passport-ui": "0.7.0",
"@pcd/pcd-types": "0.7.0",
"@pcd/tsconfig": "*",
"@pcd/util": "0.1.0",
"@types/elliptic": "^6.4.14",
"@types/mocha": "^10.0.1",
"@types/react": "^18.0.22",
Expand Down
23 changes: 15 additions & 8 deletions packages/halo-nonce-pcd/src/HaLoNoncePCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
PCD,
PCDPackage,
SerializedPCD,
StringArgument,
StringArgument
} from "@pcd/pcd-types";
import { requireDefinedParameter } from "@pcd/util";
import { ec } from "elliptic";
import { sha256 } from "js-sha256";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -85,7 +86,7 @@ export async function prove(args: HaLoNoncePCDArgs): Promise<HaLoNoncePCD> {

const claim: HaLoNoncePCDClaim = {
nonce: parseInt(args.rnd.value.substring(0, 8), 16),
pubkeyHex: args.pk2.value,
pubkeyHex: args.pk2.value
};

if (isNaN(claim.nonce)) {
Expand All @@ -107,7 +108,7 @@ export async function prove(args: HaLoNoncePCDArgs): Promise<HaLoNoncePCD> {

const proof: HaLoNoncePCDProof = {
signedDigest: args.rnd.value,
cleanedSignature: cutSig,
cleanedSignature: cutSig
};

return new HaLoNoncePCD(uuid(), claim, proof);
Expand All @@ -128,7 +129,7 @@ export async function verify(pcd: HaLoNoncePCD): Promise<boolean> {
.update(
Buffer.concat([
Buffer.from([0x19]),
Buffer.from("Attest counter pk2:\n", "utf8"),
Buffer.from("Attest counter pk2:\n", "utf8")
])
)
.update(rndBuf)
Expand All @@ -149,17 +150,23 @@ export async function serialize(
): Promise<SerializedPCD<HaLoNoncePCD>> {
return {
type: HaLoNoncePCDTypeName,
pcd: JSON.stringify(pcd),
pcd: JSON.stringify(pcd)
} as SerializedPCD<HaLoNoncePCD>;
}

export async function deserialize(serialized: string): Promise<HaLoNoncePCD> {
return JSON.parse(serialized);
const { id, claim, proof } = JSON.parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new HaLoNoncePCD(id, claim, proof);
}

export function getDisplayOptions(pcd: HaLoNoncePCD): DisplayOptions {
return {
displayName: "halo-nonce-" + pcd.id.substring(0, 4),
displayName: "halo-nonce-" + pcd.id.substring(0, 4)
};
}

Expand All @@ -181,5 +188,5 @@ export const HaLoNoncePCDPackage: PCDPackage<
prove,
verify,
serialize,
deserialize,
deserialize
};
1 change: 1 addition & 0 deletions packages/rln-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@pcd/pcd-types": "^0.7.0",
"@pcd/semaphore-group-pcd": "^0.7.0",
"@pcd/semaphore-identity-pcd": "^0.7.0",
"@pcd/util": "0.1.0",
"@semaphore-protocol/group": "^3.11.0",
"@semaphore-protocol/identity": "^3.11.0",
"json-bigint": "^1.0.0",
Expand Down
34 changes: 20 additions & 14 deletions packages/rln-pcd/src/RLNPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import {
PCDArgument,
PCDPackage,
SerializedPCD,
StringArgument,
StringArgument
} from "@pcd/pcd-types";
import {
SemaphoreIdentityPCD,
SemaphoreIdentityPCDPackage,
SemaphoreIdentityPCDPackage
} from "@pcd/semaphore-identity-pcd";
import {
deserializeSemaphoreGroup,
SerializedSemaphoreGroup,
SerializedSemaphoreGroup
} from "@pcd/semaphore-group-pcd";

import verificationKeyJSON from "../artifacts/16.json";
import { requireDefinedParameter } from "@pcd/util";

let initArgs: RLNPCDInitArgs | undefined = undefined;

Expand Down Expand Up @@ -87,11 +88,11 @@ export class RLNPCD implements PCD<RLNPCDClaim, RLNPCDProof> {
rlnIdentifier: rlnFullProof.rlnIdentifier,
yShare: BigInt(publicSignals.yShare),
merkleRoot: BigInt(publicSignals.merkleRoot),
internalNullifier: BigInt(publicSignals.internalNullifier),
internalNullifier: BigInt(publicSignals.internalNullifier)
};
const proof: RLNPCDProof = {
proof: rlnFullProof.snarkProof.proof,
externalNullifier: BigInt(publicSignals.externalNullifier),
externalNullifier: BigInt(publicSignals.externalNullifier)
};
return new RLNPCD(uuid(), claim, proof);
}
Expand All @@ -105,11 +106,11 @@ export class RLNPCD implements PCD<RLNPCDClaim, RLNPCDProof> {
merkleRoot: this.claim.merkleRoot,
internalNullifier: this.claim.internalNullifier,
signalHash: this.claim.x,
externalNullifier: this.proof.externalNullifier,
},
externalNullifier: this.proof.externalNullifier
}
},
epoch: this.claim.epoch,
rlnIdentifier: this.claim.rlnIdentifier,
rlnIdentifier: this.claim.rlnIdentifier
};
}
}
Expand Down Expand Up @@ -206,15 +207,20 @@ function getRLNInstance(rlnIdentifier: bigint, identity?: Identity) {
export async function serialize(pcd: RLNPCD): Promise<SerializedPCD<RLNPCD>> {
return {
type: RLNPCDTypeName,
pcd: JSONBig({ useNativeBigInt: true }).stringify(pcd),
pcd: JSONBig({ useNativeBigInt: true }).stringify(pcd)
} as SerializedPCD<RLNPCD>;
}

export async function deserialize(serialized: string): Promise<RLNPCD> {
const parsed = JSONBig({ useNativeBigInt: true }).parse(serialized);
const proof = parsed.proof;
const claim = parsed.claim;
return new RLNPCD(parsed.id, claim, proof);
const { id, claim, proof } = JSONBig({ useNativeBigInt: true }).parse(
serialized
);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new RLNPCD(id, claim, proof);
}

/**
Expand All @@ -232,5 +238,5 @@ export const RLNPCDPackage: PCDPackage<
prove,
verify,
serialize,
deserialize,
deserialize
};
1 change: 1 addition & 0 deletions packages/rsa-pcd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@pcd/passport-ui": "0.7.0",
"@pcd/pcd-types": "0.7.0",
"@pcd/util": "0.1.0",
"chai": "^4.3.7",
"json-bigint": "^1.0.0",
"node-rsa": "^1.1.1",
Expand Down
17 changes: 12 additions & 5 deletions packages/rsa-pcd/src/RSAPCD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
PCD,
PCDPackage,
SerializedPCD,
StringArgument,
StringArgument
} from "@pcd/pcd-types";
import { requireDefinedParameter } from "@pcd/util";
import JSONBig from "json-bigint";
import NodeRSA from "node-rsa";
import { v4 as uuid } from "uuid";
Expand Down Expand Up @@ -92,18 +93,24 @@ export async function verify(pcd: RSAPCD): Promise<boolean> {
export async function serialize(pcd: RSAPCD): Promise<SerializedPCD<RSAPCD>> {
return {
type: RSAPCDTypeName,
pcd: JSONBig().stringify(pcd),
pcd: JSONBig().stringify(pcd)
} as SerializedPCD<RSAPCD>;
}

export async function deserialize(serialized: string): Promise<RSAPCD> {
return JSONBig().parse(serialized);
const { id, claim, proof } = JSONBig().parse(serialized);

requireDefinedParameter(id, "id");
requireDefinedParameter(claim, "claim");
requireDefinedParameter(proof, "proof");

return new RSAPCD(id, claim, proof);
}

export function getDisplayOptions(pcd: RSAPCD): DisplayOptions {
return {
header: "RSA Signature",
displayName: "rsa-sig-" + pcd.id.substring(0, 4),
displayName: "rsa-sig-" + pcd.id.substring(0, 4)
};
}

Expand All @@ -122,5 +129,5 @@ export const RSAPCDPackage: PCDPackage<
prove,
verify,
serialize,
deserialize,
deserialize
};
22 changes: 9 additions & 13 deletions packages/rsa-pcd/test/RSAPCD.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { v4 as uuid } from "uuid";
import { RSAPCD, RSAPCDPackage } from "../src";

async function copyPcd(pcd: RSAPCD): Promise<RSAPCD> {
return await RSAPCDPackage.deserialize(
await (
await RSAPCDPackage.serialize(pcd)
).pcd
);
return RSAPCDPackage.deserialize((await RSAPCDPackage.serialize(pcd)).pcd);
}

describe("RSA signature PCD should work", function () {
Expand All @@ -25,16 +21,16 @@ describe("RSA signature PCD should work", function () {
pcd = await RSAPCDPackage.prove({
privateKey: {
argumentType: ArgumentTypeName.String,
value: exportedKey,
value: exportedKey
},
signedMessage: {
argumentType: ArgumentTypeName.String,
value: message,
value: message
},
id: {
argumentType: ArgumentTypeName.String,
value: undefined,
},
value: undefined
}
});

expect(await copyPcd(pcd)).to.deep.eq(pcd);
Expand All @@ -47,16 +43,16 @@ describe("RSA signature PCD should work", function () {
pcd = await RSAPCDPackage.prove({
privateKey: {
argumentType: ArgumentTypeName.String,
value: exportedKey,
value: exportedKey
},
signedMessage: {
argumentType: ArgumentTypeName.String,
value: message,
value: message
},
id: {
argumentType: ArgumentTypeName.String,
value: customId,
},
value: customId
}
});

expect(pcd.id).to.eq(customId);
Expand Down
Loading

0 comments on commit 2b63c59

Please sign in to comment.