@semaphore-protocol/identity 4.0.0-beta.1 → 4.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,86 @@
1
- import Identity from "./identity";
2
- export { Identity };
1
+ import type { Point } from "@zk-kit/baby-jubjub";
2
+ import { Signature } from "@zk-kit/eddsa-poseidon";
3
+ import type { BigNumberish } from "@zk-kit/utils";
4
+ /**
5
+ * The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
6
+ * public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
7
+ * in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
8
+ * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
9
+ * In addition, the commitment, i.e. the hash of the public key, is used to represent
10
+ * Semaphore identities in groups, adding an additional layer of privacy and security.
11
+ * The private key of the identity is stored as a hexadecimal string or text.
12
+ * The other attributes are stored as stringified bigint.
13
+ */
14
+ export declare class Identity {
15
+ private _privateKey;
16
+ private _secretScalar;
17
+ private _publicKey;
18
+ private _commitment;
19
+ /**
20
+ * Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text.
21
+ * Hexadecimal strings must not start with '0x' or '0X'.
22
+ * If the private key is not passed as a parameter, a random hexadecimal key will be generated.
23
+ * The EdDSAPoseidon class is used to generate the secret scalar and the public key.
24
+ * Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
25
+ *
26
+ * @example
27
+ * // Generates an identity.
28
+ * const { privateKey, publicKey, commitment } = new Identity("private-key")
29
+ * @example
30
+ * // Generates an identity with a random private key.
31
+ * const { privateKey, publicKey, commitment } = new Identity()
32
+ *
33
+ * @param privateKey The private key used to derive the public key (hexadecimal or string).
34
+ */
35
+ constructor(privateKey?: string);
36
+ /**
37
+ * Returns the private key.
38
+ * @returns The private key as a string (hexadecimal or text).
39
+ */
40
+ get privateKey(): string;
41
+ /**
42
+ * Returns the secret scalar.
43
+ * @returns The secret scalar as a string.
44
+ */
45
+ get secretScalar(): bigint;
46
+ /**
47
+ * Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
48
+ * @returns The public key as a point.
49
+ */
50
+ get publicKey(): Point<bigint>;
51
+ /**
52
+ * Returns the commitment hash of the public key.
53
+ * @returns The commitment as a string.
54
+ */
55
+ get commitment(): bigint;
56
+ /**
57
+ * Generates a signature for a given message using the private key.
58
+ * This method demonstrates how to sign a message and could be used
59
+ * for authentication or data integrity.
60
+ *
61
+ * @example
62
+ * const identity = new Identity()
63
+ * const signature = identity.signMessage("message")
64
+ *
65
+ * @param message The message to be signed.
66
+ * @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
67
+ */
68
+ signMessage(message: BigNumberish): Signature<bigint>;
69
+ /**
70
+ * Verifies a signature against a given message and public key.
71
+ * This static method allows for the verification of signatures without needing
72
+ * an instance of the Identity class. It's useful for cases where you only have
73
+ * the public key, the message and a signature, and need to verify if they match.
74
+ *
75
+ * @example
76
+ * const identity = new Identity()
77
+ * const signature = identity.signMessage("message")
78
+ * Identity.verifySignature("message", signature, identity.publicKey)
79
+ *
80
+ * @param message The message that was signed.
81
+ * @param signature The signature to verify.
82
+ * @param publicKey The public key to use for verification.
83
+ * @returns A boolean indicating whether the signature is valid.
84
+ */
85
+ static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean;
86
+ }
package/package.json CHANGED
@@ -1,21 +1,15 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/identity",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.10",
4
4
  "description": "A library to create Semaphore identities.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
- "main": "dist/index.browser.js",
7
+ "main": "dist/index.js",
8
8
  "types": "dist/types/index.d.ts",
9
9
  "exports": {
10
- ".": {
11
- "types": "./dist/types/index.d.ts",
12
- "node": {
13
- "require": "./dist/index.node.cjs",
14
- "default": "./dist/index.node.js"
15
- },
16
- "browser": "./dist/index.browser.js",
17
- "default": "./dist/index.browser.js"
18
- }
10
+ "types": "./dist/types/index.d.ts",
11
+ "require": "./dist/index.cjs",
12
+ "default": "./dist/index.js"
19
13
  },
20
14
  "files": [
21
15
  "dist/",
@@ -29,9 +23,7 @@
29
23
  "url": "https://github.com/semaphore-protocol/semaphore.git/issues"
30
24
  },
31
25
  "scripts": {
32
- "build": "rimraf dist && yarn build:browser && yarn build:node",
33
- "build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
34
- "build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
26
+ "build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
35
27
  "prepublishOnly": "yarn build"
36
28
  },
37
29
  "publishConfig": {
@@ -41,13 +33,15 @@
41
33
  "@rollup/plugin-alias": "^5.1.0",
42
34
  "@rollup/plugin-commonjs": "^25.0.7",
43
35
  "@rollup/plugin-node-resolve": "^15.2.3",
44
- "rollup-plugin-cleanup": "^3.2.1",
45
- "rollup-plugin-typescript2": "^0.36.0"
36
+ "@rollup/plugin-typescript": "^11.1.6",
37
+ "rimraf": "^5.0.5",
38
+ "rollup": "^4.12.0",
39
+ "rollup-plugin-cleanup": "^3.2.1"
46
40
  },
47
41
  "dependencies": {
48
- "@zk-kit/baby-jubjub": "0.2.0",
49
- "@zk-kit/eddsa-poseidon": "0.6.0",
50
- "@zk-kit/utils": "0.3.0",
42
+ "@zk-kit/baby-jubjub": "1.0.0",
43
+ "@zk-kit/eddsa-poseidon": "1.0.0",
44
+ "@zk-kit/utils": "1.0.0-beta.4",
51
45
  "poseidon-lite": "0.2.0"
52
46
  }
53
47
  }
package/src/index.ts CHANGED
@@ -1,4 +1,141 @@
1
- import Identity from "./identity"
1
+ import type { Point } from "@zk-kit/baby-jubjub"
2
+ import { EdDSAPoseidon, Signature, signMessage, verifySignature } from "@zk-kit/eddsa-poseidon"
3
+ import type { BigNumberish } from "@zk-kit/utils"
4
+ import { hexadecimalToBuffer } from "@zk-kit/utils/conversions"
5
+ import { requireString } from "@zk-kit/utils/error-handlers"
6
+ import { isHexadecimal } from "@zk-kit/utils/type-checks"
7
+ import { poseidon2 } from "poseidon-lite/poseidon2"
2
8
 
3
- // eslint-disable-next-line import/prefer-default-export
4
- export { Identity }
9
+ /**
10
+ * The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
11
+ * public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
12
+ * in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
13
+ * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
14
+ * In addition, the commitment, i.e. the hash of the public key, is used to represent
15
+ * Semaphore identities in groups, adding an additional layer of privacy and security.
16
+ * The private key of the identity is stored as a hexadecimal string or text.
17
+ * The other attributes are stored as stringified bigint.
18
+ */
19
+ export class Identity {
20
+ // The EdDSA private key, passed as a parameter or generated randomly.
21
+ private _privateKey: string
22
+ // The secret scalar derived from the private key.
23
+ // It is used in circuits to derive the public key.
24
+ private _secretScalar: bigint
25
+ // The EdDSA public key, derived from the private key.
26
+ private _publicKey: Point<bigint>
27
+ // The identity commitment used as a public value in Semaphore groups.
28
+ private _commitment: bigint
29
+
30
+ /**
31
+ * Initializes the class attributes based on a given private key, which must be a hexadecimal string or a text.
32
+ * Hexadecimal strings must not start with '0x' or '0X'.
33
+ * If the private key is not passed as a parameter, a random hexadecimal key will be generated.
34
+ * The EdDSAPoseidon class is used to generate the secret scalar and the public key.
35
+ * Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).
36
+ *
37
+ * @example
38
+ * // Generates an identity.
39
+ * const { privateKey, publicKey, commitment } = new Identity("private-key")
40
+ * @example
41
+ * // Generates an identity with a random private key.
42
+ * const { privateKey, publicKey, commitment } = new Identity()
43
+ *
44
+ * @param privateKey The private key used to derive the public key (hexadecimal or string).
45
+ */
46
+ constructor(privateKey?: string) {
47
+ let eddsa: EdDSAPoseidon
48
+
49
+ if (privateKey) {
50
+ requireString(privateKey, "privateKey")
51
+
52
+ this._privateKey = privateKey
53
+
54
+ if (isHexadecimal(privateKey, false)) {
55
+ eddsa = new EdDSAPoseidon(hexadecimalToBuffer(privateKey))
56
+ } else {
57
+ eddsa = new EdDSAPoseidon(privateKey)
58
+ }
59
+ } else {
60
+ eddsa = new EdDSAPoseidon()
61
+
62
+ this._privateKey = eddsa.privateKey as string
63
+ }
64
+
65
+ this._secretScalar = eddsa.secretScalar
66
+ this._publicKey = eddsa.publicKey
67
+ this._commitment = poseidon2(this._publicKey)
68
+ }
69
+
70
+ /**
71
+ * Returns the private key.
72
+ * @returns The private key as a string (hexadecimal or text).
73
+ */
74
+ public get privateKey(): string {
75
+ return this._privateKey
76
+ }
77
+
78
+ /**
79
+ * Returns the secret scalar.
80
+ * @returns The secret scalar as a string.
81
+ */
82
+ public get secretScalar(): bigint {
83
+ return this._secretScalar
84
+ }
85
+
86
+ /**
87
+ * Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
88
+ * @returns The public key as a point.
89
+ */
90
+ public get publicKey(): Point<bigint> {
91
+ return this._publicKey
92
+ }
93
+
94
+ /**
95
+ * Returns the commitment hash of the public key.
96
+ * @returns The commitment as a string.
97
+ */
98
+ public get commitment(): bigint {
99
+ return this._commitment
100
+ }
101
+
102
+ /**
103
+ * Generates a signature for a given message using the private key.
104
+ * This method demonstrates how to sign a message and could be used
105
+ * for authentication or data integrity.
106
+ *
107
+ * @example
108
+ * const identity = new Identity()
109
+ * const signature = identity.signMessage("message")
110
+ *
111
+ * @param message The message to be signed.
112
+ * @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
113
+ */
114
+ public signMessage(message: BigNumberish): Signature<bigint> {
115
+ const privateKey = isHexadecimal(this.privateKey, false)
116
+ ? hexadecimalToBuffer(this.privateKey)
117
+ : this.privateKey
118
+
119
+ return signMessage(privateKey, message)
120
+ }
121
+
122
+ /**
123
+ * Verifies a signature against a given message and public key.
124
+ * This static method allows for the verification of signatures without needing
125
+ * an instance of the Identity class. It's useful for cases where you only have
126
+ * the public key, the message and a signature, and need to verify if they match.
127
+ *
128
+ * @example
129
+ * const identity = new Identity()
130
+ * const signature = identity.signMessage("message")
131
+ * Identity.verifySignature("message", signature, identity.publicKey)
132
+ *
133
+ * @param message The message that was signed.
134
+ * @param signature The signature to verify.
135
+ * @param publicKey The public key to use for verification.
136
+ * @returns A boolean indicating whether the signature is valid.
137
+ */
138
+ static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
139
+ return verifySignature(message, signature, publicKey)
140
+ }
141
+ }