Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GMP/MPIR bindings fixups #157

Open
wants to merge 26 commits into
base: libgmp-transition
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ jobs:
python -m pip install maturin
rustup target add x86_64-unknown-linux-musl

- name: Install GMP
if: startsWith(matrix.os, 'ubuntu')
run: |
sudo apt-get install lzip -y
curl -L https://gmplib.org/download/gmp/gmp-6.2.1.tar.lz | lzip -dc | tar x && \
cd gmp-6.2.1 && ./configure --enable-fat --with-pic && \
make -j 6 && sudo make install && cd .. && rm -rf gmp-6.2.1

- name: Build Windows
if: startsWith(matrix.os, 'windows')
run: |
Expand All @@ -54,6 +62,7 @@ jobs:
git clone https://github.com/Chia-Network/clvm_tools.git --branch=main --single-branch
python -m pip install ./clvm_tools
python -m pip install colorama
git clone https://github.com/Chia-Network/mpir_gc_x64.git --depth 1
maturin develop --release

- name: Run benchmarks (Windows)
Expand All @@ -62,6 +71,13 @@ jobs:
. .\venv\Scripts\Activate.ps1
python benchmark/run-benchmark.py

- name: Install GMP
if: ${{ startsWith(matrix.os, 'ubuntu') }}
run: |
sudo apt install libgmp3-dev
ls -la /usr/lib64
ls -la /usr/lib

- name: Build
if: ${{ !startsWith(matrix.os, 'windows') }}
env:
Expand Down Expand Up @@ -106,6 +122,10 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get install lzip -y
curl -L https://gmplib.org/download/gmp/gmp-6.2.1.tar.lz | lzip -dc | tar x && \
cd gmp-6.2.1 && ./configure --enable-fat --with-pic && \
make -j 6 && sudo make install && cd .. && rm -rf gmp-6.2.1
python -m pip install maturin
rustup target add x86_64-unknown-linux-musl

Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/build-arm64-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
os: [ ARM64 ] # , ubuntu-latest ]

steps:

- uses: Chia-Network/actions/clean-workspace@main

- name: Checkout repository
uses: actions/checkout@v2
with:
Expand All @@ -41,7 +44,10 @@ jobs:
echo $PATH && \
curl -L https://sh.rustup.rs > rustup-init.sh && \
sh rustup-init.sh -y && \
yum -y install openssl-devel && \
yum -y install sudo openssl-devel && \
curl -L https://gmplib.org/download/gmp/gmp-6.2.1.tar.bz2 | tar -xj && \
cd gmp-6.2.1 && ./configure --enable-fat --with-pic && \
make -j 6 && sudo make install && cd .. && rm -rf gmp-6.2.1 && \
source $HOME/.cargo/env && \
rustup target add aarch64-unknown-linux-musl && \
rm -rf venv && \
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/build-crate-and-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ jobs:
with:
fetch-depth: 0

- name: Install GMP
run: |
sudo apt-get install lzip -y
curl -L https://gmplib.org/download/gmp/gmp-6.2.1.tar.lz | lzip -dc | tar x && \
cd gmp-6.2.1 && ./configure --enable-fat --with-pic && \
make -j 6 && sudo make install && cd .. && rm -rf gmp-6.2.1

- name: Set up rusts
uses: actions-rs/toolchain@v1
with:
Expand All @@ -39,6 +46,8 @@ jobs:
run: cargo +stable fmt -- --files-with-diff --check
- name: clippy (stable)
run: cargo +stable clippy
- name: install GMP
run: sudo apt install libgmp3-dev
- name: tests
run: cargo test && cargo test --release
- name: build
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/build-m1-wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ jobs:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rust.sh
arch -arm64 sh rust.sh -y

- name: Install GMP
run: |
curl -L https://gmplib.org/download/gmp/gmp-6.2.1.tar.lz | tar x
cd gmp-6.2.1
./configure --enable-fat --with-pic
make -j 6
sudo make install
cd ..
rm -rf gmp-6.2.1

- name: Build m1 wheels
run: |
arch -arm64 python3 -m venv venv
. ./venv/bin/activate
export PATH=~/.cargo/bin:$PATH
arch -arm64 pip install maturin
arch -arm64 maturin build --no-sdist -i python --release --strip --cargo-extra-args="--features=openssl"
arch -arm64 maturin build --no-sdist --release --strip --cargo-extra-args="--features=openssl"

- name: Install clvm_rs wheel
run: |
Expand Down
21 changes: 20 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
- name: Build MacOs with maturin on Python ${{ matrix.python }}
if: startsWith(matrix.os, 'macos')
run: |
brew install gmp
python -m venv venv
ln -s venv/bin/activate
. ./activate
Expand All @@ -69,7 +70,10 @@ jobs:
bash -exc '\
curl -L https://sh.rustup.rs > rustup-init.sh && \
sh rustup-init.sh -y && \
yum -y install openssl-devel && \
yum -y install sudo openssl-devel && \
curl -L https://gmplib.org/download/gmp/gmp-6.2.1.tar.bz2 | tar -xj && \
cd gmp-6.2.1 && ./configure --enable-fat --with-pic && \
make -j 6 && sudo make install && cd .. && rm -rf gmp-6.2.1 && \
source $HOME/.cargo/env && \
rustup target add x86_64-unknown-linux-musl && \
rm -rf venv && \
Expand All @@ -95,6 +99,7 @@ jobs:
python -m venv venv
. .\venv\Scripts\Activate.ps1
ln -s venv\Scripts\Activate.ps1 activate
git clone https://github.com/Chia-Network/mpir_gc_x64.git --depth 1
maturin build --no-sdist -i python --release --strip
# this will install into the venv
# it'd be better to use the wheel, but I can't figure out how to do that
Expand Down Expand Up @@ -227,6 +232,8 @@ jobs:
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
- name: install GMP
run: sudo apt install libgmp3-dev
- name: cargo-fuzz
run: cargo +nightly install cargo-fuzz
- name: build
Expand All @@ -239,10 +246,22 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 1
- name: Install GMP
run: |
sudo apt-get install lzip -y
curl -L https://gmplib.org/download/gmp/gmp-6.2.1.tar.lz | lzip -dc | tar x
cd gmp-6.2.1
./configure --enable-fat --with-pic
make -j 6
sudo make install
cd ..
rm -rf gmp-6.2.1
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: rustfmt, clippy
- name: install GMP
run: sudo apt install libgmp3-dev
- name: cargo test
run: cargo test
33 changes: 0 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ default = ["extension-module"]
[dependencies]
hex = "=0.4.3"
lazy_static = "=1.4.0"
num-bigint = "=0.4.0"
num-traits = "=0.2.14"
num-integer = "=0.1.44"
bls12_381 = "=0.5.0"
sha2 = "=0.9.5"
openssl = { version = "0.10.35", features = ["vendored"], optional = true }
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Use `maturin` to build the python interface. First, install into current virtual
$ pip install maturin
```

As we need `MPIR` for MSVC builds, prepare this dependency with

```
$ git clone https://github.com/Chia-Network/mpir_gc_x64.git --depth 1
```

Build `clvm_rs` directly into the current virtualenv with

```
Expand Down
10 changes: 10 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
if cfg!(windows) {
println!("cargo:rustc-link-lib=mpir");
println!("cargo:rustc-link-search=mpir_gc_x64");
} else {
println!("cargo:rustc-link-lib=gmp");
println!("cargo:rustc-link-search=/usr/local/lib");
println!("cargo:rustc-link-search=/opt/homebrew/lib");
}
}
4 changes: 1 addition & 3 deletions src/gen/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,6 @@ use crate::serialize::node_to_bytes;
#[cfg(test)]
use hex::FromHex;
#[cfg(test)]
use num_traits::Num;
#[cfg(test)]
use std::collections::HashMap;

#[cfg(test)]
Expand Down Expand Up @@ -689,7 +687,7 @@ fn parse_list_impl(
(a.new_atom(&buf).unwrap(), v.len() + 1)
} else if input.starts_with("-") || "0123456789".contains(input.get(0..1).unwrap()) {
let v = input.split_once(" ").unwrap().0;
let num = Number::from_str_radix(v, 10).unwrap();
let num = Number::from_str_radix(v, 10);
(ptr_from_number(a, &num).unwrap(), v.len() + 1)
} else {
panic!("atom not supported \"{}\"", input);
Expand Down
118 changes: 118 additions & 0 deletions src/gmp_ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#![allow(non_camel_case_types, non_snake_case)]

use core::ptr::NonNull;
use std::ffi::c_void;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct mpz_t {
pub alloc: c_int,
pub size: c_int,
pub d: NonNull<c_ulonglong>,
}

type c_int = i32;
type c_long = i64;
type c_ulong = u64;
type c_ulonglong = u64;
type mpz_srcptr = *const mpz_t;
type mpz_ptr = *mut mpz_t;
type bitcnt_t = c_ulong;

extern "C" {
#[link_name = "__gmpz_init"]
pub fn mpz_init(x: mpz_ptr);
#[link_name = "__gmpz_import"]
pub fn mpz_import(
rop: mpz_ptr,
count: usize,
order: c_int,
size: usize,
endian: c_int,
nails: usize,
op: *const c_void,
);
#[link_name = "__gmpz_add_ui"]
pub fn mpz_add_ui(rop: mpz_ptr, op1: mpz_srcptr, op2: c_ulong);
#[link_name = "__gmpz_set"]
pub fn mpz_set(rop: mpz_ptr, op: mpz_srcptr);
#[link_name = "__gmpz_export"]
pub fn mpz_export(
rop: *mut c_void,
countp: *mut usize,
order: c_int,
size: usize,
endian: c_int,
nails: usize,
op: mpz_srcptr,
) -> *mut c_void;
#[link_name = "__gmpz_sizeinbase"]
pub fn mpz_sizeinbase(arg1: mpz_srcptr, arg2: c_int) -> usize;
#[link_name = "__gmpz_fdiv_qr"]
pub fn mpz_fdiv_qr(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
#[link_name = "__gmpz_fdiv_q"]
pub fn mpz_fdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
#[link_name = "__gmpz_fdiv_r"]
pub fn mpz_fdiv_r(r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
#[link_name = "__gmpz_fdiv_q_2exp"]
pub fn mpz_fdiv_q_2exp(q: mpz_ptr, n: mpz_srcptr, b: bitcnt_t);
#[link_name = "__gmpz_init_set_ui"]
pub fn mpz_init_set_ui(rop: mpz_ptr, op: c_ulong);
#[link_name = "__gmpz_init_set_si"]
pub fn mpz_init_set_si(rop: mpz_ptr, op: c_long);
#[link_name = "__gmpz_clear"]
pub fn mpz_clear(x: mpz_ptr);
#[link_name = "__gmpz_add"]
pub fn mpz_add(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
#[link_name = "__gmpz_sub"]
pub fn mpz_sub(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
#[link_name = "__gmpz_mul"]
pub fn mpz_mul(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
#[link_name = "__gmpz_mul_2exp"]
pub fn mpz_mul_2exp(rop: mpz_ptr, op1: mpz_srcptr, op2: bitcnt_t);
#[link_name = "__gmpz_get_si"]
pub fn mpz_get_si(op: mpz_srcptr) -> c_long;
#[link_name = "__gmpz_and"]
pub fn mpz_and(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
#[link_name = "__gmpz_ior"]
pub fn mpz_ior(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
#[link_name = "__gmpz_xor"]
pub fn mpz_xor(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
#[link_name = "__gmpz_com"]
pub fn mpz_com(rop: mpz_ptr, op: mpz_srcptr);
#[link_name = "__gmpz_cmp"]
pub fn mpz_cmp(op1: mpz_srcptr, op2: mpz_srcptr) -> c_int;
#[link_name = "__gmpz_cmp_si"]
pub fn mpz_cmp_si(op1: mpz_srcptr, op2: c_long) -> c_int;
#[link_name = "__gmpz_cmp_ui"]
pub fn mpz_cmp_ui(op1: mpz_srcptr, op2: c_ulong) -> c_int;
}

#[cfg(test)]
type c_char = i8;

#[cfg(test)]
extern "C" {
#[link_name = "__gmpz_init_set_str"]
pub fn mpz_init_set_str(rop: mpz_ptr, str: *const c_char, base: c_int) -> c_int;
#[link_name = "__gmpz_get_str"]
pub fn mpz_get_str(str: *mut c_char, base: c_int, op: mpz_srcptr) -> *mut c_char;
}

#[inline]
pub unsafe extern "C" fn mpz_neg(rop: mpz_ptr, op: mpz_srcptr) {
if rop as mpz_srcptr != op {
mpz_set(rop, op);
}
(*rop).size = -(*rop).size;
}

#[inline]
pub unsafe extern "C" fn mpz_get_ui(op: mpz_srcptr) -> c_ulong {
if { (*op).size } != 0 {
let p = (*op).d.as_ptr();
(*p) as c_ulong
} else {
0
}
}
Loading