aluvm/
lib.rs

1// Reference rust implementation of AluVM (arithmetic logic unit virtual machine).
2// To find more on AluVM please check <https://aluvm.org>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Designed in 2021-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7// Written in 2021-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
8//
9// Copyright (C) 2021-2024 LNP/BP Standards Association, Switzerland.
10// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
11//                         Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
12// Copyright (C) 2021-2025 Dr Maxim Orlovsky.
13// All rights under the above copyrights are reserved.
14//
15// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
16// in compliance with the License. You may obtain a copy of the License at
17//
18//        http://www.apache.org/licenses/LICENSE-2.0
19//
20// Unless required by applicable law or agreed to in writing, software distributed under the License
21// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
22// or implied. See the License for the specific language governing permissions and limitations under
23// the License.
24
25#![deny(
26    unsafe_code,
27    dead_code,
28    missing_docs,
29    unused_variables,
30    unused_mut,
31    unused_imports,
32    non_upper_case_globals,
33    non_camel_case_types,
34    non_snake_case
35)]
36#![allow(clippy::bool_assert_comparison)]
37#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
38#![cfg_attr(docsrs, feature(doc_auto_cfg))]
39
40//! Rust implementation of AluVM (arithmetic logic unit virtual machine) and assembler from Alu
41//! Assembly language into bytecode.
42//!
43//! AluVM is a pure functional register-based highly deterministic & exception-less instruction set
44//! architecture (ISA) and virtual machine (VM) without random memory access, capable of performing
45//! arithmetic operations, including operations on elliptic curves. The AluVM ISA can be extended by
46//! the environment running the virtual machine (host environment), providing an ability to load
47//! data to the VM registers and support application-specific instructions (like SIMD).
48//!
49//! The main purpose for ALuVM is to be used in distributed systems whether robustness,
50//! platform-independent determinism are more important than the speed of computation. The main area
51//! of AluVM applications (using appropriate ISA extensions) is blockchain environments,
52//! consensus-critical computations, multiparty computing (including deterministic machine
53//! learning), client-side-validation, sandboxed Internet2 computing, and genetic algorithms.
54//!
55//! For more details on AluVM, please check [the specification][AluVM]
56//!
57//!
58//! ## Design
59//!
60//! The robustness lies at the very core of AluVM. It is designed to avoid any
61//! undefined behavior. Specifically,
62//! * All registers may be in the undefined statel
63//! * Impossible/incorrect operations put destination register into a special *undefined state*;
64//! * Code always extended to 2^16 bytes with zeros, which corresponds to no-operation;
65//! * There are no invalid jump operations;
66//! * There are no invalid instructions;
67//! * Cycles & jumps are counted with 2^16 limit (bounded-time execution);
68//! * No ambiguity: any two distinct byte strings always represent strictly distinct programs;
69//! * Code is always signed;
70//! * Data segment is always signed;
71//! * Code commits to the used ISA extensions;
72//! * Libraries identified by the signature;
73//! * Code does not run if not all libraries are present;
74//!
75//! ![Comparison table](doc/comparison.png)
76//!
77//!
78//! ## Instruction Set Architecture
79//!
80//! ![Instruction set architecture](doc/isa.png)
81//!
82//! ### Instruction opcodes
83//!
84//! One will find all opcode implementation details documented in [`isa::Instr`] API docs.
85//!
86//! - RISC: only 256 instructions
87//! - 3 families of core instructions:
88//!   * Control flow
89//!   * Data load / movement between registers
90//!   * ALU (including cryptography)
91//! - Extensible with ISA extensions: 127 of the operations are reserved for extensions
92//!   * More cryptography
93//!   * Custom data I/O (blockchain, LN, client-side-validation)
94//!   * Genetic algorithms / code self-modification
95//!   
96//! The arithmetic ISA is designed with strong robustness goals:
97//! - Impossible arithmetic operation (0/0, Inf/inf) always sets the destination register into
98//!   undefined state (unlike NaN in IEEE-754 it has only a single unique value)
99//! - Operation resulting in the value which can't fit the bit dimensions under a used encoding,
100//!   including representation of infinity for integer encodings (x/0 if x != 0) results in:
101//!   * for float underflows, subnormally encoded number,
102//!   * for x/0 if x != 0 on float numbers, ±Inf float value,
103//!   * for overflows in integer checked operations and floats: undefined value, setting `CK` to
104//!     false,
105//!   * for overflows in integer wrapped operations, modulo division on the maximum register value
106//!
107//! Most of the arithmetic operations has to be provided with flags specifying which of the encoding
108//! and exception handling should be used:
109//! * Integer encodings have two flags:
110//!   - one for signed/unsigned variant of the encoding
111//!   - one for checked or wrapped variant of exception handling
112//! * Float encoding has 4 variants of rounding, matching IEEE-754 options
113//!
114//! Thus, many arithmetic instructions have 8 variants, indicating the used encoding (unsigned,
115//! signed integer or float) and operation behavior in a situation when resulting value does not fit
116//! into the register (overflow or wrap for integers and one of four rounding options for floats).
117//!
118//! Check [the specification][AluVM] for the details.
119//!
120//! ### Registers
121//!
122//! **ALU registers:** 8 blocks of 32 registers
123//! - Integer arithmetic (A-registers) blocks: 8, 16, 32, 64, 128, 256, 512, 1024 bits
124//! - Float arithmetic (F-registers) blocks:
125//!   * IEEE: binary-half, single, double, quad, oct precision
126//!   * IEEE extension: 80-bit X87 register
127//!   * BFloat16 register, used in Machine learning
128//! - Cryptographic operations (R-registers) blocks: 128, 160, 256, 512, 1024, 2048, 4096, 8192 bits
129//! - String registers (S-registers): 1 block of 256 registers, 64kb each
130//!
131//! **Control flow registers:**
132//! - Status (`CK`), boolean (one bit)
133//! - Cycle counter (`CY`), 16 bits
134//! - Instruction complexity accumulator (`CA`), 16 bits
135//! - Call stack register (`CS`), 3*2^16 bits (192kB block)
136//! - Call stack pointer register (`CP`), 16 bits
137//!
138//! [AluVM]: https://github.com/AluVM/aluvm-spec
139
140extern crate alloc;
141
142#[macro_use]
143extern crate amplify;
144#[macro_use]
145extern crate strict_encoding;
146#[macro_use]
147extern crate commit_verify;
148#[cfg(feature = "serde")]
149#[macro_use]
150extern crate serde;
151
152mod core;
153#[macro_use]
154pub mod isa;
155mod library;
156mod vm;
157#[cfg(feature = "stl")]
158pub mod stl;
159
160/// Module providing register information
161pub mod regs {
162    pub use crate::core::{Status, CALL_STACK_SIZE_MAX};
163}
164
165pub use isa::{ExecStep, IsaId, ISA_ID_MAX_LEN};
166#[cfg(feature = "armor")]
167pub use library::armor::LibArmorError;
168pub use library::{
169    AssemblerError, CompiledLib, CompilerError, Lib, LibId, LibSite, LibsSeg, MarshallError,
170    Marshaller,
171};
172#[doc(hidden)]
173pub use paste::paste;
174pub use vm::Vm;
175
176pub use self::core::{Core, CoreConfig, CoreExt, NoExt, NoRegs, Register, Site, SiteId, Supercore};
177
178/// Name of the strict types library for AluVM.
179pub const LIB_NAME_ALUVM: &str = "AluVM";