Compact Standard Library
Complete reference for CompactStandardLibrary - the built-in module providing cryptographic functions, utility types, token operations, and time functions.
Import
import { persistentHash, Maybe, mintToken, blockTime } from "CompactStandardLibrary";
Quick Reference
Cryptographic Functions
| Function |
Safe? |
Purpose |
persistentCommit(value) |
Yes |
Create hiding commitment with nonce (cross-transaction stable) |
transientCommit(value) |
Yes |
Create hiding commitment with nonce (transaction-local) |
persistentHash(domain, value) |
No |
Domain-separated hash (cross-transaction stable) |
transientHash(domain, value) |
No |
Domain-separated hash (transaction-local) |
ecAdd(p1, p2) |
- |
Elliptic curve point addition |
ecMul(scalar, point) |
- |
Elliptic curve scalar multiplication |
Safe vs Unsafe: Commit functions include a random nonce, making them hiding. Hash functions do not include a nonce - if the input has low entropy, the hash can be brute-forced.
Utility Types
| Type |
Purpose |
Variants |
Maybe<T> |
Optional value |
Some(T), None |
Either<L, R> |
Result/choice |
Left(L), Right(R) |
Token Operations
| Function |
Purpose |
mintToken(info) |
Create new tokens |
send(coin, recipient) |
Send tokens to address |
receive() |
Receive tokens in circuit |
mergeCoin(coins) |
Combine multiple coins |
Time Functions
| Function |
Purpose |
blockTime() |
Current block timestamp |
blockTimeBefore(time) |
Assert current time < time |
blockTimeAfter(time) |
Assert current time > time |
Common Patterns
Safe Commitment
import { persistentCommit } from "CompactStandardLibrary";
witness get_secret(): Field;
export circuit commit_secret(): Bytes<32> {
const secret = get_secret();
// Safe: commitment hides the secret
return persistentCommit(secret);
}
Nullifier Generation
import { persistentHash } from "CompactStandardLibrary";
witness get_secret(): Field;
export circuit generate_nullifier(): Bytes<32> {
const secret = get_secret();
// Unsafe but intentional: nullifier should be deterministic
return persistentHash("nullifier", secret);
}
Optional Value Handling
import { Maybe } from "CompactStandardLibrary";
ledger values: Map<Bytes<32>, Field>;
export circuit get_or_default(key: Bytes<32>): Field {
const result = values.lookup(key);
return if result is Maybe::Some(v) { v } else { 0 };
}
Time-Locked Action
import { blockTime, blockTimeAfter } from "CompactStandardLibrary";
ledger unlock_time: Cell<Uint<64>>;
export circuit withdraw(): [] {
// Fails if current block time <= unlock_time
blockTimeAfter(unlock_time.read());
// Perform withdrawal...
}
References
Examples
1---2name: compact-core-standard-library3description: Use when importing from CompactStandardLibrary, working with crypto functions (persistentHash, persistentCommit, ecAdd, ecMul), utility types (Maybe, Either), token operations (mintToken, send, receive, mergeCoin), or time functions (blockTime, blockTimeBefore, blockTimeAfter).4---5
6# Compact Standard Library
7
8Complete reference for `CompactStandardLibrary` - the built-in module providing cryptographic functions, utility types, token operations, and time functions.
9
10## Import
11
12```compact
13import { persistentHash, Maybe, mintToken, blockTime } from "CompactStandardLibrary";
14```
15
16## Quick Reference
17
18### Cryptographic Functions
19
20| Function | Safe? | Purpose |
21|----------|-------|---------|
22| `persistentCommit(value)` | Yes | Create hiding commitment with nonce (cross-transaction stable) |
23| `transientCommit(value)` | Yes | Create hiding commitment with nonce (transaction-local) |
24| `persistentHash(domain, value)` | No | Domain-separated hash (cross-transaction stable) |
25| `transientHash(domain, value)` | No | Domain-separated hash (transaction-local) |
26| `ecAdd(p1, p2)` | - | Elliptic curve point addition |
27| `ecMul(scalar, point)` | - | Elliptic curve scalar multiplication |
28
29**Safe vs Unsafe**: Commit functions include a random nonce, making them hiding. Hash functions do not include a nonce - if the input has low entropy, the hash can be brute-forced.
30
31### Utility Types
32
33| Type | Purpose | Variants |
34|------|---------|----------|
35| `Maybe<T>` | Optional value | `Some(T)`, `None` |
36| `Either<L, R>` | Result/choice | `Left(L)`, `Right(R)` |
37
38### Token Operations
39
40| Function | Purpose |
41|----------|---------|
42| `mintToken(info)` | Create new tokens |
43| `send(coin, recipient)` | Send tokens to address |
44| `receive()` | Receive tokens in circuit |
45| `mergeCoin(coins)` | Combine multiple coins |
46
47### Time Functions
48
49| Function | Purpose |
50|----------|---------|
51| `blockTime()` | Current block timestamp |
52| `blockTimeBefore(time)` | Assert current time < time |
53| `blockTimeAfter(time)` | Assert current time > time |
54
55## Common Patterns
56
57### Safe Commitment
58
59```compact
60import { persistentCommit } from "CompactStandardLibrary";
61
62witness get_secret(): Field;
63
64export circuit commit_secret(): Bytes<32> {
65 const secret = get_secret();
66 // Safe: commitment hides the secret
67 return persistentCommit(secret);
68}
69```
70
71### Nullifier Generation
72
73```compact
74import { persistentHash } from "CompactStandardLibrary";
75
76witness get_secret(): Field;
77
78export circuit generate_nullifier(): Bytes<32> {
79 const secret = get_secret();
80 // Unsafe but intentional: nullifier should be deterministic
81 return persistentHash("nullifier", secret);
82}
83```
84
85### Optional Value Handling
86
87```compact
88import { Maybe } from "CompactStandardLibrary";
89
90ledger values: Map<Bytes<32>, Field>;
91
92export circuit get_or_default(key: Bytes<32>): Field {
93 const result = values.lookup(key);
94 return if result is Maybe::Some(v) { v } else { 0 };
95}
96```
97
98### Time-Locked Action
99
100```compact
101import { blockTime, blockTimeAfter } from "CompactStandardLibrary";
102
103ledger unlock_time: Cell<Uint<64>>;
104
105export circuit withdraw(): [] {
106 // Fails if current block time <= unlock_time
107 blockTimeAfter(unlock_time.read());
108 // Perform withdrawal...
109}
110```
111
112## References
113
114- [Crypto Functions](./references/crypto-functions.md) - Hash, commit, EC operations
115- [Utility Types](./references/utility-types.md) - Maybe, Either patterns
116- [Token Operations](./references/token-operations.md) - mintToken, send, receive, mergeCoin
117- [Time Functions](./references/time-functions.md) - blockTime and time constraints
118
119## Examples
120
121- [Crypto Patterns](./examples/crypto-patterns.compact) - Cryptographic function usage
122- [Token Contract](./examples/token-contract.compact) - Token operations example
123- [Time-Locked](./examples/time-locked.compact) - Time-based logic example