gmp bindings for rust

Related tags

Computation rust-gmp
Overview

Build Status

Documentation

The following functions are intentionally left out of the bindings:

  • gmp_randinit (not thread-safe, obsolete)
  • mpz_random (not thread-safe, obsolete)
  • mpz_random2 (not thread-safe, obsolete)
  • mpf_set_default_prec (not thread-safe)
  • mpf_get_default_prec (not thread-safe)
  • mpf_init (not thread-safe)
  • mpf_inits (not thread-safe, va_list wrapper)
  • mpf_clears (va_list wrapper)
  • mpf_swap (no better than rust's swap)
  • mpf_set_prec_raw (could be exposed with an unsafe function if needed)
  • mpz_inits (va_list wrapper)
  • mpz_clears (va_list wrapper)
  • mpz_swap (no better than rust's swap)
  • mpq_inits (va_list wrapper)
  • mpq_clears (va_list wrapper)
  • mpq_swap (no better than rust's swap)
Comments
  • Clear Clippy warnings

    Clear Clippy warnings

    This PR clears all Clippy warnings (incl. nightly) except for documentation of unsafe

    warning: unsafe function's docs miss `# Safety` section
    

    Please note, the first patch runs cargo fmt if that is not wanted I can remove it.

    Commit d7f6956 Use std::mem::MaybeUninit needs special attention please.

    This is kinda a big PR for my first one so if you'd like me to split the more trivial patches out into a separate PR I can do that also.

    Thanks

    opened by tcharding 9
  • impl for num_traits::{Zero, One}

    impl for num_traits::{Zero, One}

    As far as I can tell the corresponding traits from std are deprecated in favour of the traits from num. This also matches with what is done in e.g. ramp.

    opened by mortendahl 3
  • Internals should probably not be pub

    Internals should probably not be pub

    I noticed that the mpz_struct inside Mpz is public, but it really shouldn't be. It's not safe to modify the contents. If clients need access to the contents, there should be safe accessors instead.

    opened by taralx 3
  • Mpq::ratio is unsafe

    Mpq::ratio is unsafe

    Mpq::ratio is unsafe:

    extern crate gmp;
    use gmp::mpq::Mpq;
    use gmp::mpz::Mpz;
    fn main() {
        println!("{:?}", Mpq::ratio(&Mpz::one(), &-Mpz::one()));
        //                                        ^--- (!)
    }
    

    results in:

    zsh: segmentation fault (core dumped)
    

    Tested this using gmp-6.1.2 on Arch Linux.

    To quote the manual:

    Note that if an assignment to the numerator and/or denominator could take an mpq_t out of the canonical form described at the start of this chapter (see Rational Number Functions) then mpq_canonicalize must be called before any other mpq functions are applied to that mpq_t.

    There are probably other rational functions that need to be scrutinized as well.

    opened by Rufflewind 1
  • added mpz, mpq, and mpf sign

    added mpz, mpq, and mpf sign

    Added functions to get the sign of Mpz, Mpq, and Mpf. Although mp{z,q,f}_sgn functions appear to exist in the original C++ API, they are actually macros and cannot be used here.

    Old way of getting sign:

    match n.cmp(&Mpz::zero()) { // allocation :(
        Ordering::Less => ...,
        Ordering::Equal => ...,
        Ordering::Greater => ...,
    }
    

    New way:

    match n.sign() {
        Sign::Negative => ...,
        Sign::Zero => ...,
        Sign::Positive => ...,
    }
    

    I'm new to Rust, so any feedback is appreciated!

    opened by mhogrefe 1
  • support for powm_sec

    support for powm_sec

    When using GMP for cryptographic applications it is often desirable to have functions resilient to timing attacks, such as powm_sec for integer exponentiation (https://gmplib.org/manual/Integer-Exponentiation.html#Integer-Exponentiation). This PR exposes this function.

    opened by mortendahl 1
  • added set_z and set_q for mpf

    added set_z and set_q for mpf

    Hey,

    This pull request contains linking for the c functions to create Mpf structs from Mpz and Mpq structs.

    The functions works exacly like the set_z function on Mpq.

    Cheers!

    opened by Nojgaard 1
  • Modifies the Mpf type to add functionality.

    Modifies the Mpf type to add functionality.

    Mpf::zero() creates a new Mpf with value zero Mpf::set_from_str() sets the Mpf to the value in str in the given base Mpf::set_from_si() sets the value from a signed int Mpf::get_str() gets a string of the Mpf, see GMP docs for formatting. Mpf::sqrt() is self-explanatory

    Also increments version number.

    opened by timp3289 1
  • (..)::FromPrimitive is not implemented for the type (..)::Mpz

    (..)::FromPrimitive is not implemented for the type (..)::Mpz

    this is the behavior I'm experiencing. Let me know if you need more context. I'm also on nightly, which could just totally be screwing this up.

    extern crate gmp;
    extern crate num;
    
    use self::gmp::mpz::Mpz;
    use self::num::FromPrimitive;
    
    let i = 5;
    let mpz_i: Mpz = FromPrimitive::from_u64(i as u64).unwrap();
    
    opened by zachdaniel 1
  • Mpf::get_str causes segfault

    Mpf::get_str causes segfault

    The wrapper for mpf_get_str passes it a pointer to a string of length 0. This means that when a user specifies the number of digits, the function overwrites memory starting from the empty string. This will overwrite the null on the end of the CString.

    However, the fix for this is relatively simple.

    When mpf_get_str has a null passed to it as the first argument, it uses malloc to create a CString of correct size, which it then returns. By using this, and then freeing the data afterwards (as to_string takes a copy), this is fixed with no memory leaks!

    opened by Cyclic3 0
  • Mpz, Mpq, Mpf, RandState: mark Sync

    Mpz, Mpq, Mpf, RandState: mark Sync

    GMP guarantees that it’s safe for two threads to read from the same GMP variable simultaneously (https://gmplib.org/manual/Reentrancy.html), so the types should be marked Sync. The Rust type system still protects against read/write and write/write races.

    opened by andersk 0
  • Add impl From<u128, u16, u8, usize, i128, i16, i8, isize> for Mpz

    Add impl From for Mpz

    These seem arbitrarily left out, which means you need to jump through a few additional hoops if you want, for example, Mpz::from(vec.len()) or the like.

    opened by emlun 0
  • Adding serde support feature for Mpz

    Adding serde support feature for Mpz

    We needed serde for Mpz in one of our upper layer (dependency). Here is a PR if you are interested, I added it as a feature.

    cargo build --features "serde_support"
    cargo test --features "serde_support"
    

    Thanks,

    opened by gbenattar 4
  • Duplicated crates of rust-gmp ?

    Duplicated crates of rust-gmp ?

    I'm a bit confused, looking on https://crates.io/ for a rust wrapper for the GMP library I found both:

    linking this GitHub repository!

    opened by yvan-sraka 8
  • Mpf get str fixups

    Mpf get str fixups

    bug was introduced in 877104f58239fd19a4f3f5aed13bdd677c65f28b.

    the intent here is to allocate enough space for gmpf_get_str to plop a string. if all digits are requested (n_digits == 0), we defer to gmpf_get_str itself to figure out how much space is needed (because the limb nonsense is too tedious to deal with) by passing nullptr to the first argument, as documented in the manual:

    If str is NULL, the result string is allocated using the current allocation function (see Custom Allocation). The block will be strlen(str)+1 bytes, that being exactly enough for the string and null-terminator.

    if the number of digits requested is known (non-zero n_digits), then simply allocate a correctly sized cstring (unlike 877104f58239fd19a4f3f5aed13bdd677c65f28b, which allocates a 0-byte string).

    to avoid memory leaks from repeated get_str(n_digits=0) calls, a wrapper is used to de-allocate on drop the gmp-allocated string.

    with this patch, #24 passes.

    opened by bryant 1
Owner
Bartłomiej Kamiński
Physics graduate, programming at work and as a hobby
Bartłomiej Kamiński
Mathematical optimization in pure Rust

argmin A pure Rust optimization framework This crate offers a numerical optimization toolbox/framework written entirely in Rust. It is at the moment p

argmin 549 Jan 1, 2023
Rust wrapper for ArrayFire

Arrayfire Rust Bindings ArrayFire is a high performance library for parallel computing with an easy-to-use API. It enables users to write scientific c

ArrayFire 696 Dec 30, 2022
Scientific Computing Library in Rust

SciRust Scientific computing library written in Rust programming language. The objective is to design a generic library which can be used as a backbon

In Digits 242 Dec 16, 2022
Statistical computation library for Rust

statrs Current Version: v0.13.0 Should work for both nightly and stable Rust. NOTE: While I will try to maintain backwards compatibility as much as po

Michael Ma 384 Dec 27, 2022
Collection of Optimization algorithm in Rust

rustimization A rust optimization library which includes L-BFGS-B and Conjugate Gradient algorithm. Documentation The simplest way to use these optimi

Naushad Karim 47 Sep 23, 2022
The write-once-run-anywhere GPGPU library for Rust

The old version of Emu (which used macros) is here. Overview Emu is a GPGPU library for Rust with a focus on portability, modularity, and performance.

Caleb Winston 1.5k Dec 30, 2022
Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

null 294 Dec 23, 2022
An esoteric language/compiler written with Rust and Rust LLVM bindings

MeidoLang (メイドラング) A not so useful and esoteric language. The goal of this project was to contain some quirky or novel syntax in a stack-style program

null 0 Dec 24, 2021
Rust based WASM/JS bindings for ur-rust

ur-wasm-js WASM/JS bindings for the ur-rust rust library Getting started Installation Either build the library yourself with wasm-pack or install for

Lightning Digital Entertainment 5 Feb 28, 2024
Rust bindings for libinjection

libinjection-rs Rust bindings for libinjection. How to use Add libinjection to dependencies of Cargo.toml: libinjection = "0.2" Import crate: extern c

ArvanCloud 35 Sep 24, 2022
A project for generating C bindings from Rust code

cbindgen   Read the full user docs here! cbindgen creates C/C++11 headers for Rust libraries which expose a public C API. While you could do this by h

Ryan Hunt 1.7k Jan 3, 2023
Automatically generates Rust FFI bindings to C (and some C++) libraries.

bindgen bindgen automatically generates Rust FFI bindings to C (and some C++) libraries. For example, given the C header doggo.h: typedef struct Doggo

The Rust Programming Language 3.2k Jan 4, 2023
Rust-JDBC bindings

jdbc A Rust library that allows you to use JDBC and JDBC drivers. Usage First, add the following to your Cargo.toml: [dependencies] jdbc = "0.1" Next,

Aurora 18 Feb 9, 2022
Lua 5.3 bindings for Rust

rust-lua53 Aims to be complete Rust bindings for Lua 5.3 and beyond. Currently, master is tracking Lua 5.3.3. Requires a Unix-like environment. On Win

J.C. Moyer 150 Dec 14, 2022
Safe Rust bindings to Lua 5.1

rust-lua Copyright 2014 Lily Ballard Description This is a set of Rust bindings to Lua 5.1. The goal is to provide a (relatively) safe interface to Lu

Lily Ballard 124 Jan 5, 2023
mruby safe bindings for Rust

mrusty. mruby safe bindings for Rust mrusty lets you: run Ruby 1.9 files with a very restricted API (without having to install Ruby) reflect Rust stru

Anima 200 Oct 12, 2022
Rust bindings for writing safe and fast native Node.js modules.

Rust bindings for writing safe and fast native Node.js modules. Getting started Once you have the platform dependencies installed, getting started is

The Neon Project 7k Jan 4, 2023
Objective-C Runtime bindings and wrapper for Rust.

Objective-C Runtime bindings and wrapper for Rust. Documentation: http://ssheldon.github.io/rust-objc/objc/ Crate: https://crates.io/crates/objc Messa

Steven Sheldon 336 Jan 2, 2023
High-level Rust bindings to Perl XS API

Perl XS for Rust High-level Rust bindings to Perl XS API. Example xs! { package Array::Sum; sub sum_array(ctx, array: AV) { array.iter().map(|

Vickenty Fesunov 59 Oct 6, 2022
Rust <-> Python bindings

rust-cpython Rust bindings for the python interpreter. Documentation Cargo package: cpython Copyright (c) 2015-2020 Daniel Grunwald. Rust-cpython is l

Daniel Grunwald 1.7k Dec 29, 2022