@semaphore-protocol/identity 4.0.0-beta → 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.
package/src/identity.ts DELETED
@@ -1,121 +0,0 @@
1
- import {
2
- BigNumberish,
3
- Point,
4
- Signature,
5
- derivePublicKey,
6
- deriveSecretScalar,
7
- signMessage,
8
- verifySignature
9
- } from "@zk-kit/eddsa-poseidon"
10
- import { poseidon2 } from "poseidon-lite/poseidon2"
11
- import { randomNumber } from "./random-number.node"
12
-
13
- /**
14
- * The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
15
- * public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
16
- * in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
17
- * and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
18
- * In addition, the commitment, i.e. the hash of the public key, is used to represent
19
- * Semaphore identities in groups, adding an additional layer of privacy and security.
20
- */
21
- export default class Identity {
22
- // The EdDSA private key, passed as a parameter or generated randomly.
23
- private _privateKey: BigNumberish
24
- // The secret scalar derived from the private key.
25
- // It is used in circuits to derive the public key.
26
- private _secretScalar: string
27
- // The EdDSA public key, derived from the private key.
28
- private _publicKey: Point<string>
29
- // The identity commitment used as a public value in Semaphore groups.
30
- private _commitment: string
31
-
32
- /**
33
- * Initializes the class attributes based on a given private key.
34
- * If the private key is not passed as a parameter, a random key is generated.
35
- * The constructor calculates the secret scalar and public key from the private key,
36
- * and computes a commitment of the public key using a hash function (Poseidon).
37
- *
38
- * @example
39
- * // Generates an identity.
40
- * const { privateKey, publicKey, commitment } = new Identity("private-key")
41
- * @example
42
- * // Generates an identity with a random private key.
43
- * const { privateKey, publicKey, commitment } = new Identity()
44
- *
45
- * @param privateKey The private key used to derive the public key.
46
- */
47
- constructor(privateKey: BigNumberish = randomNumber().toString()) {
48
- this._privateKey = privateKey
49
- this._secretScalar = deriveSecretScalar(privateKey)
50
- this._publicKey = derivePublicKey(privateKey)
51
- this._commitment = poseidon2(this._publicKey).toString()
52
- }
53
-
54
- /**
55
- * Returns the private key.
56
- * @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
57
- */
58
- public get privateKey(): BigNumberish {
59
- return this._privateKey
60
- }
61
-
62
- /**
63
- * Returns the secret scalar.
64
- * @returns The secret scalar as a string.
65
- */
66
- public get secretScalar(): string {
67
- return this._secretScalar
68
- }
69
-
70
- /**
71
- * Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
72
- * @returns The public key as a point.
73
- */
74
- public get publicKey(): Point<string> {
75
- return this._publicKey
76
- }
77
-
78
- /**
79
- * Returns the commitment hash of the public key.
80
- * @returns The commitment as a string.
81
- */
82
- public get commitment(): string {
83
- return this._commitment
84
- }
85
-
86
- /**
87
- * Generates a signature for a given message using the private key.
88
- * This method demonstrates how to sign a message and could be used
89
- * for authentication or data integrity.
90
- *
91
- * @example
92
- * const identity = new Identity()
93
- * const signature = identity.signMessage("message")
94
- *
95
- * @param message The message to be signed.
96
- * @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
97
- */
98
- public signMessage(message: BigNumberish): Signature<string> {
99
- return signMessage(this.privateKey, message)
100
- }
101
-
102
- /**
103
- * Verifies a signature against a given message and public key.
104
- * This static method allows for the verification of signatures without needing
105
- * an instance of the Identity class. It's useful for cases where you only have
106
- * the public key, the message and a signature, and need to verify if they match.
107
- *
108
- * @example
109
- * const identity = new Identity()
110
- * const signature = identity.signMessage("message")
111
- * Identity.verifySignature("message", signature, identity.publicKey)
112
- *
113
- * @param message The message that was signed.
114
- * @param signature The signature to verify.
115
- * @param publicKey The public key to use for verification.
116
- * @returns A boolean indicating whether the signature is valid.
117
- */
118
- static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
119
- return verifySignature(message, signature, publicKey)
120
- }
121
- }
@@ -1,8 +0,0 @@
1
- /* istanbul ignore file */
2
- import { bytesToBigint } from "./bytes-to-bigint"
3
-
4
- export function randomNumber(): bigint {
5
- const bytes = crypto.getRandomValues(new Uint8Array(32))
6
-
7
- return bytesToBigint(bytes)
8
- }
@@ -1,9 +0,0 @@
1
- import { randomBytes } from "node:crypto"
2
-
3
- export function randomNumber(): bigint {
4
- const bytes = randomBytes(32)
5
-
6
- const hex = `0x${bytes.toString("hex")}`
7
-
8
- return BigInt(hex)
9
- }