AgentSkillsCN

compact-core:language-reference

当您编写Compact智能合约,需要查阅原始类型(Field、Boolean、Uint)、复合类型(struct、enum、Vector、Bytes)、电路/见证语法、控制流(if/else、for循环、assert),或模块系统(import、include、export)时,可选用此功能。

SKILL.md
--- frontmatter
name: compact-core:language-reference
description: Use when writing Compact smart contracts and need reference for primitive types (Field, Boolean, Uint), composite types (struct, enum, Vector, Bytes), circuit/witness syntax, control flow (if/else, for loops, assert), or module system (import, include, export).

Compact Language Reference

Complete reference for the Compact smart contract language used on Midnight Network.

Quick Reference

Primitive Types

TypeDescriptionExample
FieldZK-native field element (~254 bits)const f: Field = 42;
BooleanTrue/false valueconst b: Boolean = true;
Uint<N>Unsigned integer (N bits, N ≤ 248)const n: Uint<64> = 100;

Composite Types

TypeDescriptionExample
Bytes<N>Fixed-size byte arrayconst h: Bytes<32> = ...;
structNamed record typestruct Point { x: Field, y: Field }
enumTagged unionenum Maybe<T> { Some(T), None }
Vector<T, N>Fixed-size arrayconst v: Vector<Field, 3> = [1, 2, 3];

Special Types

TypeDescriptionExample
Opaque<'string'>UTF-8 string from TypeScriptwitness get_name(): Opaque<'string'>;
Opaque<'Uint8Array'>Binary data from TypeScriptwitness get_data(): Opaque<'Uint8Array'>;

Circuit Syntax

compact
// Public circuit callable from TypeScript
export circuit transfer(to: Bytes<32>, amount: Uint<64>): [] {
    // Circuit body
}

// Private helper circuit
circuit helper(x: Field): Field {
    return x * x;
}

Witness Syntax

compact
// Declaration (implemented in TypeScript)
witness get_private_key(): Bytes<32>;

// Usage in circuit
export circuit sign(message: Bytes<32>): Bytes<64> {
    const key = get_private_key();
    // Use key...
}

Control Flow

compact
// Conditional
if (condition) {
    // then branch
} else {
    // else branch
}

// Bounded loop (bounds must be compile-time constant)
for i in 0..10 {
    // loop body
}

// Assertion
assert condition, "Error message";

Module System

compact
// Import specific items
import { hash } from "CompactStandardLibrary";

// Include entire file
include "utils.compact";

// Export for external use
export circuit public_fn(): Field { ... }

References

For detailed documentation on each topic:

  • Type System - Complete type reference with constraints
  • Circuits - Circuit and witness patterns
  • Control Flow - Conditionals, loops, assertions
  • Modules - Import, include, export, COMPACT_PATH