<!DOCTYPE HTML>
<html lang="en" class="light" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Big Integer Representation v1.0 - ClockinChain Big Integer Specifications</title>
<!-- Custom HTML head -->
<meta name="description" content="Normative specifications for ClockinChain big integer arithmetic">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
</head>
<body class="sidebar-visible no-js">
<div id="body-container">
<!-- Provide site root to javascript -->
<script>
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('light')
html.classList.add(theme);
var body = document.querySelector('body');
body.classList.remove('no-js')
body.classList.add('js');
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var body = document.querySelector('body');
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
body.classList.remove('sidebar-visible');
body.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item affix "><a href="index.html">Introduction</a></li><li class="chapter-item affix "><li class="part-title">Specifications</li><li class="chapter-item expanded "><a href="representation.html" class="active"><strong aria-hidden="true">1.</strong> Big Integer Representation v1.0</a></li><li class="chapter-item "><a href="arithmetic.html"><strong aria-hidden="true">2.</strong> Arithmetic Algorithms v1.0</a></li><li class="chapter-item "><a href="montgomery.html"><strong aria-hidden="true">3.</strong> Montgomery & Modular Exponentiation v1.0</a></li><li class="chapter-item "><a href="gas.html"><strong aria-hidden="true">4.</strong> Gas Schedule v1.0</a></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
</nav>
<!-- Track and set sidebar scroll position -->
<script>
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
sidebarScrollbox.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
sidebarScrollbox.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
</script>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">ClockinChain Big Integer Specifications</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/olyntar-labs/clock-bigint" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/olyntar-labs/clock-bigint/edit/main/src/representation.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="clockinchain-big-integer-representation-specification-v10"><a class="header" href="#clockinchain-big-integer-representation-specification-v10">ClockinChain Big Integer Representation Specification v1.0</a></h1>
<h2 id="overview"><a class="header" href="#overview">Overview</a></h2>
<p>This specification defines the canonical representation, memory layout, and encoding format for big integers in the ClockinChain ecosystem.</p>
<h2 id="1-memory-layout"><a class="header" href="#1-memory-layout">1. Memory Layout</a></h2>
<h3 id="11-limb-definition"><a class="header" href="#11-limb-definition">1.1 Limb Definition</a></h3>
<p>Big integers are represented as arrays of 64-bit unsigned integers called "limbs":</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>type Limb = u64;
const LIMB_BITS: usize = 64;
<span class="boring">}</span></code></pre></pre>
<h3 id="12-endianness"><a class="header" href="#12-endianness">1.2 Endianness</a></h3>
<p>All limb arrays use <strong>little-endian</strong> ordering:</p>
<ul>
<li><code>limbs[0]</code> contains the least significant limb</li>
<li><code>limbs[n-1]</code> contains the most significant limb</li>
</ul>
<h3 id="13-word-size"><a class="header" href="#13-word-size">1.3 Word Size</a></h3>
<p>The base B = 2^64, so each limb represents a value in the range [0, 2^64 - 1].</p>
<h2 id="2-canonical-form"><a class="header" href="#2-canonical-form">2. Canonical Form</a></h2>
<h3 id="21-zero-representation"><a class="header" href="#21-zero-representation">2.1 Zero Representation</a></h3>
<p>Zero MUST be represented as:</p>
<ul>
<li><code>sign = false</code></li>
<li><code>limbs = [0]</code> (exactly one limb)</li>
<li>No leading zero limbs allowed</li>
</ul>
<h3 id="22-non-zero-representation"><a class="header" href="#22-non-zero-representation">2.2 Non-zero Representation</a></h3>
<p>Non-zero integers MUST satisfy:</p>
<ul>
<li>No leading zero limbs: <code>limbs[limbs.len() - 1] != 0</code></li>
<li>Minimal limb count: smallest n such that the above holds</li>
<li>Sign bit indicates negative values</li>
</ul>
<h3 id="23-negative-zero-prohibition"><a class="header" href="#23-negative-zero-prohibition">2.3 Negative Zero Prohibition</a></h3>
<p>Negative zero is explicitly forbidden:</p>
<ul>
<li><code>sign = true</code> AND <code>value = 0</code> is invalid</li>
<li>All zero values MUST have <code>sign = false</code></li>
</ul>
<h2 id="3-type-system"><a class="header" href="#3-type-system">3. Type System</a></h2>
<h3 id="31-dynamic-bigint"><a class="header" href="#31-dynamic-bigint">3.1 Dynamic BigInt</a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>struct BigInt {
sign: bool, // false = positive, true = negative
limbs: Vec<Limb>, // little-endian limb array
max_limbs: usize, // capacity limit
}
<span class="boring">}</span></code></pre></pre>
<h3 id="32-fixed-size-bigint"><a class="header" href="#32-fixed-size-bigint">3.2 Fixed-Size BigInt</a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>struct BigIntFixed<const L: usize> {
limbs: [Limb; L], // fixed-size array
}
<span class="boring">}</span></code></pre></pre>
<h3 id="33-type-aliases"><a class="header" href="#33-type-aliases">3.3 Type Aliases</a></h3>
<p>Common fixed sizes:</p>
<ul>
<li><code>U256 = BigIntFixed<4></code> (256 bits)</li>
<li><code>U512 = BigIntFixed<8></code> (512 bits)</li>
<li><code>U1024 = BigIntFixed<16></code> (1024 bits)</li>
<li><code>U2048 = BigIntFixed<32></code> (2048 bits)</li>
</ul>
<h2 id="4-encoding-format"><a class="header" href="#4-encoding-format">4. Encoding Format</a></h2>
<h3 id="41-binary-format"><a class="header" href="#41-binary-format">4.1 Binary Format</a></h3>
<p>Canonical binary encoding uses the following format:</p>
<pre><code>[sign: u8][limb_count: u32 LE][limbs: u64[] LE]
</code></pre>
<p>Where:</p>
<ul>
<li><code>sign</code>: 0 for positive, 1 for negative</li>
<li><code>limb_count</code>: number of limbs (u32, little-endian)</li>
<li><code>limbs</code>: limb array in little-endian byte order</li>
</ul>
<h3 id="42-encoding-rules"><a class="header" href="#42-encoding-rules">4.2 Encoding Rules</a></h3>
<ol>
<li><strong>Canonical encoding</strong>: Every integer has exactly one valid encoding</li>
<li><strong>No leading zeros</strong>: Encoded limb arrays must not contain leading zero limbs</li>
<li><strong>Minimal length</strong>: Use the smallest limb count that satisfies canonical form</li>
<li><strong>Deterministic</strong>: Same integer always produces identical encoding</li>
</ol>
<h3 id="43-decoding-validation"><a class="header" href="#43-decoding-validation">4.3 Decoding Validation</a></h3>
<p>Decoding MUST reject:</p>
<ul>
<li>Invalid sign values (not 0 or 1)</li>
<li>Leading zero limbs in non-zero values</li>
<li>Negative zero representations</li>
<li>Malformed binary data</li>
</ul>
<h2 id="5-memory-management"><a class="header" href="#5-memory-management">5. Memory Management</a></h2>
<h3 id="51-allocation-strategy"><a class="header" href="#51-allocation-strategy">5.1 Allocation Strategy</a></h3>
<ul>
<li>Dynamic BigInt uses heap allocation with Vec<Limb></li>
<li>Fixed BigInt uses stack allocation when L ≤ 8</li>
<li>Heap allocation for L > 8 or dynamic types</li>
</ul>
<h3 id="52-capacity-limits"><a class="header" href="#52-capacity-limits">5.2 Capacity Limits</a></h3>
<ul>
<li><code>MAX_LIMBS = 512</code> (32768 bits maximum)</li>
<li>Prevents denial-of-service attacks</li>
<li>Enforced at allocation time</li>
</ul>
<h3 id="53-canonicalization"><a class="header" href="#53-canonicalization">5.3 Canonicalization</a></h3>
<p>All BigInt values MUST be canonicalized after operations:</p>
<ul>
<li>Remove leading zero limbs</li>
<li>Enforce zero sign rule</li>
<li>Maintain minimal representation</li>
</ul>
<h2 id="6-cross-platform-compatibility"><a class="header" href="#6-cross-platform-compatibility">6. Cross-Platform Compatibility</a></h2>
<h3 id="61-endianness"><a class="header" href="#61-endianness">6.1 Endianness</a></h3>
<ul>
<li>All encodings use little-endian byte order</li>
<li>Limb arrays are always little-endian</li>
<li>Platform endianness is irrelevant</li>
</ul>
<h3 id="62-word-size"><a class="header" href="#62-word-size">6.2 Word Size</a></h3>
<ul>
<li>Assumes 64-bit limbs</li>
<li>No support for 32-bit platforms</li>
<li>All operations assume 64-bit arithmetic</li>
</ul>
<h3 id="63-determinism"><a class="header" href="#63-determinism">6.3 Determinism</a></h3>
<ul>
<li>Identical inputs produce identical outputs</li>
<li>No platform-specific behavior</li>
<li>No undefined behavior in arithmetic</li>
</ul>
<h2 id="7-error-conditions"><a class="header" href="#7-error-conditions">7. Error Conditions</a></h2>
<h3 id="71-invalid-encoding"><a class="header" href="#71-invalid-encoding">7.1 Invalid Encoding</a></h3>
<p><code>InvalidEncoding</code> error for:</p>
<ul>
<li>Malformed binary data</li>
<li>Non-canonical representations</li>
<li>Invalid sign values</li>
</ul>
<h3 id="72-capacity-exceeded"><a class="header" href="#72-capacity-exceeded">7.2 Capacity Exceeded</a></h3>
<p><code>Overflow</code> error for:</p>
<ul>
<li>Operations exceeding max_limbs</li>
<li>Fixed-size overflow (const generics)</li>
</ul>
<h3 id="73-invalid-operations"><a class="header" href="#73-invalid-operations">7.3 Invalid Operations</a></h3>
<p><code>InvalidModulus</code> error for:</p>
<ul>
<li>Even moduli in Montgomery operations</li>
<li>Zero moduli</li>
<li>Invalid modulus sizes</li>
</ul>
<h2 id="8-testing-requirements"><a class="header" href="#8-testing-requirements">8. Testing Requirements</a></h2>
<h3 id="81-canonical-form-tests"><a class="header" href="#81-canonical-form-tests">8.1 Canonical Form Tests</a></h3>
<ul>
<li>Zero representation uniqueness</li>
<li>No leading zeros in encodings</li>
<li>Sign consistency validation</li>
<li>Cross-platform determinism</li>
</ul>
<h3 id="82-encoding-tests"><a class="header" href="#82-encoding-tests">8.2 Encoding Tests</a></h3>
<ul>
<li>Round-trip encoding/decoding</li>
<li>Rejection of invalid encodings</li>
<li>Canonical encoding uniqueness</li>
<li>Performance benchmarks</li>
</ul>
<h3 id="83-memory-safety-tests"><a class="header" href="#83-memory-safety-tests">8.3 Memory Safety Tests</a></h3>
<ul>
<li>Bounds checking validation</li>
<li>Allocation limit enforcement</li>
<li>Memory leak prevention</li>
<li>Stack overflow prevention</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="index.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="arithmetic.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="index.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="arithmetic.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js"></script>
<script src="mark.min.js"></script>
<script src="searcher.js"></script>
<script src="clipboard.min.js"></script>
<script src="highlight.js"></script>
<script src="book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>