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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// ALICE-Auth Unity C# Bindings
// Ed25519 ZKP authentication via P/Invoke (C-ABI FFI)
//
// Author: Moroya Sakamoto
// License: AGPL-3.0
//
// Usage:
// 1. Build alice-auth as cdylib: cargo build --release --features "ffi,nizk"
// 2. Copy libalice_auth.dylib/.so/.dll to Assets/Plugins/
// 3. Use AliceIdentity, AliceNizk, etc. in your scripts.
using System;
using System.Runtime.InteropServices;
namespace Alice.Auth
{
// ========================================================================
// Native P/Invoke declarations (all 28 C-ABI functions)
// ========================================================================
internal static class Native
{
const string Lib = "alice_auth";
// --- Core Identity ---
[DllImport(Lib)] internal static extern IntPtr aa_new();
[DllImport(Lib)] internal static extern void aa_id(IntPtr h, byte[] o);
[DllImport(Lib)] internal static extern void aa_sign(IntPtr h, byte[] c, byte[] o);
[DllImport(Lib)] internal static extern int aa_verify(byte[] pk, byte[] m, UIntPtr ml, byte[] s);
[DllImport(Lib)] internal static extern void aa_free(IntPtr h);
// --- Endorsement ---
[DllImport(Lib)] internal static extern void aa_endorse(IntPtr h, byte[] target, ulong now_ms, ulong ttl_ms, byte[] o);
[DllImport(Lib)] internal static extern int aa_verify_endorsement(byte[] data, ulong now_ms);
// --- RotatingIdentity ---
[DllImport(Lib)] internal static extern IntPtr aa_rotating_new();
[DllImport(Lib)] internal static extern int aa_rotating_rotate(IntPtr h, ulong now_ms, byte[] o);
[DllImport(Lib)] internal static extern void aa_rotating_id(IntPtr h, byte[] o);
[DllImport(Lib)] internal static extern int aa_rotating_verify(IntPtr h, byte[] pk, byte[] m, UIntPtr ml, byte[] s);
[DllImport(Lib)] internal static extern uint aa_rotating_generation_count(IntPtr h);
[DllImport(Lib)] internal static extern void aa_rotating_free(IntPtr h);
// --- AuthToken ---
[DllImport(Lib)] internal static extern void aa_token_create(ulong now_ms, ulong ttl_ms, byte[] o);
[DllImport(Lib)] internal static extern int aa_token_is_expired(byte[] data, ulong now_ms);
// --- RevocationList ---
[DllImport(Lib)] internal static extern IntPtr aa_revlist_new();
[DllImport(Lib)] internal static extern void aa_revlist_revoke(IntPtr h, byte[] token, ulong now_ms);
[DllImport(Lib)] internal static extern int aa_revlist_is_revoked(IntPtr h, byte[] token);
[DllImport(Lib)] internal static extern uint aa_revlist_auto_purge(IntPtr h, ulong now_ms, ulong ttl_ms);
[DllImport(Lib)] internal static extern void aa_revlist_free(IntPtr h);
// --- PolicyEngine (RBAC) ---
[DllImport(Lib)] internal static extern IntPtr aa_policy_new();
[DllImport(Lib)] internal static extern void aa_policy_assign(IntPtr h, byte[] id, byte mask);
[DllImport(Lib)] internal static extern int aa_policy_check(IntPtr h, byte[] id, byte perm);
[DllImport(Lib)] internal static extern void aa_policy_free(IntPtr h);
// --- NIZK (requires ffi+nizk features) ---
[DllImport(Lib)] internal static extern int aa_nizk_prove(IntPtr h, byte[] m, UIntPtr ml, byte[] o);
[DllImport(Lib)] internal static extern int aa_nizk_verify(byte[] pk, byte[] m, UIntPtr ml, byte[] proof);
// --- Crypto (requires ffi+crypto features) ---
[DllImport(Lib)] internal static extern IntPtr aa_derive_child(IntPtr h, uint index);
[DllImport(Lib)] internal static extern void aa_derive_session_key(byte[] id_a, byte[] id_b, byte[] secret, UIntPtr secret_len, byte[] o);
}
// ========================================================================
// AliceIdentity (IDisposable, opaque handle)
// ========================================================================
/// <summary>
/// Ed25519 identity. Secret key stays in Rust memory.
/// </summary>
public sealed class AliceIdentity : IDisposable
{
internal IntPtr handle;
/// <summary>Generate a new random Ed25519 identity.</summary>
public AliceIdentity()
{
handle = Native.aa_new();
if (handle == IntPtr.Zero)
throw new InvalidOperationException("ALICE-Auth: keygen failed (RNG error)");
}
internal AliceIdentity(IntPtr ptr)
{
handle = ptr;
if (handle == IntPtr.Zero)
throw new InvalidOperationException("ALICE-Auth: null handle");
}
/// <summary>Get the 32-byte public identity.</summary>
public byte[] Id()
{
var o = new byte[32];
Native.aa_id(handle, o);
return o;
}
/// <summary>Sign a 32-byte challenge. Returns 64-byte signature.</summary>
public byte[] Sign(byte[] challenge)
{
if (challenge == null || challenge.Length != 32)
throw new ArgumentException("challenge must be 32 bytes");
var o = new byte[64];
Native.aa_sign(handle, challenge, o);
return o;
}
/// <summary>Verify an Ed25519 signature over a message.</summary>
public static bool Verify(byte[] publicKey, byte[] message, byte[] signature)
{
if (publicKey == null || publicKey.Length != 32) return false;
if (message == null) return false;
if (signature == null || signature.Length != 64) return false;
return Native.aa_verify(publicKey, message, (UIntPtr)message.Length, signature) != 0;
}
public void Dispose()
{
if (handle != IntPtr.Zero)
{
Native.aa_free(handle);
handle = IntPtr.Zero;
}
}
~AliceIdentity() => Dispose();
}
// ========================================================================
// AliceEndorsement
// ========================================================================
/// <summary>Trust chain endorsement with expiry.</summary>
public static class AliceEndorsement
{
/// <summary>
/// Endorse a target identity. Returns 176-byte endorsement.
/// Layout: endorser(32) || endorsed(32) || sig(64) || issued_ms(8) || expires_ms(8) || pad(32)
/// </summary>
public static byte[] Endorse(AliceIdentity signer, byte[] targetId, ulong nowMs, ulong ttlMs)
{
if (targetId == null || targetId.Length != 32)
throw new ArgumentException("targetId must be 32 bytes");
var o = new byte[176];
Native.aa_endorse(signer.handle, targetId, nowMs, ttlMs, o);
return o;
}
/// <summary>Verify an endorsement (144 bytes minimum). Returns true if valid and not expired.</summary>
public static bool Verify(byte[] endorsementData, ulong nowMs)
{
if (endorsementData == null || endorsementData.Length < 144) return false;
return Native.aa_verify_endorsement(endorsementData, nowMs) != 0;
}
}
// ========================================================================
// AliceRotatingIdentity (IDisposable)
// ========================================================================
/// <summary>Identity with N-generation key rotation.</summary>
public sealed class AliceRotatingIdentity : IDisposable
{
IntPtr handle;
public AliceRotatingIdentity()
{
handle = Native.aa_rotating_new();
if (handle == IntPtr.Zero)
throw new InvalidOperationException("ALICE-Auth: rotating keygen failed");
}
/// <summary>Rotate to a new keypair. Returns the new 32-byte public ID.</summary>
public byte[] Rotate(ulong nowMs)
{
var o = new byte[32];
if (Native.aa_rotating_rotate(handle, nowMs, o) == 0)
throw new InvalidOperationException("ALICE-Auth: rotation failed");
return o;
}
/// <summary>Get the current 32-byte public ID.</summary>
public byte[] CurrentId()
{
var o = new byte[32];
Native.aa_rotating_id(handle, o);
return o;
}
/// <summary>Verify a signature against any key (current + all previous).</summary>
public bool VerifyAny(byte[] publicKey, byte[] message, byte[] signature)
{
if (publicKey == null || publicKey.Length != 32) return false;
if (message == null) return false;
if (signature == null || signature.Length != 64) return false;
return Native.aa_rotating_verify(handle, publicKey, message, (UIntPtr)message.Length, signature) != 0;
}
/// <summary>Number of retained previous generations.</summary>
public uint GenerationCount => Native.aa_rotating_generation_count(handle);
public void Dispose()
{
if (handle != IntPtr.Zero)
{
Native.aa_rotating_free(handle);
handle = IntPtr.Zero;
}
}
~AliceRotatingIdentity() => Dispose();
}
// ========================================================================
// AliceAuthToken
// ========================================================================
/// <summary>Structured authentication token (17 bytes).</summary>
public static class AliceAuthToken
{
/// <summary>Create a token. Returns 17 bytes.</summary>
public static byte[] Create(ulong nowMs, ulong ttlMs)
{
var o = new byte[17];
Native.aa_token_create(nowMs, ttlMs, o);
return o;
}
/// <summary>Check if a token is expired.</summary>
public static bool IsExpired(byte[] tokenData, ulong nowMs)
{
if (tokenData == null || tokenData.Length < 17) return true;
return Native.aa_token_is_expired(tokenData, nowMs) != 0;
}
}
// ========================================================================
// AliceRevocationList (IDisposable)
// ========================================================================
/// <summary>Session token revocation list with constant-time checks.</summary>
public sealed class AliceRevocationList : IDisposable
{
IntPtr handle;
public AliceRevocationList()
{
handle = Native.aa_revlist_new();
if (handle == IntPtr.Zero)
throw new InvalidOperationException("ALICE-Auth: revlist alloc failed");
}
/// <summary>Revoke a 16-byte session token.</summary>
public void Revoke(byte[] token, ulong nowMs)
{
if (token == null || token.Length != 16)
throw new ArgumentException("token must be 16 bytes");
Native.aa_revlist_revoke(handle, token, nowMs);
}
/// <summary>Check if a token is revoked (constant-time).</summary>
public bool IsRevoked(byte[] token)
{
if (token == null || token.Length != 16) return false;
return Native.aa_revlist_is_revoked(handle, token) != 0;
}
/// <summary>Purge tokens revoked more than ttlMs ago. Returns count purged.</summary>
public uint AutoPurge(ulong nowMs, ulong ttlMs) => Native.aa_revlist_auto_purge(handle, nowMs, ttlMs);
public void Dispose()
{
if (handle != IntPtr.Zero)
{
Native.aa_revlist_free(handle);
handle = IntPtr.Zero;
}
}
~AliceRevocationList() => Dispose();
}
// ========================================================================
// AlicePolicyEngine (IDisposable, RBAC)
// ========================================================================
/// <summary>Role-based access control engine.</summary>
public sealed class AlicePolicyEngine : IDisposable
{
IntPtr handle;
/// <summary>Permission flags (bit index).</summary>
public const byte Read = 0;
public const byte Write = 1;
public const byte Admin = 2;
public const byte Execute = 3;
public AlicePolicyEngine()
{
handle = Native.aa_policy_new();
if (handle == IntPtr.Zero)
throw new InvalidOperationException("ALICE-Auth: policy alloc failed");
}
/// <summary>Assign a role mask to an identity.</summary>
public void Assign(byte[] id, byte mask)
{
if (id == null || id.Length != 32)
throw new ArgumentException("id must be 32 bytes");
Native.aa_policy_assign(handle, id, mask);
}
/// <summary>Check if an identity has a permission (0=Read,1=Write,2=Admin,3=Execute).</summary>
public bool Check(byte[] id, byte permission)
{
if (id == null || id.Length != 32) return false;
return Native.aa_policy_check(handle, id, permission) != 0;
}
public void Dispose()
{
if (handle != IntPtr.Zero)
{
Native.aa_policy_free(handle);
handle = IntPtr.Zero;
}
}
~AlicePolicyEngine() => Dispose();
}
// ========================================================================
// AliceNizk (Schnorr NIZK — requires ffi+nizk features)
// ========================================================================
/// <summary>Schnorr NIZK zero-knowledge proof.</summary>
public static class AliceNizk
{
/// <summary>Generate a NIZK proof. Returns 64 bytes (R || s).</summary>
public static byte[] Prove(AliceIdentity identity, byte[] message)
{
if (message == null) throw new ArgumentNullException(nameof(message));
var o = new byte[64];
if (Native.aa_nizk_prove(identity.handle, message, (UIntPtr)message.Length, o) == 0)
throw new InvalidOperationException("ALICE-Auth: NIZK prove failed");
return o;
}
/// <summary>Verify a NIZK proof. Returns true if valid.</summary>
public static bool Verify(byte[] publicKey, byte[] message, byte[] proof)
{
if (publicKey == null || publicKey.Length != 32) return false;
if (message == null) return false;
if (proof == null || proof.Length != 64) return false;
return Native.aa_nizk_verify(publicKey, message, (UIntPtr)message.Length, proof) != 0;
}
}
// ========================================================================
// AliceCrypto (HD derivation — requires ffi+crypto features)
// ========================================================================
/// <summary>Cryptographic utilities: HD key derivation, session keys.</summary>
public static class AliceCrypto
{
/// <summary>Derive a child identity from parent + index.</summary>
public static AliceIdentity DeriveChild(AliceIdentity parent, uint index)
{
var ptr = Native.aa_derive_child(parent.handle, index);
return new AliceIdentity(ptr);
}
/// <summary>Derive a 32-byte session key from two IDs and a shared secret.</summary>
public static byte[] DeriveSessionKey(byte[] idA, byte[] idB, byte[] sharedSecret)
{
if (idA == null || idA.Length != 32) throw new ArgumentException("idA must be 32 bytes");
if (idB == null || idB.Length != 32) throw new ArgumentException("idB must be 32 bytes");
if (sharedSecret == null) throw new ArgumentNullException(nameof(sharedSecret));
var o = new byte[32];
Native.aa_derive_session_key(idA, idB, sharedSecret, (UIntPtr)sharedSecret.Length, o);
return o;
}
}
}