Skip to content

Commit

Permalink
Correct BaseField trait bound
Browse files Browse the repository at this point in the history
  • Loading branch information
darth-cy committed Dec 3, 2024
1 parent 70dd37c commit 8b5db3a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
1 change: 1 addition & 0 deletions spartan_parallel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![allow(clippy::assertions_on_result_states)]
#![feature(associated_type_defaults)]

// TODO: Can we allow split in R1CSGens?
// TODO: Can we parallelize the proofs?
Expand Down
36 changes: 33 additions & 3 deletions spartan_parallel/src/scalar/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::borrow::Borrow;
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use ff::{Field, FromUniformBytes};
use goldilocks::Goldilocks;
use goldilocks::{ExtensionField, Goldilocks};
use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use std::ops::Neg;
Expand All @@ -15,9 +15,39 @@ use zeroize::Zeroize;
#[derive(Clone, Copy, Eq, Serialize, Deserialize, Hash, Debug)]
pub struct Scalar(Goldilocks);

impl Mul<Goldilocks> for Scalar {
type Output = Scalar;

#[inline]
fn mul(self, rhs: Goldilocks) -> Self::Output {
(*self.inner() * rhs).into()
}
}
impl<'a> Mul<&'a Goldilocks> for Scalar {
type Output = Self;

#[inline]
fn mul(mut self, rhs: &'a Goldilocks) -> Self::Output {
self *= rhs;
self
}
}
impl MulAssign<&Goldilocks> for Scalar {
#[inline]
fn mul_assign(&mut self, rhs: &Goldilocks) {
self.0 *= rhs;
}
}
impl MulAssign<Goldilocks> for Scalar {
#[inline]
fn mul_assign(&mut self, rhs: Goldilocks) {
self.mul_assign(&rhs)
}
}

impl SpartanExtensionField for Scalar {
type InnerType = Goldilocks;
type BaseField = Self;
type BaseField = Goldilocks;

fn inner(&self) -> &Goldilocks {
&self.0
Expand All @@ -33,7 +63,7 @@ impl SpartanExtensionField for Scalar {

/// Build a self from a base element; pad ext with 0s.
fn from_base(b: &Self::BaseField) -> Self {
*b
Self::InnerType::from_base(b).into()
}

fn random<Rng: RngCore + CryptoRng>(rng: &mut Rng) -> Self {
Expand Down
25 changes: 12 additions & 13 deletions spartan_parallel/src/scalar/fp2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::SpartanExtensionField;
use crate::scalar::Scalar;
use crate::{AppendToTranscript, ProofTranscript, Transcript};
use core::borrow::Borrow;
use core::iter::{Product, Sum};
Expand All @@ -21,38 +20,38 @@ impl From<GoldilocksExt2> for ScalarExt2 {
}
}

impl Mul<Scalar> for ScalarExt2 {
impl Mul<Goldilocks> for ScalarExt2 {
type Output = ScalarExt2;

#[inline]
fn mul(self, rhs: Scalar) -> Self::Output {
(self.inner() * &rhs.inner()).into()
fn mul(self, rhs: Goldilocks) -> Self::Output {
(self.inner() * &rhs).into()
}
}
impl<'a> Mul<&'a Scalar> for ScalarExt2 {
impl<'a> Mul<&'a Goldilocks> for ScalarExt2 {
type Output = Self;

#[inline]
fn mul(mut self, rhs: &'a Scalar) -> Self::Output {
fn mul(mut self, rhs: &'a Goldilocks) -> Self::Output {
self *= rhs;
self
}
}
impl MulAssign<&Scalar> for ScalarExt2 {
impl MulAssign<&Goldilocks> for ScalarExt2 {
#[inline]
fn mul_assign(&mut self, rhs: &Scalar) {
self.0 *= rhs.inner();
fn mul_assign(&mut self, rhs: &Goldilocks) {
self.0 *= rhs;
}
}
impl MulAssign<Scalar> for ScalarExt2 {
impl MulAssign<Goldilocks> for ScalarExt2 {
#[inline]
fn mul_assign(&mut self, rhs: Scalar) {
fn mul_assign(&mut self, rhs: Goldilocks) {
self.mul_assign(&rhs)
}
}
impl SpartanExtensionField for ScalarExt2 {
type InnerType = GoldilocksExt2;
type BaseField = Scalar;
type BaseField = Goldilocks;

fn inner(&self) -> &GoldilocksExt2 {
&self.0
Expand All @@ -68,7 +67,7 @@ impl SpartanExtensionField for ScalarExt2 {

/// Build a self from a base element; pad ext with 0s.
fn from_base(b: &Self::BaseField) -> Self {
GoldilocksExt2::from_base(b.inner()).into()
GoldilocksExt2::from_base(b).into()
}

fn random<Rng: RngCore + CryptoRng>(rng: &mut Rng) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion spartan_parallel/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait SpartanExtensionField:
type InnerType: ExtensionField + Field;

/// Basefield for conserving computational resources
type BaseField: SpartanExtensionField;
type BaseField: Field;

/// Return inner Goldilocks field element
fn inner(&self) -> &Self::InnerType;
Expand Down

0 comments on commit 8b5db3a

Please sign in to comment.