Skip to content
Open
34 changes: 19 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "amcl_wrapper"
version = "0.3.5"
authors = ["lovesh harchandani <lovesh.bond@gmail.com>"]
name = "amcl_wrapper_ml"
version = "0.5.0"
authors = ["lovesh harchandani <lovesh.bond@gmail.com>", "Michael Lodder <redmike7@gmail.com>"]
description = "Wapper over Milagro Cryptographic Library (version 3)"
repository = "https://github.com/lovesh/amcl_rust_wrapper"
repository = "https://github.com/mikelodder7/amcl_rust_wrapper"

license = "Apache-2.0"
edition = "2018"
Expand All @@ -20,20 +20,24 @@ secp256k1 = []
ed25519 = []

[dependencies]
arrayref = "0.3"
byteorder = "1.3"
lazy_static = "1.3"
rand = "0.7"
lazy_static = "1.3.0"
byteorder = "1.3.1"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
zeroize = "1.1.0"
#tiny-keccak = "1.5"
sha3 = "0.8.2"
rayon = "1.3"
subtle-encoding = "0.5.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.8"
sha3 = "0.8"
subtle-encoding = "0.5"
zeroize = "1.1"

[dependencies.hash2curve]
version = "0.0.6"
features = ["bls"]

[dependencies.amcl]
package = "miracl_amcl"
version = "3.2.5"
package = "amcl-milagro"
version = "3.2.6"
features = ["bls381", "bn254", "secp256k1", "ed25519"]

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ let b = G1::from_msg_hash(msg.as_bytes());
4. Create vectors of field elements and do some operations
```rust
// creates a vector of size 10 with all elements as 0
let mut a = FieldElementVector::new(10);
let mut a = CurveOrderElementVector::new(10);
// Add 2 more elements to the above vector
a.push(FieldElement::random());
a.push(FieldElement::random());
Expand All @@ -154,15 +154,15 @@ a.sum(); // sum of elements of vector
```rust
// Return a Vandermonde vector of a given field element, i.e. given element `k` and size `n`, return vector as `vec![1, k, k^2, k^3, ... k^n-1]`
let k = FieldElement::random();
let van_vec = FieldElementVector::new_vandermonde_vector(&k, 5);
let van_vec = CurveOrderElementVector::new_vandermonde_vector(&k, 5);
```

```rust
// creates a vector of size 10 with randomly generated field elements
let rands: Vec<_> = (0..10).map(|_| FieldElement::random()).collect();

// an alternative way of creating vector of size 10 of random field elements
let rands_1 = FieldElementVector::random(10);
let rands_1 = CurveOrderElementVector::random(10);
```

```rust
Expand Down Expand Up @@ -209,7 +209,7 @@ let diff_vec = rands.minus(&rands_1);
// eg. given a vector of group elements and field elements, G and F respectively, compute G[0]*F[0] + G[1]*F[1] + G[2]*F[2] + .. G[n-1]*F[n-1]
// requires vectors to be of same length
let g = G1Vector::random(10);
let f = FieldElementVector::random(10);
let f = CurveOrderElementVector::random(10);

// Uses constant time multi-scalar multiplication `multi_scalar_mul_const_time` underneath.
let ip = g.inner_product_const_time(&f);
Expand Down Expand Up @@ -267,7 +267,7 @@ assert!(poly.is_zero());

// Create a polynomial from field elements as coefficients, the following polynomial will be c_0 + c_1*x + c_2*x^2 + c_3*x^3 + ... + c_d*x^d
let coeffs: Vec<FieldElement> = vec![c_0, c_1, ... coefficients for smaller to higher degrees ..., c_d];
let poly1 = UnivarPolynomial(FieldElementVector::from(coeffs));
let poly1 = UnivarPolynomial(CurveOrderElementVector::from(coeffs));

// Create a polynomial of degree `d` with random coefficients
let poly2 = UnivarPolynomial::random(d);
Expand Down
12 changes: 6 additions & 6 deletions src/commitment.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::errors::ValueError;
use crate::field_elem::{FieldElement, FieldElementVector};
use crate::curve_order_elem::{CurveOrderElement, CurveOrderElementVector};
use crate::group_elem::GroupElementVector;
use crate::group_elem_g1::{G1Vector, G1};

/// Commit to field element `elem` with randomness `r` given groups elements `g` and `h`, i.e. compute g^elem.h^r
pub fn commit_to_field_element(g: &G1, h: &G1, elem: &FieldElement, r: &FieldElement) -> G1 {
pub fn commit_to_field_element(g: &G1, h: &G1, elem: &CurveOrderElement, r: &CurveOrderElement) -> G1 {
g.binary_scalar_mul(h, elem, r)
}

Expand All @@ -15,9 +15,9 @@ pub fn commit_to_field_element_vectors(
g: &G1Vector,
h: &G1Vector,
u: &G1,
a: &FieldElementVector,
b: &FieldElementVector,
c: &FieldElement,
a: &CurveOrderElementVector,
b: &CurveOrderElementVector,
c: &CurveOrderElement,
) -> Result<G1, ValueError> {
/*let a_g = g.inner_product_const_time(a)?;
let b_h = h.inner_product_const_time(b)?;
Expand All @@ -28,7 +28,7 @@ pub fn commit_to_field_element_vectors(
combined_g.extend_from_slice(h.as_slice());
combined_g.push(u.clone());

let mut combined_f: Vec<FieldElement> = vec![];
let mut combined_f: Vec<CurveOrderElement> = vec![];
combined_f.extend_from_slice(a.as_slice());
combined_f.extend_from_slice(b.as_slice());
combined_f.push(c.clone());
Expand Down
39 changes: 24 additions & 15 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,48 @@ use super::ECCurve::rom;

pub const MODBYTES: usize = curve_MODBYTES;
pub const NLEN: usize = curve_NLEN;
pub const BigNumBits: usize = BASEBITS;
pub const BIG_NUM_BITS: usize = BASEBITS;

pub const FIELD_ORDER_ELEMENT_SIZE: usize = MODBYTES;
#[cfg(feature = "bls381")]
pub const CURVE_ORDER_ELEMENT_SIZE: usize = 32;
#[cfg(feature = "bn254")]
pub const CURVE_ORDER_ELEMENT_SIZE: usize = 32;
#[cfg(feature = "secp256k1")]
pub const CURVE_ORDER_ELEMENT_SIZE: usize = 32;
#[cfg(feature = "ed25519")]
pub const CURVE_ORDER_ELEMENT_SIZE: usize = 32;

// Byte size of element in group G1, 1 extra byte for compression flag
pub const FieldElement_SIZE: usize = MODBYTES;
pub const GROUP_G1_SIZE: usize = (2 * MODBYTES + 1) as usize;

// Byte size of element in group G1, 1 extra byte for compression flag
pub const GroupG1_SIZE: usize = (2 * MODBYTES + 1) as usize;
pub const MODULUS: BigNum = BigNum { w: rom::MODULUS };
pub const CURVE_ORDER: BigNum = BigNum { w: rom::CURVE_ORDER };
pub const FIELD_ELEMENT_ZERO: BigNum = BigNum { w: [0; NLEN] };

lazy_static! {
pub static ref GeneratorG1: GroupG1 = GroupG1::generator();
pub static ref CurveOrder: BigNum = BigNum::new_ints(&rom::CURVE_ORDER);
pub static ref CurveOrderBitSize: usize = CurveOrder.nbits();
pub static ref FieldElementZero: BigNum = BigNum::new();
pub static ref BarrettRedc_k: usize = CurveOrder.nbits();
pub static ref BarrettRedc_u: BigNum = {
let k = CurveOrder.nbits();
pub static ref GENERATOR_G1: GroupG1 = GroupG1::generator();
pub static ref BARRETT_REDC_K: usize = MODULUS.nbits();
pub static ref BARRETT_REDC_U: BigNum = {
let k = CURVE_ORDER.nbits();
let mut u = DoubleBigNum::new();
u.w[0] = 1;
// `u.shl(2*k)` crashes, so perform shl(k) twice
u.shl(k);
u.shl(k);

// div returns floored value
u.div(&CurveOrder)
u.div(&CURVE_ORDER)
};

pub static ref BarrettRedc_v: BigNum = {
let k = CurveOrder.nbits();
pub static ref BARRETT_REDC_V: BigNum = {
let k = CURVE_ORDER.nbits();
let mut v = BigNum::new_int(1isize);
v.shl(k+1);
v
};
}

#[cfg(any(feature = "bls381", feature = "bn254"))]
pub use crate::types_g2::{GeneratorG2, GroupG2_SIZE, GroupGT_SIZE};
pub use crate::types_g2::{GENERATOR_G2, GROUP_G2_SIZE, GROUP_GT_SIZE};

Loading