Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush committed Jul 24, 2023
1 parent a8b872e commit 47cd419
Show file tree
Hide file tree
Showing 40 changed files with 2,875 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ jobs:
echo "[patch.crates-io]";
echo "ark-ff = { path = 'algebra/ff' }";
echo "ark-serialize = { path = 'algebra/serialize' }";
echo "ark-ff-macros = { path = 'algebra/ff-macros' }";
echo "ark-algebra-macros = { path = 'algebra/common-macros' }";
echo "ark-ff-asm = { path = 'algebra/ff-asm' }";
echo "ark-ec = { path = 'algebra/ec' }";
echo "ark-algebra-bench-templates = { path = 'algebra/bench-templates' }"
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ members = [
"serialize",
"serialize-derive",

"group",
"algebra-core",

"ff-macros",
"common-macros",
"ff-asm",
"ff",

Expand Down
9 changes: 5 additions & 4 deletions group/Cargo.toml → algebra-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "ark-group"
name = "ark-algebra-core"
version = "0.4.2"
authors = [ "arkworks contributors" ]
description = "A library for finite groups"
description = "A library for core algebraic structures used in arkworks"
homepage = "https://arkworks.rs"
repository = "https://github.com/arkworks-rs/algebra"
documentation = "https://docs.rs/ark-group/"
documentation = "https://docs.rs/ark-algebra-core/"
keywords = ["cryptography", "groups" ]
categories = ["cryptography"]
include = ["Cargo.toml", "build.rs", "src", "doc", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
Expand All @@ -16,12 +16,13 @@ rust-version = "1.63"
[dependencies]
ark-std = { version = "0.4.0", default-features = false }
ark-serialize = { version = "0.4.2", path = "../serialize", default-features = false }
ark-algebra-macros = { version = "0.4.2", path = "../common-macros", default-features = false }
derivative = { version = "2", features = ["use_core"] }
num-traits = { version = "0.2", default-features = false }
rayon = { version = "1", optional = true }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"] }
num-bigint = { version = "0.4", default-features = false }
itertools = { version = "0.10", default-features = false }
itertools = { version = "0.11", default-features = false }

[dev-dependencies]
ark-test-curves = { version = "0.4.2", path = "../test-curves", default-features = false, features = [ "bls12_381_curve", "mnt6_753", "secp256k1"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub fn mac_discard(a: u64, b: u64, c: u64, carry: &mut u64) {
*carry = (tmp >> 64) as u64;
}

#[macro_export]
macro_rules! mac_with_carry {
($a:expr, $b:expr, $c:expr, &mut $carry:expr$(,)?) => {{
let tmp = ($a as u128) + ($b as u128 * $c as u128) + ($carry as u128);
Expand All @@ -109,6 +110,7 @@ macro_rules! mac_with_carry {
}};
}

#[macro_export]
macro_rules! mac {
($a:expr, $b:expr, $c:expr, &mut $carry:expr$(,)?) => {{
let tmp = ($a as u128) + ($b as u128 * $c as u128);
Expand Down
49 changes: 43 additions & 6 deletions ff/src/biginteger/mod.rs → algebra-core/src/biginteger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
bits::{BitIteratorBE, BitIteratorLE},
const_for, UniformRand,
const_for,
module::{Scalar, Sign},
};
#[allow(unused)]
use ark_ff_macros::unroll_for_loops;
use ark_algebra_macros::unroll_for_loops;
use ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
};
Expand All @@ -16,6 +17,7 @@ use ark_std::{
Rng,
},
vec::Vec,
UniformRand,
};
use num_bigint::BigUint;
use zeroize::Zeroize;
Expand All @@ -26,6 +28,34 @@ pub mod arithmetic;
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, Zeroize)]
pub struct BigInt<const N: usize>(pub [u64; N]);

impl<const N: usize> Scalar for BigInt<N> {
const MAX_BIT_SIZE: Option<u32> = Some(N as u32 * 64);
type U64Ref = [u64; N];
type U8Ref = ScalarU8Buffer<N>;

fn as_bytes(&self) -> (Sign, ScalarU8Buffer<N>) {
let mut buf = ScalarU8Buffer::<N>([[0u8; 8]; N]);
for (i, limb) in self.0.iter().enumerate() {
buf.0[i] = limb.to_le_bytes();
}
(Sign::Positive, buf)
}

fn as_u64s(&self) -> (Sign, Self::U64Ref) {
(Sign::Positive, self.0)
}
}

#[doc(hidden)]
#[repr(C, align(1))]
pub struct ScalarU8Buffer<const N: usize>(pub [[u8; 8]; N]);

impl<const N: usize> AsRef<[u8]> for ScalarU8Buffer<N> {
fn as_ref(&self) -> &[u8] {
unsafe { ark_std::slice::from_raw_parts(self as *const Self as *const u8, N * 8) }
}
}

impl<const N: usize> Default for BigInt<N> {
fn default() -> Self {
Self([0u64; N])
Expand Down Expand Up @@ -229,7 +259,8 @@ impl<const N: usize> BigInt<N> {
}

#[inline]
pub(crate) const fn const_sub_with_borrow(mut self, other: &Self) -> (Self, bool) {
#[doc(hidden)]
pub const fn const_sub_with_borrow(mut self, other: &Self) -> (Self, bool) {
let mut borrow = 0;

const_for!((i in 0..N) {
Expand All @@ -240,7 +271,8 @@ impl<const N: usize> BigInt<N> {
}

#[inline]
pub(crate) const fn const_add_with_carry(mut self, other: &Self) -> (Self, bool) {
#[doc(hidden)]
pub const fn const_add_with_carry(mut self, other: &Self) -> (Self, bool) {
let mut carry = 0;

crate::const_for!((i in 0..N) {
Expand All @@ -250,7 +282,9 @@ impl<const N: usize> BigInt<N> {
(self, carry != 0)
}

const fn const_mul2_with_carry(mut self) -> (Self, bool) {
#[inline]
#[doc(hidden)]
pub const fn const_mul2_with_carry(mut self) -> (Self, bool) {
let mut last = 0;
crate::const_for!((i in 0..N) {
let a = self.0[i];
Expand All @@ -262,7 +296,9 @@ impl<const N: usize> BigInt<N> {
(self, last != 0)
}

pub(crate) const fn const_is_zero(&self) -> bool {
#[inline]
#[doc(hidden)]
pub const fn const_is_zero(&self) -> bool {
let mut is_zero = true;
crate::const_for!((i in 0..N) {
is_zero &= self.0[i] == 0;
Expand Down Expand Up @@ -737,6 +773,7 @@ pub trait BigInteger:
+ From<u8>
+ TryFrom<BigUint, Error = ()>
+ Into<BigUint>
+ Scalar
{
/// Number of 64-bit limbs representing `Self`.
const NUM_LIMBS: usize;
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 47cd419

Please sign in to comment.