File size: 2,847 Bytes
224e773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*

 * qra.h — Discrete Agent Routing Automata (QRA)

 *

 * Routing alphabet (6 glyphs):

 *   Π (PI, 0x01)     - identity/continuation

 *   Γ (GAMMA, 0x03)  - intermediate state

 *   Δ (DELTA, 0x04)  - transition

 *   Ω (OMEGA, 0x0A)  - absorbing element (termination)

 *   Λ (LAMBDA, 0xFF) - left identity

 *   Ψ (PSI, 0x0B)    - control element

 *

 * Routing tensor Q ∈ {0,...,5}^{6×6}:

 *   Q[current_glyph][previous_glyph] = next_glyph

 *

 * Key properties:

 *   - H(next | current, previous) = 0 nats (deterministic)

 *   - Λ is left identity: Q[Λ][j] = j

 *   - Ω is absorber: Q[Ω][j] = Ω

 *   - Token lifetime via algebraic exhaustion (witness → absorption)

 */

#pragma once
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct RoutingTensor RoutingTensor;

typedef struct {
    int glyphs[3];  /* [w0, w1, w2]: current witness state */
} Witness;

/**

 * hk_qra_step — Deterministic routing step

 *

 * @param current    Current glyph

 * @param previous   Previous glyph

 * @param rt         Routing tensor

 * @return Next glyph

 */
int hk_qra_step(int current, int previous, RoutingTensor *rt);

/**

 * hk_qra_evolve_witness — Evolve witness vector

 *

 * w' = [Q(w0, w1), Q(w1, w2), Q(w2, w0)]

 *

 * @param w   Current witness

 * @param rt  Routing tensor

 * @return Next witness

 */
Witness hk_qra_evolve_witness(Witness w, RoutingTensor *rt);

/**

 * hk_qra_is_absorbed — Check if witness has reached absorber

 *

 * Returns 1 if all three glyphs are Ω (omega).

 *

 * @param w  Witness

 * @return 1 if absorbed, 0 otherwise

 */
int hk_qra_is_absorbed(Witness w);

/**

 * hk_qra_is_fixed — Check if witness is in fixed-point loop

 *

 * Returns 1 if all three glyphs are Λ (lambda, left identity).

 * Fixed points are INVALID for tokens (no replay resistance).

 *

 * @param w  Witness

 * @return 1 if fixed point, 0 otherwise

 */
int hk_qra_is_fixed(Witness w);

/**

 * hk_qra_token_lifetime — Compute steps to absorption

 *

 * Iteratively evolves witness until absorption (or max_steps reached).

 * Returns number of evolution steps. Rejects fixed points (returns -1).

 *

 * Token lifetime defines stateful replay resistance:

 *   - Token issued with witness w_0

 *   - Each use evolves witness: w_i → w_{i+1}

 *   - After T steps, w_T = [Ω, Ω, Ω] (absorbed, token invalid)

 *

 * @param initial   Initial witness

 * @param rt        Routing tensor

 * @param max_steps Maximum evolution iterations

 * @return Steps to absorption, or -1 if invalid/no absorption

 */
int hk_qra_token_lifetime(Witness initial, RoutingTensor *rt, int max_steps);

/**

 * Debug output

 */
void hk_qra_print_tensor(RoutingTensor *rt);
void hk_qra_print_witness(Witness w);

#ifdef __cplusplus
}
#endif