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

refactor: generic over curve #37

Open
wants to merge 1 commit 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
110 changes: 55 additions & 55 deletions src/proofs/inner_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ version 3 of the License, or (at your option) any later version.
// based on the paper: https://eprint.iacr.org/2017/1066.pdf
use curv::arithmetic::traits::*;
use curv::cryptographic_primitives::hashing::{Digest, DigestExt};
use curv::elliptic::curves::{secp256_k1::Secp256k1, Point, Scalar};
use curv::elliptic::curves::{Curve, Point, Scalar};
use curv::BigInt;
use sha2::Sha256;

use Errors::{self, InnerProductError};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct InnerProductArg {
pub(super) L: Vec<Point<Secp256k1>>,
pub(super) R: Vec<Point<Secp256k1>>,
pub struct InnerProductArg<C: Curve> {
pub(super) L: Vec<Point<C>>,
pub(super) R: Vec<Point<C>>,
pub(super) a_tag: BigInt,
pub(super) b_tag: BigInt,
}

impl InnerProductArg {
impl<C: Curve> InnerProductArg<C> {
pub fn prove(
G: &[Point<Secp256k1>],
H: &[Point<Secp256k1>],
ux: &Point<Secp256k1>,
P: &Point<Secp256k1>,
G: &[Point<C>],
H: &[Point<C>],
ux: &Point<C>,
P: &Point<C>,
a: &[BigInt],
b: &[BigInt],
mut L_vec: Vec<Point<Secp256k1>>,
mut R_vec: Vec<Point<Secp256k1>>,
) -> InnerProductArg {
mut L_vec: Vec<Point<C>>,
mut R_vec: Vec<Point<C>>,
) -> Self {
let n = G.len();

// All of the input vectors must have the same length.
Expand All @@ -61,28 +61,28 @@ impl InnerProductArg {
let (G_L, G_R) = G.split_at(n);
let (H_L, H_R) = H.split_at(n);

let c_L = inner_product(a_L, b_R);
let c_R = inner_product(a_R, b_L);
let c_L = inner_product::<C>(a_L, b_R);
let c_R = inner_product::<C>(a_R, b_L);

// Note that no element in vectors a_L and b_R can be 0
// since 0 is an invalid secret key!
//
// L = <a_L * G_R> + <b_R * H_L> + c_L * ux
let c_L_fe = Scalar::<Secp256k1>::from(&c_L);
let ux_CL: Point<Secp256k1> = ux * &c_L_fe;
let c_L_fe = Scalar::<C>::from(&c_L);
let ux_CL: Point<C> = ux * &c_L_fe;
let aL_GR = G_R.iter().zip(a_L).fold(ux_CL, |acc, x| {
if x.1 != &BigInt::zero() {
let aLi = Scalar::<Secp256k1>::from(x.1);
let aLi_GRi: Point<Secp256k1> = x.0 * &aLi;
let aLi = Scalar::<C>::from(x.1);
let aLi_GRi: Point<C> = x.0 * &aLi;
acc + &aLi_GRi
} else {
acc
}
});
let L = H_L.iter().zip(b_R).fold(aL_GR, |acc, x| {
if x.1 != &BigInt::zero() {
let bRi = Scalar::<Secp256k1>::from(x.1);
let bRi_HLi: Point<Secp256k1> = x.0 * &bRi;
let bRi = Scalar::<C>::from(x.1);
let bRi_HLi: Point<C> = x.0 * &bRi;
acc + &bRi_HLi
} else {
acc
Expand All @@ -93,21 +93,21 @@ impl InnerProductArg {
// since 0 is an invalid secret key!
//
// R = <a_R * G_L> + <b_L * H_R> + c_R * ux
let c_R_fe = Scalar::<Secp256k1>::from(&c_R);
let ux_CR: Point<Secp256k1> = ux * &c_R_fe;
let c_R_fe = Scalar::<C>::from(&c_R);
let ux_CR: Point<C> = ux * &c_R_fe;
let aR_GL = G_L.iter().zip(a_R).fold(ux_CR, |acc, x| {
if x.1 != &BigInt::zero() {
let aRi = Scalar::<Secp256k1>::from(x.1);
let aRi_GLi: Point<Secp256k1> = x.0 * &aRi;
let aRi = Scalar::<C>::from(x.1);
let aRi_GLi: Point<C> = x.0 * &aRi;
acc + &aRi_GLi
} else {
acc
}
});
let R = H_R.iter().zip(b_L).fold(aR_GL, |acc, x| {
if x.1 != &BigInt::zero() {
let bLi = Scalar::<Secp256k1>::from(x.1);
let bLi_HRi: Point<Secp256k1> = x.0 * &bLi;
let bLi = Scalar::<C>::from(x.1);
let bLi_HRi: Point<C> = x.0 * &bLi;
acc + &bLi_HRi
} else {
acc
Expand All @@ -116,7 +116,7 @@ impl InnerProductArg {

let x = Sha256::new().chain_points([&L, &R, ux]).result_scalar();
let x_bn = x.to_bigint();
let order = Scalar::<Secp256k1>::group_order();
let order = Scalar::<C>::group_order();
let x_inv_fe = x.invert().unwrap();

let a_new = (0..n)
Expand All @@ -143,7 +143,7 @@ impl InnerProductArg {
let GRx = &G_R[i] * &x;
GRx + GLx_inv
})
.collect::<Vec<Point<Secp256k1>>>();
.collect::<Vec<Point<C>>>();
// G = &mut G_new[..];

let H_new = (0..n)
Expand All @@ -152,7 +152,7 @@ impl InnerProductArg {
let HRx_inv = &H_R[i] * &x_inv_fe;
HLx + HRx_inv
})
.collect::<Vec<Point<Secp256k1>>>();
.collect::<Vec<Point<C>>>();
// H = &mut H_new[..];

L_vec.push(L);
Expand All @@ -170,10 +170,10 @@ impl InnerProductArg {

pub fn verify(
&self,
g_vec: &[Point<Secp256k1>],
hi_tag: &[Point<Secp256k1>],
ux: &Point<Secp256k1>,
P: &Point<Secp256k1>,
g_vec: &[Point<C>],
hi_tag: &[Point<C>],
ux: &Point<C>,
P: &Point<C>,
) -> Result<(), Errors> {
let G = g_vec;
let H = hi_tag;
Expand All @@ -193,20 +193,20 @@ impl InnerProductArg {
.chain_points([&self.L[0], &self.R[0], ux])
.result_scalar();
let x_bn = x.to_bigint();
let order = Scalar::<Secp256k1>::group_order();
let order = Scalar::<C>::group_order();
let x_inv_fe = x.invert().unwrap();
let x_sq_bn = BigInt::mod_mul(&x_bn, &x_bn, order);
let x_inv_sq_bn = BigInt::mod_mul(&x_inv_fe.to_bigint(), &x_inv_fe.to_bigint(), order);
let x_sq_fe = Scalar::<Secp256k1>::from(&x_sq_bn);
let x_inv_sq_fe = Scalar::<Secp256k1>::from(&x_inv_sq_bn);
let x_sq_fe = Scalar::<C>::from(&x_sq_bn);
let x_inv_sq_fe = Scalar::<C>::from(&x_inv_sq_bn);

let G_new = (0..n)
.map(|i| {
let GLx_inv = &G_L[i] * &x_inv_fe;
let GRx = &G_R[i] * &x;
GRx + GLx_inv
})
.collect::<Vec<Point<Secp256k1>>>();
.collect::<Vec<Point<C>>>();
// G = &mut G_new[..];

let H_new = (0..n)
Expand All @@ -215,7 +215,7 @@ impl InnerProductArg {
let HRx_inv = &H_R[i] * &x_inv_fe;
HLx + HRx_inv
})
.collect::<Vec<Point<Secp256k1>>>();
.collect::<Vec<Point<C>>>();
// H = &mut H_new[..];
let Lx_sq = &self.L[0] * &x_sq_fe;
let Rx_sq_inv = &self.R[0] * &x_inv_sq_fe;
Expand All @@ -229,8 +229,8 @@ impl InnerProductArg {
return ip.verify(&G_new, &H_new, ux, &P_tag);
}

let a_fe = Scalar::<Secp256k1>::from(&self.a_tag);
let b_fe = Scalar::<Secp256k1>::from(&self.b_tag);
let a_fe = Scalar::<C>::from(&self.a_tag);
let b_fe = Scalar::<C>::from(&self.b_tag);
let c = &a_fe * &b_fe;
let Ga = &G[0] * &a_fe;
let Hb = &H[0] * &b_fe;
Expand All @@ -252,15 +252,15 @@ impl InnerProductArg {
///
pub fn fast_verify(
&self,
g_vec: &[Point<Secp256k1>],
hi_tag: &[Point<Secp256k1>],
ux: &Point<Secp256k1>,
P: &Point<Secp256k1>,
g_vec: &[Point<C>],
hi_tag: &[Point<C>],
ux: &Point<C>,
P: &Point<C>,
) -> Result<(), Errors> {
let G = g_vec;
let H = hi_tag;
let n = G.len();
let order = Scalar::<Secp256k1>::group_order();
let order = Scalar::<C>::group_order();

// All of the input vectors must have the same length.
assert_eq!(G.len(), n);
Expand All @@ -279,7 +279,7 @@ impl InnerProductArg {
let mut minus_x_inv_sq_vec: Vec<BigInt> = Vec::with_capacity(lg_n);
let mut allinv = BigInt::one();
for (Li, Ri) in self.L.iter().zip(self.R.iter()) {
let x: Scalar<Secp256k1> = Sha256::new().chain_points([Li, Ri, ux]).result_scalar();
let x: Scalar<C> = Sha256::new().chain_points([Li, Ri, ux]).result_scalar();
let x_bn = x.to_bigint();
let x_inv_fe = x.invert().unwrap();
let x_inv_bn = x_inv_fe.to_bigint();
Expand Down Expand Up @@ -322,20 +322,20 @@ impl InnerProductArg {
scalars.extend_from_slice(&minus_x_sq_vec);
scalars.extend_from_slice(&minus_x_inv_sq_vec);

let mut points: Vec<Point<Secp256k1>> = Vec::with_capacity(2 * n + 2 * lg_n + 1);
let mut points: Vec<Point<C>> = Vec::with_capacity(2 * n + 2 * lg_n + 1);
points.extend_from_slice(g_vec);
points.extend_from_slice(hi_tag);
points.extend_from_slice(&self.L);
points.extend_from_slice(&self.R);

let c = BigInt::mod_mul(&self.a_tag, &self.b_tag, order);
let ux_c = ux * &Scalar::<Secp256k1>::from(&c);
let ux_c = ux * &Scalar::<C>::from(&c);

let tot_len = points.len();

let expect_P = (0..tot_len)
.map(|i| &points[i] * &Scalar::<Secp256k1>::from(&scalars[i]))
.fold(ux_c, |acc, x| acc + x as Point<Secp256k1>);
.map(|i| &points[i] * &Scalar::<C>::from(&scalars[i]))
.fold(ux_c, |acc, x| acc + x as Point<C>);

if *P == expect_P {
Ok(())
Expand All @@ -345,14 +345,14 @@ impl InnerProductArg {
}
}

fn inner_product(a: &[BigInt], b: &[BigInt]) -> BigInt {
fn inner_product<C: Curve>(a: &[BigInt], b: &[BigInt]) -> BigInt {
assert_eq!(
a.len(),
b.len(),
"inner_product(a,b): lengths of vectors do not match"
);
let out = BigInt::zero();
let order = Scalar::<Secp256k1>::group_order();
let order = Scalar::<C>::group_order();
let out = a.iter().zip(b).fold(out, |acc, x| {
let aibi = BigInt::mod_mul(x.0, x.1, order);
BigInt::mod_add(&acc, &aibi, order)
Expand Down Expand Up @@ -408,7 +408,7 @@ mod tests {
rand.to_bigint()
})
.collect();
let c = super::inner_product(&a, &b);
let c = super::inner_product::<Secp256k1>(&a, &b);

let y = Scalar::<Secp256k1>::random();
let order = Scalar::<Secp256k1>::group_order();
Expand Down Expand Up @@ -488,7 +488,7 @@ mod tests {
rand.to_bigint()
})
.collect();
let c = super::inner_product(&a, &b);
let c = super::inner_product::<Secp256k1>(&a, &b);

let y = Scalar::<Secp256k1>::random();
let order = Scalar::<Secp256k1>::group_order();
Expand Down Expand Up @@ -555,7 +555,7 @@ mod tests {
let hash = Sha512::new().chain_bigint(&label).result_bigint();
let Gx = generate_random_point(&Converter::to_bytes(&hash));

let c = super::inner_product(a, b);
let c = super::inner_product::<Secp256k1>(a, b);

let y = Scalar::<Secp256k1>::random();
let order = Scalar::<Secp256k1>::group_order();
Expand Down
Loading