Skip to content

Commit de67fc9

Browse files
committed
Add basefield arithmetics
1 parent 223e359 commit de67fc9

File tree

6 files changed

+61
-12
lines changed

6 files changed

+61
-12
lines changed

spartan_parallel/src/dense_mlpoly.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
443443
let mut R_list: Vec<Vec<S>> = Vec::new();
444444
let mut Zc_list: Vec<S> = Vec::new();
445445

446-
let c_base = transcript.challenge_scalar(b"challenge_c");
446+
let c_base: S = transcript.challenge_scalar(b"challenge_c");
447447
let mut c = S::field_one();
448448
for i in 0..r_list.len() {
449449
let eq = EqPolynomial::new(r_list[i].to_vec());
@@ -516,7 +516,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
516516
let mut R_list: Vec<Vec<S>> = Vec::new();
517517
let mut Zc_list: Vec<S> = Vec::new();
518518

519-
let c_base = transcript.challenge_scalar(b"challenge_c");
519+
let c_base: S = transcript.challenge_scalar(b"challenge_c");
520520
let mut c = S::field_one();
521521
for i in 0..r_list.len() {
522522
let eq = EqPolynomial::new(r_list[i].to_vec());
@@ -570,7 +570,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
570570
let mut R_list: Vec<Vec<S>> = Vec::new();
571571

572572
// generate coefficient for RLC
573-
let c_base = transcript.challenge_scalar(b"challenge_c");
573+
let c_base: S = transcript.challenge_scalar(b"challenge_c");
574574
let mut c = S::field_one();
575575
let zero = S::field_zero();
576576
for i in 0..poly_list.len() {
@@ -658,7 +658,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
658658
let mut R_list: Vec<Vec<S>> = Vec::new();
659659

660660
// generate coefficient for RLC
661-
let c_base = transcript.challenge_scalar(b"challenge_c");
661+
let c_base: S = transcript.challenge_scalar(b"challenge_c");
662662
let mut c = S::field_one();
663663
let zero = S::field_zero();
664664

@@ -724,7 +724,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
724724
let mut R_list = Vec::new();
725725

726726
// generate coefficient for RLC
727-
let c_base = transcript.challenge_scalar(b"challenge_c");
727+
let c_base: S = transcript.challenge_scalar(b"challenge_c");
728728
let mut c = S::field_one();
729729
let zero = S::field_zero();
730730
for i in 0..poly_list.len() {
@@ -822,7 +822,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
822822
let mut R_list = Vec::new();
823823

824824
// generate coefficient for RLC
825-
let c_base = transcript.challenge_scalar(b"challenge_c");
825+
let c_base: S = transcript.challenge_scalar(b"challenge_c");
826826
let mut c = S::field_one();
827827
let zero = S::field_zero();
828828

@@ -917,7 +917,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
917917

918918
// compute the vector underneath L*Z
919919
// compute vector-matrix product between L and Z viewed as a matrix
920-
let c_base = transcript.challenge_scalar(b"challenge_c");
920+
let c_base: S = transcript.challenge_scalar(b"challenge_c");
921921
let mut c = S::field_one();
922922
let mut LZ_comb = vec![zero; R_size];
923923
let mut Zr_comb = zero;
@@ -995,7 +995,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
995995
let mut L_map: HashMap<usize, Vec<S>> = HashMap::new();
996996

997997
// compute a weighted sum of commitments and L
998-
let c_base = transcript.challenge_scalar(b"challenge_c");
998+
let c_base: S = transcript.challenge_scalar(b"challenge_c");
999999
let mut c = S::field_one();
10001000

10011001
for i in 0..poly_size.len() {

spartan_parallel/src/nizk/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<S: SpartanExtensionField> KnowledgeProof<S> {
3636
let t1 = random_tape.random_scalar(b"t1");
3737
let t2 = random_tape.random_scalar(b"t2");
3838

39-
let c = transcript.challenge_scalar(b"c");
39+
let c: S = transcript.challenge_scalar(b"c");
4040

4141
let z1 = *x * c + t1;
4242
let z2 = *r * c + t2;

spartan_parallel/src/product_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl<S: SpartanExtensionField> ProductCircuitEvalProofBatched<S> {
399399
}
400400

401401
// produce random coefficients, one for each instance
402-
let coeff_vec =
402+
let coeff_vec: Vec<S> =
403403
transcript.challenge_vector(b"rand_coeffs_next_layer", claims_to_verify.len());
404404

405405
// produce a joint claim

spartan_parallel/src/scalar/fp.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct Scalar(Goldilocks);
1717

1818
impl SpartanExtensionField for Scalar {
1919
type InnerType = Goldilocks;
20+
type BaseField = Self;
2021

2122
fn inner(&self) -> &Goldilocks {
2223
&self.0
@@ -30,6 +31,11 @@ impl SpartanExtensionField for Scalar {
3031
Goldilocks::ONE.into()
3132
}
3233

34+
/// Build a self from a base element; pad ext with 0s.
35+
fn from_base(b: &Self::BaseField) -> Self {
36+
*b
37+
}
38+
3339
fn random<Rng: RngCore + CryptoRng>(rng: &mut Rng) -> Self {
3440
Goldilocks::random(rng).into()
3541
}

spartan_parallel/src/scalar/fp2.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rand::{CryptoRng, RngCore};
99
use serde::{Deserialize, Serialize};
1010
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
1111
use zeroize::Zeroize;
12+
use crate::scalar::Scalar;
1213

1314
/// Field wrapper around ext2 Goldilocks
1415
#[derive(Clone, Copy, Eq, Serialize, Deserialize, Hash, Debug)]
@@ -20,8 +21,38 @@ impl From<GoldilocksExt2> for ScalarExt2 {
2021
}
2122
}
2223

24+
impl Mul<Scalar> for ScalarExt2 {
25+
type Output = ScalarExt2;
26+
27+
#[inline]
28+
fn mul(self, rhs: Scalar) -> Self::Output {
29+
(self.inner() * &rhs.inner()).into()
30+
}
31+
}
32+
impl<'a> Mul<&'a Scalar> for ScalarExt2 {
33+
type Output = Self;
34+
35+
#[inline]
36+
fn mul(mut self, rhs: &'a Scalar) -> Self::Output {
37+
self *= rhs;
38+
self
39+
}
40+
}
41+
impl MulAssign<&Scalar> for ScalarExt2 {
42+
#[inline]
43+
fn mul_assign(&mut self, rhs: &Scalar) {
44+
self.0 *= rhs.inner();
45+
}
46+
}
47+
impl MulAssign<Scalar> for ScalarExt2 {
48+
#[inline]
49+
fn mul_assign(&mut self, rhs: Scalar) {
50+
self.mul_assign(&rhs)
51+
}
52+
}
2353
impl SpartanExtensionField for ScalarExt2 {
2454
type InnerType = GoldilocksExt2;
55+
type BaseField = Scalar;
2556

2657
fn inner(&self) -> &GoldilocksExt2 {
2758
&self.0
@@ -35,6 +66,11 @@ impl SpartanExtensionField for ScalarExt2 {
3566
GoldilocksExt2::ONE.into()
3667
}
3768

69+
/// Build a self from a base element; pad ext with 0s.
70+
fn from_base(b: &Self::BaseField) -> Self {
71+
GoldilocksExt2::from_base(b.inner()).into()
72+
}
73+
3874
fn random<Rng: RngCore + CryptoRng>(rng: &mut Rng) -> Self {
3975
GoldilocksExt2::random(rng).into()
4076
}
@@ -136,7 +172,6 @@ impl ScalarExt2 {
136172
Self(GoldilocksExt2::ONE)
137173
}
138174
}
139-
140175
impl<'a, 'b> Add<&'b ScalarExt2> for &'a ScalarExt2 {
141176
type Output = ScalarExt2;
142177

spartan_parallel/src/scalar/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
cmp::Eq,
1414
hash::Hash,
1515
iter::{Product, Sum},
16-
ops::{Add, Mul, Neg, Sub},
16+
ops::{Add, Mul, Neg, Sub, MulAssign},
1717
};
1818
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
1919
use zeroize::Zeroize;
@@ -42,10 +42,15 @@ pub trait SpartanExtensionField:
4242
+ Hash
4343
+ From<Self::InnerType>
4444
+ fmt::Debug
45+
+ Mul<Self::BaseField>
46+
+ MulAssign<Self::BaseField>
4547
{
4648
/// Inner Goldilocks extension field
4749
type InnerType: ExtensionField + Field;
4850

51+
/// Basefield for conserving computational resources
52+
type BaseField: SpartanExtensionField;
53+
4954
/// Return inner Goldilocks field element
5055
fn inner(&self) -> &Self::InnerType;
5156

@@ -55,6 +60,9 @@ pub trait SpartanExtensionField:
5560
/// Return the multiplicative identity
5661
fn field_one() -> Self;
5762

63+
/// Build a self from a base element; pad ext with 0s.
64+
fn from_base(b: &Self::BaseField) -> Self;
65+
5866
/// Sample field element
5967
fn random<Rng: RngCore + CryptoRng>(rng: &mut Rng) -> Self;
6068

0 commit comments

Comments
 (0)