LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
BinaryBuffer.h
Go to the documentation of this file.
1//===-- BinaryBuffer.h - Little-endian binary buffer ------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2026 Project LLZK
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
10#pragma once
11
13
14#include <llvm/ADT/ArrayRef.h>
15#include <llvm/ADT/SmallVector.h>
16#include <llvm/Support/Endian.h>
17
18#include <climits>
19#include <cstdint>
20
21namespace llzk {
22
25public:
26 void writeU32(uint32_t value) { writeInteger(value); }
27 void writeU64(uint64_t value) { writeInteger(value); }
28
29 void writeBytes(llvm::ArrayRef<char> bytes) { buffer_.append(bytes.begin(), bytes.end()); }
30
31 void writeFieldElement(uint32_t size, const llvm::DynamicAPInt &value) {
32 llvm::APInt exact = toExactWidthAPInt(value, size * CHAR_BIT);
33 for (uint32_t i = 0; i < size; ++i) {
34 buffer_.push_back(static_cast<char>(exact.extractBitsAsZExtValue(8, i * 8)));
35 }
36 }
37
38 uint64_t size() const { return static_cast<uint64_t>(buffer_.size()); }
39 llvm::ArrayRef<char> bytes() const { return buffer_; }
40
41private:
42 template <typename T> void writeInteger(T value) {
43 char bytes[sizeof(T)];
44 llvm::support::endian::write<T, llvm::endianness::little>(bytes, value);
46 }
47
48 llvm::SmallVector<char> buffer_;
49};
50
51} // namespace llzk
This file implements helper methods for constructing DynamicAPInts.
Accumulate fixed-width integers and field elements in little-endian order.
void writeBytes(llvm::ArrayRef< char > bytes)
uint64_t size() const
llvm::ArrayRef< char > bytes() const
void writeFieldElement(uint32_t size, const llvm::DynamicAPInt &value)
void writeU64(uint64_t value)
void writeU32(uint32_t value)
APInt toExactWidthAPInt(const DynamicAPInt &val, unsigned bitWidth)