Cairo-integer-types - A library for bitwise integer types (e.g. int64 or uint32) in Cairo, with a test suite

Overview

The Cairo bitwise integer library (cairo-bitwise-int v0.1.1)

The Cairo smart test library (cairo-smart-test v0.1.1)

Author: Jamie Gabbay

What is this?

The Cairo Abstract Machine's primitive notion of counting is a finite field over a prime of size approximately 2^251. This differs significantly from that of a typical Intel or ARM chip, which is typically a 64-bit integer bitwise representation.

This directory contains:

  • cairo-bitwise-int: a collection of Cairo libraries to emulate signed and unsigned integer datatypes of various bit lengths (e.g. 8-bit unsigned integers, also known as 'bytes'), and
  • cairo-smart-test: an automated unit- and property-based test suite.

I am pleased to share this with the Cairo community, and feedback and suggestions are welcome.

How to use the Cairo bitwise library off-the-shelf

The code directory contains prepared source files. For example:

  • uint8.cairo is a library for unsigned 8-bit integers (i.e. "bytes").
  • uint32.cairo is a library for unsigned 32-bit integers (i.e. "words").
  • int32.cairo is a library for signed 32-bit integers (i.e. "signed words").

Assuming you are writing Cairo code, You can import these libraries using the usual Cairo library import mechanism.

How to customise the library

Templates and BIT_LENGTH

The code is templated over a BIT_LENGTH parameter which may vary between 4 and 125.

  • The templates are in the templates directory.
  • Generation of code from templates is controlled by the file run-this-file-to-build-code-directory-from-template-directory.py, which also contains a list of bit lengths to use.

This means that if you want an int93.cairo library, you can have one, by following the instructions below.

The instructions

You'll need a working Cairo installation. Cairo install instructions are here -- so do that first! The rest of these instructions assume you're in a Cairo virtual environment.

We'll also assume you're using a Linux system; YMMV on other systems but the overall idea should be the same.

To set up:

Let's say that again in code:

source ./enter-enviroment.sh
pip3 install jinja2 hypothesis
python3 run-this-file-to-build-code-directory-from-template-directory.py
cd code
bash run_all_tests.sh

For custom bit lengths, just edit the list of bit_lengths in run-this-file-to-build-code-directory-from-template-directory.py (direct link to line, correct at time of writing).

That's it! The bitwise integer library files should now be in your code directory and (optionally) fully tested.

The Cairo smart test suite

The unit- and property-based test suite is in the file templates/cairo_smart_test_framework.py. The smart test suite is applied here to the bitwise library, but it exists independently and provides a comprehensive test framework for Cairo code.

You can use the smart test suite in your own Cairo development just by copying the templates/cairo_smart_test_framework.py file into your development and using it, following

Usage is designed to be straightforward but I'm happy to answer questions and act on feedback.

Why do we need a Cairo bitwise integer library?

As you may know, Cairo's primitive numerical datatype is a felt (field element) which is a number between 0 and a constant DEFAULT_PRIME, currently set to just over 2^251.

However, the difference between the number model of Cairo and that of a typical computer chip goes deeper than the difference between 2^64 and 2^251. For instance:

  • In Cairo, every number element has a multiplicative inverse, since we are in a finite field. So for example "divide by two" is well-defined and is a bijection on the number space. This is not the case for a typical bitwise representation!
  • Conversely, Cairo has no native notions of "shift left one bit" or "shift right one bit" (multiplying or dividing by two is not the same thing, in a finite field) -- nor of "add with overflow" (there is no native notion of overflow, again because we are in a finite field), and so forth.

This sets up a representational mismatch between Cairo and bitwise-based models of computing on numbers.

The Cairo bitwise integer library helps to bridge this gap with suitable library emulations of "bitwise-flavoured" datatypes for numerical computations -- the ones you're probably used to, such as "64-bit unsigned integers" (see code/uint64.cairo).

Why do we need a Cairo test framework?

Seriously? You do! The code in this repo has been tested using a unit- and property-based test suite specifically designed to work well with Cairo.

Feedback and comments ...

... are very welcome. Thanks in advance.

Python: Wrangled and unpivoted gaming datasets. Tableau: created dashboards - Market Beacon and Player’s Shopping Guide.

Created two information products for GameStop. Using Python, wrangled and unpivoted datasets, and created Tableau dashboards.

Zinaida Dvoskina 2 Jan 29, 2022
Cloud Native sample microservices showcasing Full Stack Observability using AppDynamics and ThousandEyes

Cloud Native Sample Bookinfo App Observability Bookinfo is a sample application composed of four Microservices written in different languages.

Cisco DevNet 13 Jul 21, 2022
Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses

Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses. Obtain the postfix expression of the infix expression Step 1.Reverse the postfix expres

Sazzad Hossen 1 Jan 04, 2022
ToDo - A simple bot to keep track of things you need to do

ToDo A simple bot to keep track of things you need to do. Installation You will

3 Sep 18, 2022
Yandex Media Browser

Браузер медиа для плагина Yandex Station Включайте музыку, плейлисты и радио на Яндекс.Станции из Home Assistant! Скриншот Корневой раздел: Библиотека

Alexander Ryazanov 35 Dec 19, 2022
A simple python script to convert Rubber Ducky payloads into AutoHotKey scripts

AHKDuckyReplacer A simple python script to convert Rubber Ducky payloads into AutoHotKey scripts. I have also added a sample payload for testing. I wi

Krizsan0596 5 Sep 28, 2022
Cross-platform MachO/ObjC Static binary analysis tool & library. class-dump + otool + lipo + more

ktool Static Mach-O binary metadata analysis tool / information dumper pip3 install k2l Development is currently taking place on the @python3.10 branc

Kritanta 301 Dec 28, 2022
addon for blender to import mocap data from tools like easymocap, frankmocap and Vibe

b3d_mocap_import addon for blender to import mocap data from tools like easymocap, frankmocap and Vibe ==================VIBE================== To use

Carlos Barreto 97 Dec 07, 2022
Reproduce digital electronics in Python

Pylectronics Reproduce digital electronics in Python Report Bug · Request Feature Table of Contents About The Project Getting Started Prerequisites In

Filipe Garcia 45 Dec 20, 2021
Easily Generate Revolut Business Cards

RevBusinessCardGen Easily Generate Revolut Business Cards Prerequisites Before you begin, ensure you have met the following requirements: You have ins

Younes™ 35 Dec 14, 2022
Python library to interact with Move Hub / PoweredUp Hubs

Python library to interact with Move Hub / PoweredUp Hubs Move Hub is central controller block of LEGO® Boost Robotics Set. In fact, Move Hub is just

Andrey Pokhilko 499 Jan 04, 2023
solsim is the Solana complex systems simulator. It simulates behavior of dynamical systems—DeFi protocols, DAO governance, cryptocurrencies, and more—built on the Solana blockchain

solsim is the Solana complex systems simulator. It simulates behavior of dynamical systems—DeFi protocols, DAO governance, cryptocurrencies, and more—built on the Solana blockchain

William Wolf 12 Jul 13, 2022
Chess bot can play automatically as white or black on lichess.com, chess.com and any website using drag and drop to move pieces

Chessbot "Why create another chessbot ?" The explanation is simple : I did not find a free bot I liked online : all the bots I saw on internet are par

Dhimas Bagus Prayoga 2 Nov 11, 2021
PyGo custom language, New but similar language programming

New but similar language programming. Now we are capable to program in a very similar language to Python but at the same time get the efficiency of Go.

Fernando Perez 4 Nov 19, 2022
tagls is a language server based on gtags.

tagls tagls is a language server based on gtags. Why I wrote it? Almost all modern editors have great support to LSP, but language servers based on se

daquexian 31 Dec 01, 2022
A framework to create reusable Dash layout.

dash_component_template A framework to create reusable Dash layout.

The TolTEC Project 4 Aug 04, 2022
Tool to audit and fix Python project requirements.

Requirement Auditor Utility to revise and updated python requirement files.

Luis Carlos Berrocal 1 Nov 07, 2021
A module that can manage you're gtps

Growtopia Private Server Controler Module For Controle Your GTPS | Build in Python3 Creator Information

iFanpS 6 Jan 14, 2022
Just imagine normal bancho, but you can have multiple profiles and funorange speed up maps ranked

Local osu! server Just imagine normal bancho, but you can have multiple profiles and funorange speed up maps ranked (coming soon)! Windows Setup Insta

Cover 25 Nov 15, 2022
Localization and multifractal properties of the long-range Kitaev chain in the presence of an Aubry-André-Harper modulation

This repository contains the code for the paper Localization and multifractal properties of the long-range Kitaev chain in the presence of an Aubry-André-Harper modulation.

Joana Fraxanet 2 Apr 17, 2022