1 Commits
box2d ... seif

Author SHA1 Message Date
John Alanbrook
f4ea552271 initial try at seif handshake example
Some checks failed
Build and Deploy / build-macos (push) Failing after 5s
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled
2025-05-25 00:07:20 -05:00
17 changed files with 581 additions and 2817 deletions

View File

@@ -1,8 +0,0 @@
var config = {
title: "Box2D Physics Demo",
width: 1280,
height: 720,
fullscreen: false
}
return config

View File

@@ -1,343 +0,0 @@
var box2d = use('box2d')
var moth = use('moth', $_.delay)
moth.initialize()
var draw2d = use('draw2d')
// Physics world setup
var world = new box2d.World({
gravity: {x: 0, y: -10}
})
// Ground body (static)
var ground = world.createBody({
type: 'static',
position: {x: 0, y: -10}
})
var groundShape = ground.createBoxShape({
width: 50,
height: 0.5,
density: 0,
friction: 0.7
})
// Walls
var leftWall = world.createBody({
type: 'static',
position: {x: -25, y: 0}
})
leftWall.createBoxShape({
width: 0.5,
height: 30,
density: 0
})
var rightWall = world.createBody({
type: 'static',
position: {x: 25, y: 0}
})
rightWall.createBoxShape({
width: 0.5,
height: 30,
density: 0
})
// Dynamic bodies array
var boxes = []
var circles = []
// Create some dynamic boxes
for (var i = 0; i < 5; i++) {
var box = world.createBody({
type: 'dynamic',
position: {x: -10 + i * 4, y: 5 + i * 3},
angle: Math.random() * Math.PI
})
box.createBoxShape({
width: 2,
height: 2,
density: 1.0,
friction: 0.3,
restitution: 0.5
})
boxes.push(box)
}
// Create some circles
for (var i = 0; i < 3; i++) {
var circle = world.createBody({
type: 'dynamic',
position: {x: 5 + i * 3, y: 10 + i * 2}
})
circle.createCircleShape({
radius: 1,
density: 0.5,
friction: 0.2,
restitution: 0.8
})
circles.push(circle)
}
// Connected bodies with distance joint
var bodyA = world.createBody({
type: 'dynamic',
position: {x: -5, y: 15}
})
bodyA.createCircleShape({
radius: 0.5,
density: 1.0
})
var bodyB = world.createBody({
type: 'dynamic',
position: {x: 0, y: 15}
})
bodyB.createCircleShape({
radius: 0.5,
density: 1.0
})
var joint = world.createDistanceJoint({
bodyA: bodyA,
bodyB: bodyB,
localAnchorA: {x: 0, y: 0},
localAnchorB: {x: 0, y: 0},
length: 5
})
// Mouse interaction
var mouseBody = null
var mousePressed = false
// Game state
var camera = {x: 0, y: 0, zoom: 10}
// Input state
var keys = {}
var mouse = {
pos: {x: 0, y: 0},
buttons: [false, false, false]
}
// Main update function
prosperon.on('update', function(dt) {
// Step physics simulation
world.step(dt, 4)
// Camera controls
if (keys[4]) camera.x -= 20 * dt // A
if (keys[7]) camera.x += 20 * dt // D
if (keys[26]) camera.y += 20 * dt // W
if (keys[22]) camera.y -= 20 * dt // S
if (keys[20]) camera.zoom *= 1 + dt // Q
if (keys[8]) camera.zoom *= 1 - dt // E
// Mouse interaction
var mouseWorld = screenToWorld(mouse.pos)
// Raycast to find body under mouse
var result = world.rayCast(mouseWorld, {x: 0, y: -1}, 0.1)
if (!result.hit) {
// Create new body at mouse position
if (keys[225]) { // LSHIFT
// Create circle
var newCircle = world.createBody({
type: 'dynamic',
position: mouseWorld
})
newCircle.createCircleShape({
radius: 0.5 + Math.random(),
density: 1.0,
restitution: 0.3 + Math.random() * 0.5
})
circles.push(newCircle)
} else {
// Create box
var newBox = world.createBody({
type: 'dynamic',
position: mouseWorld,
angle: Math.random() * Math.PI * 2
})
newBox.createBoxShape({
width: 1 + Math.random() * 2,
height: 1 + Math.random() * 2,
density: 1.0,
restitution: 0.2 + Math.random() * 0.3
})
boxes.push(newBox)
}
}
boxes.forEach(function(box) {
var dir = {
x: box.position.x - mouseWorld.x,
y: box.position.y - mouseWorld.y
}
var dist = Math.sqrt(dir.x * dir.x + dir.y * dir.y)
if (dist < 10 && dist > 0.1) {
dir.x /= dist
dir.y /= dist
box.applyLinearImpulse({x: dir.x * 50, y: dir.y * 50})
}
})
// Reset with R
if (keys[21] && !keys[21 + '_prev']) { // R key pressed
// Reset all dynamic bodies
boxes.forEach(function(box) {
box.position = {x: Math.random() * 20 - 10, y: 10 + Math.random() * 10}
box.angle = Math.random() * Math.PI * 2
box.linearVelocity = {x: 0, y: 0}
box.angularVelocity = 0
})
circles.forEach(function(circle) {
circle.position = {x: Math.random() * 20 - 10, y: 10 + Math.random() * 10}
circle.linearVelocity = {x: 0, y: 0}
})
}
// Update previous key states
for (var k in keys) {
if (k.indexOf('_prev') === -1) {
keys[k + '_prev'] = keys[k]
}
}
})
// Event handlers
prosperon.on('key_down', function(e) {
keys[e.scancode] = true
})
prosperon.on('key_up', function(e) {
keys[e.scancode] = false
})
prosperon.on('mouse_button_down', function(e) {
mouse.buttons[e.which] = true
if (e.which === 0 && !mousePressed) {
mousePressed = true
var mouseWorld = screenToWorld(mouse.pos)
// Raycast to find body under mouse
var result = world.rayCast(mouseWorld, {x: 0, y: -1}, 0.1)
if (!result.hit) {
// Create new body at mouse position
if (keys[225]) { // LSHIFT
// Create circle
var newCircle = world.createBody({
type: 'dynamic',
position: mouseWorld
})
newCircle.createCircleShape({
radius: 0.5 + Math.random(),
density: 1.0,
restitution: 0.3 + Math.random() * 0.5
})
circles.push(newCircle)
} else {
// Create box
var newBox = world.createBody({
type: 'dynamic',
position: mouseWorld,
angle: Math.random() * Math.PI * 2
})
newBox.createBoxShape({
width: 1 + Math.random() * 2,
height: 1 + Math.random() * 2,
density: 1.0,
restitution: 0.2 + Math.random() * 0.3
})
boxes.push(newBox)
}
}
}
if (e.which === 1) {
// Apply impulse with right click
var mouseWorld = screenToWorld(mouse.pos)
boxes.forEach(function(box) {
var dir = {
x: box.position.x - mouseWorld.x,
y: box.position.y - mouseWorld.y
}
var dist = Math.sqrt(dir.x * dir.x + dir.y * dir.y)
if (dist < 10 && dist > 0.1) {
dir.x /= dist
dir.y /= dist
box.applyLinearImpulse({x: dir.x * 50, y: dir.y * 50})
}
})
}
})
prosperon.on('mouse_button_up', function(e) {
mouse.buttons[e.which] = false
if (e.which === 0) {
mousePressed = false
}
})
prosperon.on('mouse_motion', function(e) {
mouse.pos = e.pos
})
// Rendering
prosperon.on('draw', function() {
// Clear background
// Draw ground
drawBox(ground.position, 50, 0.5, ground.angle, {r: 0.5, g: 0.5, b: 0.5, a: 1})
// Draw walls
drawBox(leftWall.position, 0.5, 30, 0, {r: 0.5, g: 0.5, b: 0.5, a: 1})
drawBox(rightWall.position, 0.5, 30, 0, {r: 0.5, g: 0.5, b: 0.5, a: 1})
// Draw boxes
boxes.forEach(function(box) {
drawBox(box.position, 2, 2, box.angle, {r: 0.8, g: 0.3, b: 0.3, a: 1})
})
// Draw circles
circles.forEach(function(circle) {
draw2d.circle(circle.position, 1, {r: 0.3, g: 0.8, b: 0.3, a: 1})
})
// Draw connected bodies
draw2d.circle(bodyA.position, 0.5, {r: 0.8, g: 0.8, b: 0.3, a: 1})
draw2d.circle(bodyB.position, 0.5, {r: 0.8, g: 0.8, b: 0.3, a: 1})
draw2d.line([bodyA.position, bodyB.position], {r: 1, g: 1, b: 0, a: 0.5})
// Draw UI
draw2d.text("Box2D Demo", {x: 10, y: 10}, 20, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("Controls:", {x: 10, y: 40}, 16, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- WASD: Move camera", {x: 10, y: 60}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- Q/E: Zoom in/out", {x: 10, y: 80}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- Left click: Create box", {x: 10, y: 100}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- Shift + Left click: Create circle", {x: 10, y: 120}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- Right click: Apply impulse", {x: 10, y: 140}, 14, {r: 1, g: 1, b: 1, a: 1})
draw2d.text("- R: Reset bodies", {x: 10, y: 160}, 14, {r: 1, g: 1, b: 1, a: 1})
// Show physics stats
draw2d.text("Bodies: " + (boxes.length + circles.length + 4), {x: 10, y: 200}, 14, {r: 1, g: 1, b: 1, a: 1})
})
// Helper functions
function drawBox(pos, width, height, angle, color) {
draw2d.rectangle({x:pos.x,y:pos.y,width, height})
}
function screenToWorld(screenPos) {
return {
x: (screenPos.x - prosperon.x * 0.5) / camera.zoom + camera.x,
y: (screenPos.y - prosperon.y * 0.5) / camera.zoom + camera.y
}
}

50
examples/seif_client.js Normal file
View File

@@ -0,0 +1,50 @@
// Seif Handshake Client Example
// Implements the Seif Protocol handshake client side
var crypto = use('crypto');
var json = use('json');
var io = use('io');
// Alice's key pair
var alice_keys = crypto.keypair();
console.log("Alice public key:", alice_keys.public);
console.log("Alice private key:", alice_keys.private);
// Bob's public key (in real usage, this would be obtained separately)
// For this example, we'll use a hardcoded key or read from file
var bob_public_key = null;
// Try to read Bob's public key from file if it exists
if (io.exists('bob_public.key')) {
bob_public_key = io.slurp('bob_public.key').trim();
console.log("Loaded Bob's public key from file:", bob_public_key);
} else {
// For testing, use the server's public key printed by seif_server.js
console.log("Please create bob_public.key with the server's public key");
console.log("Run seif_server.js first to get the public key");
return;
}
// Generate random handshake key
var handshake_key = crypto.keypair().public; // Using public key as random 32-byte value
console.log("Generated handshake key:", handshake_key);
console.log("Sending handshake to server...");
// Contact the server
$_.contact((actor, reason) => {
if (!actor) {
console.error("Could not establish connection:", reason);
return;
}
}, {
address: "localhost",
port: 5678,
seif: 1,
handshake: crypto.encrypt_pk(bob_public_key, handshake_key),
payload: crypto.encrypt(handshake_key, alice_keys.public)
});
$_.receiver(e => {
console.log("Received message:", e);
});

View File

@@ -0,0 +1,83 @@
# Seif Handshake Examples
This directory contains examples demonstrating the Seif Protocol handshake implementation in Prosperon.
## Files
- `seif_simple.js` - A standalone demonstration of the Seif handshake cryptographic operations
- `seif_server.js` - A server that accepts Seif handshake connections
- `seif_client.js` - A client that initiates Seif handshake with a server
## Running the Examples
### Simple Demo
To see the cryptographic operations in action:
```bash
./prosperon examples/seif_simple.js
```
### Client-Server Demo
1. First, start the server:
```bash
./prosperon examples/seif_server.js
```
2. Note the server's public key that is printed
3. Create a file `bob_public.key` with the server's public key:
```bash
echo "SERVER_PUBLIC_KEY_HERE" > bob_public.key
```
4. In another terminal, run the client:
```bash
./prosperon examples/seif_client.js
```
## The Seif Protocol
The Seif handshake establishes a secure session in one round trip:
1. **Alice's Message**:
- Generates random `handshake_key`
- Sends: `{seif: 1, handshake: encrypt_pk(bob_public, handshake_key), payload: encrypt(handshake_key, alice_public)}`
2. **Bob's Response**:
- Decrypts `handshake_key` using his private key
- Decrypts Alice's public key from payload
- Generates `session_key`
- Sends: `encrypt(handshake_key, {session: encrypt_pk(alice_public, session_key)})`
3. **Result**: Both parties share `session_key` for symmetric encryption
## Actor System Integration
In Prosperon's actor system:
- Actor objects can serve as public key identifiers (they contain unique IDs)
- The `$_.portal()` function creates a listening endpoint
- The `$_.contact()` function initiates connections
- Messages are automatically routed through the actor system
## Security Properties
- **Authentication**: Both parties prove possession of their private keys
- **Forward Secrecy**: Session keys are ephemeral
- **Man-in-the-Middle Protection**: Requires knowledge of both private keys
- **One Round Trip**: Efficient session establishment
## Notes on Implementation
The current implementation uses the actor object's ID as part of the identity system. In a production system, you might want to:
1. Store the public key as part of the actor's data
2. Use a proper key derivation function for session keys
3. Add additional metadata in the handshake (timestamps, nonces, etc.)
4. Implement key rotation and session management
The crypto module provides:
- `crypto.keypair()` - Generate X25519 key pairs
- `crypto.encrypt_pk(public_key, data)` - Public key encryption
- `crypto.decrypt_pk(private_key, data)` - Public key decryption
- `crypto.encrypt(key, data)` - Symmetric encryption
- `crypto.decrypt(key, data)` - Symmetric decryption

85
examples/seif_server.js Normal file
View File

@@ -0,0 +1,85 @@
// Seif Handshake Server Example
// Implements the Seif Protocol handshake as described in the documentation
var crypto = use('crypto');
var json = use('json');
var io = use('io');
// Server's key pair
var server_keys = crypto.keypair();
console.log("Server public key:", server_keys.public);
console.log("Server private key:", server_keys.private);
// Store connected clients
var clients = {};
$_.portal(e => {
// Verify the handshake message format
if (!e.seif || !e.handshake || !e.payload) {
send(e, {error:"Invalid Seif handshake format"});
return;
}
if (e.seif !== 1) {
send(e, {error:"Unsupported Seif protocol version:", e.seif});
return;
}
try {
// Decrypt the handshake key with server's private key
var handshake_key_encrypted = e.handshake;
var handshake_key_hex = crypto.decrypt_pk(server_keys.private, handshake_key_encrypted);
// Convert ArrayBuffer to hex string
var handshake_key_bytes = new Uint8Array(handshake_key_hex);
var handshake_key = '';
for (var i = 0; i < handshake_key_bytes.length; i++) {
var hex = handshake_key_bytes[i].toString(16);
handshake_key += (hex.length === 1 ? '0' : '') + hex;
}
console.log("Decrypted handshake key:", handshake_key);
// Decrypt the payload (client's public key) with handshake key
var client_public_encrypted = e.payload;
var client_public_buffer = crypto.decrypt(handshake_key, client_public_encrypted);
// Convert decrypted buffer to string
var client_public_bytes = new Uint8Array(client_public_buffer);
var client_public = '';
for (var i = 0; i < client_public_bytes.length; i++) {
client_public += String.fromCharCode(client_public_bytes[i]);
}
console.log("Client's public key:", client_public);
// Generate session key
var session_key = crypto.keypair();
console.log("Generated session key:", session_key.public);
// Create response encrypted with handshake key
var response = {
session: crypto.encrypt_pk(client_public, session_key.public)
};
var response_encrypted = crypto.encrypt(handshake_key, json.encode(response));
// Send encrypted response
send(e, response_encrypted);
console.log("Handshake complete with client:", client_public);
} catch (err) {
send(e, {error:err})
}
}, 5678);
// Handle messages from connected clients
$_.receiver(e => {
if (e.type === 'encrypted_message') {
console.log("Received encrypted message");
// In a real implementation, decrypt with session key and process
}
});
console.log("Seif server listening on port 5678");

118
examples/seif_simple.js Normal file
View File

@@ -0,0 +1,118 @@
// Simplified Seif Handshake Example
// This demonstrates the core cryptographic operations of the Seif handshake
var crypto = use('crypto');
var json = use('json');
console.log("=== Seif Handshake Demo ===\n");
// Step 1: Generate key pairs for Alice and Bob
console.log("1. Generating key pairs...");
var alice_keys = crypto.keypair();
var bob_keys = crypto.keypair();
console.log("Alice's public key:", alice_keys.public);
console.log("Bob's public key:", bob_keys.public);
// Step 2: Alice initiates handshake
console.log("\n2. Alice initiates handshake...");
// Alice generates a random handshake key
var handshake_key = crypto.keypair().public; // Using public key generation for random 32 bytes
console.log("Handshake key:", handshake_key);
// Alice creates the handshake message
var alice_message = {
seif: 1,
handshake: crypto.encrypt_pk(bob_keys.public, handshake_key),
payload: crypto.encrypt(handshake_key, alice_keys.public)
};
console.log("Alice's message created (encrypted components)");
// Step 3: Bob processes the handshake
console.log("\n3. Bob processes the handshake...");
// Bob decrypts the handshake key
var decrypted_handshake_key_buffer = crypto.decrypt_pk(bob_keys.private, alice_message.handshake);
// Convert buffer to hex string
var handshake_key_bytes = new Uint8Array(decrypted_handshake_key_buffer);
var recovered_handshake_key = '';
for (var i = 0; i < handshake_key_bytes.length; i++) {
var hex = handshake_key_bytes[i].toString(16);
recovered_handshake_key += (hex.length === 1 ? '0' : '') + hex;
}
console.log("Bob recovered handshake key:", recovered_handshake_key);
console.log("Keys match:", recovered_handshake_key === handshake_key);
// Bob decrypts Alice's public key
var alice_public_buffer = crypto.decrypt(recovered_handshake_key, alice_message.payload);
var alice_public_bytes = new Uint8Array(alice_public_buffer);
var recovered_alice_public = '';
for (var i = 0; i < alice_public_bytes.length; i++) {
recovered_alice_public += String.fromCharCode(alice_public_bytes[i]);
}
console.log("Bob recovered Alice's public key:", recovered_alice_public);
console.log("Public keys match:", recovered_alice_public === alice_keys.public);
// Step 4: Bob generates session key and responds
console.log("\n4. Bob generates session key and responds...");
// Generate a random session key
var session_key = crypto.keypair().public;
console.log("Session key:", session_key);
// Bob encrypts the session key with Alice's public key
var bob_response = {
session: crypto.encrypt_pk(alice_keys.public, session_key)
};
// Encrypt the entire response with the handshake key
var encrypted_response = crypto.encrypt(handshake_key, json.encode(bob_response));
console.log("Bob's encrypted response created");
// Step 5: Alice processes Bob's response
console.log("\n5. Alice processes Bob's response...");
// Alice decrypts the response
var decrypted_response_buffer = crypto.decrypt(handshake_key, encrypted_response);
var response_json = '';
var response_bytes = new Uint8Array(decrypted_response_buffer);
for (var i = 0; i < response_bytes.length; i++) {
response_json += String.fromCharCode(response_bytes[i]);
}
var response_data = json.decode(response_json);
// Alice decrypts the session key
var session_key_buffer = crypto.decrypt_pk(alice_keys.private, response_data.session);
var session_key_bytes = new Uint8Array(session_key_buffer);
var recovered_session_key = '';
for (var i = 0; i < session_key_bytes.length; i++) {
var hex = session_key_bytes[i].toString(16);
recovered_session_key += (hex.length === 1 ? '0' : '') + hex;
}
console.log("Alice recovered session key:", recovered_session_key);
console.log("Session keys match:", recovered_session_key === session_key);
// Step 6: Demonstrate secure communication
console.log("\n6. Secure communication established!");
console.log("Both parties now share the session key and can communicate securely.");
// Example encrypted message
var message = "Hello, this is a secret message!";
var encrypted_msg = crypto.encrypt(session_key, message);
console.log("\nAlice encrypts:", message);
var decrypted_msg_buffer = crypto.decrypt(session_key, encrypted_msg);
var decrypted_msg = '';
var msg_bytes = new Uint8Array(decrypted_msg_buffer);
for (var i = 0; i < msg_bytes.length; i++) {
decrypted_msg += String.fromCharCode(msg_bytes[i]);
}
console.log("Bob decrypts:", decrypted_msg);
console.log("\n=== Seif Handshake Complete ===");

View File

@@ -94,18 +94,6 @@ sdl3_opts.add_cmake_defines({
'SDL_PULSEAUDIO': 'ON',
})
box2d_opts = cmake.subproject_options()
box2d_opts.add_cmake_defines({
'BOX2D_SAMPLES': 'OFF',
'BOX2D_BUILD_STATIC': 'ON',
'BOX2d_AVX2': 'ON',
'BOX2D_BUILD_SHARED': 'OFF',
'CMAKE_BUILD_TYPE': 'Release',
})
box2d_proj = cmake.subproject('box2d', options: box2d_opts)
deps += box2d_proj.dependency('box2d')
cc = meson.get_compiler('c')
if host_machine.system() == 'darwin'
@@ -154,6 +142,7 @@ deps += dependency('qjs-layout', static:true)
deps += dependency('qjs-miniz', static:true)
deps += dependency('physfs', static:true)
deps += dependency('threads')
deps += dependency('chipmunk', static:true)
if host_machine.system() != 'emscripten'
deps += dependency('enet', static:true)
@@ -203,14 +192,12 @@ endif
link_args = link
sources = []
src += [
'anim.c', 'config.c', 'datastream.c','font.c','HandmadeMath.c','jsffi.c','model.c',
'render.c','simplex.c','spline.c', 'transform.c','prosperon.c', 'wildmatch.c',
'sprite.c', 'rtree.c', 'qjs_nota.c', 'qjs_soloud.c', 'qjs_sdl.c', 'qjs_math.c', 'qjs_geometry.c', 'qjs_transform.c', 'qjs_sprite.c', 'qjs_io.c', 'qjs_os.c', 'qjs_actor.c',
'qjs_qr.c', 'qjs_wota.c', 'monocypher.c', 'qjs_blob.c', 'qjs_crypto.c', 'qjs_time.c', 'qjs_http.c', 'qjs_rtree.c', 'qjs_spline.c', 'qjs_js.c', 'qjs_debug.c'
]
src += 'qjs_box2d.c'
# quirc src
src += [
'thirdparty/quirc/quirc.c', 'thirdparty/quirc/decode.c',

View File

@@ -1,7 +1,5 @@
#include "HandmadeMath.h"
#include <stdlib.h>
const HMM_Vec2 v2zero = {0,0};
const HMM_Vec2 v2one = {1,1};
const HMM_Vec3 v3zero = {0,0,0};

View File

@@ -96,6 +96,8 @@
#ifndef HANDMADE_MATH_H
#define HANDMADE_MATH_H
#include <chipmunk/chipmunk.h>
#if !defined(HANDMADE_MATH_NO_SIMD)
#if defined(__ARM_NEON) || defined(__ARM_NEON__)
#define HANDMADE_MATH__USE_NEON 1
@@ -270,6 +272,12 @@ typedef union HMM_Vec3 {
HMM_Vec2 VW;
};
struct
{
HMM_Vec2 cp;
float _Ignored5;
};
float Elements[3];
float e[3];
@@ -365,6 +373,12 @@ typedef union HMM_Vec4 {
HMM_Vec2 ZW;
};
struct
{
HMM_Vec2 cp;
HMM_Vec2 wh;
};
HMM_Quat quat;
struct {float x, y, z, w; };
struct {float r, g, b, a; };

View File

@@ -32,7 +32,6 @@
#include "qjs_dmon.h"
#include "qjs_nota.h"
#include "qjs_wota.h"
#include "qjs_box2d.h"
#include "qjs_enet.h"
#include "qjs_soloud.h"
#include "qjs_qr.h"
@@ -49,7 +48,6 @@
#include "qjs_spline.h"
#include "qjs_js.h"
#include "qjs_debug.h"
#include "qjs_box2d.h"
#ifndef NSTEAM
#include "qjs_steam.h"
#endif
@@ -2897,7 +2895,6 @@ void ffi_load(JSContext *js)
arrput(rt->module_registry, MISTLINE(enet));
arrput(rt->module_registry, MISTLINE(qr));
arrput(rt->module_registry, MISTLINE(wota));
arrput(rt->module_registry, MISTLINE(box2d));
arrput(rt->module_registry, MISTLINE(crypto));
arrput(rt->module_registry, MISTLINE(blob));
arrput(rt->module_registry, MISTLINE(http));
@@ -2906,7 +2903,6 @@ void ffi_load(JSContext *js)
arrput(rt->module_registry, MISTLINE(rtree));
arrput(rt->module_registry, MISTLINE(sprite));
arrput(rt->module_registry, MISTLINE(transform));
arrput(rt->module_registry, MISTLINE(box2d));
#ifdef TRACY_ENABLE
arrput(rt->module_registry, MISTLINE(tracy));

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +0,0 @@
#ifndef QJS_BOX2D_H
#define QJS_BOX2D_H
#include "quickjs.h"
JSValue js_box2d_use(JSContext*);
#endif

View File

@@ -199,10 +199,240 @@ JSValue js_crypto_random(JSContext *js, JSValue self, int argc, JSValue *argv)
return JS_NewFloat64(js, val);
}
JSValue js_crypto_encrypt_pk(JSContext *js, JSValue self, int argc, JSValue *argv)
{
if (argc < 2) {
return JS_ThrowTypeError(js, "crypto.encrypt_pk: expected 2 arguments (public_key, plaintext)");
}
uint8_t public_key[32];
js2crypto(js, argv[0], public_key);
size_t plaintext_len;
uint8_t *plaintext;
if (JS_IsString(argv[1])) {
const char *str = JS_ToCStringLen(js, &plaintext_len, argv[1]);
if (!str) return JS_EXCEPTION;
plaintext = (uint8_t *)str;
} else {
plaintext = JS_GetArrayBuffer(js, &plaintext_len, argv[1]);
if (!plaintext) {
return JS_ThrowTypeError(js, "crypto.encrypt_pk: plaintext must be string or ArrayBuffer");
}
}
// Generate ephemeral keypair
uint8_t ephemeral_secret[32];
uint8_t ephemeral_public[32];
randombytes(ephemeral_secret, 32);
ephemeral_secret[0] &= 248;
ephemeral_secret[31] &= 127;
ephemeral_secret[31] |= 64;
crypto_x25519_public_key(ephemeral_public, ephemeral_secret);
// Compute shared secret
uint8_t shared[32];
crypto_x25519(shared, ephemeral_secret, public_key);
// Derive encryption key using BLAKE2b
uint8_t key[32];
crypto_blake2b(key, 32, shared, 32);
// Generate random nonce
uint8_t nonce[24];
randombytes(nonce, 24);
// Allocate output buffer: ephemeral_public(32) + nonce(24) + ciphertext + mac(16)
size_t output_size = 32 + 24 + plaintext_len + 16;
uint8_t *output = js_malloc(js, output_size);
if (!output) {
if (JS_IsString(argv[1])) JS_FreeCString(js, (const char *)plaintext);
return JS_EXCEPTION;
}
// Copy ephemeral public key and nonce to output
memcpy(output, ephemeral_public, 32);
memcpy(output + 32, nonce, 24);
// Encrypt
crypto_aead_lock(output + 32 + 24, output + 32 + 24 + plaintext_len,
key, nonce, NULL, 0, plaintext, plaintext_len);
if (JS_IsString(argv[1])) JS_FreeCString(js, (const char *)plaintext);
// Wipe sensitive data
crypto_wipe(ephemeral_secret, 32);
crypto_wipe(shared, 32);
crypto_wipe(key, 32);
JSValue result = JS_NewArrayBufferCopy(js, output, output_size);
js_free(js, output);
return result;
}
JSValue js_crypto_decrypt_pk(JSContext *js, JSValue self, int argc, JSValue *argv)
{
if (argc < 2) {
return JS_ThrowTypeError(js, "crypto.decrypt_pk: expected 2 arguments (private_key, ciphertext)");
}
uint8_t private_key[32];
js2crypto(js, argv[0], private_key);
size_t ciphertext_len;
uint8_t *ciphertext = JS_GetArrayBuffer(js, &ciphertext_len, argv[1]);
if (!ciphertext) {
return JS_ThrowTypeError(js, "crypto.decrypt_pk: ciphertext must be ArrayBuffer");
}
if (ciphertext_len < 32 + 24 + 16) {
return JS_ThrowTypeError(js, "crypto.decrypt_pk: ciphertext too short");
}
// Extract ephemeral public key and nonce
uint8_t ephemeral_public[32];
uint8_t nonce[24];
memcpy(ephemeral_public, ciphertext, 32);
memcpy(nonce, ciphertext + 32, 24);
// Compute shared secret
uint8_t shared[32];
crypto_x25519(shared, private_key, ephemeral_public);
// Derive decryption key
uint8_t key[32];
crypto_blake2b(key, 32, shared, 32);
// Decrypt
size_t plaintext_len = ciphertext_len - 32 - 24 - 16;
uint8_t *plaintext = js_malloc(js, plaintext_len);
if (!plaintext) {
crypto_wipe(shared, 32);
crypto_wipe(key, 32);
return JS_EXCEPTION;
}
int result = crypto_aead_unlock(plaintext,
ciphertext + ciphertext_len - 16,
key, nonce, NULL, 0,
ciphertext + 32 + 24, plaintext_len);
crypto_wipe(shared, 32);
crypto_wipe(key, 32);
if (result != 0) {
js_free(js, plaintext);
return JS_ThrowTypeError(js, "crypto.decrypt_pk: decryption failed");
}
JSValue ret = JS_NewArrayBufferCopy(js, plaintext, plaintext_len);
js_free(js, plaintext);
return ret;
}
JSValue js_crypto_encrypt(JSContext *js, JSValue self, int argc, JSValue *argv)
{
if (argc < 2) {
return JS_ThrowTypeError(js, "crypto.encrypt: expected 2 arguments (key, plaintext)");
}
uint8_t key[32];
js2crypto(js, argv[0], key);
size_t plaintext_len;
uint8_t *plaintext;
if (JS_IsString(argv[1])) {
const char *str = JS_ToCStringLen(js, &plaintext_len, argv[1]);
if (!str) return JS_EXCEPTION;
plaintext = (uint8_t *)str;
} else {
plaintext = JS_GetArrayBuffer(js, &plaintext_len, argv[1]);
if (!plaintext) {
return JS_ThrowTypeError(js, "crypto.encrypt: plaintext must be string or ArrayBuffer");
}
}
// Generate random nonce
uint8_t nonce[24];
randombytes(nonce, 24);
// Allocate output buffer: nonce(24) + ciphertext + mac(16)
size_t output_size = 24 + plaintext_len + 16;
uint8_t *output = js_malloc(js, output_size);
if (!output) {
if (JS_IsString(argv[1])) JS_FreeCString(js, (const char *)plaintext);
return JS_EXCEPTION;
}
// Copy nonce to output
memcpy(output, nonce, 24);
// Encrypt
crypto_aead_lock(output + 24, output + 24 + plaintext_len,
key, nonce, NULL, 0, plaintext, plaintext_len);
if (JS_IsString(argv[1])) JS_FreeCString(js, (const char *)plaintext);
JSValue result = JS_NewArrayBufferCopy(js, output, output_size);
js_free(js, output);
return result;
}
JSValue js_crypto_decrypt(JSContext *js, JSValue self, int argc, JSValue *argv)
{
if (argc < 2) {
return JS_ThrowTypeError(js, "crypto.decrypt: expected 2 arguments (key, ciphertext)");
}
uint8_t key[32];
js2crypto(js, argv[0], key);
size_t ciphertext_len;
uint8_t *ciphertext = JS_GetArrayBuffer(js, &ciphertext_len, argv[1]);
if (!ciphertext) {
return JS_ThrowTypeError(js, "crypto.decrypt: ciphertext must be ArrayBuffer");
}
if (ciphertext_len < 24 + 16) {
return JS_ThrowTypeError(js, "crypto.decrypt: ciphertext too short");
}
// Extract nonce
uint8_t nonce[24];
memcpy(nonce, ciphertext, 24);
// Decrypt
size_t plaintext_len = ciphertext_len - 24 - 16;
uint8_t *plaintext = js_malloc(js, plaintext_len);
if (!plaintext) {
return JS_EXCEPTION;
}
int result = crypto_aead_unlock(plaintext,
ciphertext + ciphertext_len - 16,
key, nonce, NULL, 0,
ciphertext + 24, plaintext_len);
if (result != 0) {
js_free(js, plaintext);
return JS_ThrowTypeError(js, "crypto.decrypt: decryption failed");
}
JSValue ret = JS_NewArrayBufferCopy(js, plaintext, plaintext_len);
js_free(js, plaintext);
return ret;
}
static const JSCFunctionListEntry js_crypto_funcs[] = {
JS_CFUNC_DEF("keypair", 0, js_crypto_keypair),
JS_CFUNC_DEF("shared", 1, js_crypto_shared),
JS_CFUNC_DEF("random", 0, js_crypto_random),
JS_CFUNC_DEF("encrypt_pk", 2, js_crypto_encrypt_pk),
JS_CFUNC_DEF("decrypt_pk", 2, js_crypto_decrypt_pk),
JS_CFUNC_DEF("encrypt", 2, js_crypto_encrypt),
JS_CFUNC_DEF("decrypt", 2, js_crypto_decrypt),
};
JSValue js_crypto_use(JSContext *js)

View File

@@ -2,7 +2,6 @@
#include "stb_ds.h"
#include "transform.h"
#include "math.h"
#include "float.h"
/* -------------------------------------------------------------------------
Cubic Spline Basis Matrices

View File

@@ -1,7 +1,5 @@
#include "sprite.h"
#include <stdlib.h>
sprite *make_sprite(void)
{
sprite *sprite = calloc(sizeof(*sprite),1);

View File

@@ -1,4 +0,0 @@
[wrap-git]
directory=box2d
url=https://github.com/erincatto/box2d.git
revision=v3.0.0

View File

@@ -1,90 +0,0 @@
var box2d = use('box2d')
// Create world with constructor
var world = new box2d.World({gravity: {x: 0, y: -9.8}})
console.log("Initial gravity:", world.gravity)
// Create a dynamic body
var body = world.createBody({
type: 'dynamic',
position: {x: 0, y: 10}
})
// Add a box shape to the body
var shape = body.createBoxShape({
width: 1,
height: 1,
density: 1.0,
friction: 0.3,
restitution: 0.5
})
console.log("Initial position:", body.position)
console.log("Initial velocity:", body.linearVelocity)
// Simulate
world.step(1/60, 4);
console.log("After 1 step:", body.position)
world.step(1/60, 4);
console.log("After 2 steps:", body.position)
world.step(1/60, 4);
console.log("After 3 steps:", body.position)
// Test applying forces
body.applyForce({x: 100, y: 0})
world.step(1/60, 4);
console.log("After force:", body.position, "velocity:", body.linearVelocity)
// Test properties
console.log("Body type:", body.type)
console.log("Body mass:", body.getMass())
console.log("Body angle:", body.angle)
// Test string body types
var staticBody = world.createBody({
type: 'static',
position: {x: 0, y: 0}
})
console.log("Static body type:", staticBody.type)
// Test ray casting
var rayResult = world.rayCast({x: -5, y: 10}, {x: 10, y: 0}, 1.0)
console.log("Ray cast result:", rayResult)
// Test distance joint
var bodyA = world.createBody({
type: 'dynamic',
position: {x: -2, y: 5}
})
bodyA.createCircleShape({
radius: 0.5,
density: 1.0
})
var bodyB = world.createBody({
type: 'dynamic',
position: {x: 2, y: 5}
})
bodyB.createCircleShape({
radius: 0.5,
density: 1.0
})
var joint = world.createDistanceJoint({
bodyA: bodyA,
bodyB: bodyB,
localAnchorA: {x: 0, y: 0},
localAnchorB: {x: 0, y: 0},
length: 4.0
})
console.log("Created distance joint")
// Test multiple steps with joint
for (var i = 0; i < 10; i++) {
world.step(1/60, 4)
}
console.log("Body A position after joint simulation:", bodyA.position)
console.log("Body B position after joint simulation:", bodyB.position)