@decryption/curves 0.2.0

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.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/abstract/bls.d.ts +410 -0
  4. package/abstract/bls.js +433 -0
  5. package/abstract/curve.d.ts +441 -0
  6. package/abstract/curve.js +622 -0
  7. package/abstract/edwards.d.ts +319 -0
  8. package/abstract/edwards.js +697 -0
  9. package/abstract/fft.d.ts +438 -0
  10. package/abstract/fft.js +580 -0
  11. package/abstract/frost.d.ts +292 -0
  12. package/abstract/frost.js +705 -0
  13. package/abstract/hash-to-curve.d.ts +238 -0
  14. package/abstract/hash-to-curve.js +353 -0
  15. package/abstract/modular.d.ts +550 -0
  16. package/abstract/modular.js +872 -0
  17. package/abstract/montgomery.d.ts +95 -0
  18. package/abstract/montgomery.js +180 -0
  19. package/abstract/oprf.d.ts +354 -0
  20. package/abstract/oprf.js +358 -0
  21. package/abstract/poseidon.d.ts +198 -0
  22. package/abstract/poseidon.js +471 -0
  23. package/abstract/tower.d.ts +213 -0
  24. package/abstract/tower.js +963 -0
  25. package/abstract/weierstrass.d.ts +587 -0
  26. package/abstract/weierstrass.js +1421 -0
  27. package/bls12-381.d.ts +23 -0
  28. package/bls12-381.js +680 -0
  29. package/bn254.d.ts +74 -0
  30. package/bn254.js +248 -0
  31. package/ed25519.d.ts +211 -0
  32. package/ed25519.js +626 -0
  33. package/ed448.d.ts +184 -0
  34. package/ed448.js +596 -0
  35. package/esm/abstract/bls.js +429 -0
  36. package/esm/abstract/curve.js +610 -0
  37. package/esm/abstract/edwards.js +691 -0
  38. package/esm/abstract/fft.js +567 -0
  39. package/esm/abstract/frost.js +702 -0
  40. package/esm/abstract/hash-to-curve.js +345 -0
  41. package/esm/abstract/modular.js +849 -0
  42. package/esm/abstract/montgomery.js +177 -0
  43. package/esm/abstract/oprf.js +355 -0
  44. package/esm/abstract/poseidon.js +462 -0
  45. package/esm/abstract/tower.js +925 -0
  46. package/esm/abstract/weierstrass.js +1411 -0
  47. package/esm/bls12-381.js +677 -0
  48. package/esm/bn254.js +244 -0
  49. package/esm/ed25519.js +622 -0
  50. package/esm/ed448.js +593 -0
  51. package/esm/index.js +38 -0
  52. package/esm/misc.js +211 -0
  53. package/esm/nist.js +266 -0
  54. package/esm/secp256k1.js +465 -0
  55. package/esm/utils.js +597 -0
  56. package/esm/webcrypto.js +362 -0
  57. package/index.d.ts +0 -0
  58. package/index.js +38 -0
  59. package/misc.d.ts +113 -0
  60. package/misc.js +216 -0
  61. package/nist.d.ts +136 -0
  62. package/nist.js +269 -0
  63. package/package.json +46 -0
  64. package/secp256k1.d.ts +152 -0
  65. package/secp256k1.js +468 -0
  66. package/utils.d.ts +535 -0
  67. package/utils.js +626 -0
  68. package/webcrypto.d.ts +240 -0
  69. package/webcrypto.js +365 -0
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Montgomery curve methods. It's not really whole montgomery curve,
3
+ * just bunch of very specific methods for X25519 / X448 from
4
+ * [RFC 7748](https://www.rfc-editor.org/rfc/rfc7748)
5
+ * @module
6
+ */
7
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
8
+ import { type TArg, type TRet } from '../utils';
9
+ import { type CurveLengths } from './curve';
10
+ /** Curve-specific hooks required to build one X25519/X448 helper. */
11
+ export type MontgomeryOpts = {
12
+ /** Prime field modulus. */
13
+ P: bigint;
14
+ /** RFC 7748 variant name. */
15
+ type: 'x25519' | 'x448';
16
+ /**
17
+ * Clamp or otherwise normalize one scalar byte string before use.
18
+ * @param bytes - Raw secret scalar bytes.
19
+ * @returns Adjusted scalar bytes ready for Montgomery multiplication.
20
+ */
21
+ adjustScalarBytes: (bytes: TArg<Uint8Array>) => TRet<Uint8Array>;
22
+ /**
23
+ * Invert one field element with exponentiation by `p - 2`.
24
+ * @param x - Field element to invert.
25
+ * @returns Multiplicative inverse of `x`.
26
+ */
27
+ powPminus2: (x: bigint) => bigint;
28
+ /**
29
+ * Optional randomness source for `keygen()` and `utils.randomSecretKey()`.
30
+ * Receives the requested byte length and returns fresh random bytes.
31
+ */
32
+ randomBytes?: (bytesLength?: number) => TRet<Uint8Array>;
33
+ };
34
+ /** Public X25519/X448 ECDH API built on a Montgomery ladder. */
35
+ export type MontgomeryECDH = {
36
+ /**
37
+ * Multiply one scalar by one Montgomery `u` coordinate.
38
+ * @param scalar - Secret scalar bytes.
39
+ * @param u - Public Montgomery `u` coordinate.
40
+ * @returns Shared point encoded as bytes.
41
+ */
42
+ scalarMult: (scalar: TArg<Uint8Array>, u: TArg<Uint8Array>) => TRet<Uint8Array>;
43
+ /**
44
+ * Multiply one scalar by the curve base point.
45
+ * @param scalar - Secret scalar bytes.
46
+ * @returns Public key bytes.
47
+ */
48
+ scalarMultBase: (scalar: TArg<Uint8Array>) => TRet<Uint8Array>;
49
+ /**
50
+ * Derive a shared secret from a local secret key and peer public key.
51
+ * @param secretKeyA - Local secret key bytes.
52
+ * @param publicKeyB - Peer public key bytes.
53
+ * Rejects low-order public inputs instead of returning the all-zero shared secret.
54
+ * @returns Shared secret bytes.
55
+ */
56
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>) => TRet<Uint8Array>;
57
+ /**
58
+ * Derive one public key from a secret key.
59
+ * @param secretKey - Secret key bytes.
60
+ * @returns Public key bytes.
61
+ */
62
+ getPublicKey: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;
63
+ /** Utility helpers for secret-key generation. */
64
+ utils: {
65
+ /** Generate one random secret key with the curve's expected byte length. */
66
+ randomSecretKey: () => TRet<Uint8Array>;
67
+ };
68
+ /** Encoded Montgomery base point `u`. */
69
+ GuBytes: TRet<Uint8Array>;
70
+ /** Public lengths for keys and seeds. */
71
+ lengths: CurveLengths;
72
+ /**
73
+ * Generate one random secret/public keypair.
74
+ * @param seed - Optional seed bytes to use instead of random generation.
75
+ * @returns Fresh secret/public keypair.
76
+ */
77
+ keygen: (seed?: TArg<Uint8Array>) => {
78
+ secretKey: TRet<Uint8Array>;
79
+ publicKey: TRet<Uint8Array>;
80
+ };
81
+ };
82
+ /**
83
+ * @param curveDef - Montgomery curve definition.
84
+ * @returns ECDH helper namespace.
85
+ * @throws If the curve definition or derived shared point is invalid. {@link Error}
86
+ * @example
87
+ * Perform one X25519 key exchange through the generic Montgomery helper.
88
+ *
89
+ * ```ts
90
+ * import { x25519 } from '../ed25519';
91
+ * const alice = x25519.keygen();
92
+ * const shared = x25519.getSharedSecret(alice.secretKey, alice.publicKey);
93
+ * ```
94
+ */
95
+ export declare function montgomery(curveDef: TArg<MontgomeryOpts>): TRet<MontgomeryECDH>;
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.montgomery = montgomery;
4
+ /**
5
+ * Montgomery curve methods. It's not really whole montgomery curve,
6
+ * just bunch of very specific methods for X25519 / X448 from
7
+ * [RFC 7748](https://www.rfc-editor.org/rfc/rfc7748)
8
+ * @module
9
+ */
10
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
11
+ const utils_1 = require("../utils");
12
+ const curve_1 = require("./curve");
13
+ const modular_1 = require("./modular");
14
+ const _0n = BigInt(0);
15
+ const _1n = BigInt(1);
16
+ const _2n = BigInt(2);
17
+ function validateOpts(curve) {
18
+ // Validate constructor config eagerly, but do not call user-provided hooks here:
19
+ // `randomBytes` may be transcript-backed or otherwise contextual. Runtime type checks are
20
+ // enough to fail fast on malformed configs without consuming user state.
21
+ (0, utils_1.validateObject)(curve, {
22
+ P: 'bigint',
23
+ type: 'string',
24
+ adjustScalarBytes: 'function',
25
+ powPminus2: 'function',
26
+ }, {
27
+ randomBytes: 'function',
28
+ });
29
+ return Object.freeze({ ...curve });
30
+ }
31
+ /**
32
+ * @param curveDef - Montgomery curve definition.
33
+ * @returns ECDH helper namespace.
34
+ * @throws If the curve definition or derived shared point is invalid. {@link Error}
35
+ * @example
36
+ * Perform one X25519 key exchange through the generic Montgomery helper.
37
+ *
38
+ * ```ts
39
+ * import { x25519 } from '../ed25519';
40
+ * const alice = x25519.keygen();
41
+ * const shared = x25519.getSharedSecret(alice.secretKey, alice.publicKey);
42
+ * ```
43
+ */
44
+ function montgomery(curveDef) {
45
+ const CURVE = validateOpts(curveDef);
46
+ const { P, type, adjustScalarBytes, powPminus2, randomBytes: rand } = CURVE;
47
+ const is25519 = type === 'x25519';
48
+ if (!is25519 && type !== 'x448')
49
+ throw new Error('invalid type');
50
+ const randomBytes_ = rand === undefined ? utils_1.randomBytes : rand;
51
+ const montgomeryBits = is25519 ? 255 : 448;
52
+ const fieldLen = is25519 ? 32 : 56;
53
+ const Gu = is25519 ? BigInt(9) : BigInt(5);
54
+ // RFC 7748 #5:
55
+ // The constant a24 is (486662 - 2) / 4 = 121665 for curve25519/X25519 and
56
+ // (156326 - 2) / 4 = 39081 for curve448/X448
57
+ // const a = is25519 ? 486662n : 156326n;
58
+ const a24 = is25519 ? BigInt(121665) : BigInt(39081);
59
+ // RFC: x25519 "the resulting integer is of the form 2^254 plus
60
+ // eight times a value between 0 and 2^251 - 1 (inclusive)"
61
+ // x448: "2^447 plus four times a value between 0 and 2^445 - 1 (inclusive)"
62
+ const minScalar = is25519 ? _2n ** BigInt(254) : _2n ** BigInt(447);
63
+ const maxAdded = is25519
64
+ ? BigInt(8) * _2n ** BigInt(251) - _1n
65
+ : BigInt(4) * _2n ** BigInt(445) - _1n;
66
+ const maxScalar = minScalar + maxAdded + _1n; // (inclusive)
67
+ const modP = (n) => (0, modular_1.mod)(n, P);
68
+ const GuBytes = encodeU(Gu);
69
+ function encodeU(u) {
70
+ return (0, utils_1.numberToBytesLE)(modP(u), fieldLen);
71
+ }
72
+ function decodeU(u) {
73
+ const _u = (0, utils_1.copyBytes)((0, utils_1.abytes)(u, fieldLen, 'uCoordinate'));
74
+ // RFC: When receiving such an array, implementations of X25519
75
+ // (but not X448) MUST mask the most significant bit in the final byte.
76
+ if (is25519)
77
+ _u[31] &= 127; // 0b0111_1111
78
+ // RFC: Implementations MUST accept non-canonical values and process them as
79
+ // if they had been reduced modulo the field prime. The non-canonical
80
+ // values are 2^255 - 19 through 2^255 - 1 for X25519 and 2^448 - 2^224
81
+ // - 1 through 2^448 - 1 for X448.
82
+ return modP((0, utils_1.bytesToNumberLE)(_u));
83
+ }
84
+ function decodeScalar(scalar) {
85
+ return (0, utils_1.bytesToNumberLE)(adjustScalarBytes((0, utils_1.copyBytes)((0, utils_1.abytes)(scalar, fieldLen, 'scalar'))));
86
+ }
87
+ function scalarMult(scalar, u) {
88
+ const pu = montgomeryLadder(decodeU(u), decodeScalar(scalar));
89
+ // Some public keys are useless, of low-order. Curve author doesn't think
90
+ // it needs to be validated, but we do it nonetheless.
91
+ // https://cr.yp.to/ecdh.html#validate
92
+ if (pu === _0n)
93
+ throw new Error('invalid private or public key received');
94
+ return encodeU(pu);
95
+ }
96
+ // Computes public key from private. By doing scalar multiplication of base point.
97
+ function scalarMultBase(scalar) {
98
+ return scalarMult(scalar, GuBytes);
99
+ }
100
+ const getPublicKey = scalarMultBase;
101
+ const getSharedSecret = scalarMult;
102
+ // cswap from RFC7748 "example code"
103
+ function cswap(swap, x_2, x_3) {
104
+ // dummy = mask(swap) AND (x_2 XOR x_3)
105
+ // Where mask(swap) is the all-1 or all-0 word of the same length as x_2
106
+ // and x_3, computed, e.g., as mask(swap) = 0 - swap.
107
+ const dummy = modP(swap * (x_2 - x_3));
108
+ x_2 = modP(x_2 - dummy); // x_2 = x_2 XOR dummy
109
+ x_3 = modP(x_3 + dummy); // x_3 = x_3 XOR dummy
110
+ return { x_2, x_3 };
111
+ }
112
+ /**
113
+ * Montgomery x-only multiplication ladder for the selected X25519/X448 curve.
114
+ * @param pointU - decoded Montgomery u coordinate for the selected curve
115
+ * @param scalar - decoded clamped scalar by which the point is multiplied
116
+ * @returns resulting Montgomery u coordinate for the selected curve
117
+ */
118
+ function montgomeryLadder(u, scalar) {
119
+ (0, utils_1.aInRange)('u', u, _0n, P);
120
+ (0, utils_1.aInRange)('scalar', scalar, minScalar, maxScalar);
121
+ const k = scalar;
122
+ const x_1 = u;
123
+ let x_2 = _1n;
124
+ let z_2 = _0n;
125
+ let x_3 = u;
126
+ let z_3 = _1n;
127
+ let swap = _0n;
128
+ for (let t = BigInt(montgomeryBits - 1); t >= _0n; t--) {
129
+ const k_t = (k >> t) & _1n;
130
+ swap ^= k_t;
131
+ ({ x_2, x_3 } = cswap(swap, x_2, x_3));
132
+ ({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
133
+ swap = k_t;
134
+ const A = x_2 + z_2;
135
+ const AA = modP(A * A);
136
+ const B = x_2 - z_2;
137
+ const BB = modP(B * B);
138
+ const E = AA - BB;
139
+ const C = x_3 + z_3;
140
+ const D = x_3 - z_3;
141
+ const DA = modP(D * A);
142
+ const CB = modP(C * B);
143
+ const dacb = DA + CB;
144
+ const da_cb = DA - CB;
145
+ x_3 = modP(dacb * dacb);
146
+ z_3 = modP(x_1 * modP(da_cb * da_cb));
147
+ x_2 = modP(AA * BB);
148
+ z_2 = modP(E * (AA + modP(a24 * E)));
149
+ }
150
+ ({ x_2, x_3 } = cswap(swap, x_2, x_3));
151
+ ({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
152
+ const z2 = powPminus2(z_2); // `Fp.pow(x, P - _2n)` is much slower equivalent
153
+ return modP(x_2 * z2); // Return x_2 * (z_2^(p - 2))
154
+ }
155
+ const lengths = {
156
+ secretKey: fieldLen,
157
+ publicKey: fieldLen,
158
+ seed: fieldLen,
159
+ };
160
+ const randomSecretKey = (seed) => {
161
+ seed = seed === undefined ? randomBytes_(fieldLen) : seed;
162
+ (0, utils_1.abytes)(seed, lengths.seed, 'seed');
163
+ // Reuse caller-supplied seed bytes verbatim; clamping is deferred until
164
+ // decodeScalar(...) when the secret key is actually used.
165
+ return seed;
166
+ };
167
+ const utils = { randomSecretKey };
168
+ Object.freeze(lengths);
169
+ Object.freeze(utils);
170
+ return Object.freeze({
171
+ keygen: (0, curve_1.createKeygen)(randomSecretKey, getPublicKey),
172
+ getSharedSecret,
173
+ getPublicKey,
174
+ scalarMult,
175
+ scalarMultBase,
176
+ utils,
177
+ GuBytes: GuBytes.slice(),
178
+ lengths,
179
+ });
180
+ }
@@ -0,0 +1,354 @@
1
+ /**
2
+ * RFC 9497: Oblivious Pseudorandom Functions (OPRFs) Using Prime-Order Groups.
3
+ * https://www.rfc-editor.org/rfc/rfc9497
4
+ *
5
+
6
+ OPRF allows to interactively create an `Output = PRF(Input, serverSecretKey)`:
7
+
8
+ - Server cannot calculate Output by itself: it doesn't know Input
9
+ - Client cannot calculate Output by itself: it doesn't know server secretKey
10
+ - An attacker interception the communication can't restore Input/Output/serverSecretKey and can't
11
+ link Input to some value.
12
+
13
+ ## Issues
14
+
15
+ - Low-entropy inputs (e.g. password '123') enable brute-forced dictionary attacks by the server
16
+ (solveable by domain separation in POPRF)
17
+ - High-level protocol needs to be constructed on top, because OPRF is low-level
18
+
19
+ ## Use cases
20
+
21
+ 1. **Password-Authenticated Key Exchange (PAKE):** Enables secure password login (e.g., OPAQUE)
22
+ without revealing the password to the server.
23
+ 2. **Private Set Intersection (PSI):** Allows two parties to compute the intersection of their
24
+ private sets without revealing non-intersecting elements.
25
+ 3. **Anonymous Credential Systems:** Supports issuance of anonymous, unlinkable credentials
26
+ (e.g., Privacy Pass) using blind OPRF evaluation.
27
+ 4. **Private Information Retrieval (PIR):** Helps users query databases without revealing which
28
+ item they accessed.
29
+ 5. **Encrypted Search / Secure Indexing:** Enables keyword search over encrypted data while keeping
30
+ queries private.
31
+ 6. **Spam Prevention and Rate-Limiting:** Issues anonymous tokens to prevent abuse
32
+ (e.g., CAPTCHA bypass) without compromising user privacy.
33
+
34
+ ## Modes
35
+
36
+ - OPRF: simple mode, client doesn't need to know server public key
37
+ - VOPRF: verifiable mode. It lets the client verify that the server used the
38
+ secret key corresponding to a known public key
39
+ - POPRF: partially oblivious mode, VOPRF + domain separation
40
+
41
+ There is also non-interactive mode (Evaluate), which creates Output
42
+ non-interactively with knowledge of the secret key.
43
+
44
+ Flow:
45
+ - (once) Server generates secret and public keys, distributes public keys to clients
46
+ - deterministically: `deriveKeyPair` or just random: `generateKeyPair`
47
+ - Client blinds input: `blind(secretInput)`
48
+ - Server evaluates blinded input: `blindEvaluate` generated by client, sends result to client
49
+ - Client creates output using result of evaluation via 'finalize'
50
+
51
+ * @module
52
+ */
53
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
54
+ import { randomBytes, type TArg, type TRet } from '../utils';
55
+ import { type CurvePoint, type CurvePointCons } from './curve';
56
+ import { type H2CDSTOpts } from './hash-to-curve';
57
+ /** Serialized group element passed between OPRF participants. */
58
+ export type PointBytes = Uint8Array;
59
+ /** Serialized scalar used for blinds and server secret keys. */
60
+ export type ScalarBytes = Uint8Array;
61
+ /** Arbitrary byte input or output used by the OPRF protocol. */
62
+ export type Bytes = Uint8Array;
63
+ /** Cryptographically secure byte generator used for blinds and proofs. */
64
+ export type RNG = typeof randomBytes;
65
+ /** Curve and hash hooks required to instantiate one OPRF ciphersuite. */
66
+ export type OPRFOpts<P extends CurvePoint<any, P>> = {
67
+ /** Human-readable suite identifier used for domain separation. */
68
+ name: string;
69
+ /**
70
+ * Prime-order group used by the OPRF construction.
71
+ * Kept generic because the suite returns serialized points.
72
+ */
73
+ Point: CurvePointCons<P>;
74
+ /**
75
+ * Hash function used for transcripts, proofs, and outputs.
76
+ * @param msg - Message bytes to hash.
77
+ * @returns Digest bytes.
78
+ */
79
+ hash(msg: TArg<Bytes>): TRet<Bytes>;
80
+ /**
81
+ * Hash arbitrary bytes into one scalar in the suite order.
82
+ * @param msg - Message bytes to map.
83
+ * @param options - Hash-to-field domain-separation options. See {@link H2CDSTOpts}.
84
+ * Implementations MUST treat `msg` and `options` as read-only.
85
+ * @returns Scalar in the suite order.
86
+ */
87
+ hashToScalar(msg: TArg<Uint8Array>, options: TArg<H2CDSTOpts>): bigint;
88
+ /**
89
+ * Hash arbitrary bytes directly onto one curve point.
90
+ * @param msg - Message bytes to map.
91
+ * @param options - Hash-to-curve domain-separation options. See {@link H2CDSTOpts}.
92
+ * Implementations MUST treat `msg` and `options` as read-only.
93
+ * @returns Point on the suite curve.
94
+ */
95
+ hashToGroup(msg: TArg<Uint8Array>, options: TArg<H2CDSTOpts>): P;
96
+ };
97
+ /** Server keypair for one OPRF suite. */
98
+ export type OPRFKeys = {
99
+ /** Secret scalar kept by the server. */
100
+ secretKey: TRet<ScalarBytes>;
101
+ /** Public point distributed to clients in verifiable modes. */
102
+ publicKey: TRet<PointBytes>;
103
+ };
104
+ /** Result of the client-side blind step. */
105
+ export type OPRFBlind = {
106
+ /** Secret blind scalar that the client keeps locally. */
107
+ blind: TRet<ScalarBytes>;
108
+ /** Blinded group element sent to the server. */
109
+ blinded: TRet<PointBytes>;
110
+ };
111
+ /** Server response for one verifiable OPRF evaluation. */
112
+ export type OPRFBlindEval = {
113
+ /** Evaluated group element returned by the server. */
114
+ evaluated: TRet<PointBytes>;
115
+ /** DLEQ proof binding the evaluation to the server public key. */
116
+ proof: TRet<Bytes>;
117
+ };
118
+ /** Server response for a batch of verifiable OPRF evaluations. */
119
+ export type OPRFBlindEvalBatch = {
120
+ /** Evaluated group elements returned for each blinded input. */
121
+ evaluated: TRet<PointBytes[]>;
122
+ /** Batch proof covering all evaluated elements. */
123
+ proof: TRet<Bytes>;
124
+ };
125
+ /** One finalized transcript item used by batch verification helpers. */
126
+ export type OPRFFinalizeItem = {
127
+ /** Original client input. */
128
+ input: Bytes;
129
+ /** Secret blind scalar used for the input. */
130
+ blind: ScalarBytes;
131
+ /** Evaluated point returned by the server. */
132
+ evaluated: PointBytes;
133
+ /** Blinded point originally sent to the server. */
134
+ blinded: PointBytes;
135
+ };
136
+ /** Result of the POPRF client-side blind step with the tweaked server public key. */
137
+ export type OPRFBlindTweaked = OPRFBlind & {
138
+ tweakedKey: TRet<PointBytes>;
139
+ };
140
+ /**
141
+ * Represents a full OPRF ciphersuite implementation according to RFC 9497.
142
+ * This object bundles the three protocol variants (OPRF, VOPRF, POPRF) for a specific
143
+ * prime-order group and hash function combination.
144
+ *
145
+ * @see https://www.rfc-editor.org/rfc/rfc9497.html
146
+ */
147
+ export type OPRF = {
148
+ /**
149
+ * The unique identifier for the ciphersuite, e.g., "ristretto255-SHA512".
150
+ * This name is used for domain separation to prevent cross-protocol attacks.
151
+ */
152
+ readonly name: string;
153
+ /**
154
+ * The base Oblivious Pseudorandom Function (OPRF) mode (mode 0x00).
155
+ * This is a two-party protocol between a client and a server to compute F(k, x)
156
+ * where 'k' is the server's key and 'x' is the client's input.
157
+ *
158
+ * The client learns the output F(k, x) but nothing about 'k'.
159
+ * The server learns nothing about 'x' or F(k, x).
160
+ * This mode is NOT verifiable; the client cannot prove the server used a specific key.
161
+ */
162
+ readonly oprf: {
163
+ /**
164
+ * (Server-side) Generates a new random private/public key pair for the server.
165
+ * @returns A new key pair.
166
+ */
167
+ generateKeyPair(): TRet<OPRFKeys>;
168
+ /**
169
+ * (Server-side) Deterministically derives a private/public key pair from a seed.
170
+ * @param seed - A 32-byte cryptographically secure random seed.
171
+ * @param keyInfo - An optional byte string for domain separation.
172
+ * @returns The derived key pair.
173
+ */
174
+ deriveKeyPair(seed: TArg<Bytes>, keyInfo: TArg<Bytes>): TRet<OPRFKeys>;
175
+ /**
176
+ * (Client-side) The first step of the protocol. The client blinds its private input.
177
+ * @param input - The client's private input bytes.
178
+ * @param rng - An optional cryptographically secure random number generator.
179
+ * @returns An object containing the `blind` scalar (which the client MUST keep secret)
180
+ * and the `blinded` element (which the client sends to the server).
181
+ */
182
+ blind(input: TArg<Bytes>, rng?: RNG): TRet<OPRFBlind>;
183
+ /**
184
+ * (Server-side) The second step. The server evaluates the client's blinded element
185
+ * using its secret key.
186
+ * @param secretKey - The server's private key.
187
+ * @param blinded - The blinded group element received from the client.
188
+ * @returns The evaluated group element, to be sent back to the client.
189
+ */
190
+ blindEvaluate(secretKey: TArg<ScalarBytes>, blinded: TArg<PointBytes>): TRet<PointBytes>;
191
+ /**
192
+ * (Client-side) The final step. The client unblinds the server's response to
193
+ * compute the final OPRF output.
194
+ * @param input - The original private input from the `blind` step.
195
+ * @param blind - The secret scalar from the `blind` step.
196
+ * @param evaluated - The evaluated group element received from the server.
197
+ * @returns The final OPRF output, `Hash(len(input)||input||len(unblinded)||unblinded||"Finalize")`.
198
+ */
199
+ finalize(input: TArg<Bytes>, blind: TArg<ScalarBytes>, evaluated: TArg<PointBytes>): TRet<Bytes>;
200
+ };
201
+ /**
202
+ * The Verifiable Oblivious Pseudorandom Function (VOPRF) mode (mode 0x01).
203
+ * This mode extends the base OPRF by providing a proof that the server used the
204
+ * secret key corresponding to its known public key.
205
+ */
206
+ readonly voprf: {
207
+ /** (Server-side) Generates a key pair for the VOPRF mode. */
208
+ generateKeyPair(): TRet<OPRFKeys>;
209
+ /** (Server-side) Deterministically derives a key pair for the VOPRF mode. */
210
+ deriveKeyPair(seed: TArg<Bytes>, keyInfo: TArg<Bytes>): TRet<OPRFKeys>;
211
+ /** (Client-side) Blinds the client's private input for the VOPRF protocol. */
212
+ blind(input: TArg<Bytes>, rng?: RNG): TRet<OPRFBlind>;
213
+ /**
214
+ * (Server-side) Evaluates the client's blinded element and generates a DLEQ proof
215
+ * of correctness.
216
+ * @param secretKey - The server's private key.
217
+ * @param publicKey - The server's public key, used in proof generation.
218
+ * @param blinded - The blinded group element received from the client.
219
+ * @param rng - An optional cryptographically secure random number generator for the proof.
220
+ * @returns The evaluated element and a proof of correct computation.
221
+ */
222
+ blindEvaluate(secretKey: TArg<ScalarBytes>, publicKey: TArg<PointBytes>, blinded: TArg<PointBytes>, rng?: RNG): TRet<OPRFBlindEval>;
223
+ /**
224
+ * (Server-side) An optimized batch version of `blindEvaluate`. It evaluates multiple
225
+ * blinded elements and produces a single, constant-size proof for the entire batch,
226
+ * amortizing the cost of proof generation.
227
+ * @param secretKey - The server's private key.
228
+ * @param publicKey - The server's public key.
229
+ * @param blinded - An array of blinded group elements from one or more clients.
230
+ * @param rng - An optional cryptographically secure random number generator for the proof.
231
+ * @returns An array of evaluated elements and a single proof for the batch.
232
+ */
233
+ blindEvaluateBatch(secretKey: TArg<ScalarBytes>, publicKey: TArg<PointBytes>, blinded: TArg<PointBytes[]>, rng?: RNG): TRet<OPRFBlindEvalBatch>;
234
+ /**
235
+ * (Client-side) The final step. The client verifies the server's proof, and if valid,
236
+ * unblinds the result to compute the final VOPRF output.
237
+ * @param input - The original private input.
238
+ * @param blind - The secret scalar from the `blind` step.
239
+ * @param evaluated - The evaluated element from the server.
240
+ * @param blinded - The blinded element sent to the server (needed for proof verification).
241
+ * @param publicKey - The server's public key against which the proof is verified.
242
+ * @param proof - The DLEQ proof from the server.
243
+ * @returns The final VOPRF output.
244
+ * @throws If the proof verification fails. {@link Error}
245
+ */
246
+ finalize(input: TArg<Bytes>, blind: TArg<ScalarBytes>, evaluated: TArg<PointBytes>, blinded: TArg<PointBytes>, publicKey: TArg<PointBytes>, proof: TArg<Bytes>): TRet<Bytes>;
247
+ /**
248
+ * (Client-side) The batch-aware version of `finalize`. It verifies a single batch proof
249
+ * against a list of corresponding inputs and outputs.
250
+ * @param items - An array of objects, each containing the parameters for a single finalization.
251
+ * @param publicKey - The server's public key.
252
+ * @param proof - The single DLEQ proof for the entire batch.
253
+ * @returns An array of final VOPRF outputs, one for each item in the input.
254
+ * @throws If the proof verification fails. {@link Error}
255
+ */
256
+ finalizeBatch(items: TArg<OPRFFinalizeItem[]>, publicKey: TArg<PointBytes>, proof: TArg<Bytes>): TRet<Bytes[]>;
257
+ };
258
+ /**
259
+ * A factory for the Partially Oblivious Pseudorandom Function (POPRF) mode (mode 0x02).
260
+ * This mode extends VOPRF to include a public `info` parameter, known to both client and
261
+ * server, which is cryptographically bound to the final output.
262
+ * This is useful for domain separation at the application level.
263
+ * @param info - A public byte string to be mixed into the computation.
264
+ * @returns An object with the POPRF protocol functions.
265
+ */
266
+ readonly poprf: (info: TArg<Bytes>) => {
267
+ /** (Server-side) Generates a key pair for the POPRF mode. */
268
+ generateKeyPair(): TRet<OPRFKeys>;
269
+ /** (Server-side) Deterministically derives a key pair for the POPRF mode. */
270
+ deriveKeyPair(seed: TArg<Bytes>, keyInfo: TArg<Bytes>): TRet<OPRFKeys>;
271
+ /**
272
+ * (Client-side) Blinds the client's private input and computes the "tweaked key".
273
+ * The tweaked key is a public value derived from the server's public key and the public `info`.
274
+ * @param input - The client's private input.
275
+ * @param publicKey - The server's public key.
276
+ * @param rng - An optional cryptographically secure random number generator.
277
+ * @returns The `blind`, `blinded` element, and the `tweakedKey`
278
+ * the client uses for verification.
279
+ */
280
+ blind(input: TArg<Bytes>, publicKey: TArg<PointBytes>, rng?: RNG): TRet<OPRFBlindTweaked>;
281
+ /**
282
+ * (Server-side) Evaluates the blinded element using a key derived from
283
+ * its secret key and the public `info`.
284
+ * It generates a DLEQ proof against the tweaked key.
285
+ * @param secretKey - The server's private key.
286
+ * @param blinded - The blinded element from the client.
287
+ * @param rng - An optional RNG for the proof.
288
+ * @returns The evaluated element and a proof of correct computation.
289
+ */
290
+ blindEvaluate(secretKey: TArg<ScalarBytes>, blinded: TArg<PointBytes>, rng?: RNG): TRet<OPRFBlindEval>;
291
+ /**
292
+ * (Server-side) A batch-aware version of `blindEvaluate` for the POPRF mode.
293
+ * @param secretKey - The server's private key.
294
+ * @param blinded - An array of blinded elements.
295
+ * @param rng - An optional RNG for the proof.
296
+ * @returns An array of evaluated elements and a single proof for the batch.
297
+ */
298
+ blindEvaluateBatch(secretKey: TArg<ScalarBytes>, blinded: TArg<PointBytes[]>, rng: RNG): TRet<OPRFBlindEvalBatch>;
299
+ /**
300
+ * (Client-side) A batch-aware version of `finalize` for the POPRF mode.
301
+ * It verifies the proof against the tweaked key.
302
+ * @param items - An array containing the parameters for each finalization.
303
+ * @param proof - The single DLEQ proof for the batch.
304
+ * @param tweakedKey - The tweaked key corresponding to the proof.
305
+ * All items must share the same `info` and `publicKey`.
306
+ * @returns An array of final POPRF outputs.
307
+ * @throws If proof verification fails. {@link Error}
308
+ */
309
+ finalizeBatch(items: TArg<OPRFFinalizeItem[]>, proof: TArg<Bytes>, tweakedKey: TArg<PointBytes>): TRet<Bytes[]>;
310
+ /**
311
+ * (Client-side) Finalizes the POPRF protocol. It verifies the server's proof against the
312
+ * `tweakedKey` computed in the `blind` step. The final output is bound to the public `info`.
313
+ * @param input - The original private input.
314
+ * @param blind - The secret scalar.
315
+ * @param evaluated - The evaluated element from the server.
316
+ * @param blinded - The blinded element sent to the server.
317
+ * @param proof - The DLEQ proof from the server.
318
+ * @param tweakedKey - The public tweaked key computed by the client during the `blind` step.
319
+ * @returns The final POPRF output.
320
+ * @throws If proof verification fails. {@link Error}
321
+ */
322
+ finalize(input: TArg<Bytes>, blind: TArg<ScalarBytes>, evaluated: TArg<PointBytes>, blinded: TArg<PointBytes>, proof: TArg<Bytes>, tweakedKey: TArg<PointBytes>): TRet<Bytes>;
323
+ /**
324
+ * A non-interactive evaluation function for an entity that knows all inputs.
325
+ * Computes the final POPRF output directly. Useful for testing or specific applications
326
+ * where the server needs to compute the output for a known input.
327
+ * @param secretKey - The server's private key.
328
+ * @param input - The client's private input.
329
+ * @returns The final POPRF output.
330
+ */
331
+ evaluate(secretKey: TArg<ScalarBytes>, input: TArg<Bytes>): TRet<Bytes>;
332
+ };
333
+ };
334
+ /**
335
+ * @param opts - OPRF ciphersuite options. See {@link OPRFOpts}.
336
+ * @returns OPRF helper namespace.
337
+ * @example
338
+ * Instantiate an OPRF suite from curve-specific hashing hooks.
339
+ *
340
+ * ```ts
341
+ * import { createOPRF } from './oprf';
342
+ * import { p256, p256_hasher } from '../nist';
343
+ * import { sha256 } from '@decryption/hashes/sha2';
344
+ * const oprf = createOPRF({
345
+ * name: 'P256-SHA256',
346
+ * Point: p256.Point,
347
+ * hash: sha256,
348
+ * hashToGroup: p256_hasher.hashToCurve,
349
+ * hashToScalar: p256_hasher.hashToScalar,
350
+ * });
351
+ * const keys = oprf.oprf.generateKeyPair();
352
+ * ```
353
+ */
354
+ export declare function createOPRF<P extends CurvePoint<any, P>>(opts: OPRFOpts<P>): TRet<OPRF>;