@aleph-ai/tinyaleph 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +278 -0
- package/backends/cryptographic/index.js +196 -0
- package/backends/index.js +15 -0
- package/backends/interface.js +89 -0
- package/backends/scientific/index.js +272 -0
- package/backends/semantic/index.js +527 -0
- package/backends/semantic/surface.js +393 -0
- package/backends/semantic/two-layer.js +375 -0
- package/core/fano.js +127 -0
- package/core/hilbert.js +564 -0
- package/core/hypercomplex.js +141 -0
- package/core/index.js +133 -0
- package/core/llm.js +132 -0
- package/core/prime.js +184 -0
- package/core/resonance.js +695 -0
- package/core/rformer-tf.js +1086 -0
- package/core/rformer.js +806 -0
- package/core/sieve.js +350 -0
- package/data.json +8163 -0
- package/docs/EXAMPLES_PLAN.md +293 -0
- package/docs/README.md +159 -0
- package/docs/design/ALEPH_CHAT_ARCHITECTURE.md +499 -0
- package/docs/guide/01-quickstart.md +298 -0
- package/docs/guide/02-semantic-computing.md +409 -0
- package/docs/guide/03-cryptographic.md +420 -0
- package/docs/guide/04-scientific.md +494 -0
- package/docs/guide/05-llm-integration.md +568 -0
- package/docs/guide/06-advanced.md +996 -0
- package/docs/guide/README.md +188 -0
- package/docs/reference/01-core.md +695 -0
- package/docs/reference/02-physics.md +601 -0
- package/docs/reference/03-backends.md +892 -0
- package/docs/reference/04-engine.md +632 -0
- package/docs/reference/README.md +252 -0
- package/docs/theory/01-prime-semantics.md +327 -0
- package/docs/theory/02-hypercomplex-algebra.md +421 -0
- package/docs/theory/03-phase-synchronization.md +364 -0
- package/docs/theory/04-entropy-reasoning.md +348 -0
- package/docs/theory/05-non-commutativity.md +402 -0
- package/docs/theory/06-two-layer-meaning.md +414 -0
- package/docs/theory/07-resonant-field-interface.md +419 -0
- package/docs/theory/08-semantic-sieve.md +520 -0
- package/docs/theory/09-temporal-emergence.md +298 -0
- package/docs/theory/10-quaternionic-memory.md +415 -0
- package/docs/theory/README.md +162 -0
- package/engine/aleph.js +418 -0
- package/engine/index.js +7 -0
- package/index.js +23 -0
- package/modular.js +254 -0
- package/package.json +99 -0
- package/physics/collapse.js +95 -0
- package/physics/entropy.js +88 -0
- package/physics/index.js +65 -0
- package/physics/kuramoto.js +91 -0
- package/physics/lyapunov.js +80 -0
- package/physics/oscillator.js +95 -0
- package/types/index.d.ts +575 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Sebastian Schepis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
# @aleph-ai/tinyaleph
|
|
2
|
+
|
|
3
|
+
**Prime-resonant semantic computing framework**
|
|
4
|
+
|
|
5
|
+
A novel computational paradigm that encodes meaning as prime number signatures, embeds them in hypercomplex space, and performs reasoning through entropy minimization and oscillator synchronization.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@aleph-ai/tinyaleph)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Prime Semantics**: Encode concepts as unique prime number signatures
|
|
13
|
+
- **Hypercomplex Algebra**: 16-dimensional sedenion space with non-commutative multiplication
|
|
14
|
+
- **Oscillator Dynamics**: Kuramoto-model synchronization for coherent reasoning
|
|
15
|
+
- **Entropy Minimization**: Reasoning as reduction of semantic uncertainty
|
|
16
|
+
- **Multiple Backends**: Semantic (NLP), Cryptographic (hashing), Scientific (quantum-inspired)
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @aleph-ai/tinyaleph
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
const { createEngine, SemanticBackend } = require('@aleph-ai/tinyaleph');
|
|
28
|
+
|
|
29
|
+
// Load configuration
|
|
30
|
+
const config = require('@aleph-ai/tinyaleph/data.json');
|
|
31
|
+
|
|
32
|
+
// Create a semantic engine
|
|
33
|
+
const engine = createEngine('semantic', config);
|
|
34
|
+
|
|
35
|
+
// Process a query
|
|
36
|
+
const result = engine.run('What is the relationship between wisdom and truth?');
|
|
37
|
+
|
|
38
|
+
console.log('Output:', result.output);
|
|
39
|
+
console.log('Entropy:', result.entropy);
|
|
40
|
+
console.log('Steps:', result.steps.length);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Core Concepts
|
|
44
|
+
|
|
45
|
+
### Prime Encoding
|
|
46
|
+
|
|
47
|
+
Every concept maps to a unique set of prime numbers:
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
const backend = new SemanticBackend(config);
|
|
51
|
+
|
|
52
|
+
const primes = backend.encode('love and wisdom');
|
|
53
|
+
console.log(primes); // [2, 3, 5, 7, 11, ...]
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Hypercomplex States
|
|
57
|
+
|
|
58
|
+
Primes embed into 16-dimensional sedenion space:
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
const { Hypercomplex } = require('@aleph-ai/tinyaleph');
|
|
62
|
+
|
|
63
|
+
// Create a state
|
|
64
|
+
const state = new Hypercomplex(16);
|
|
65
|
+
state.excite([2, 3, 5]); // Excite with primes
|
|
66
|
+
|
|
67
|
+
// States support multiplication (non-commutative!)
|
|
68
|
+
const combined = state1.multiply(state2);
|
|
69
|
+
console.log(state1.multiply(state2) !== state2.multiply(state1)); // true
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Entropy-Based Reasoning
|
|
73
|
+
|
|
74
|
+
Reasoning reduces entropy through semantic transforms:
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
const engine = createEngine('semantic', config);
|
|
78
|
+
const result = engine.run('Confused question here');
|
|
79
|
+
|
|
80
|
+
// Watch entropy decrease through reasoning steps
|
|
81
|
+
for (const step of result.steps) {
|
|
82
|
+
console.log(`Step ${step.step}: entropy ${step.entropyAfter.toFixed(3)}`);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Backends
|
|
87
|
+
|
|
88
|
+
### Semantic Backend
|
|
89
|
+
|
|
90
|
+
Natural language understanding and concept mapping:
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
const { SemanticBackend } = require('@aleph-ai/tinyaleph');
|
|
94
|
+
|
|
95
|
+
const backend = new SemanticBackend(config);
|
|
96
|
+
|
|
97
|
+
// Tokenize
|
|
98
|
+
const tokens = backend.tokenize('Love is truth');
|
|
99
|
+
|
|
100
|
+
// Encode to primes
|
|
101
|
+
const primes = backend.encode('Love is truth');
|
|
102
|
+
|
|
103
|
+
// Decode back
|
|
104
|
+
const text = backend.decode(primes);
|
|
105
|
+
|
|
106
|
+
// Compare concepts
|
|
107
|
+
const state1 = backend.textToOrderedState('wisdom');
|
|
108
|
+
const state2 = backend.textToOrderedState('knowledge');
|
|
109
|
+
console.log('Similarity:', state1.coherence(state2));
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Cryptographic Backend
|
|
113
|
+
|
|
114
|
+
Semantic hashing and key derivation:
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
const { CryptographicBackend, hash, deriveKey } = require('@aleph-ai/tinyaleph');
|
|
118
|
+
|
|
119
|
+
// Quick hash
|
|
120
|
+
const h = hash('my secret data');
|
|
121
|
+
|
|
122
|
+
// Key derivation
|
|
123
|
+
const key = deriveKey('password', 'salt', 32, 10000);
|
|
124
|
+
|
|
125
|
+
// Full backend
|
|
126
|
+
const crypto = new CryptographicBackend(config);
|
|
127
|
+
const semanticHash = crypto.hash('similar meanings produce similar hashes');
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Scientific Backend
|
|
131
|
+
|
|
132
|
+
Quantum-inspired computation:
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
const { ScientificBackend } = require('@aleph-ai/tinyaleph');
|
|
136
|
+
|
|
137
|
+
const backend = new ScientificBackend(config);
|
|
138
|
+
|
|
139
|
+
// Create quantum-like states
|
|
140
|
+
const state = backend.createRandomState();
|
|
141
|
+
const basis = backend.createBasisState(0);
|
|
142
|
+
|
|
143
|
+
// Superposition
|
|
144
|
+
const superposition = backend.superpose(state, 0.5, basis, 0.5);
|
|
145
|
+
|
|
146
|
+
// Measurement
|
|
147
|
+
const result = backend.measure(superposition, [basis]);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Physics Engine
|
|
151
|
+
|
|
152
|
+
### Oscillators
|
|
153
|
+
|
|
154
|
+
```javascript
|
|
155
|
+
const { Oscillator, OscillatorBank, KuramotoModel } = require('@aleph-ai/tinyaleph');
|
|
156
|
+
|
|
157
|
+
// Create oscillator bank
|
|
158
|
+
const bank = new OscillatorBank(16);
|
|
159
|
+
|
|
160
|
+
// Excite with primes
|
|
161
|
+
bank.excite([2, 3, 5, 7]);
|
|
162
|
+
|
|
163
|
+
// Kuramoto synchronization
|
|
164
|
+
const kuramoto = new KuramotoModel(bank, { coupling: 0.1 });
|
|
165
|
+
kuramoto.step(0.01);
|
|
166
|
+
|
|
167
|
+
console.log('Order parameter:', kuramoto.orderParameter());
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Entropy and Stability
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
const { shannonEntropy, estimateLyapunov, stateEntropy } = require('@aleph-ai/tinyaleph');
|
|
174
|
+
|
|
175
|
+
// Calculate entropy
|
|
176
|
+
const entropy = stateEntropy(state);
|
|
177
|
+
|
|
178
|
+
// Estimate Lyapunov exponent for stability
|
|
179
|
+
const lambda = estimateLyapunov(entropyTimeSeries);
|
|
180
|
+
console.log('Stable:', lambda < 0);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## API Overview
|
|
184
|
+
|
|
185
|
+
### Main Exports
|
|
186
|
+
|
|
187
|
+
| Export | Description |
|
|
188
|
+
|--------|-------------|
|
|
189
|
+
| `createEngine(type, config)` | Create engine with backend |
|
|
190
|
+
| `AlephEngine` | Unified computation engine |
|
|
191
|
+
| `SemanticBackend` | Natural language processing |
|
|
192
|
+
| `CryptographicBackend` | Hashing and key derivation |
|
|
193
|
+
| `ScientificBackend` | Quantum-inspired computation |
|
|
194
|
+
| `Hypercomplex` | Sedenion algebra |
|
|
195
|
+
| `Oscillator` / `OscillatorBank` | Phase-amplitude oscillators |
|
|
196
|
+
| `KuramotoModel` | Coupled oscillator synchronization |
|
|
197
|
+
| `hash(input)` | Quick semantic hash |
|
|
198
|
+
| `deriveKey(pass, salt)` | Quick key derivation |
|
|
199
|
+
|
|
200
|
+
### Sub-modules
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
// Direct module access
|
|
204
|
+
const { core, physics, backends, engine } = require('@aleph-ai/tinyaleph');
|
|
205
|
+
|
|
206
|
+
// Or import sub-modules directly
|
|
207
|
+
const core = require('@aleph-ai/tinyaleph/core');
|
|
208
|
+
const physics = require('@aleph-ai/tinyaleph/physics');
|
|
209
|
+
const backends = require('@aleph-ai/tinyaleph/backends');
|
|
210
|
+
const engine = require('@aleph-ai/tinyaleph/engine');
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Documentation
|
|
214
|
+
|
|
215
|
+
Full documentation is available in the `docs/` directory:
|
|
216
|
+
|
|
217
|
+
- **[Theory](./docs/theory/README.md)**: Mathematical foundations
|
|
218
|
+
- Prime semantics, hypercomplex algebra, oscillator dynamics
|
|
219
|
+
- Entropy minimization, non-commutativity, temporal emergence
|
|
220
|
+
|
|
221
|
+
- **[Guide](./docs/guide/README.md)**: Practical tutorials
|
|
222
|
+
- Quick start, semantic computing, cryptographic applications
|
|
223
|
+
- Scientific computing, LLM integration, advanced topics
|
|
224
|
+
|
|
225
|
+
- **[Reference](./docs/reference/README.md)**: Complete API documentation
|
|
226
|
+
- Core module, physics module, backends, engine
|
|
227
|
+
|
|
228
|
+
## Examples
|
|
229
|
+
|
|
230
|
+
Run the included demos:
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
# Basic modular demo
|
|
234
|
+
npm run demo
|
|
235
|
+
|
|
236
|
+
# Two-layer meaning demo
|
|
237
|
+
npm run demo:two-layer
|
|
238
|
+
|
|
239
|
+
# Performance benchmark
|
|
240
|
+
npm run benchmark
|
|
241
|
+
|
|
242
|
+
# Interactive chat
|
|
243
|
+
npm run chat
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Architecture
|
|
247
|
+
|
|
248
|
+
```
|
|
249
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
250
|
+
│ AlephEngine │
|
|
251
|
+
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
|
|
252
|
+
│ │ Oscillators │◄─┤ Field │◄─┤ Transform │ │
|
|
253
|
+
│ │ (Kuramoto) │ │ (Sedenion) │ │ Pipeline │ │
|
|
254
|
+
│ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
|
|
255
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
256
|
+
│
|
|
257
|
+
┌────────────────────┼────────────────────┐
|
|
258
|
+
▼ ▼ ▼
|
|
259
|
+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
|
260
|
+
│ SemanticBackend │ │CryptographicBack│ │ScientificBackend│
|
|
261
|
+
│ │ │ │ │ │
|
|
262
|
+
│ • Tokenization │ │ • Hash │ │ • Quantum sim │
|
|
263
|
+
│ • Prime encode │ │ • Key derive │ │ • Wave collapse │
|
|
264
|
+
│ • Transforms │ │ • Verify │ │ • Measurement │
|
|
265
|
+
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Requirements
|
|
269
|
+
|
|
270
|
+
- Node.js >= 14.0.0
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
MIT © Sebastian Schepis
|
|
275
|
+
|
|
276
|
+
## Contributing
|
|
277
|
+
|
|
278
|
+
Contributions welcome! Please read the documentation in `docs/` before submitting PRs.
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cryptographic Backend - Prime-based hashing, encryption, and key derivation
|
|
3
|
+
*/
|
|
4
|
+
const { Backend } = require('../interface');
|
|
5
|
+
const { Hypercomplex } = require('../../core/hypercomplex');
|
|
6
|
+
const { GaussianInteger, primeToFrequency, isPrime, firstNPrimes } = require('../../core/prime');
|
|
7
|
+
|
|
8
|
+
class CryptographicBackend extends Backend {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
super(config);
|
|
11
|
+
this.dimension = config.dimension || 32; // Higher dimension for security
|
|
12
|
+
this.keyPrimes = config.keyPrimes || this.generateKeyPrimes(256);
|
|
13
|
+
this.config.primes = this.keyPrimes;
|
|
14
|
+
this.transforms = config.transforms || this.generateDefaultTransforms();
|
|
15
|
+
this.rounds = config.rounds || 16;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
generateKeyPrimes(count) {
|
|
19
|
+
return firstNPrimes(count);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
generateDefaultTransforms() {
|
|
23
|
+
// Generate mixing transforms for cryptographic operations
|
|
24
|
+
const transforms = [];
|
|
25
|
+
for (let i = 0; i < 16; i++) {
|
|
26
|
+
transforms.push({
|
|
27
|
+
n: `mix_${i}`,
|
|
28
|
+
key: this.keyPrimes.slice(i * 4, i * 4 + 4)
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return transforms;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
encode(input) {
|
|
35
|
+
const bytes = typeof input === 'string'
|
|
36
|
+
? Buffer.from(input, 'utf8')
|
|
37
|
+
: Buffer.isBuffer(input)
|
|
38
|
+
? input
|
|
39
|
+
: Buffer.from(input);
|
|
40
|
+
return [...bytes].map(b => this.keyPrimes[b % this.keyPrimes.length]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
decode(primes) {
|
|
44
|
+
const primeToIndex = new Map(this.keyPrimes.map((p, i) => [p, i]));
|
|
45
|
+
const bytes = primes.map(p => primeToIndex.get(p) || 0);
|
|
46
|
+
return Buffer.from(bytes);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
primesToState(primes) {
|
|
50
|
+
const state = Hypercomplex.zero(this.dimension);
|
|
51
|
+
for (let i = 0; i < primes.length; i++) {
|
|
52
|
+
const p = primes[i];
|
|
53
|
+
const angle = (2 * Math.PI * p) / this.dimension;
|
|
54
|
+
const idx = i % this.dimension;
|
|
55
|
+
|
|
56
|
+
// Use Gaussian integer decomposition for primes ≡ 1 (mod 4)
|
|
57
|
+
if (p % 4 === 1) {
|
|
58
|
+
const gi = new GaussianInteger(Math.cos(angle), Math.sin(angle));
|
|
59
|
+
state.c[idx] += gi.real / Math.sqrt(primes.length || 1);
|
|
60
|
+
state.c[(idx + 1) % this.dimension] += gi.imag / Math.sqrt(primes.length || 1);
|
|
61
|
+
} else {
|
|
62
|
+
state.c[idx] += Math.cos(angle * p) / Math.sqrt(primes.length || 1);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return state.normalize();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
primesToFrequencies(primes) {
|
|
69
|
+
// Use tighter frequency range for cryptographic mixing
|
|
70
|
+
return primes.map(p => primeToFrequency(p, 1, 5));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
applyTransform(inputPrimes, transform) {
|
|
74
|
+
const key = transform.key || [2, 3, 5, 7];
|
|
75
|
+
return inputPrimes.map((p, i) => {
|
|
76
|
+
const k = key[i % key.length];
|
|
77
|
+
const product = p * k;
|
|
78
|
+
return this.keyPrimes[product % this.keyPrimes.length];
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Hash input using hypercomplex state mixing
|
|
84
|
+
*/
|
|
85
|
+
hash(input, outputLength = 32) {
|
|
86
|
+
const primes = this.encode(input);
|
|
87
|
+
let state = this.primesToState(primes);
|
|
88
|
+
|
|
89
|
+
// Create input-dependent mixing constants
|
|
90
|
+
const inputSum = primes.reduce((a, b) => a + b, 0);
|
|
91
|
+
const inputXor = primes.reduce((a, b) => a ^ b, 0);
|
|
92
|
+
|
|
93
|
+
// Multiple rounds of mixing
|
|
94
|
+
for (let i = 0; i < this.rounds; i++) {
|
|
95
|
+
// Mix with shifted version of self for non-commutativity
|
|
96
|
+
const shifted = Hypercomplex.zero(this.dimension);
|
|
97
|
+
for (let j = 0; j < this.dimension; j++) {
|
|
98
|
+
shifted.c[(j + i + 1) % this.dimension] = state.c[j];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Combine original and shifted
|
|
102
|
+
state = state.mul(shifted).normalize();
|
|
103
|
+
|
|
104
|
+
// Add input-dependent round constant
|
|
105
|
+
const roundConst = Hypercomplex.zero(this.dimension);
|
|
106
|
+
const idx = (i + inputXor) % this.dimension;
|
|
107
|
+
roundConst.c[idx] = 0.1 * Math.sin(inputSum + i);
|
|
108
|
+
roundConst.c[(idx + 7) % this.dimension] = 0.1 * Math.cos(inputSum + i);
|
|
109
|
+
state = state.add(roundConst).normalize();
|
|
110
|
+
|
|
111
|
+
// Non-linear transformation
|
|
112
|
+
for (let j = 0; j < this.dimension; j++) {
|
|
113
|
+
state.c[j] = Math.tanh(state.c[j] * 2);
|
|
114
|
+
}
|
|
115
|
+
state = state.normalize();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Extract hash bytes from state with input-dependent scrambling
|
|
119
|
+
const hashBytes = [];
|
|
120
|
+
for (let i = 0; i < outputLength; i++) {
|
|
121
|
+
const idx = (i + inputXor) % this.dimension;
|
|
122
|
+
const val = Math.abs(state.c[idx] * 127.5 + state.c[(idx + 1) % this.dimension] * 127.5);
|
|
123
|
+
hashBytes.push(Math.floor(val) & 0xFF);
|
|
124
|
+
}
|
|
125
|
+
return Buffer.from(hashBytes);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Derive key from password using salt and iterations
|
|
130
|
+
*/
|
|
131
|
+
deriveKey(password, salt, keyLength = 32, iterations = 10000) {
|
|
132
|
+
let primes = this.encode(password);
|
|
133
|
+
const saltPrimes = this.encode(salt);
|
|
134
|
+
|
|
135
|
+
for (let i = 0; i < iterations; i++) {
|
|
136
|
+
// Mix password and salt primes
|
|
137
|
+
primes = [...primes, ...saltPrimes];
|
|
138
|
+
const state = this.primesToState(primes);
|
|
139
|
+
|
|
140
|
+
// Apply iteration-dependent mixing
|
|
141
|
+
const mixed = state.mul(Hypercomplex.fromReal(this.dimension, i + 1));
|
|
142
|
+
|
|
143
|
+
// Extract new primes from mixed state
|
|
144
|
+
primes = [];
|
|
145
|
+
for (let j = 0; j < this.dimension; j++) {
|
|
146
|
+
const idx = Math.floor(Math.abs(mixed.c[j]) * this.keyPrimes.length);
|
|
147
|
+
primes.push(this.keyPrimes[idx % this.keyPrimes.length]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return this.hash(Buffer.from(primes), keyLength);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Generate random primes for key material
|
|
156
|
+
*/
|
|
157
|
+
generateRandomPrimes(count) {
|
|
158
|
+
const primes = [];
|
|
159
|
+
for (let i = 0; i < count; i++) {
|
|
160
|
+
const idx = Math.floor(Math.random() * this.keyPrimes.length);
|
|
161
|
+
primes.push(this.keyPrimes[idx]);
|
|
162
|
+
}
|
|
163
|
+
return primes;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* XOR-style prime mixing for encryption
|
|
168
|
+
*/
|
|
169
|
+
mixPrimes(dataPrimes, keyPrimes) {
|
|
170
|
+
return dataPrimes.map((p, i) => {
|
|
171
|
+
const k = keyPrimes[i % keyPrimes.length];
|
|
172
|
+
const mixed = (p * k) % (this.keyPrimes[this.keyPrimes.length - 1] + 1);
|
|
173
|
+
// Find nearest prime
|
|
174
|
+
let result = this.keyPrimes.find(q => q >= mixed) || this.keyPrimes[0];
|
|
175
|
+
return result;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Compute HMAC-like authentication code
|
|
181
|
+
*/
|
|
182
|
+
hmac(key, message, outputLength = 32) {
|
|
183
|
+
const keyPrimes = this.encode(key);
|
|
184
|
+
const msgPrimes = this.encode(message);
|
|
185
|
+
|
|
186
|
+
// Inner hash
|
|
187
|
+
const innerPrimes = this.mixPrimes(msgPrimes, keyPrimes);
|
|
188
|
+
const innerHash = this.hash(Buffer.from(innerPrimes), this.dimension);
|
|
189
|
+
|
|
190
|
+
// Outer hash
|
|
191
|
+
const outerPrimes = this.mixPrimes(this.encode(innerHash), keyPrimes);
|
|
192
|
+
return this.hash(Buffer.from(outerPrimes), outputLength);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = { CryptographicBackend };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backends - exports all domain backends
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const { Backend } = require('./interface');
|
|
6
|
+
const { SemanticBackend } = require('./semantic');
|
|
7
|
+
const { CryptographicBackend } = require('./cryptographic');
|
|
8
|
+
const { ScientificBackend } = require('./scientific');
|
|
9
|
+
|
|
10
|
+
module.exports = {
|
|
11
|
+
Backend,
|
|
12
|
+
SemanticBackend,
|
|
13
|
+
CryptographicBackend,
|
|
14
|
+
ScientificBackend
|
|
15
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common interface for all domain backends
|
|
3
|
+
*/
|
|
4
|
+
class Backend {
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.config = config;
|
|
7
|
+
this.dimension = config.dimension || 16;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Encode domain-specific input into prime representation
|
|
12
|
+
* @param {*} input - Domain-specific input
|
|
13
|
+
* @returns {number[]} Array of primes
|
|
14
|
+
*/
|
|
15
|
+
encode(input) {
|
|
16
|
+
throw new Error('Backend.encode() must be implemented');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Decode prime representation back to domain-specific output
|
|
21
|
+
* @param {number[]} primes - Array of primes
|
|
22
|
+
* @returns {*} Domain-specific output
|
|
23
|
+
*/
|
|
24
|
+
decode(primes) {
|
|
25
|
+
throw new Error('Backend.decode() must be implemented');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Convert primes to hypercomplex state vector
|
|
30
|
+
* @param {number[]} primes - Array of primes
|
|
31
|
+
* @returns {Hypercomplex} State vector
|
|
32
|
+
*/
|
|
33
|
+
primesToState(primes) {
|
|
34
|
+
throw new Error('Backend.primesToState() must be implemented');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Convert primes to oscillator frequencies
|
|
39
|
+
* @param {number[]} primes - Array of primes
|
|
40
|
+
* @returns {number[]} Array of frequencies
|
|
41
|
+
*/
|
|
42
|
+
primesToFrequencies(primes) {
|
|
43
|
+
throw new Error('Backend.primesToFrequencies() must be implemented');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Apply a domain-specific transform to primes
|
|
48
|
+
* @param {number[]} inputPrimes - Input primes
|
|
49
|
+
* @param {object} transform - Transform specification
|
|
50
|
+
* @returns {number[]} Transformed primes
|
|
51
|
+
*/
|
|
52
|
+
applyTransform(inputPrimes, transform) {
|
|
53
|
+
throw new Error('Backend.applyTransform() must be implemented');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get available transforms
|
|
58
|
+
* @returns {object[]} Array of transform specifications
|
|
59
|
+
*/
|
|
60
|
+
getTransforms() {
|
|
61
|
+
return this.config.transforms || [];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Get prime list used by this backend
|
|
66
|
+
* @returns {number[]} Array of primes
|
|
67
|
+
*/
|
|
68
|
+
getPrimes() {
|
|
69
|
+
return this.config.primes;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Get semantic axes (for backends that use them)
|
|
74
|
+
* @returns {object} Axes mapping
|
|
75
|
+
*/
|
|
76
|
+
getAxes() {
|
|
77
|
+
return this.config.axes;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get backend name/type
|
|
82
|
+
* @returns {string}
|
|
83
|
+
*/
|
|
84
|
+
getName() {
|
|
85
|
+
return this.constructor.name;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = { Backend };
|