Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Acir formal proofs #10973

Merged
merged 35 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a837887
IT FINALLY WORKS :D
jewelofchaos9 Nov 18, 2024
5750f4c
i forgot cmakelists
jewelofchaos9 Nov 18, 2024
cc71c30
save artifacts
jewelofchaos9 Nov 18, 2024
a791e76
stable version for verifying
jewelofchaos9 Nov 21, 2024
56559cf
dummy
jewelofchaos9 Nov 22, 2024
c364e63
IT FINALLY WORKS :D
jewelofchaos9 Nov 18, 2024
579c5ff
i forgot cmakelists
jewelofchaos9 Nov 18, 2024
be6ef36
save artifacts
jewelofchaos9 Nov 18, 2024
6b27d0e
stable version for verifying
jewelofchaos9 Nov 21, 2024
84319b2
dummy
jewelofchaos9 Nov 22, 2024
28cd1f9
smol refactor
jewelofchaos9 Nov 25, 2024
8ea00fc
smol refactor
jewelofchaos9 Nov 25, 2024
2ff5084
smol refactor
jewelofchaos9 Nov 25, 2024
58c1268
forgot to resolve merge conflicts
jewelofchaos9 Nov 25, 2024
e1a6541
i forgor to resolve conflicts in cargolock...
Nov 26, 2024
7dd7e98
hmm
jewelofchaos9 Nov 27, 2024
d58a19a
smol refactor
jewelofchaos9 Nov 29, 2024
4f07105
ples have a patient i have problems
jewelofchaos9 Dec 2, 2024
e33a29f
verif changes
jewelofchaos9 Dec 3, 2024
802b0cd
smol shifts
jewelofchaos9 Dec 9, 2024
d7dd95d
deleted codegen + shl + shr
jewelofchaos9 Dec 12, 2024
2cf19f0
bug in acir builder
jewelofchaos9 Dec 12, 2024
10b361f
recaftor
jewelofchaos9 Dec 16, 2024
3cc7f44
docs + small refactor
jewelofchaos9 Dec 27, 2024
884b2aa
readme mistake, artifacts dirs
jewelofchaos9 Dec 27, 2024
ef9c730
restore cmake list
jewelofchaos9 Dec 27, 2024
208f9ce
bug in verify_mod
jewelofchaos9 Dec 27, 2024
97bfad1
still bug in verify_mod
jewelofchaos9 Dec 27, 2024
1c90845
deleted gitignores + table + shl64 bug + directory flag for binary
jewelofchaos9 Jan 6, 2025
442e387
readme mistake, deleted all noir stuff
jewelofchaos9 Jan 6, 2025
29ed080
broken table
jewelofchaos9 Jan 6, 2025
f32a27e
last readme fix
jewelofchaos9 Jan 6, 2025
d9d77f5
Merge branch 'master' into sa/acir_formal_proofs
jewelofchaos9 Jan 6, 2025
d800214
Merge branch 'master' of https://github.com/AztecProtocol/aztec-packa…
jewelofchaos9 Jan 7, 2025
9dd4709
ACIR_FORMAL_PROOFS flag for cmake
jewelofchaos9 Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions barretenberg/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ add_subdirectory(barretenberg/ultra_honk)
add_subdirectory(barretenberg/vm)
add_subdirectory(barretenberg/wasi)
add_subdirectory(barretenberg/world_state)
add_subdirectory(barretenberg/acir_formal_proofs)

if(SMT)
add_subdirectory(barretenberg/smt_verification)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
barretenberg_module(acir_formal_proofs dsl circuit_checker smt_verification common)
47 changes: 47 additions & 0 deletions barretenberg/cpp/src/barretenberg/acir_formal_proofs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Formal Verification of ACIR Instructions

This module provides formal verification capabilities for ACIR (Arithmetic Circuit Intermediate Representation) instructions generated from Noir SSA code.

## Overview

The verifier uses SMT (Satisfiability Modulo Theories) solving to formally verify the correctness of ACIR instructions. It supports verification of:

- Arithmetic operations (add, subtract, multiply, divide)
- Bitwise operations (AND, OR, XOR, NOT)
- Shifts (left shift, right shift)
- Comparisons (equality, less than, greater than)
- Field arithmetic

## Tests

⚠️ **WARNING**: Do not run these tests on a local machine without sufficient memory (>32GB RAM). The tests can consume large amounts of memory and CPU resources. Some tests like integer division can run for multiple days. It is recommended to run these tests in a controlled environment with adequate resources.

### Results

| Opcode | Lhs type/size | Rhs type/size | Time/seconds | Memory/GB | Success | SMT Term Type | Reason |
| ----------- | ------------- | ------------- | ------------ | --------- | ------- | ---------------- | -------------------------- |
| Binary::Add | Field | Field | 0.024 | - | ✓ | TermType::FFTerm | |
| Binary::Add | Unsigned_127 | Unsigned_127 | 2.8 | - | ✓ | TermType::BVTerm | |
| Binary::And | Unsigned_32 | Unsigned_32 | 6.7 | - | ✓ | TermType::BVTerm | |
| Binary::And | Unsigned_127 | Unsigned_127 | 7.5 | - | ✗ | TermType::BVTerm | Probably bug in smt solver |
| Binary::Div | Field | Field | 0.024 | - | ✓ | TermType::FFTerm | |
| Binary::Div | Unsigned_126 | Unsigned_126 | 402.7 | 3.5 | ✗ | TermType::BVTerm | Analysis in progress |
| Binary::Div | Signed_126 | Signed_126 | >17 days | 5.1 | ✗ | TermType::ITerm | Test takes too long |
| Binary::Eq | Field | Field | 19.2 | - | ✓ | TermType::FFTerm | |
| Binary::Eq | Unsigned_127 | Unsigned_127 | 22.8 | - | ✓ | TermType::BVTerm | |
| Binary::Lt | Unsigned_127 | Unsigned_127 | 56.7 | - | ✓ | TermType::BVTerm | |
| Binary::Mod | Unsigned_127 | Unsigned_127 | - | 3.2 | ✗ | TermType::BVTerm | Analysis in progress |
| Binary::Mul | Field | Field | 0.024 | - | ✓ | TermType::FFTerm | |
| Binary::Mul | Unsigned_127 | Unsigned_127 | 10.0 | - | ✓ | TermType::BVTerm | |
| Binary::Or | Unsigned_32 | Unsigned_32 | 18.0 | - | ✓ | TermType::BVTerm | |
| Binary::Or | Unsigned_127 | Unsigned_127 | 7.5 | - | ✗ | TermType::BVTerm | Probably bug in smt solver |
| Binary::Shl | Unsigned_64 | Unsigned_8 | 42331.61 | 63.2 | ✓ | TermType::BVTerm | |
| Binary::Shl | Unsigned_32 | Unsigned_8 | 4574.0 | 30 | ✓ | TermType::BVTerm | |
| Binary::Shr | Unsigned_64 | Unsigned_8 | 3927.88 | 10 | ✓ | TermType::BVTerm | |
| Binary::Sub | Unsigned_127 | Unsigned_127 | 3.3 | - | ✓ | TermType::BVTerm | |
| Binary::Xor | Unsigned_32 | Unsigned_32 | 14.7 | - | ✓ | TermType::BVTerm | |
| Binary::Xor | Unsigned_127 | Unsigned_127 | 7.5 | - | ✗ | TermType::BVTerm | Probably bug in smt solver |
| Not | Unsigned_127 | - | 0.2 | - | ✓ | TermType::BVTerm | |


Each test attempts to find counterexamples that violate the expected behavior. A passing test indicates the operation is correctly implemented, while a failing test reveals potential issues.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "acir_loader.hpp"
#include "barretenberg/dsl/acir_format/acir_format.hpp"
#include "barretenberg/dsl/acir_format/acir_to_constraint_buf.hpp"
#include "barretenberg/smt_verification/circuit/ultra_circuit.hpp"
#include "barretenberg/smt_verification/terms/term.hpp"
#include "msgpack/v3/sbuffer_decl.hpp"
#include <fstream>
#include <string>
#include <vector>

std::vector<uint8_t> readFile(std::string filename)
{
std::ifstream file(filename, std::ios::binary);
file.unsetf(std::ios::skipws);

std::streampos fileSize;

file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);

std::vector<uint8_t> vec;

vec.insert(vec.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
file.close();
return vec;
}

AcirToSmtLoader::AcirToSmtLoader(std::string filename)
{
this->acir_program_buf = readFile(filename);
this->instruction_name = filename;
this->constraint_system = acir_format::program_buf_to_acir_format(this->acir_program_buf, false).at(0);
this->circuit_buf = this->get_circuit_builder().export_circuit();
}

bb::UltraCircuitBuilder AcirToSmtLoader::get_circuit_builder()
{
bb::UltraCircuitBuilder builder = acir_format::create_circuit(this->constraint_system, false);
builder.set_variable_name(0, "a");
builder.set_variable_name(1, "b");
builder.set_variable_name(2, "c");
return builder;
}

smt_solver::Solver AcirToSmtLoader::get_smt_solver()
{
smt_circuit::CircuitSchema circuit_info = smt_circuit_schema::unpack_from_buffer(this->circuit_buf);
// In circuits generated by the shift left (shl) opcode, there is a variable with bit length 197.
// This is likely because the shl operation internally calls truncate opcode to handle overflow
return smt_solver::Solver(circuit_info.modulus, smt_circuit::default_solver_config, 16, 240);
}

smt_circuit::UltraCircuit AcirToSmtLoader::get_bitvec_smt_circuit(smt_solver::Solver* solver)
{
smt_circuit::CircuitSchema circuit_info = smt_circuit_schema::unpack_from_buffer(this->circuit_buf);
return smt_circuit::UltraCircuit(circuit_info, solver, smt_terms::TermType::BVTerm);
}

smt_circuit::UltraCircuit AcirToSmtLoader::get_field_smt_circuit(smt_solver::Solver* solver)
{
smt_circuit::CircuitSchema circuit_info = smt_circuit_schema::unpack_from_buffer(this->circuit_buf);
return smt_circuit::UltraCircuit(circuit_info, solver, smt_terms::TermType::FFTerm);
}

smt_circuit::UltraCircuit AcirToSmtLoader::get_integer_smt_circuit(smt_solver::Solver* solver)
{
smt_circuit::CircuitSchema circuit_info = smt_circuit_schema::unpack_from_buffer(this->circuit_buf);
return smt_circuit::UltraCircuit(circuit_info, solver, smt_terms::TermType::ITerm);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once
#include "barretenberg/dsl/acir_format/acir_format.hpp"
#include "barretenberg/smt_verification/circuit/ultra_circuit.hpp"
#include "msgpack/v3/sbuffer_decl.hpp"
#include <cstdint>
#include <string>
#include <vector>

/**
* @brief Class for loading ACIR (Arithmetic Circuit Intermediate Representation) programs and converting them to SMT
* format
*
* This class handles loading ACIR programs from files and provides functionality to:
* - Convert the ACIR program to various SMT circuit representations
* - Access the underlying constraint systems
* - Build circuits for verification
*
* The loader reads an ACIR program file, creates constraint systems, and allows conversion
* to different SMT circuit types (bitvector, field, integer) for formal verification.
*/
class AcirToSmtLoader {
public:
// Deleted constructors/operators to prevent copying/moving
AcirToSmtLoader() = delete;
AcirToSmtLoader(const AcirToSmtLoader& other) = delete;
AcirToSmtLoader(AcirToSmtLoader&& other) = delete;
AcirToSmtLoader& operator=(const AcirToSmtLoader other) = delete;
AcirToSmtLoader&& operator=(AcirToSmtLoader&& other) = delete;

~AcirToSmtLoader() = default;

/**
* @brief Constructs loader from an ACIR program file
* @param filename Path to the ACIR program file to load
*
* Reads the ACIR program from file, initializes the constraint system,
* and prepares the circuit buffer for later use.
*/
AcirToSmtLoader(std::string filename);

/**
* @brief Gets the constraint systems from the loaded ACIR program
* @return Reference to the ACIR format constraint systems
*/
acir_format::AcirFormat& get_constraint_systems() { return this->constraint_system; }

/**
* @brief Creates a circuit builder for the loaded program
* @return UltraCircuitBuilder instance
*
* Creates and returns a circuit builder with predefined variable names:
* - Variable 0 named "a"
* - Variable 1 named "b"
* - Variable 2 named "c"
*/
bb::UltraCircuitBuilder get_circuit_builder();

/**
* @brief Gets an SMT solver instance
* @return Solver instance for SMT solving
*
* Creates a solver configured with:
* - Circuit modulus from schema
* - Default solver configuration
* - Minimum bit width of 16
* - Maximum bit width of 240
*/
smt_solver::Solver get_smt_solver();

/**
* @brief Creates an SMT circuit for bitvector operations
* @param solver Pointer to SMT solver to use
* @return UltraCircuit configured for bitvector operations
*/
smt_circuit::UltraCircuit get_bitvec_smt_circuit(smt_solver::Solver* solver);

/**
* @brief Creates an SMT circuit for field operations
* @param solver Pointer to SMT solver to use
* @return UltraCircuit configured for field operations
*/
smt_circuit::UltraCircuit get_field_smt_circuit(smt_solver::Solver* solver);

/**
* @brief Creates an SMT circuit for integer operations
* @param solver Pointer to SMT solver to use
* @return UltraCircuit configured for integer operations
*/
smt_circuit::UltraCircuit get_integer_smt_circuit(smt_solver::Solver* solver);

private:
std::string instruction_name; ///< Name of the instruction/filename being processed
std::vector<uint8_t> acir_program_buf; ///< Buffer containing the raw ACIR program data read from file
acir_format::AcirFormat constraint_system; ///< The parsed constraint system from the ACIR program
msgpack::sbuffer circuit_buf; ///< Buffer for circuit serialization using MessagePack
};
Loading
Loading