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

Improve algebra structure #665

Open
wants to merge 5 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
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: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ members = [
"serialize",
"serialize-derive",

"ff-macros",
"algebra-core",

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

Expand Down
38 changes: 38 additions & 0 deletions algebra-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "ark-algebra-core"
version = "0.4.2"
authors = [ "arkworks contributors" ]
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-algebra-core/"
keywords = ["cryptography", "groups" ]
categories = ["cryptography"]
include = ["Cargo.toml", "build.rs", "src", "doc", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
edition = "2021"
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.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"] }
sha2 = { version = "0.10", default-features = false }
hex = "0.4"

[features]
default = []
std = [ "ark-std/std", "ark-serialize/std", "itertools/use_std" ]
parallel = [ "std", "rayon", "ark-std/parallel" ]

[package.metadata.docs.rs]
rustdoc-args = ["--html-in-header", "./doc/katex-header.html"]
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
Loading