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

experiment: alloc uninitialised buffer for incoming decrypted data #337

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 8 additions & 8 deletions perf/impl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ QUIC_GO_SUBDIRS := $(wildcard quic-go/*/.)
JS_SUBDIRS := $(wildcard js-libp2p/*/.)

all: $(RUST_SUBDIRS) $(GO_SUBDIRS) $(HTTPS_SUBDIRS) $(QUIC_GO_SUBDIRS) $(JS_SUBDIRS)
$(RUST_SUBDIRS):
$(MAKE) -C $@
$(GO_SUBDIRS):
$(MAKE) -C $@
$(HTTPS_SUBDIRS):
$(MAKE) -C $@
$(QUIC_GO_SUBDIRS):
$(MAKE) -C $@
# $(RUST_SUBDIRS):
# $(MAKE) -C $@
# $(GO_SUBDIRS):
# $(MAKE) -C $@
# $(HTTPS_SUBDIRS):
# $(MAKE) -C $@
# $(QUIC_GO_SUBDIRS):
# $(MAKE) -C $@
$(JS_SUBDIRS):
$(MAKE) -C $@

Expand Down
12 changes: 12 additions & 0 deletions perf/impl/js-libp2p/v1.0-noise-alloc-smart/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DOCKER_IMAGE := node:20-alpine
DOCKER_RUN := docker run --rm -v "$(shell pwd)":/usr/src/myapp -w /usr/src/myapp $(DOCKER_IMAGE)

all: perf

perf:
$(DOCKER_RUN) npm ci

clean:
rm -rf node_modules

.PHONY: all clean perf
112 changes: 112 additions & 0 deletions perf/impl/js-libp2p/v1.0-noise-alloc-smart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { tcp } from '@libp2p/tcp'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'
import { perf } from '@libp2p/perf'
import { parseArgs } from 'node:util'

const argv = parseArgs({
options: {
'run-server': {
type: 'string',
default: 'false'
},
'server-address': {
type: 'string'
},
transport: {
type: 'string',
default: 'tcp'
},
'upload-bytes': {
type: 'string',
default: '0'
},
'download-bytes': {
type: 'string',
default: '0'
}
}
})

/**
* @param {boolean} runServer
* @param {string} serverIpAddress
* @param {string} transport
* @param {number} uploadBytes
* @param {number} downloadBytes
*/
export async function main (runServer, serverIpAddress, transport, uploadBytes, downloadBytes) {
const { host, port } = splitHostPort(serverIpAddress)

const config = {
//peerId,
transports: [tcp({
socket: {
noDelay: true
},
server: {
noDelay: true
}
})],
streamMuxers: [yamux()],
connectionEncryption: [
noise()
],
connectionManager: {
minConnections: 0
},
services: {
perf: perf()
}
}

if (runServer) {
Object.assign(config, {
addresses: {
listen: [
// #TODO: right now we only support tcp
`/ip4/${host}/tcp/${port}`
]
}
})
}

const node = await createLibp2p(config)

await node.start()

if (!runServer) {
for await (const output of node.services.perf.measurePerformance(multiaddr(`/ip4/${host}/tcp/${port}`), uploadBytes, downloadBytes)) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(output))
}

await node.stop()
}
}

/**
* @param {string} address
* @returns { host: string, port?: string }
*/
function splitHostPort (address) {
try {
const parts = address.split(':')
const host = parts[0]
const port = parts[1]
return {
host,
port
}
} catch (error) {
throw Error('Invalid server address')
}
}

main(argv.values['run-server'] === 'true', argv.values['server-address'], argv.values.transport, Number(argv.values['upload-bytes']), Number(argv.values['download-bytes'])).catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})
51 changes: 51 additions & 0 deletions perf/impl/js-libp2p/v1.0-noise-alloc-smart/noise/dist/index.min.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type bytes = Uint8Array;
export type bytes32 = Uint8Array;
export type bytes16 = Uint8Array;
export type uint64 = number;
//# sourceMappingURL=basic.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { NoiseSession } from './handshake.js';
import type { NoiseExtensions } from '../proto/payload.js';
import type { PeerId } from '@libp2p/interface';
import type { Uint8ArrayList } from 'uint8arraylist';
export interface IHandshake {
session: NoiseSession;
remotePeer: PeerId;
remoteExtensions: NoiseExtensions;
encrypt(plaintext: Uint8Array | Uint8ArrayList, session: NoiseSession): Uint8Array | Uint8ArrayList;
decrypt(ciphertext: Uint8Array | Uint8ArrayList, session: NoiseSession, dst?: Uint8Array): {
plaintext: Uint8Array | Uint8ArrayList;
valid: boolean;
};
}
//# sourceMappingURL=handshake-interface.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { bytes, bytes32, uint64 } from './basic.js';
import type { KeyPair } from './libp2p.js';
import type { Nonce } from '../nonce.js';
import type { Uint8ArrayList } from 'uint8arraylist';
export type Hkdf = [bytes, bytes, bytes];
export interface MessageBuffer {
ne: bytes32;
ns: Uint8Array | Uint8ArrayList;
ciphertext: Uint8Array | Uint8ArrayList;
}
export interface CipherState {
k: bytes32;
n: Nonce;
}
export interface SymmetricState {
cs: CipherState;
ck: bytes32;
h: bytes32;
}
export interface HandshakeState {
ss: SymmetricState;
s: KeyPair;
e?: KeyPair;
rs: Uint8Array | Uint8ArrayList;
re: bytes32;
psk: bytes32;
}
export interface NoiseSession {
hs: HandshakeState;
h?: bytes32;
cs1?: CipherState;
cs2?: CipherState;
mc: uint64;
i: boolean;
}
export interface INoisePayload {
identityKey: bytes;
identitySig: bytes;
data: bytes;
}
//# sourceMappingURL=handshake.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { bytes32 } from './basic.js';
import type { NoiseExtensions } from '../proto/payload.js';
import type { ConnectionEncrypter } from '@libp2p/interface';
export interface KeyPair {
publicKey: bytes32;
privateKey: bytes32;
}
export interface INoiseConnection extends ConnectionEncrypter<NoiseExtensions> {
}
//# sourceMappingURL=libp2p.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare const NOISE_MSG_MAX_LENGTH_BYTES = 65535;
export declare const NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG: number;
export declare const DUMP_SESSION_KEYS: boolean;
//# sourceMappingURL=constants.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type Uint8ArrayList } from 'uint8arraylist';
import type { bytes32 } from './@types/basic.js';
import type { Hkdf } from './@types/handshake.js';
import type { KeyPair } from './@types/libp2p.js';
export interface ICryptoInterface {
hashSHA256(data: Uint8Array | Uint8ArrayList): Uint8Array;
getHKDF(ck: bytes32, ikm: Uint8Array): Hkdf;
generateX25519KeyPair(): KeyPair;
generateX25519KeyPairFromSeed(seed: Uint8Array): KeyPair;
generateX25519SharedKey(privateKey: Uint8Array | Uint8ArrayList, publicKey: Uint8Array | Uint8ArrayList): Uint8Array;
chaCha20Poly1305Encrypt(plaintext: Uint8Array | Uint8ArrayList, nonce: Uint8Array, ad: Uint8Array, k: bytes32): Uint8ArrayList | Uint8Array;
chaCha20Poly1305Decrypt(ciphertext: Uint8Array | Uint8ArrayList, nonce: Uint8Array, ad: Uint8Array, k: bytes32, dst?: Uint8Array): Uint8ArrayList | Uint8Array | null;
}
//# sourceMappingURL=crypto.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare const defaultCrypto: import("../crypto.js").ICryptoInterface;
//# sourceMappingURL=index.browser.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { ICryptoInterface } from '../crypto.js';
export declare const defaultCrypto: ICryptoInterface;
//# sourceMappingURL=index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading