Skip to content

Commit 6d7ac70

Browse files
committed
chore: wip
1 parent 1fa2150 commit 6d7ac70

19 files changed

Lines changed: 281 additions & 215 deletions

File tree

.vscode/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ preinstall
6464
primeinc
6565
quickfix
6666
rootca
67+
RSAES
6768
scalarbase
6869
scalarmult
6970
shikijs

src/algorithms/asymmetric/ed25519.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
* https://github.com/dchest/tweetnacl-js
88
*/
99

10+
import { sha512 as sha } from '../../algorithms/hash/sha512'
1011
import { asn1 } from '../../encoding/asn1'
11-
import { asn1Validator } from '../../validators/asn1-validator'
1212
import { oids } from '../../oids'
13-
import { getBytesSync } from '../../utils/random'
14-
import { sha512 as sha } from '../../algorithms/hash/sha512'
1513
import { ByteBuffer, ByteStringBuffer } from '../../utils'
14+
import { getBytesSync } from '../../utils/random'
15+
import { asn1Validator } from '../../validators/asn1-validator'
1616

1717
const publicKeyValidator = asn1Validator.ans1PublicKeyValidator
1818
const privateKeyValidator = asn1Validator.ans1PrivateKeyValidator

src/algorithms/asymmetric/jsbn.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -648,18 +648,22 @@ export class BigInteger {
648648
let i = x.getLowestSetBit()
649649
let g = y.getLowestSetBit()
650650

651-
if (g < 0) return x
651+
if (g < 0)
652+
return x
652653

653-
if (i < g) g = i
654+
if (i < g)
655+
g = i
654656

655657
if (g > 0) {
656658
x.rShiftTo(g, x)
657659
y.rShiftTo(g, y)
658660
}
659661

660662
while (x.signum() > 0) {
661-
if ((i = x.getLowestSetBit()) > 0) x.rShiftTo(i, x)
662-
if ((i = y.getLowestSetBit()) > 0) y.rShiftTo(i, y)
663+
if ((i = x.getLowestSetBit()) > 0)
664+
x.rShiftTo(i, x)
665+
if ((i = y.getLowestSetBit()) > 0)
666+
y.rShiftTo(i, y)
663667

664668
if (x.compareTo(y) >= 0) {
665669
x.subTo(y, x)
@@ -671,7 +675,8 @@ export class BigInteger {
671675
}
672676
}
673677

674-
if (g > 0) y.lShiftTo(g, y)
678+
if (g > 0)
679+
y.lShiftTo(g, y)
675680
return y
676681
}
677682

@@ -1080,7 +1085,7 @@ export class BigInteger {
10801085
private exp(e: number, z: IReducer): BigInteger {
10811086
if (e > 0xFFFFFFFF || e < 1)
10821087
return BigInteger.ONE
1083-
let r = new BigInteger()
1088+
let r = new BigInteger(1)
10841089
let r2 = new BigInteger()
10851090
const g = z.convert(this)
10861091
let i = this.nbits(e) - 1
@@ -1099,6 +1104,43 @@ export class BigInteger {
10991104
}
11001105
return z.revert(r)
11011106
}
1107+
1108+
public modInverse(m: BigInteger): BigInteger {
1109+
const g = this.gcd(m)
1110+
if (!g.equals(BigInteger.ONE)) {
1111+
throw new Error('Inverse does not exist')
1112+
}
1113+
1114+
const u = this.mod(m)
1115+
const a = new BigInteger()
1116+
const b = new BigInteger()
1117+
this.extendedGcd(u, m, a, b)
1118+
1119+
return a.mod(m)
1120+
}
1121+
1122+
private extendedGcd(a: BigInteger, b: BigInteger, x: BigInteger, y: BigInteger): BigInteger {
1123+
if (b.equals(BigInteger.ZERO)) {
1124+
x.fromInt(1)
1125+
y.fromInt(0)
1126+
return a
1127+
}
1128+
1129+
const x1 = new BigInteger()
1130+
const y1 = new BigInteger()
1131+
const g = this.extendedGcd(b, a.mod(b), x1, y1)
1132+
1133+
// Calculate x = y1
1134+
x.fromInt(y1.intValue())
1135+
1136+
// Calculate y = x1 - (a/b) * y1
1137+
const quotient = a.divide(b)
1138+
const product = quotient.multiply(y1)
1139+
const diff = x1.subtract(product)
1140+
y.fromInt(diff.intValue())
1141+
1142+
return g
1143+
}
11021144
}
11031145

11041146
// Constants
@@ -1257,4 +1299,3 @@ class Barrett implements IReducer {
12571299
this.reduce(r)
12581300
}
12591301
}
1260-

src/algorithms/asymmetric/prime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* @author Chris Breuer
66
*/
77

8-
import { BigInteger } from './jsbn'
98
import { estimateCores, random } from '../../utils'
9+
import { BigInteger } from './jsbn'
1010

1111
// primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
1212
const GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2]

0 commit comments

Comments
 (0)