Skip to content

Commit

Permalink
Merge pull request #701 from semaphore-protocol/refactor/update-zk-kit
Browse files Browse the repository at this point in the history
Update `utils` package and identity private key types.
  • Loading branch information
cedoor authored Mar 15, 2024
2 parents d045bf9 + 0987dee commit 3b167fb
Show file tree
Hide file tree
Showing 37 changed files with 227 additions and 671 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"dependencies": {
"@semaphore-protocol/data": "4.0.0-beta.2",
"@semaphore-protocol/utils": "4.0.0-beta.2",
"axios": "^1.6.7",
"boxen": "^7.1.1",
"chalk": "^5.3.0",
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ const banner = `#!/usr/bin/env node
export default {
input: "src/index.ts",
output: [{ file: pkg.bin.semaphore, format: "es", banner }],
external: [...Object.keys(pkg.dependencies), "url", "fs", "path", "child_process"],
external: [
...Object.keys(pkg.dependencies),
"url",
"fs",
"path",
"child_process",
"@semaphore-protocol/utils/supported-networks"
],
plugins: [
typescript({
tsconfig: "./build.tsconfig.json"
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GroupResponse, SemaphoreEthers, SemaphoreSubgraph, getSupportedNetworks } from "@semaphore-protocol/data"
import { GroupResponse, SemaphoreEthers, SemaphoreSubgraph } from "@semaphore-protocol/data"
import supportedNetworks from "@semaphore-protocol/utils/supported-networks"
import chalk from "chalk"
import { program } from "commander"
import decompress from "decompress"
Expand All @@ -16,8 +17,6 @@ import Spinner from "./spinner.js"
const packagePath = `${dirname(fileURLToPath(import.meta.url))}/..`
const { description, version } = JSON.parse(readFileSync(`${packagePath}/package.json`, "utf8"))

const supportedNetworks = getSupportedNetworks()

const supportedTemplates = [
{
value: "monorepo-ethers",
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/test/Semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Semaphore", () => {
const merkleTreeDepth = 12

const groupId = 0
const members = Array.from({ length: 3 }, (_, i) => new Identity(i)).map(({ commitment }) => commitment)
const members = Array.from({ length: 3 }, (_, i) => new Identity(i.toString())).map(({ commitment }) => commitment)

before(async () => {
const { semaphore } = await run("deploy", {
Expand Down
9 changes: 0 additions & 9 deletions packages/data/src/getSupportedNetworks.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import SemaphoreEthers from "./ethers"
import getSupportedNetworks from "./getSupportedNetworks"
import SemaphoreSubgraph from "./subgraph"

export * from "./types"
export { SemaphoreSubgraph, SemaphoreEthers, getSupportedNetworks }
export { SemaphoreSubgraph, SemaphoreEthers }
12 changes: 0 additions & 12 deletions packages/data/tests/index.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/heyauthn/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
GenerateRegistrationOptionsOpts as RegistrationOptions
} from "@simplewebauthn/server"

import { HeyAuthn } from "../src"
import HeyAuthn from "../src/hey-authn"

jest.mock("@simplewebauthn/browser", () => ({
startRegistration: async () => ({
Expand Down
24 changes: 8 additions & 16 deletions packages/identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
"description": "A library to create Semaphore identities.",
"type": "module",
"license": "MIT",
"main": "dist/index.browser.js",
"main": "dist/index.js",
"types": "dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"node": {
"require": "./dist/index.node.cjs",
"default": "./dist/index.node.js"
},
"browser": "./dist/index.browser.js",
"default": "./dist/index.browser.js"
}
"types": "./dist/types/index.d.ts",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"files": [
"dist/",
Expand All @@ -29,9 +23,7 @@
"url": "https://github.com/semaphore-protocol/semaphore.git/issues"
},
"scripts": {
"build": "rimraf dist && yarn build:browser && yarn build:node",
"build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
"build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
"prepublishOnly": "yarn build"
},
"publishConfig": {
Expand All @@ -47,9 +39,9 @@
"rollup-plugin-cleanup": "^3.2.1"
},
"dependencies": {
"@zk-kit/baby-jubjub": "0.2.0",
"@zk-kit/eddsa-poseidon": "0.6.0",
"@zk-kit/utils": "0.6.0",
"@zk-kit/baby-jubjub": "0.3.0",
"@zk-kit/eddsa-poseidon": "0.10.0",
"@zk-kit/utils": "0.8.1",
"poseidon-lite": "0.2.0"
}
}
41 changes: 0 additions & 41 deletions packages/identity/rollup.browser.config.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ const banner = `/**
export default {
input: "src/index.ts",
output: [
{ file: pkg.exports["."].node.require, format: "cjs", banner, exports: "auto" },
{ file: pkg.exports["."].node.default, format: "es", banner }
{ file: pkg.exports.require, format: "cjs", banner, exports: "auto" },
{ file: pkg.exports.default, format: "es", banner }
],
external: [...Object.keys(pkg.dependencies), "node:crypto"],
external: Object.keys(pkg.dependencies),
plugins: [
typescript({
tsconfig: "./build.tsconfig.json"
}),
commonjs(),
nodeResolve(),
nodeResolve({ preferBuiltins: false }),
cleanup({ comments: "jsdoc" })
]
}
9 changes: 0 additions & 9 deletions packages/identity/src/bytes-to-bigint.ts

This file was deleted.

66 changes: 44 additions & 22 deletions packages/identity/src/identity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Point } from "@zk-kit/baby-jubjub"
import { Signature, derivePublicKey, deriveSecretScalar, signMessage, verifySignature } from "@zk-kit/eddsa-poseidon"
import { EdDSAPoseidon, Signature, signMessage, verifySignature } from "@zk-kit/eddsa-poseidon"
import type { BigNumberish } from "@zk-kit/utils"
import { bufferToHexadecimal, hexadecimalToBuffer } from "@zk-kit/utils/conversions"
import { requireString } from "@zk-kit/utils/error-handlers"
import { isHexadecimal } from "@zk-kit/utils/type-checks"
import { poseidon2 } from "poseidon-lite/poseidon2"
import { randomNumber } from "./random-number.node"

/**
* The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
Expand All @@ -11,23 +13,26 @@ import { randomNumber } from "./random-number.node"
* and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
* In addition, the commitment, i.e. the hash of the public key, is used to represent
* Semaphore identities in groups, adding an additional layer of privacy and security.
* The private key of the identity is stored as a hexadecimal string or text.
* The other attributes are stored as stringified bigint.
*/
export default class Identity {
// The EdDSA private key, passed as a parameter or generated randomly.
private _privateKey: BigNumberish
private _privateKey: string
// The secret scalar derived from the private key.
// It is used in circuits to derive the public key.
private _secretScalar: string
private _secretScalar: bigint
// The EdDSA public key, derived from the private key.
private _publicKey: Point<string>
private _publicKey: Point<bigint>
// The identity commitment used as a public value in Semaphore groups.
private _commitment: string
private _commitment: bigint

/**
* Initializes the class attributes based on a given private key.
* If the private key is not passed as a parameter, a random key is generated.
* The constructor calculates the secret scalar and public key from the private key,
* and computes a commitment of the public key using a hash function (Poseidon).
* Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text.
* Hexadecimal strings must not start with '0x' or '0X'.
* If the private key is not passed as a parameter, a random hexadecimal key will be generated.
* The EdDSAPoseidon class is used to generate the secret scalar and the public key.
* Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
*
* @example
* // Generates an identity.
Expand All @@ -36,44 +41,61 @@ export default class Identity {
* // Generates an identity with a random private key.
* const { privateKey, publicKey, commitment } = new Identity()
*
* @param privateKey The private key used to derive the public key.
* @param privateKey The private key used to derive the public key (hexadecimal or string).
*/
constructor(privateKey: BigNumberish = randomNumber().toString()) {
this._privateKey = privateKey
this._secretScalar = deriveSecretScalar(privateKey)
this._publicKey = derivePublicKey(privateKey)
this._commitment = poseidon2(this._publicKey).toString()
constructor(privateKey?: string) {
let eddsa: EdDSAPoseidon

if (privateKey) {
requireString(privateKey, "privateKey")

this._privateKey = privateKey

if (isHexadecimal(privateKey, false)) {
eddsa = new EdDSAPoseidon(hexadecimalToBuffer(privateKey))
} else {
eddsa = new EdDSAPoseidon(privateKey)
}
} else {
eddsa = new EdDSAPoseidon()

this._privateKey = bufferToHexadecimal(eddsa.privateKey as any)
}

this._secretScalar = eddsa.secretScalar
this._publicKey = eddsa.publicKey
this._commitment = poseidon2(this._publicKey)
}

/**
* Returns the private key.
* @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
* @returns The private key as a string (hexadecimal or text).
*/
public get privateKey(): BigNumberish {
public get privateKey(): string {
return this._privateKey
}

/**
* Returns the secret scalar.
* @returns The secret scalar as a string.
*/
public get secretScalar(): string {
public get secretScalar(): bigint {
return this._secretScalar
}

/**
* Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
* @returns The public key as a point.
*/
public get publicKey(): Point<string> {
public get publicKey(): Point<bigint> {
return this._publicKey
}

/**
* Returns the commitment hash of the public key.
* @returns The commitment as a string.
*/
public get commitment(): string {
public get commitment(): bigint {
return this._commitment
}

Expand All @@ -89,7 +111,7 @@ export default class Identity {
* @param message The message to be signed.
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
*/
public signMessage(message: BigNumberish): Signature<string> {
public signMessage(message: BigNumberish): Signature<bigint> {
return signMessage(this.privateKey, message)
}

Expand Down
8 changes: 0 additions & 8 deletions packages/identity/src/random-number.browser.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/identity/src/random-number.node.ts

This file was deleted.

Loading

0 comments on commit 3b167fb

Please sign in to comment.