All Projects → JeffreySarnoff → SaferIntegers.jl

JeffreySarnoff / SaferIntegers.jl

Licence: MIT license
These integer types use checked arithmetic, otherwise they are as system types.

Programming Languages

julia
2034 projects

Projects that are alternatives of or similar to SaferIntegers.jl

bs-Zarith
Support Bigint, Q (rational numbers) and Z (integer numbers) in BuckleScript.
Stars: ✭ 15 (-67.39%)
Mutual labels:  integer
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (-15.22%)
Mutual labels:  integer
tua-body-scroll-lock
🔐 Body scroll locking that just works with everything
Stars: ✭ 304 (+560.87%)
Mutual labels:  overflow
SwiftRadix
Easily convert integers to binary/hex/octal strings and back again with clean functional syntax.
Stars: ✭ 34 (-26.09%)
Mutual labels:  integer
nim-nint128
128-bit integers
Stars: ✭ 17 (-63.04%)
Mutual labels:  integer
FPChecker
A dynamic analysis tool to detect floating-point errors in HPC applications.
Stars: ✭ 26 (-43.48%)
Mutual labels:  overflow
google-one-tap
Google One Tap Login
Stars: ✭ 37 (-19.57%)
Mutual labels:  signed
transparency
Read-only mirror of https://owo.codes/whats-this/transparency
Stars: ✭ 13 (-71.74%)
Mutual labels:  signed
overflow
A command-line tool for exploiting stack-based buffer overflow vulnerabilities.
Stars: ✭ 66 (+43.48%)
Mutual labels:  overflow
SAP vulnerabilities
DoS PoC's for SAP products
Stars: ✭ 47 (+2.17%)
Mutual labels:  overflow
sicherboot
Unmaintained systemd-boot integration with secure boot support; consider https://github.com/Foxboron/sbctl instead.
Stars: ✭ 31 (-32.61%)
Mutual labels:  signed
max-safe-integer
ES2015 Number.MAX_SAFE_INTEGER ponyfill
Stars: ✭ 15 (-67.39%)
Mutual labels:  integer
Body Scroll Lock
Body scroll locking that just works with everything 😏
Stars: ✭ 3,357 (+7197.83%)
Mutual labels:  overflow
postcss-momentum-scrolling
PostCSS plugin add 'momentum' style scrolling behavior (-webkit-overflow-scrolling: touch) for elements with overflow (scroll, auto) on iOS
Stars: ✭ 69 (+50%)
Mutual labels:  overflow
react-overflow-indicator
Detect overflow and render shadows, fades, arrows, etc.
Stars: ✭ 66 (+43.48%)
Mutual labels:  overflow
mixed-precision-pytorch
Training with FP16 weights in PyTorch
Stars: ✭ 72 (+56.52%)
Mutual labels:  overflow
BhimIntegers
BhimIntegers🚀 is a C++ library that is useful when we are dealing with BigIntegers💥💥. We can handle big integers (integers having a size bigger than the long long int data type) and we can perform arithmetic operations📘 like addition, multiplication, subtraction, division, equality check, etc📐📐. Also, there are several functions like factorial, …
Stars: ✭ 43 (-6.52%)
Mutual labels:  integer
Unity-Int-Vectors
Library that adds Vector2i and Vector3i classes, which are integer counterparts to Vector2 and Vector3.
Stars: ✭ 23 (-50%)
Mutual labels:  integer

SaferIntegers

These integer types do not ignore arithmetic overflows and underflows.


Copyright © 2018-2019 by Jeffrey Sarnoff.    This work is made available under The MIT License.


Docs        Build Status    Test Coverage    Test Coverage         MIT license


A Safer Way

Using the default Int or UInt types allows overflow and underflow errors to occur silently, without notice. These incorrect values propagate and such errors are difficult to recognize after the fact.

This package exports safer versions. These types check for overflow and underflow in each of the basic arithmetic functions. The processing will stop with a message in the event of overflow or underflow. On one machine, the overhead relative to the built-in integer types is <= 1.2x.

Background

Integer overflow occurs when an integer type is increased beyond its maximum value. Integer underflow occurs when an integer type is decreased below its minimum value. Signed and Unsigned values are subject to overflow and underflow. With Julia, you can see the rollover using Int or UInt types:

typemax(Int) + one(Int) < 0
typemin(Int) - one(Int) > 0
typemax(UInt) + one(UInt) == typemin(UInt)
typemin(UInt) - one(UInt) == typemax(UInt)

There are security implications for integer overflow in certain situations.

 for i in 1:a
    secure(biohazard[i])
 end
 
 a = Int16(456) * Int16(567)
 a == -3592
 # the for loop does not execute

Highlights

Why Does This Package Exist?

  • Your work may require that integer calculations be secure, well-behaved or unsurprising.

  • Your clients may expect your package/app/product calculates with care and correctness.

  • Your software may become part of a system on which the health or assets of others depends.

  • Your prefer to publish research results that are free of error, and you work with integers.

What Does This Package Offer?

  • SaferIntegers let you work more cleanly and always alerts otherwise silent problems.

  • This package is designed for easy use and written to be performant in many sorts of use.

  • All exported types are stable (e.g. typeof(SafeInt32 + 1) == SafeInt32)

  • Using SaferIntegers can preclude some known ways that insecure systems are breached.

  • Safer Rationals just work: SafeRational(161803398875,100000000000)

Test code for integer safety

  • @saferintegers include(join("PackageTestDirectory", "PackageTests.jl"))
    • includes a type modified PackageTests.jl in the current environment
    • all Integer types in PackageTests.jl are become SafeInteger types
    • run it and see if there are any problems!

A Basic Guide

To use safer integers within your computations, where you have been using
explict digit sequences put them inside the safe integer constructors,
SafeInt(11) or SafeUInt(0x015A) and similarly for the bitsize-named versions
SafeInt8, SafeInt16 .. SafeInt128 and SafeUInt8 .. SafeUInt128

Where you had usedInt or UInt now use SafeInt or SafeUInt and similarly with the bitsize-named versions.

SafeInt and SafeUInt give you these arithmetic operators:
+, -, *, div, rem, fld, mod, fld1, mod1, ^
which have become overflow and underflow aware.

The Int and UInt types can fail at simple arithmetic
and will continue carrying the incorrectness forward.
The validity of values obtained is difficult to ascertain.

Most calculations proceed without incident, and when used SafeInts operate as Ints should a calculation encouter an overflow or underflow, we are alerted and the calculation does not proceed.

Give them a whirl.

Get the package: Pkg.add("SaferIntegers")
Use the package: using SaferIntegers

  • These functions check for overflow/underflow automatically:
    • abs, neg, div, fld, fld1, cld, rem, mod, mod1
    • divrem, fldmod, fldmod1
    • -, +, *, ^
    • so does /, before converting to Float64

Exported Types and Constructors / Converters

  • SafeInt8, SafeInt16, SafeInt32, SafeInt64, SafeInt128
  • SafeUInt8, SafeUInt16, SafeUInt32, SafeUInt64, SafeUInt128
  • SafeSigned, SafeUnsigned, SafeInteger
  • SafeRational

They check for overflow, even when multiplied by the usual Int and UInt types.
Otherwise, they should be unsurprising.

Type definitions

abstract type SafeUnsigned <: Unsigned end
abstract type SafeSigned <: Signed end
const SafeInteger = Union{SafeUnsigned, SafeSigned}

(thanks to Mark Kittisopikul's PR and TimHoly's design of Ratios.jl)

Other Conversions

Signed(x::SafeSigned) returns an signed integer of the same bitwidth as x
Unsigned(x::SafeUnsigned) returns an unsigned integer of the same bitwidth as x
Integer(x::SafeInteger) returns an Integer of the same bitwidth and either Signed or Unsigned as is x

SafeSigned(x::Signed) returns a safe signed integer of the same bitwidth as x
SafeUnsigned(x::Unsigned) returns a safe unsigned integer of the same bitwidth as x
SafeInteger(x::Integer) returns a safe Integer of the same bitwidth and is either Signed or Unsigned as matches x

Supports

  • signbit, sign, abs, abs2
  • count_ones, count_zeros
  • leading_zeros, trailing_zeros, leading_ones, trailing_ones
  • ndigits0z
  • isless, isequal, <=, <, ==, !=, >=, >
  • >>>, >>, <<, +, -, *, \, ^
  • div, fld, fld1, cld, rem, mod, mod1
  • divrem, fldmod, fldmod1
  • zero, one
  • typemin, typemax, widen

Test code for integer safety

test snippets

julia> @saferintegers begin
         x = 64
         y = Int16(16)
         z = x + y + SafeInt128(x)
         x, y, z
         end
(64, 16, 144)

julia> typeof.(ans)
(SafeInt64, SafeInt16, SafeInt128)

test source file

julia> cd(<source file directory>)
julia> @saferintegers include(<filename.jl>)

Benchmarking (on one machine)

julia v1.1-dev

using SaferIntegers
using BenchmarkTools
BenchmarkTools.DEFAULT_PARAMETERS.time_tolerance=0.005

@noinline function test(n, f, a,b,c,d)
   result = a;
   i = 0
   while true
       i += 1
       i > n && break       
       result += f(d,c)+f(b,a)+f(d,b)+f(c,a)
   end
   return result
end

hundredths(x) = round(x, digits=2)

a = 17; b = 721; c = 75; d = 567;
sa, sb, sc, sd = SafeInt.((a, b, c, d));
n = 10_000;

hundredths( (@belapsed test(n, +, $sa, $sb, $sc, $sd)) /
            (@belapsed test(n, +, $a, $b, $c, $d))        )
1.25

hundredths( (@belapsed test(n, *, $sa, $sb, $sc, $sd)) /
            (@belapsed test(n, *, $a, $b, $c, $d))        )
1.25

hundredths( (@belapsed test(n, div, $sa, $sb, $sc, $sd)) /
            (@belapsed test(n, div, $a, $b, $c, $d))      )
1.14

credits

This work derives from RoundingIntegers.jl.

The @saferintegers machinery is heavily informed by ChangePrecision.jl.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].