Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type to prime and prime.worker messages. #941

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Forge ChangeLog
### Fixed
- [x509] 'Expected' and 'Actual' issuers were backwards in verification failure
message.
- [prime,prime.worker] Add type to messages and ignore unexpected messages.

### Added
- [oid,x509]: Added OID `1.3.14.3.2.29 / sha1WithRSASignature` for sha1 with
Expand Down
6 changes: 6 additions & 0 deletions lib/prime.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ function primeincFindPrimeWithWorkers(bits, rng, options, callback) {
return;
}

// ignore other messages
if(e.data.type !== 'forge.prime.worker.find.response') {
return;
}

--running;
var data = e.data;
if(data.found) {
Expand All @@ -240,6 +245,7 @@ function primeincFindPrimeWithWorkers(bits, rng, options, callback) {

// start prime search
e.target.postMessage({
type: 'forge.prime.worker.find.request',
hex: hex,
workLoad: workLoad
});
Expand Down
19 changes: 16 additions & 3 deletions lib/prime.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ var BIG_TWO = new BigInteger(null);
BIG_TWO.fromInt(2);

self.addEventListener('message', function(e) {
if(e.data.type !== 'forge.prime.worker.find.request') {
return;
}
var result = findPrime(e.data);
self.postMessage(result);
});

// start receiving ranges to check
self.postMessage({found: false});
self.postMessage({
type: 'forge.prime.worker.find.response',
found: false
});

// primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
Expand All @@ -46,13 +52,20 @@ function findPrime(data) {
for(var i = 0; i < workLoad; ++i) {
// do primality test
if(isProbablePrime(num)) {
return {found: true, prime: num.toString(16)};
return {
type: 'forge.prime.worker.find.response',
found: true,
prime: num.toString(16)
};
}
// get next potential prime
num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
}

return {found: false};
return {
type: 'forge.prime.worker.find.response',
found: false
};
}

function isProbablePrime(n) {
Expand Down