@arcium-hq/client 0.9.2 → 0.9.3

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.
Files changed (92) hide show
  1. package/build/index.cjs +15 -3
  2. package/build/index.mjs +15 -4
  3. package/build/types/arcis/arcisModule.d.ts +26 -0
  4. package/build/types/arcis/arcisModule.d.ts.map +1 -0
  5. package/build/types/arcis/arcisType.d.ts +76 -0
  6. package/build/types/arcis/arcisType.d.ts.map +1 -0
  7. package/build/types/arcis/packer.d.ts +63 -0
  8. package/build/types/arcis/packer.d.ts.map +1 -0
  9. package/build/types/arcis/packing.d.ts +33 -0
  10. package/build/types/arcis/packing.d.ts.map +1 -0
  11. package/build/types/callback.d.ts +21 -0
  12. package/build/types/callback.d.ts.map +1 -0
  13. package/build/types/constants.d.ts +101 -0
  14. package/build/types/constants.d.ts.map +1 -0
  15. package/build/types/cryptography/aes128Cipher.d.ts +14 -0
  16. package/build/types/cryptography/aes128Cipher.d.ts.map +1 -0
  17. package/build/types/cryptography/aes192Cipher.d.ts +14 -0
  18. package/build/types/cryptography/aes192Cipher.d.ts.map +1 -0
  19. package/build/types/cryptography/aes256Cipher.d.ts +14 -0
  20. package/build/types/cryptography/aes256Cipher.d.ts.map +1 -0
  21. package/build/types/cryptography/aesCtrCipher.d.ts +36 -0
  22. package/build/types/cryptography/aesCtrCipher.d.ts.map +1 -0
  23. package/build/types/cryptography/arcisEd25519.d.ts +8 -0
  24. package/build/types/cryptography/arcisEd25519.d.ts.map +1 -0
  25. package/build/types/cryptography/cSplRescueCipher.d.ts +29 -0
  26. package/build/types/cryptography/cSplRescueCipher.d.ts.map +1 -0
  27. package/build/types/cryptography/cryptography.d.ts +38 -0
  28. package/build/types/cryptography/cryptography.d.ts.map +1 -0
  29. package/build/types/cryptography/hkdf.d.ts +37 -0
  30. package/build/types/cryptography/hkdf.d.ts.map +1 -0
  31. package/build/types/cryptography/hmac.d.ts +22 -0
  32. package/build/types/cryptography/hmac.d.ts.map +1 -0
  33. package/build/types/cryptography/rescueCipher.d.ts +29 -0
  34. package/build/types/cryptography/rescueCipher.d.ts.map +1 -0
  35. package/build/types/cryptography/rescueCipherCommon.d.ts +45 -0
  36. package/build/types/cryptography/rescueCipherCommon.d.ts.map +1 -0
  37. package/build/types/cryptography/rescueDesc.d.ts +80 -0
  38. package/build/types/cryptography/rescueDesc.d.ts.map +1 -0
  39. package/build/types/cryptography/rescuePrimeHash.d.ts +23 -0
  40. package/build/types/cryptography/rescuePrimeHash.d.ts.map +1 -0
  41. package/build/types/ctUtils.d.ts +50 -0
  42. package/build/types/ctUtils.d.ts.map +1 -0
  43. package/build/{index.d.ts → types/idl/arcium.d.ts} +5 -901
  44. package/build/types/idl/arcium.d.ts.map +1 -0
  45. package/build/types/idl/arcium_staking.d.ts +4589 -0
  46. package/build/types/idl/arcium_staking.d.ts.map +1 -0
  47. package/build/types/idl/index.d.ts +15 -0
  48. package/build/types/idl/index.d.ts.map +1 -0
  49. package/build/types/index.d.ts +33 -0
  50. package/build/types/index.d.ts.map +1 -0
  51. package/build/types/localEnv.d.ts +15 -0
  52. package/build/types/localEnv.d.ts.map +1 -0
  53. package/build/types/matrix.d.ts +39 -0
  54. package/build/types/matrix.d.ts.map +1 -0
  55. package/build/types/onchain.d.ts +223 -0
  56. package/build/types/onchain.d.ts.map +1 -0
  57. package/build/types/pda.d.ts +89 -0
  58. package/build/types/pda.d.ts.map +1 -0
  59. package/build/types/utils.d.ts +65 -0
  60. package/build/types/utils.d.ts.map +1 -0
  61. package/package.json +6 -6
  62. package/src/arcis/arcisModule.ts +39 -0
  63. package/src/arcis/arcisType.ts +303 -0
  64. package/src/arcis/packer.ts +152 -0
  65. package/src/arcis/packing.ts +115 -0
  66. package/src/callback.ts +101 -0
  67. package/src/constants.ts +104 -0
  68. package/src/cryptography/aes128Cipher.ts +16 -0
  69. package/src/cryptography/aes192Cipher.ts +16 -0
  70. package/src/cryptography/aes256Cipher.ts +16 -0
  71. package/src/cryptography/aesCtrCipher.ts +84 -0
  72. package/src/cryptography/arcisEd25519.ts +96 -0
  73. package/src/cryptography/cSplRescueCipher.ts +41 -0
  74. package/src/cryptography/cryptography.ts +82 -0
  75. package/src/cryptography/hkdf.ts +58 -0
  76. package/src/cryptography/hmac.ts +66 -0
  77. package/src/cryptography/rescueCipher.ts +41 -0
  78. package/src/cryptography/rescueCipherCommon.ts +211 -0
  79. package/src/cryptography/rescueDesc.ts +492 -0
  80. package/src/cryptography/rescuePrimeHash.ts +72 -0
  81. package/src/ctUtils.ts +124 -0
  82. package/src/idl/arcium.json +12281 -0
  83. package/src/idl/arcium.ts +12287 -0
  84. package/src/idl/arcium_staking.json +4582 -0
  85. package/src/idl/arcium_staking.ts +4588 -0
  86. package/src/idl/index.ts +20 -0
  87. package/src/index.ts +32 -0
  88. package/src/localEnv.ts +39 -0
  89. package/src/matrix.ts +215 -0
  90. package/src/onchain.ts +1020 -0
  91. package/src/pda.ts +203 -0
  92. package/src/utils.ts +126 -0
package/src/pda.ts ADDED
@@ -0,0 +1,203 @@
1
+ import { PublicKey, AddressLookupTableProgram } from '@solana/web3.js';
2
+ import * as anchor from '@coral-xyz/anchor';
3
+ import {
4
+ ARX_NODE_ACC_SEED,
5
+ CLOCK_ACC_SEED,
6
+ CLUSTER_ACC_SEED,
7
+ COMP_DEF_ACC_SEED,
8
+ COMPUTATION_ACC_SEED,
9
+ EXEC_POOL_ACC_SEED,
10
+ MEMPOOL_ACC_SEED,
11
+ MXE_ACCOUNT_SEED,
12
+ MXE_RECOVERY_ACC_SEED,
13
+ OFFSET_BUFFER_SIZE,
14
+ POOL_ACC_SEED,
15
+ RAW_CIRCUIT_ACC_SEED,
16
+ RECOVERY_CLUSTER_ACC_SEED,
17
+ } from './constants.js';
18
+ import { ARCIUM_ADDR } from './idl/index.js';
19
+
20
+ /**
21
+ * Return the public key of the deployed Arcium program on Solana.
22
+ * @returns Arcium program's public key.
23
+ */
24
+ export function getArciumProgramId(): PublicKey {
25
+ return new anchor.web3.PublicKey(ARCIUM_ADDR);
26
+ }
27
+
28
+ /**
29
+ * Derive the computation account address for a given cluster and computation offset.
30
+ * @param clusterOffset - Offset of the cluster this computation will be executed by.
31
+ * @param computationOffset - Computation offset as an anchor.BN.
32
+ * @returns Derived computation account public key.
33
+ */
34
+ export function getComputationAccAddress(clusterOffset: number, computationOffset: anchor.BN): PublicKey {
35
+ const clOffsetBuffer = Buffer.alloc(OFFSET_BUFFER_SIZE);
36
+ clOffsetBuffer.writeUInt32LE(clusterOffset, 0);
37
+
38
+ const seeds = [Buffer.from(COMPUTATION_ACC_SEED), clOffsetBuffer, computationOffset.toArrayLike(Buffer, 'le', 8)];
39
+ return generateArciumPDAFrom(seeds)[0];
40
+ }
41
+
42
+ /**
43
+ * Derive the mempool account address for a given cluster.
44
+ * @param clusterOffset - Offset of the cluster.
45
+ * @returns Derived mempool account public key.
46
+ */
47
+ export function getMempoolAccAddress(clusterOffset: number): PublicKey {
48
+ const clOffsetBuffer = Buffer.alloc(OFFSET_BUFFER_SIZE);
49
+ clOffsetBuffer.writeUInt32LE(clusterOffset, 0);
50
+
51
+ const seeds = [Buffer.from(MEMPOOL_ACC_SEED), clOffsetBuffer];
52
+ return generateArciumPDAFrom(seeds)[0];
53
+ }
54
+
55
+ /**
56
+ * Derive the executing pool account address for a given cluster.
57
+ * @param clusterOffset - Offset of the cluster.
58
+ * @returns Derived executing pool account public key.
59
+ */
60
+ export function getExecutingPoolAccAddress(clusterOffset: number): PublicKey {
61
+ const clOffsetBuffer = Buffer.alloc(OFFSET_BUFFER_SIZE);
62
+ clOffsetBuffer.writeUInt32LE(clusterOffset, 0);
63
+
64
+ const seeds = [Buffer.from(EXEC_POOL_ACC_SEED), clOffsetBuffer];
65
+
66
+ return generateArciumPDAFrom(seeds)[0];
67
+ }
68
+
69
+ /**
70
+ * Derive the fee pool account address.
71
+ * @returns Derived fee pool account public key.
72
+ */
73
+ export function getFeePoolAccAddress(): PublicKey {
74
+ const seeds = [Buffer.from(POOL_ACC_SEED)];
75
+
76
+ return generateArciumPDAFrom(seeds)[0];
77
+ }
78
+
79
+ /**
80
+ * Derive the clock account address.
81
+ * @returns Derived clock account public key.
82
+ */
83
+ export function getClockAccAddress(): PublicKey {
84
+ const seeds = [Buffer.from(CLOCK_ACC_SEED)];
85
+
86
+ return generateArciumPDAFrom(seeds)[0];
87
+ }
88
+
89
+ /**
90
+ * Derive the cluster account address for a given offset.
91
+ * @param clusterOffset - Cluster offset as a number.
92
+ * @returns Derived cluster account public key.
93
+ */
94
+ export function getClusterAccAddress(clusterOffset: number): PublicKey {
95
+ const offsetBuffer = Buffer.alloc(OFFSET_BUFFER_SIZE);
96
+ offsetBuffer.writeUInt32LE(clusterOffset, 0);
97
+ const seeds = [Buffer.from(CLUSTER_ACC_SEED), offsetBuffer];
98
+
99
+ return generateArciumPDAFrom(seeds)[0];
100
+ }
101
+
102
+ /**
103
+ * Derive the ArxNode account address for a given offset.
104
+ * @param nodeOffset - ArxNode offset as a number.
105
+ * @returns Derived ArxNode account public key.
106
+ */
107
+ export function getArxNodeAccAddress(nodeOffset: number): PublicKey {
108
+ const offsetBuffer = Buffer.alloc(OFFSET_BUFFER_SIZE);
109
+ offsetBuffer.writeUInt32LE(nodeOffset, 0);
110
+ const seeds = [Buffer.from(ARX_NODE_ACC_SEED), offsetBuffer];
111
+
112
+ return generateArciumPDAFrom(seeds)[0];
113
+ }
114
+
115
+ /**
116
+ * Derive the MXE account address for a given MXE program ID.
117
+ * @param mxeProgramId - Public key of the MXE program.
118
+ * @returns Derived MXE account public key.
119
+ */
120
+ export function getMXEAccAddress(mxeProgramId: PublicKey): PublicKey {
121
+ const seeds = [Buffer.from(MXE_ACCOUNT_SEED), mxeProgramId.toBuffer()];
122
+
123
+ return generateArciumPDAFrom(seeds)[0];
124
+ }
125
+
126
+ /**
127
+ * Derive the computation definition account address for a given MXE program ID and offset.
128
+ * @param mxeProgramId - Public key of the MXE program.
129
+ * @param compDefOffset - Computation definition offset as a number.
130
+ * @returns Derived computation definition account public key.
131
+ */
132
+ export function getCompDefAccAddress(mxeProgramId: PublicKey, compDefOffset: number): PublicKey {
133
+ const offsetBuffer = Buffer.alloc(OFFSET_BUFFER_SIZE);
134
+ offsetBuffer.writeUInt32LE(compDefOffset, 0);
135
+ const seeds = [Buffer.from(COMP_DEF_ACC_SEED), mxeProgramId.toBuffer(), offsetBuffer];
136
+
137
+ return generateArciumPDAFrom(seeds)[0];
138
+ }
139
+
140
+ /**
141
+ * Derive the recovery cluster account address for a given MXE program ID.
142
+ * @param mxeProgramId - Public key of the MXE program.
143
+ * @returns Derived recovery cluster account public key.
144
+ */
145
+ export function getRecoveryClusterAccAddress(mxeProgramId: PublicKey): PublicKey {
146
+ const seeds = [Buffer.from(RECOVERY_CLUSTER_ACC_SEED), mxeProgramId.toBuffer()];
147
+ return generateArciumPDAFrom(seeds)[0];
148
+ }
149
+
150
+ /**
151
+ * Derive the MXE recovery account address for a key recovery session.
152
+ * @param backupMxeProgramId - Public key of the backup MXE program that will take over.
153
+ * @param originalMxeProgramId - Public key of the original MXE program being recovered.
154
+ * @returns Derived MXE recovery account public key.
155
+ */
156
+ export function getMxeRecoveryAccAddress(backupMxeProgramId: PublicKey, originalMxeProgramId: PublicKey): PublicKey {
157
+ const seeds = [
158
+ Buffer.from(MXE_RECOVERY_ACC_SEED),
159
+ backupMxeProgramId.toBuffer(),
160
+ originalMxeProgramId.toBuffer(),
161
+ ];
162
+ return generateArciumPDAFrom(seeds)[0];
163
+ }
164
+
165
+ /**
166
+ * Derive the raw circuit account address for a given computation definition and index.
167
+ * @param compDefPubkey - Public key of the computation definition account.
168
+ * @param rawCircuitIndex - Index of the raw circuit account (0-based).
169
+ * @returns Derived raw circuit account public key.
170
+ */
171
+ export function getRawCircuitAccAddress(compDefPubkey: PublicKey, rawCircuitIndex: number): PublicKey {
172
+ const seeds = [
173
+ Buffer.from(RAW_CIRCUIT_ACC_SEED),
174
+ compDefPubkey.toBuffer(),
175
+ Buffer.from([rawCircuitIndex]),
176
+ ];
177
+ return generateArciumPDAFrom(seeds)[0];
178
+ }
179
+
180
+ /**
181
+ * Derive the address lookup table address for an MXE program.
182
+ * @param mxeProgramId - Public key of the MXE program.
183
+ * @param lutOffset - Index of the lookup table, to be fetched from the mxe account.
184
+ * @returns Derived address lookup table public key.
185
+ */
186
+ export function getLookupTableAddress(mxeProgramId: PublicKey, lutOffset: anchor.BN): PublicKey {
187
+ const mxeAccount = getMXEAccAddress(mxeProgramId);
188
+ const lutIndexBuffer = lutOffset.toArrayLike(Buffer, 'le', 8);
189
+ const seeds = [mxeAccount.toBuffer(), lutIndexBuffer];
190
+ return PublicKey.findProgramAddressSync(seeds, AddressLookupTableProgram.programId)[0];
191
+ }
192
+
193
+ /**
194
+ * Generate a program-derived address (PDA) from the provided seeds and the Arcium program ID.
195
+ * @param seeds - Array of Buffer seeds used for PDA derivation.
196
+ * @returns Tuple containing the derived public key and the bump seed.
197
+ */
198
+ function generateArciumPDAFrom(
199
+ seeds: Buffer[],
200
+ ): [PublicKey, number] {
201
+ const programId = getArciumProgramId();
202
+ return PublicKey.findProgramAddressSync(seeds, programId);
203
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,126 @@
1
+ import * as anchor from '@coral-xyz/anchor';
2
+ import { CURVE25519_BASE_FIELD } from './cryptography/rescueDesc.js';
3
+ import { ctAdd, ctLt, ctSelect, ctSignBit, ctSub, verifyBinSize } from './ctUtils.js';
4
+ import { ArciumIdlType } from './idl/index.js';
5
+
6
+ /**
7
+ * Check if code is running in a browser environment.
8
+ * @returns true if window object exists, false otherwise.
9
+ */
10
+ export function isBrowser(): boolean {
11
+ return (
12
+ // eslint-disable-next-line no-prototype-builtins
13
+ typeof window !== 'undefined' && !window.process?.hasOwnProperty('type')
14
+ );
15
+ }
16
+
17
+ /**
18
+ * Conditionally logs a message if logging is enabled.
19
+ * @param log - Whether to output the log.
20
+ * @param args - Arguments to pass to console.log.
21
+ */
22
+ export function optionalLog(log: boolean, ...args: Parameters<typeof console.log>) {
23
+ if (log) {
24
+ // eslint-disable-next-line no-console
25
+ console.log(...args);
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Calculate the minimum number of bits needed to represent a value.
31
+ * Formula: floor(log2(max)) + 1 for unsigned, +1 for signed, +1 for diff of two negatives.
32
+ * @param max - Bigint value to measure.
33
+ * @returns Number of bits required.
34
+ */
35
+ export function getBinSize(max: bigint): bigint {
36
+ // floor(log2(max)) + 1 to represent unsigned elements, a +1 for signed elements
37
+ // and another +1 to account for the diff of two negative elements
38
+ return BigInt(Math.floor(Math.log2(Number(max)))) + 3n;
39
+ }
40
+
41
+ /**
42
+ * Number of mantissa bits for double-precision floating point values.
43
+ */
44
+ export const DOUBLE_PRECISION_MANTISSA = 52;
45
+
46
+ /**
47
+ * Encode a value as a bigint suitable for Rescue encryption, handling booleans, bigints, and numbers.
48
+ * The encoding is performed in constant-time to avoid leaking information through timing side-channels.
49
+ * Throws if the value is out of the supported range for the field.
50
+ * @param v - Value to encode (bigint, number, or boolean).
51
+ * @returns Encoded value as a bigint.
52
+ */
53
+ export function encodeAsRescueEncryptable(v: bigint | number | boolean): bigint {
54
+ if (typeof v === 'boolean') {
55
+ return v ? 1n : 0n;
56
+ }
57
+ if (typeof v === 'bigint') {
58
+ const binSize = getBinSize(CURVE25519_BASE_FIELD.ORDER - 1n);
59
+ if (!verifyBinSize(v, binSize - 1n) || ctLt(v, -(CURVE25519_BASE_FIELD.ORDER - 1n), binSize) || !ctLt(v, CURVE25519_BASE_FIELD.ORDER, binSize)) {
60
+ throw Error(`v must be in the range [${CURVE25519_BASE_FIELD.ORDER - 1n}, ${CURVE25519_BASE_FIELD.ORDER - 1n}]`);
61
+ }
62
+ return ctSelect(ctSignBit(v, binSize), ctAdd(v, CURVE25519_BASE_FIELD.ORDER, binSize), v, binSize);
63
+ }
64
+ if (typeof v === 'number') {
65
+ if (v < -(2 ** 75) || v >= 2 ** 75) {
66
+ throw new Error('Inputs only supported in the range [-2**75, 2**75)');
67
+ }
68
+ const vBigInt = BigInt(Math.round(v * 2 ** DOUBLE_PRECISION_MANTISSA));
69
+ const binSize = getBinSize(CURVE25519_BASE_FIELD.ORDER - 1n);
70
+ return ctSelect(ctSignBit(vBigInt, binSize), ctAdd(vBigInt, CURVE25519_BASE_FIELD.ORDER, binSize), vBigInt, binSize);
71
+ }
72
+ throw new Error('Invalid type to convert from number to bigint');
73
+ }
74
+
75
+ /**
76
+ * Decode a Rescue-decrypted value back to a signed bigint.
77
+ * Handle the conversion from field element representation to signed integer.
78
+ * @param v - Decrypted field element value.
79
+ * @returns Decoded signed bigint value.
80
+ */
81
+ export function decodeRescueDecryptedToBigInt(v: bigint): bigint {
82
+ const twoInv = (CURVE25519_BASE_FIELD.ORDER + 1n) / 2n;
83
+ const binSize = getBinSize(CURVE25519_BASE_FIELD.ORDER - 1n);
84
+ const isLtTwoInv = ctLt(v, twoInv, binSize);
85
+ return ctSelect(isLtTwoInv, v, ctSub(v, CURVE25519_BASE_FIELD.ORDER, binSize), binSize);
86
+ }
87
+
88
+ /**
89
+ * Decode a Rescue-decrypted value back to a JavaScript number.
90
+ * Convert from field element representation to a floating-point number.
91
+ * @param v - Decrypted field element value.
92
+ * @returns Decoded number value.
93
+ */
94
+ export function decodeRescueDecryptedToNumber(v: bigint): number {
95
+ const vSigned = decodeRescueDecryptedToBigInt(v);
96
+ return Number(vSigned) * 2 ** -DOUBLE_PRECISION_MANTISSA;
97
+ }
98
+
99
+ /**
100
+ * Generate a random integer in the range [min, max] (inclusive).
101
+ * @param min - Minimum value (inclusive).
102
+ * @param max - Maximum value (inclusive).
103
+ * @returns Random integer between min and max.
104
+ */
105
+ export function randomInt(min: number, max: number): number {
106
+ return Math.floor(Math.random() * (max + 1 - min)) + min;
107
+ }
108
+
109
+ /**
110
+ * Reference to a computation in a mempool or executing pool.
111
+ * Contains the computation offset and priority fee information.
112
+ */
113
+ export type ComputationReference = anchor.IdlTypes<ArciumIdlType>['computationReference'];
114
+
115
+ /**
116
+ * Check if a computation reference is null (all zeros).
117
+ * @param ref - Computation reference to check.
118
+ * @returns true if the reference is null, false otherwise.
119
+ */
120
+ export function isNullRef(ref: ComputationReference): boolean {
121
+ const bigZero = new anchor.BN(0);
122
+ return (
123
+ ref.computationOffset === bigZero
124
+ && ref.priorityFee === bigZero
125
+ );
126
+ }