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

crypto noble #696

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crypto/key/key.meta.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require \/mol/crypto/noble/curves
92 changes: 25 additions & 67 deletions crypto/key/key.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
namespace $ {

const algorithm = {
name: 'ECDSA',
hash: 'SHA-256',
namedCurve: "P-256",
}


//@ts-ignore
const curves = nobleCurves

export class $mol_crypto_key extends $mol_buffer {

static from< This extends typeof $mol_crypto_key >( this: This, serial: number | string | ArrayBufferView ) {
Expand Down Expand Up @@ -40,32 +37,11 @@ namespace $ {
static size_str = 86
static size_bin = 64

@ $mol_memo.method
async native() {
const str = this.toString()
return $mol_crypto_native.subtle.importKey(
'jwk',
{
crv: "P-256",
ext: true,
key_ops: [ 'verify' ],
kty: "EC",
x: str.slice( 0, 43 ),
y: str.slice( 43, 86 ),
},
algorithm,
true,
[ 'verify' ],
)
}

async verify( data: BufferSource, sign: BufferSource ) {
return await $mol_crypto_native.subtle.verify(
algorithm,
await this.native(),
sign,
data,
)

const pub = this.asArray().subarray( 0, 32 )
return curves.ed25519.verify( sign, data, pub )

}

}
Expand All @@ -76,50 +52,32 @@ namespace $ {
static size_bin = 96
static size_sign = 64


static async generate() {

const pair = await $mol_crypto_native.subtle.generateKey(
algorithm,
true,
[ 'sign', 'verify' ]
)

const { x, y, d } = await $mol_crypto_native.subtle.exportKey( 'jwk', pair.privateKey )
return this.from( x + y! + d! )

}

@ $mol_memo.method
async native() {
const str = this.toString()
return await $mol_crypto_native.subtle.importKey(
'jwk',
{
crv: "P-256",
ext: true,
key_ops: [ 'sign' ],
kty: "EC",
x: str.slice( 0, 43 ),
y: str.slice( 43, 86 ),
d: str.slice( 86, 129 ),
},
algorithm,
true,
[ 'sign' ],
)
const priv = curves.ed25519.utils.randomPrivateKey()
const pub = curves.ed25519.getPublicKey(priv)

const serial = new Uint8Array([
... pub,
... pub,
... priv,
])

return this.from( serial )

}

@ $mol_memo.method
public() {
return new $mol_crypto_key_public( this.buffer, this.byteOffset, this.byteOffset + 64 )
}

async sign( data: BufferSource ) {
return new Uint8Array( await $mol_crypto_native.subtle.sign(
algorithm,
await this.native(),
data
) )

const priv = this.asArray().subarray( 64 )
return curves.ed25519.sign( data, priv )

}

}
Expand Down
Loading
Loading