Skip to content

Commit 634bedc

Browse files
tniessentargos
authored andcommitted
doc,test: fix prime generation description
The previous description incorrectly explained the behavior of options.add and options.rem for primes that are not safe. PR-URL: #37085 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 6940252 commit 634bedc

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

doc/api/crypto.md

+26-16
Original file line numberDiff line numberDiff line change
@@ -2757,13 +2757,18 @@ Generates a pseudo-random prime of `size` bits.
27572757
If `options.safe` is `true`, the prime will be a safe prime -- that is,
27582758
`(prime - 1) / 2` will also be a prime.
27592759

2760-
If `options.add` and `options.rem` are set, the prime will satisfy the
2761-
condition that `prime % add = rem`. The `options.rem` is ignored if
2762-
`options.add` is not given. If `options.safe` is `true`, `options.add`
2763-
is given, and `options.rem` is `undefined`, then the prime generated
2764-
will satisfy the condition `prime % add = 3`. Otherwise if `options.safe`
2765-
is `false` and `options.rem` is `undefined`, `options.add` will be
2766-
ignored.
2760+
The `options.add` and `options.rem` parameters can be used to enforce additional
2761+
requirements, e.g., for Diffie-Hellman:
2762+
2763+
* If `options.add` and `options.rem` are both set, the prime will satisfy the
2764+
condition that `prime % add = rem`.
2765+
* If only `options.add` is set and `options.safe` is not `true`, the prime will
2766+
satisfy the condition that `prime % add = 1`.
2767+
* If only `options.add` is set and `options.safe` is set to `true`, the prime
2768+
will instead satisfy the condition that `prime % add = 3`. This is necessary
2769+
because `prime % add = 1` for `options.add > 2` would contradict the condition
2770+
enforced by `options.safe`.
2771+
* `options.rem` is ignored if `options.add` is not given.
27672772

27682773
Both `options.add` and `options.rem` must be encoded as big-endian sequences
27692774
if given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or
@@ -2790,15 +2795,20 @@ added: REPLACEME
27902795
Generates a pseudo-random prime of `size` bits.
27912796

27922797
If `options.safe` is `true`, the prime will be a safe prime -- that is,
2793-
`(prime - 1)` / 2 will also be a prime.
2794-
2795-
If `options.add` and `options.rem` are set, the prime will satisfy the
2796-
condition that `prime % add = rem`. The `options.rem` is ignored if
2797-
`options.add` is not given. If `options.safe` is `true`, `options.add`
2798-
is given, and `options.rem` is `undefined`, then the prime generated
2799-
will satisfy the condition `prime % add = 3`. Otherwise if `options.safe`
2800-
is `false` and `options.rem` is `undefined`, `options.add` will be
2801-
ignored.
2798+
`(prime - 1) / 2` will also be a prime.
2799+
2800+
The `options.add` and `options.rem` parameters can be used to enforce additional
2801+
requirements, e.g., for Diffie-Hellman:
2802+
2803+
* If `options.add` and `options.rem` are both set, the prime will satisfy the
2804+
condition that `prime % add = rem`.
2805+
* If only `options.add` is set and `options.safe` is not `true`, the prime will
2806+
satisfy the condition that `prime % add = 1`.
2807+
* If only `options.add` is set and `options.safe` is set to `true`, the prime
2808+
will instead satisfy the condition that `prime % add = 3`. This is necessary
2809+
because `prime % add = 1` for `options.add > 2` would contradict the condition
2810+
enforced by `options.safe`.
2811+
* `options.rem` is ignored if `options.add` is not given.
28022812

28032813
Both `options.add` and `options.rem` must be encoded as big-endian sequences
28042814
if given as an `ArrayBuffer`, `SharedArrayBuffer`, `TypedArray`, `Buffer`, or

test/parallel/test-crypto-prime.js

+24
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ generatePrime(
135135
assert.strictEqual(val % add, rem);
136136
}
137137

138+
{
139+
// The behavior when specifying only add without rem should depend on the
140+
// safe option.
141+
142+
if (process.versions.openssl >= '1.1.1f') {
143+
generatePrime(128, {
144+
bigint: true,
145+
add: 5n
146+
}, common.mustSucceed((prime) => {
147+
assert(checkPrimeSync(prime));
148+
assert.strictEqual(prime % 5n, 1n);
149+
}));
150+
151+
generatePrime(128, {
152+
bigint: true,
153+
safe: true,
154+
add: 5n
155+
}, common.mustSucceed((prime) => {
156+
assert(checkPrimeSync(prime));
157+
assert.strictEqual(prime % 5n, 3n);
158+
}));
159+
}
160+
}
161+
138162
[1, 'hello', {}, []].forEach((i) => {
139163
assert.throws(() => checkPrime(i), {
140164
code: 'ERR_INVALID_ARG_TYPE'

0 commit comments

Comments
 (0)