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

Prepare to publishing on crates.io #51

Open
wants to merge 7 commits into
base: master
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
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: rust
cache: cargo
rust: stable
virt: lxd

before_install:
- rustup component add rustfmt clippy

script:
- cargo build --verbose
- cargo test --verbose -- --test-threads=1
- cargo fmt --all -- --check
- cargo clippy -- -D clippy::all

deploy:
provider: cargo
token:
secure: "Qdd5mNWfpwnuFPt46TJcYkhYdA+bO4WX1+noiwRhRVim8Q04RU+DMVTxe5bysImV/u3n1u3M66gKUuVtaifn/MqCQOj72MHJGxU3a0muNXXFVaN9EzJehGxWj6Ps81jV8XXGbap8+0DCXxXLBC/f92Rnoyna3/+/GK5eUGInVkidkT2GLyxNiQMGFy2HeZywES9/oRHQTmoUX7sW1JizJbx2O0Ibf4B50lEdsHAsfb1zCP8eR1qe6oJUSfARFaFq85y0Vp88CDnBTJcNR+ACCZweN0NsWiyKcGe2+IRKnX/rFGdiemoVg20mw+D4Iyu61Pi4ZBGFenEL3NEbrFff9jsulEX1CDdHRrvk1xBlLJqgJ8biH2vVL9ALxl6doGOYN3tfa94xgS7Y3akr+BVFmZ0ofPOq3rNhINgyKnF9qUglx+LCcLDiG+UjtBUn+TCsTKdqhyu067oozs1sv9cCpFuzARJSTE1Noy3YqZe7T4rJWCyiq1K451IYVj4gcb4DNn8x/ypoSK+bzRG6fiuO2uBJqrVKJVJx5/+gXFNFT1OLEdGGOyboNlrL/SEoqVA6lL0H4qmVgfwf5qbNJsojj0UW5ipxZyvg6CldNNYhuVfCgij17JbqaQY4knbkkpqSB3p6E6zkMzw3Qcn6TqpV4S1dzmZclUmwZxK95eTC/dU="
on:
tags: true
condition: "$TRAVIS_TAG =~ ^v[0-9.]+$"
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
[package]
name = "class_group"
version = "0.5.2"
authors = ["omershlo <[email protected]>"]
version = "0.5.3"
edition = "2018"
description = "Rust library for building IQC: cryptography based on class groups (Cl) of imaginary quadratic orders"
license = "GPL-3.0-or-later"
repository = "https://github.com/ZenGo-X/class"
categories = ["cryptography", "algorithms", "api-bindings"]
keywords = ["IQC", "class-group", "imaginary", "cryptoagorithms"]

authors = ["omershlo <[email protected]>"]
links = "libpari"
build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
bindgen = "0.50"

[dependencies]
libc = "0.2.0"
serde = "1.0"
serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
ring-algorithm = "0.2.3"
num-traits = "0.2"
Expand Down
77 changes: 28 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::many_single_char_names)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

extern crate libc;
#[macro_use]
extern crate serde_derive;
extern crate curv;
extern crate serde;
extern crate serde_json;
use curv::arithmetic::traits::*;
use curv::BigInt;
use libc::c_char;

use std::ffi::CStr;
use std::mem::swap;
use std::ops::Neg;
use std::{str, ptr};
use std::{ptr, str};

use serde::{Deserialize, Serialize};

use curv::arithmetic::traits::*;
use curv::BigInt;

pub mod primitives;

Expand Down Expand Up @@ -82,32 +81,22 @@ impl BinaryQF {
}

pub fn is_reduced(&self) -> bool {
if self.is_normal() && self.a <= self.c && !(self.a == self.c && self.b < BigInt::zero()) {
return true;
} else {
return false;
}
self.is_normal() && self.a <= self.c && !(self.a == self.c && self.b < BigInt::zero())
}

pub fn normalize(&self) -> Self {
// assume delta<0 and a>0
let a_sub_b: BigInt = &self.a - &self.b;
let s_f = a_sub_b.div_floor(&(BigInt::from(2) * &self.a));
let binary_qf = BinaryQF {
BinaryQF {
a: self.a.clone(),
b: &self.b + BigInt::from(2) * &s_f * &self.a,
c: &self.a * &s_f.pow(2) + &self.b * &s_f + &self.c,
};

binary_qf
}
}

pub fn is_normal(&self) -> bool {
if self.b <= self.a && self.b > -self.a.clone() {
return true;
} else {
return false;
}
self.b <= self.a && self.b > -self.a.clone()
}
pub fn primeform(quad_disc: &BigInt, q: &BigInt) -> Self {
let quad_disc_gen = bn_to_gen(&quad_disc);
Expand All @@ -118,8 +107,7 @@ impl BinaryQF {

let bqf = BinaryQF::pari_qf_to_qf(pf);

let bqf_norm = bqf.normalize();
bqf_norm
bqf.normalize()
}

pub fn compose(&self, qf2: &BinaryQF) -> Self {
Expand All @@ -130,9 +118,7 @@ impl BinaryQF {

let qf_pari_c = unsafe { qfbcompraw(qf_pari_a, qf_pari_b) };

let qf_c = BinaryQF::pari_qf_to_qf(qf_pari_c);

qf_c
BinaryQF::pari_qf_to_qf(qf_pari_c)
}

pub fn inverse(&self) -> Self {
Expand All @@ -144,14 +130,12 @@ impl BinaryQF {
}

pub fn rho(&self) -> Self {
let qf_new = BinaryQF {
BinaryQF {
a: self.c.clone(),
b: self.b.clone().neg(),
c: self.a.clone(),
};
let h = qf_new.normalize();

h
}
.normalize()
}

pub fn reduce(&self) -> Self {
Expand All @@ -173,8 +157,7 @@ impl BinaryQF {
let pari_n = bn_to_gen(n);

let pari_qf_exp = unsafe { nupow(pari_qf, pari_n, ptr::null_mut()) };
let qf_exp = BinaryQF::pari_qf_to_qf(pari_qf_exp);
qf_exp
BinaryQF::pari_qf_to_qf(pari_qf_exp)
}
// gotoNonMax: outputs: f=phi_q^(-1)(F), a binary quadratic form of disc. delta*conductor^2
// f is non normalized
Expand All @@ -190,8 +173,7 @@ impl BinaryQF {
b: b_new,
delta,
};
let qf = BinaryQF::binary_quadratic_form_disc(&abdelta);
qf
BinaryQF::binary_quadratic_form_disc(&abdelta)
}

// compute (p^(2),p,-)^k in class group of disc. delta
Expand All @@ -201,26 +183,24 @@ impl BinaryQF {
}
let mut k_inv = BigInt::mod_inv(k, p).unwrap();
if k_inv.mod_floor(&BigInt::from(2)) == BigInt::zero() {
k_inv = k_inv - p;
k_inv -= p;
};
let k_inv_p = k_inv * p;
let abdelta = ABDeltaTriple {
a: p * p,
b: k_inv_p,
delta: delta.clone(),
};
let qf = BinaryQF::binary_quadratic_form_disc(&abdelta);
qf
BinaryQF::binary_quadratic_form_disc(&abdelta)
}

pub fn discrete_log_f(p: &BigInt, delta: &BigInt, c: &BinaryQF) -> BigInt {
let principal_qf = BinaryQF::binary_quadratic_form_principal(delta);
if c == &principal_qf {
return BigInt::zero();
BigInt::zero()
} else {
let Lk = c.b.div_floor(p);
let Lk_inv = BigInt::mod_inv(&Lk, p).unwrap();
return Lk_inv;
BigInt::mod_inv(&Lk, p).unwrap()
}
}

Expand All @@ -229,9 +209,8 @@ impl BinaryQF {
let a = bn_to_gen(&self.a);
let b = bn_to_gen(&self.b);
let c = bn_to_gen(&self.c);
let qf_pari = unsafe { qfi(a, b, c) };
// GEN qfi(GEN a, GEN b, GEN c) (assumes b^2 − 4ac < 0)
qf_pari
unsafe { qfi(a, b, c) }
}

// construct BinaryQF from pari GEN encoded qfb
Expand Down Expand Up @@ -425,14 +404,14 @@ pub fn bn_to_gen(bn: &BigInt) -> GEN {
let elem1 = mkintn(1i64, ints_vec[num_int_bound - i - 1]);
let elem2 = shifti(gen, (size_int) as i64);
gen = gadd(elem1, elem2);
i = i + 1
i += 1
}

if neg1 == -1 {
gen = gneg(gen);
}

return gen;
gen
}
}

Expand All @@ -453,7 +432,7 @@ mod tests {
use proptest::prelude::*;

use super::*;
use crate::curv::arithmetic::traits::Samplable;
use curv::arithmetic::traits::Samplable;

#[test]
fn test_qf_to_pari_qf_to_qf() {
Expand Down
Loading