Skip to content

Commit 5440920

Browse files
committed
chore: wip
1 parent 4d0613d commit 5440920

3 files changed

Lines changed: 61 additions & 18 deletions

File tree

.vscode/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ postcompile
4747
prefetch
4848
preinstall
4949
quickfix
50+
scalarbase
51+
scalarmult
5052
shikijs
5153
shoutout
5254
socio

src/asn1-validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ export const publicKeyValidator: Asn1Validator = {
9494
}]
9595
}
9696

97-
const validator: ValidatorMap = {
97+
export const asn1Validator: ValidatorMap = {
9898
privateKeyValidator,
9999
publicKeyValidator,
100100
}
101101

102-
export default validator
102+
export default asn1Validator

src/ed25519.ts

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
import { getBytesSync } from './random'
1313
import { ByteBuffer } from './utils'
14+
import { asn1 } from './asn1'
1415
import { asn1Validator } from './asn1-validator'
16+
import { oids } from './oids'
17+
import { sha512 as sha } from './sha512'
1518

1619
const publicKeyValidator = asn1Validator.publicKeyValidator
1720
const privateKeyValidator = asn1Validator.privateKeyValidator
@@ -120,20 +123,20 @@ ed25519.generateKeyPair = function (options) {
120123
ed25519.privateKeyFromAsn1 = function (obj: any) {
121124
const capture = {} as { privateKeyOid?: string, privateKey?: string }
122125
const errors: any[] = []
123-
const valid = forge.asn1.validate(obj, privateKeyValidator, capture, errors)
126+
const valid = asn1.validate(obj, privateKeyValidator, capture, errors)
124127
if (!valid) {
125128
const error = new Error('Invalid Key.') as ExtendedError
126129
error.errors = errors
127130
throw error
128131
}
129-
const oid = forge.asn1.derToOid(capture.privateKeyOid)
130-
const ed25519Oid = forge.oids.EdDSA25519
132+
const oid = asn1.derToOid(capture.privateKeyOid)
133+
const ed25519Oid = oids.EdDSA25519
131134
if (oid !== ed25519Oid) {
132135
throw new Error(`Invalid OID "${oid}"; OID must be "${ed25519Oid}".`)
133136
}
134137
const privateKey = capture.privateKey
135138
const privateKeyBytes = messageToNativeBuffer({
136-
message: forge.asn1.fromDer(privateKey).value,
139+
message: asn1.fromDer(privateKey).value,
137140
encoding: 'binary',
138141
})
139142
return { privateKeyBytes }
@@ -149,21 +152,22 @@ ed25519.privateKeyFromAsn1 = function (obj: any) {
149152
ed25519.publicKeyFromAsn1 = function (obj: any): Buffer | Uint8Array {
150153
const capture = {} as { publicKeyOid?: string, ed25519PublicKey?: Uint8Array }
151154
const errors: any[] = []
152-
const valid = forge.asn1.validate(obj, publicKeyValidator, capture, errors)
155+
const valid = asn1.validate(obj, publicKeyValidator, capture, errors)
153156
if (!valid) {
154157
const error = new Error('Invalid Key.') as ExtendedError
155158
error.errors = errors
156159
throw error
157160
}
158-
const oid = forge.asn1.derToOid(capture.publicKeyOid)
159-
const ed25519Oid = forge.oids.EdDSA25519
161+
const oid = asn1.derToOid(capture.publicKeyOid)
162+
const ed25519Oid = oids.EdDSA25519
160163
if (oid !== ed25519Oid) {
161164
throw new Error(`Invalid OID "${oid}"; OID must be "${ed25519Oid}".`)
162165
}
163166
const publicKeyBytes = capture.ed25519PublicKey
164167
if (!publicKeyBytes || publicKeyBytes.length !== ed25519.constants.PUBLIC_KEY_BYTE_LENGTH) {
165168
throw new Error('Key length is invalid.')
166169
}
170+
167171
return messageToNativeBuffer({
168172
message: publicKeyBytes,
169173
encoding: 'binary',
@@ -240,10 +244,12 @@ ed25519.verify = function (options) {
240244
ed25519.constants.SIGN_BYTE_LENGTH}`,
241245
)
242246
}
247+
243248
const publicKey = messageToNativeBuffer({
244249
message: options.publicKey,
245250
encoding: 'binary',
246251
})
252+
247253
if (publicKey.length !== ed25519.constants.PUBLIC_KEY_BYTE_LENGTH) {
248254
throw new TypeError(
249255
`"options.publicKey" must have a byte length of ${
@@ -253,6 +259,7 @@ ed25519.verify = function (options) {
253259

254260
const sm = new NativeBuffer(ed25519.constants.SIGN_BYTE_LENGTH + msg.length)
255261
const m = new NativeBuffer(ed25519.constants.SIGN_BYTE_LENGTH + msg.length)
262+
256263
let i
257264
for (i = 0; i < ed25519.constants.SIGN_BYTE_LENGTH; ++i) {
258265
sm[i] = sig[i]
@@ -435,7 +442,7 @@ const I = gf([
435442
])
436443

437444
function sha512(msg: Buffer | Uint8Array, msgLen: number): Buffer | Uint8Array {
438-
const md = forge.md.sha512.create()
445+
const md = sha.create()
439446
const buffer = new ByteBuffer()
440447
const msgStr = Buffer.from(msg).toString('binary')
441448
buffer.putString(msgStr)
@@ -545,9 +552,11 @@ function crypto_sign_open(
545552
for (i = 0; i < n; ++i) {
546553
m[i] = sm[i]
547554
}
555+
548556
for (i = 0; i < 32; ++i) {
549557
m[i + 32] = pk[i]
550558
}
559+
551560
const h = sha512(m, n)
552561
reduce(h)
553562
scalarmult(p, q, Array.from(h))
@@ -557,41 +566,52 @@ function crypto_sign_open(
557566
pack(t, p)
558567

559568
n -= 64
569+
560570
if (crypto_verify_32(sm, 0, t, 0)) {
561571
for (i = 0; i < n; ++i) {
562572
m[i] = 0
563573
}
574+
564575
return -1
565576
}
566577

567578
for (i = 0; i < n; ++i) {
568579
m[i] = sm[i + 64]
569580
}
581+
570582
mlen = n
583+
571584
return mlen
572585
}
573586

574587
function modL(r: number[], x: number[]) {
575588
let carry, i, j, k
589+
576590
for (i = 63; i >= 32; --i) {
577591
carry = 0
592+
578593
for (j = i - 32, k = i - 12; j < k; ++j) {
579594
x[j] += carry - 16 * x[i] * L[j - (i - 32)]
580595
carry = (x[j] + 128) >> 8
581596
x[j] -= carry * 256
582597
}
598+
583599
x[j] += carry
584600
x[i] = 0
585601
}
602+
586603
carry = 0
604+
587605
for (j = 0; j < 32; ++j) {
588606
x[j] += carry - (x[31] >> 4) * L[j]
589607
carry = x[j] >> 8
590608
x[j] &= 255
591609
}
610+
592611
for (j = 0; j < 32; ++j) {
593612
x[j] -= carry * L[j]
594613
}
614+
595615
for (i = 0; i < 32; ++i) {
596616
x[i + 1] += x[i] >> 8
597617
r[i] = x[i] & 255
@@ -600,10 +620,12 @@ function modL(r: number[], x: number[]) {
600620

601621
function reduce(r: Buffer | Uint8Array) {
602622
const x = new Float64Array(64)
623+
603624
for (let i = 0; i < 64; ++i) {
604625
x[i] = r[i]
605626
r[i] = 0
606627
}
628+
607629
modL(r, x)
608630
}
609631

@@ -700,44 +722,52 @@ function unpackneg(r: number[], p: number[]) {
700722

701723
S(chk, r[0])
702724
M(chk, chk, den)
725+
703726
if (neq25519(chk, num)) {
704727
M(r[0], r[0], I)
705728
}
706729

707730
S(chk, r[0])
708731
M(chk, chk, den)
732+
709733
if (neq25519(chk, num)) {
710734
return -1
711735
}
712736

713-
if (par25519(r[0]) === (p[31] >> 7)) {
737+
if (par25519(r[0]) === (p[31] >> 7))
714738
Z(r[0], gf0, r[0])
715-
}
716739

717740
M(r[3], r[0], r[1])
741+
718742
return 0
719743
}
720744

721745
function unpack25519(o: number[], n: number[]) {
722746
let i
747+
723748
for (i = 0; i < 16; ++i) {
724749
o[i] = n[2 * i] + (n[2 * i + 1] << 8)
725750
}
751+
726752
o[15] &= 0x7FFF
727753
}
728754

729755
function pow2523(o: number[], i: number[]) {
730756
const c = gf()
757+
731758
let a
732759
for (a = 0; a < 16; ++a) {
733760
c[a] = i[a]
734761
}
762+
735763
for (a = 250; a >= 0; --a) {
736764
S(c, c)
765+
737766
if (a !== 1) {
738767
M(c, c, i)
739768
}
740769
}
770+
741771
for (a = 0; a < 16; ++a) {
742772
o[a] = c[a]
743773
}
@@ -746,8 +776,10 @@ function pow2523(o: number[], i: number[]) {
746776
function neq25519(a: number[], b: number[]) {
747777
const c = new NativeBuffer(32)
748778
const d = new NativeBuffer(32)
779+
749780
pack25519(c, a)
750781
pack25519(d, b)
782+
751783
return crypto_verify_32(c, 0, d, 0)
752784
}
753785

@@ -772,10 +804,12 @@ function par25519(a: number[]) {
772804

773805
function scalarmult(p: number[], q: number[], s: number[]) {
774806
let b, i
807+
775808
set25519(p[0], gf0)
776809
set25519(p[1], gf1)
777810
set25519(p[2], gf1)
778811
set25519(p[3], gf0)
812+
779813
for (i = 255; i >= 0; --i) {
780814
b = (s[(i / 8) | 0] >> (i & 7)) & 1
781815
cswap(p, q, b)
@@ -787,6 +821,7 @@ function scalarmult(p: number[], q: number[], s: number[]) {
787821

788822
function scalarbase(p: number[], s: number[]) {
789823
const q = [gf(), gf(), gf(), gf()]
824+
790825
set25519(q[0], X)
791826
set25519(q[1], Y)
792827
set25519(q[2], gf1)
@@ -807,29 +842,37 @@ function inv25519(o: number[], i: number[]) {
807842
for (a = 0; a < 16; ++a) {
808843
c[a] = i[a]
809844
}
845+
810846
for (a = 253; a >= 0; --a) {
811847
S(c, c)
812848
if (a !== 2 && a !== 4) {
813849
M(c, c, i)
814850
}
815851
}
852+
816853
for (a = 0; a < 16; ++a) {
817854
o[a] = c[a]
818855
}
819856
}
820857

821858
function car25519(o: number[]) {
822-
let i; let v; let c = 1
859+
let i
860+
let v
861+
let c = 1
862+
823863
for (i = 0; i < 16; ++i) {
824864
v = o[i] + c + 65535
825865
c = Math.floor(v / 65536)
826866
o[i] = v - c * 65536
827867
}
868+
828869
o[0] += c - 1 + 37 * (c - 1)
829870
}
830871

831872
function sel25519(p: number[], q: number[], b: number) {
832-
let t; const c = ~(b - 1)
873+
let t
874+
const c = ~(b - 1)
875+
833876
for (let i = 0; i < 16; ++i) {
834877
t = c & (p[i] ^ q[i])
835878
p[i] ^= t
@@ -838,15 +881,13 @@ function sel25519(p: number[], q: number[], b: number) {
838881
}
839882

840883
function A(o: number[], a: number[], b: number[]) {
841-
for (let i = 0; i < 16; ++i) {
884+
for (let i = 0; i < 16; ++i)
842885
o[i] = a[i] + b[i]
843-
}
844886
}
845887

846888
function Z(o: number[], a: number[], b: number[]) {
847-
for (let i = 0; i < 16; ++i) {
889+
for (let i = 0; i < 16; ++i)
848890
o[i] = a[i] - b[i]
849-
}
850891
}
851892

852893
function S(o: number[], a: number[]) {

0 commit comments

Comments
 (0)