GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — crypto: add generatePrime/checkPrime by jasnell · Pull Request #36997 · nodejs/node

Conversation

@jasnell

@jasnell jasnell commented Jan 19, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

APIs for generating and checking pseudo-random primes.

Scratching an itch here. For a set of crypto benchmarks that I've been running I've needed the ability to generate prime test vectors but always had to rely on an external module. Which is silly since openssl has this built in and quite easy to expose.

One of the use cases is to generate primes for diffie hellman with a bit more control over the generation parameters.

crypto.generatePrime(32, {
  safe: true,
  add: 12n,
  rem: 11n,
}, (err, prime) => {
  const dh = crypto.createDiffieHellman(prime);
});

The crypto.checkPrime() does exactly what its name suggest... verifies that an input is a prime within a reasonable margin of error.

Signed-off-by: James M Snell [email protected]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss, jimmiehansson, ryzokuken, danilofuchs, and thibmeu reacted with thumbs up emoji
All reactions
  • 👍 5 reactions
@jasnell jasnell added crypto Issues and PRs related to the crypto subsystem. semver-minor PRs that contain new features and should be released in the next minor version. labels Jan 19, 2021
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. labels Jan 19, 2021
@nodejs-github-bot

This comment has been minimized.

Sign in to view
@nodejs-github-bot

nodejs-github-bot commented Jan 19, 2021 •
edited by jasnell
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/35636/ 💚

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@panva

panva commented Jan 19, 2021

Copy link
Copy Markdown
Member

cc @nodejs/crypto

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

targos
targos reviewed Jan 19, 2021
View reviewed changes
Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jasnell
jasnell force-pushed the random-prime branch from cda64b6 to 6b49bc9 Compare January 22, 2021 19:29
@jasnell

jasnell commented Jan 22, 2021

Copy link
Copy Markdown
Member Author

The PR has been updated to split checkPrime() into sync and async variants. The check prime operation can take a while based on the size of the prime candidate and the number of checks so moving that off loop makes the most sense.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

This comment has been minimized.

Sign in to view
@nodejs-github-bot

This comment has been minimized.

Sign in to view
@nodejs-github-bot

nodejs-github-bot commented Jan 22, 2021

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/35684/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell jasnell added the review wanted PRs that need review. label Jan 22, 2021
@nodejs-github-bot

nodejs-github-bot commented Jan 22, 2021 •
edited by jasnell
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/35685/ 💚

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tniessen
tniessen reviewed Jan 23, 2021
View reviewed changes

@tniessen tniessen left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using ArrayBuffer as the primary representation of prime numbers might be WebCrypto-ish, but it doesn't feel JavaScript-ish or Node.js-ish to me. Both OpenSSL and JavaScript have a BigInt/BIGNUM type, and I don't think our new APIs should steer away from that fact. While static representations of keys typically encode numbers as byte sequences, programmers should have access to types with better semantics. (Sure, higher-level types don't provide memory safety etc., but... it's JavaScript.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

tniessen
tniessen requested changes Jan 23, 2021
View reviewed changes
Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/crypto/crypto_random.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/crypto/crypto_random.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jasnell
jasnell force-pushed the random-prime branch 2 times, most recently from 688eeaa to f16510f Compare January 23, 2021 19:29
@jasnell
jasnell requested a review from tniessen January 23, 2021 19:54
@jasnell
jasnell force-pushed the random-prime branch from f16510f to 0e13f36 Compare January 23, 2021 21:14
@nodejs-github-bot

This comment has been minimized.

Sign in to view
@jasnell
jasnell force-pushed the random-prime branch from 0e13f36 to a21eb2f Compare January 24, 2021 17:16
@nodejs-github-bot

nodejs-github-bot commented Jan 24, 2021 •
edited by jasnell
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/35709/ 💚

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell
jasnell requested a review from targos January 24, 2021 17:22
jasnell
jasnell commented Jan 25, 2021
View reviewed changes
Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

jasnell
jasnell commented Jan 25, 2021
View reviewed changes
Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jasnell
jasnell force-pushed the random-prime branch 2 times, most recently from 17e8dd6 to 6ce845a Compare January 25, 2021 15:28
@jasnell jasnell changed the title crypto: add randomPrime/randomPrimeSync/checkPrime crypto: add generatePrime/checkPrime Jan 25, 2021
@jasnell

jasnell commented Jan 25, 2021

Copy link
Copy Markdown
Member Author

Update: I've renamed the function to generatePrime()/generatePrimeSync() to avoid some name bikeshedding debates later on

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tniessen
tniessen reviewed Jan 25, 2021
View reviewed changes
Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread doc/api/crypto.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/crypto/crypto_random.cc Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jasnell
crypto: add generatePrime/checkPrime
00f5546
APIs for generating and checking pseudo-random primes

Signed-off-by: James M Snell <[email protected]>
@jasnell
jasnell force-pushed the random-prime branch from cc70379 to 00f5546 Compare January 25, 2021 23:05
@jasnell
jasnell requested a review from tniessen January 25, 2021 23:06
@nodejs-github-bot

nodejs-github-bot commented Jan 25, 2021 •
edited by jasnell
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/35736/ 💚

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tniessen
tniessen approved these changes Jan 26, 2021
View reviewed changes
@tniessen

tniessen commented Jan 26, 2021

Copy link
Copy Markdown
Member

LGTM. I think it's great to support BigInt, and I understand the hesitation to use BigInt as the default output format even though I personally believe it would be a better fit. rem and add likely don't need to be in secure memory, but they are short-lived, so it doesn't matter much.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell jasnell removed the review wanted PRs that need review. label Jan 26, 2021
@jasnell

jasnell commented Jan 26, 2021

Copy link
Copy Markdown
Member Author

Landed in bb13469

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell jasnell closed this Jan 26, 2021
jasnell added a commit that referenced this pull request Jan 26, 2021
@jasnell
crypto: add generatePrime/checkPrime
bb13469
APIs for generating and checking pseudo-random primes

Signed-off-by: James M Snell <[email protected]>

PR-URL: #36997
Reviewed-By: Tobias Nießen <[email protected]>
@tniessen tniessen mentioned this pull request Feb 1, 2021
src: simplify calls to BN_bin2bn in prime gen #37169
Closed
targos pushed a commit that referenced this pull request Feb 2, 2021
@jasnell @targos
crypto: add generatePrime/checkPrime
110063d
APIs for generating and checking pseudo-random primes

Signed-off-by: James M Snell <[email protected]>

PR-URL: #36997
Reviewed-By: Tobias Nießen <[email protected]>
@targos targos mentioned this pull request Feb 2, 2021
v15.8.0 release proposal #37183
Merged
targos added a commit that referenced this pull request Feb 2, 2021
@targos
2021-02-02, Version 15.8.0 (Current)
5a59dde
Notable changes:

crypto:
  * (SEMVER-MINOR) add generatePrime/checkPrime (James M Snell) #36997
  * (SEMVER-MINOR) experimental (Ed/X)25519/(Ed/X)448 support (James M Snell) #36879
deps:
  * upgrade npm to 7.5.0 (Ruy Adorno) #37117
dgram:
  * (SEMVER-MINOR) support AbortSignal in createSocket (Nitzan Uziely) #37026
doc:
  * add Zijian Liu to collaborators (ZiJian Liu) #37075
esm:
  * deprecate legacy main lookup for modules (Guy Bedford) #36918
readline:
  * (SEMVER-MINOR) add history event and option to set initial history (Mattias Runge-Broberg) #33662
  * (SEMVER-MINOR) add support for the AbortController to the question method (Mattias Runge-Broberg) #33676

PR-URL: #37183
targos added a commit that referenced this pull request Feb 2, 2021
@targos
2021-02-02, Version 15.8.0 (Current)
bc1c428
Notable changes:

crypto:
  * (SEMVER-MINOR) add generatePrime/checkPrime (James M Snell) #36997
  * (SEMVER-MINOR) experimental (Ed/X)25519/(Ed/X)448 support (James M Snell) #36879
deps:
  * upgrade npm to 7.5.0 (Ruy Adorno) #37117
dgram:
  * (SEMVER-MINOR) support AbortSignal in createSocket (Nitzan Uziely) #37026
doc:
  * add Zijian Liu to collaborators (ZiJian Liu) #37075
esm:
  * deprecate legacy main lookup for modules (Guy Bedford) #36918
readline:
  * (SEMVER-MINOR) add history event and option to set initial history (Mattias Runge-Broberg) #33662
  * (SEMVER-MINOR) add support for the AbortController to the question method (Mattias Runge-Broberg) #33676

PR-URL: #37183
targos added a commit that referenced this pull request Feb 2, 2021
@targos
2021-02-02, Version 15.8.0 (Current)
211574b
Notable changes:

crypto:
  * (SEMVER-MINOR) add generatePrime/checkPrime (James M Snell) #36997
  * (SEMVER-MINOR) experimental (Ed/X)25519/(Ed/X)448 support (James M Snell) #36879
deps:
  * upgrade npm to 7.5.0 (Ruy Adorno) #37117
dgram:
  * (SEMVER-MINOR) support AbortSignal in createSocket (Nitzan Uziely) #37026
doc:
  * add Zijian Liu to collaborators (ZiJian Liu) #37075
esm:
  * deprecate legacy main lookup for modules (Guy Bedford) #36918
readline:
  * (SEMVER-MINOR) add history event and option to set initial history (Mattias Runge-Broberg) #33662
  * (SEMVER-MINOR) add support for the AbortController to the question method (Mattias Runge-Broberg) #33676

PR-URL: #37183
@targos targos added dont-land-on-v12.x labels Aug 8, 2021
tniessen added a commit to tniessen/node that referenced this pull request Mar 17, 2023
@tniessen
test: fix 'checks' validation test for checkPrime
2416f78
This test had two problems:

* The first argument was a number in both cases, which is what caused
  the (expected) ERR_INVALID_ARG_TYPE error -- the validity of the
  'checks' option was not actually verified at all. Thus, the first
  argument should be valid for this particular test.
* The function returned by common.mustNotCall() was passed to
  assert.throws() as a third argument instead of being passed to
  checkPrime() as a third argument. (Isn't JavaScript great?) This again
  led to the (expected) ERR_INVALID_ARG_TYPE error, but again, the
  validity of the 'checks' option was not verified.

Fix both issues by ensuring that all arguments except the 'checks'
option are valid.

Refs: nodejs#36997
@tniessen tniessen mentioned this pull request Mar 17, 2023
test: fix 'checks' validation test for checkPrime #47139
Merged
nodejs-github-bot pushed a commit that referenced this pull request Mar 19, 2023
@tniessen
test: fix 'checks' validation test for checkPrime
fbd526b
This test had two problems:

* The first argument was a number in both cases, which is what caused
  the (expected) ERR_INVALID_ARG_TYPE error -- the validity of the
  'checks' option was not actually verified at all. Thus, the first
  argument should be valid for this particular test.
* The function returned by common.mustNotCall() was passed to
  assert.throws() as a third argument instead of being passed to
  checkPrime() as a third argument. (Isn't JavaScript great?) This again
  led to the (expected) ERR_INVALID_ARG_TYPE error, but again, the
  validity of the 'checks' option was not verified.

Fix both issues by ensuring that all arguments except the 'checks'
option are valid.

Refs: #36997
PR-URL: #47139
Reviewed-By: Filip Skokan <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
RafaelGSS pushed a commit that referenced this pull request Apr 5, 2023
@tniessen @RafaelGSS
test: fix 'checks' validation test for checkPrime
fb08d74
This test had two problems:

* The first argument was a number in both cases, which is what caused
  the (expected) ERR_INVALID_ARG_TYPE error -- the validity of the
  'checks' option was not actually verified at all. Thus, the first
  argument should be valid for this particular test.
* The function returned by common.mustNotCall() was passed to
  assert.throws() as a third argument instead of being passed to
  checkPrime() as a third argument. (Isn't JavaScript great?) This again
  led to the (expected) ERR_INVALID_ARG_TYPE error, but again, the
  validity of the 'checks' option was not verified.

Fix both issues by ensuring that all arguments except the 'checks'
option are valid.

Refs: #36997
PR-URL: #47139
Reviewed-By: Filip Skokan <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
RafaelGSS pushed a commit that referenced this pull request Apr 7, 2023
@tniessen @RafaelGSS
test: fix 'checks' validation test for checkPrime
45e7b10
This test had two problems:

* The first argument was a number in both cases, which is what caused
  the (expected) ERR_INVALID_ARG_TYPE error -- the validity of the
  'checks' option was not actually verified at all. Thus, the first
  argument should be valid for this particular test.
* The function returned by common.mustNotCall() was passed to
  assert.throws() as a third argument instead of being passed to
  checkPrime() as a third argument. (Isn't JavaScript great?) This again
  led to the (expected) ERR_INVALID_ARG_TYPE error, but again, the
  validity of the 'checks' option was not verified.

Fix both issues by ensuring that all arguments except the 'checks'
option are valid.

Refs: #36997
PR-URL: #47139
Reviewed-By: Filip Skokan <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
danielleadams pushed a commit that referenced this pull request Jul 6, 2023
@tniessen @danielleadams
test: fix 'checks' validation test for checkPrime
6fee605
This test had two problems:

* The first argument was a number in both cases, which is what caused
  the (expected) ERR_INVALID_ARG_TYPE error -- the validity of the
  'checks' option was not actually verified at all. Thus, the first
  argument should be valid for this particular test.
* The function returned by common.mustNotCall() was passed to
  assert.throws() as a third argument instead of being passed to
  checkPrime() as a third argument. (Isn't JavaScript great?) This again
  led to the (expected) ERR_INVALID_ARG_TYPE error, but again, the
  validity of the 'checks' option was not verified.

Fix both issues by ensuring that all arguments except the 'checks'
option are valid.

Refs: #36997
PR-URL: #47139
Reviewed-By: Filip Skokan <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tniessen tniessen tniessen approved these changes
@targos targos Awaiting requested review from targos

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@jasnell @nodejs-github-bot @panva @tniessen @targos
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.