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

[feat] add MulAddAssign trait #9

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ ark-std = { version = "0.3" }

[dependencies]
subtle = "2.4"
ff = "0.13.0"
ff = { git = "https://github.com/scroll-tech/ff", branch = "main" }
group = "0.13.0"
pasta_curves = "0.5.0"
pasta_curves = { git = "https://github.com/scroll-tech/pasta_curves", branch = "main" }
static_assertions = "1.1.0"
rand = "0.8"
rand_core = { version = "0.6", default-features = false }
Expand All @@ -35,6 +35,9 @@ num-bigint = "0.4.3"
num-traits = "0.2"
paste = "1.0.11"

[patch.crates-io]
ff = { git = "https://github.com/scroll-tech/ff", branch = "main" }

[features]
default = []
asm = []
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2022-08-23
nightly-2023-12-03
4 changes: 3 additions & 1 deletion src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@
use crate::{
field_common, impl_add_binop_specify_output, impl_binops_additive,
impl_binops_additive_specify_output, impl_binops_multiplicative,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, impl_sum_prod,
impl_binops_multiplicative_additive, impl_binops_multiplicative_mixed,
impl_sub_binop_specify_output, impl_sum_prod,
};
impl_binops_additive!(Fq, Fq);
impl_binops_multiplicative!(Fq, Fq);
impl_binops_multiplicative_additive!(Fq, Fq);
field_common!(
Fq,
MODULUS,
Expand Down Expand Up @@ -169,7 +171,7 @@

/// Computes the square root of this element, if it exists.
fn sqrt(&self) -> CtOption<Self> {
let tmp = self.pow(&[

Check warning on line 174 in src/bn256/fq.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
0x4f082305b61f3f52,
0x65e05aa45a1c72a3,
0x6e14116da0605617,
Expand All @@ -186,7 +188,7 @@
/// Computes the multiplicative inverse of this element,
/// failing if the element is zero.
fn invert(&self) -> CtOption<Self> {
let tmp = self.pow(&[

Check warning on line 191 in src/bn256/fq.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
0x3c208c16d87cfd45,
0x97816a916871ca8d,
0xb85045b68181585d,
Expand Down
5 changes: 3 additions & 2 deletions src/bn256/fq12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@

use crate::{
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
impl_sum_prod,
impl_binops_multiplicative, impl_binops_multiplicative_additive,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, impl_sum_prod,
};
impl_binops_additive!(Fq12, Fq12);
impl_binops_multiplicative!(Fq12, Fq12);
impl_binops_multiplicative_additive!(Fq12, Fq12);
impl_sum_prod!(Fq12);

impl Fq12 {
Expand Down Expand Up @@ -582,7 +583,7 @@
let mut b = a;

for _ in 0..i {
a = a.pow_vartime(&[

Check warning on line 586 in src/bn256/fq12.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
0x3c208c16d87cfd47,
0x97816a916871ca8d,
0xb85045b68181585d,
Expand Down
5 changes: 3 additions & 2 deletions src/bn256/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@

use crate::{
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
impl_sum_prod,
impl_binops_multiplicative, impl_binops_multiplicative_additive,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, impl_sum_prod,
};
impl_binops_additive!(Fq2, Fq2);
impl_binops_multiplicative!(Fq2, Fq2);
impl_binops_multiplicative_additive!(Fq2, Fq2);
impl_sum_prod!(Fq2);

impl Fq2 {
Expand Down Expand Up @@ -360,7 +361,7 @@
0x6e14116da0605617,
0x0c19139cb84c680a,
];
let mut a1 = self.pow(&u);

Check warning on line 364 in src/bn256/fq2.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
let mut alpha = a1;

alpha.square_assign();
Expand Down Expand Up @@ -394,7 +395,7 @@
0xdc2822db40c0ac2e,
0x183227397098d014,
];
alpha = alpha.pow(&u);

Check warning on line 398 in src/bn256/fq2.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
a1.mul_assign(&alpha);
}
CtOption::new(a1, Choice::from(1))
Expand Down Expand Up @@ -761,7 +762,7 @@
let mut b = a;

for _ in 0..i {
a = a.pow(&[

Check warning on line 765 in src/bn256/fq2.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
0x3c208c16d87cfd47,
0x97816a916871ca8d,
0xb85045b68181585d,
Expand Down
5 changes: 3 additions & 2 deletions src/bn256/fq6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@

use crate::{
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
impl_sum_prod,
impl_binops_multiplicative, impl_binops_multiplicative_additive,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, impl_sum_prod,
};
impl_binops_additive!(Fq6, Fq6);
impl_binops_multiplicative!(Fq6, Fq6);
impl_binops_multiplicative_additive!(Fq6, Fq6);
impl_sum_prod!(Fq6);

impl Fq6 {
Expand Down Expand Up @@ -694,7 +695,7 @@
let mut b = a;

for _ in 0..i {
a = a.pow_vartime(&[

Check warning on line 698 in src/bn256/fq6.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
0x3c208c16d87cfd47,
0x97816a916871ca8d,
0xb85045b68181585d,
Expand Down
4 changes: 3 additions & 1 deletion src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@
use crate::{
field_common, impl_add_binop_specify_output, impl_binops_additive,
impl_binops_additive_specify_output, impl_binops_multiplicative,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, impl_sum_prod,
impl_binops_multiplicative_additive, impl_binops_multiplicative_mixed,
impl_sub_binop_specify_output, impl_sum_prod,
};
impl_binops_additive!(Fr, Fr);
impl_binops_multiplicative!(Fr, Fr);
impl_binops_multiplicative_additive!(Fr, Fr);
field_common!(
Fr,
MODULUS,
Expand Down Expand Up @@ -168,7 +170,7 @@
/// Computes the multiplicative inverse of this element,
/// failing if the element is zero.
fn invert(&self) -> CtOption<Self> {
let tmp = self.pow(&[

Check warning on line 173 in src/bn256/fr.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
0x43e1f593efffffff,
0x2833e84879b97091,
0xb85045b68181585d,
Expand All @@ -186,7 +188,7 @@
0x098d014dc2822db4,
0x0000000183227397,
];
ff::helpers::sqrt_tonelli_shanks(self, &T_MINUS1_OVER2)

Check warning on line 191 in src/bn256/fr.rs

View workflow job for this annotation

GitHub Actions / Clippy lint checks

the borrowed expression implements the required traits
}

fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self) {
Expand Down
33 changes: 33 additions & 0 deletions src/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,39 @@ macro_rules! impl_binops_multiplicative {
};
}

#[macro_export]
macro_rules! impl_binops_multiplicative_additive {
($lhs:ident, $rhs:ident) => {
impl ::ff::MulAddAssign<$lhs, $rhs> for $lhs {
#[inline]
fn mul_add_assign(&mut self, a: $lhs, b: $rhs) {
*self = *self * a + b;
}
}

impl<'a> ::ff::MulAddAssign<&'a $lhs, $rhs> for $lhs {
#[inline]
fn mul_add_assign(&mut self, a: &'a $lhs, b: $rhs) {
*self = *self * a + b;
}
}

impl<'a> ::ff::MulAddAssign<$lhs, &'a $rhs> for $lhs {
#[inline]
fn mul_add_assign(&mut self, a: $lhs, b: &'a $rhs) {
*self = *self * a + b;
}
}

impl<'a, 'b> ::ff::MulAddAssign<&'a $lhs, &'b $rhs> for $lhs {
#[inline]
fn mul_add_assign(&mut self, a: &'a $lhs, b: &'b $rhs) {
*self = *self * a + b;
}
}
};
}

#[macro_export]
macro_rules! impl_sum_prod {
($f:ident) => {
Expand Down
4 changes: 3 additions & 1 deletion src/secp256k1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ const ROOT_OF_UNITY_INV: Fp = Fp([
use crate::{
field_arithmetic, field_common, field_specific, impl_add_binop_specify_output,
impl_binops_additive, impl_binops_additive_specify_output, impl_binops_multiplicative,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, impl_sum_prod,
impl_binops_multiplicative_additive, impl_binops_multiplicative_mixed,
impl_sub_binop_specify_output, impl_sum_prod,
};
impl_binops_additive!(Fp, Fp);
impl_binops_multiplicative!(Fp, Fp);
impl_binops_multiplicative_additive!(Fp, Fp);
field_common!(
Fp,
MODULUS,
Expand Down
4 changes: 3 additions & 1 deletion src/secp256k1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ const DELTA: Fq = Fq([
use crate::{
field_arithmetic, field_common, field_specific, impl_add_binop_specify_output,
impl_binops_additive, impl_binops_additive_specify_output, impl_binops_multiplicative,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, impl_sum_prod,
impl_binops_multiplicative_additive, impl_binops_multiplicative_mixed,
impl_sub_binop_specify_output, impl_sum_prod,
};
impl_binops_additive!(Fq, Fq);
impl_binops_multiplicative!(Fq, Fq);
impl_binops_multiplicative_additive!(Fq, Fq);
field_common!(
Fq,
MODULUS,
Expand Down
Loading