Skip to content

Commit

Permalink
fixed compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kevjue committed Feb 18, 2024
1 parent e0ddee6 commit e4c93c5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
51 changes: 36 additions & 15 deletions core/src/air/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub const DEGREE: usize = 4;
#[repr(C)]
pub struct Extension<T>(pub [T; DEGREE]); // Degree 4 is hard coded for now. TODO: Change to a const generic

impl<V> Extension<V> {
impl<E: AbstractField> Extension<E> {
// Returns the one element of the extension field
pub fn one<AB: SP1AirBuilder<Var = V>>() -> Extension<AB::Expr>
pub fn one<AB: SP1AirBuilder<Expr = E>>() -> Extension<AB::Expr>
where
AB::Expr: AbstractField,
{
Expand All @@ -30,51 +30,72 @@ impl<V> Extension<V> {
}

// Converts a field element to extension element
pub fn from<AB: SP1AirBuilder<Var = V>>(x: V) -> Extension<AB::Expr>
where
AB::Expr: From<V>,
{
Extension(field_to_array(x.into()))
pub fn from<AB: SP1AirBuilder<Expr = E>>(x: E) -> Extension<AB::Expr> {
Extension(field_to_array(x))
}

// Negates an extension field Element
pub fn neg<AB: SP1AirBuilder<Var = V>>(self) -> Extension<AB::Expr> {
pub fn neg<AB: SP1AirBuilder<Expr = E>>(self) -> Extension<AB::Expr> {

Check failure on line 38 in core/src/air/extension.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

method `neg` can be confused for the standard trait method `std::ops::Neg::neg`
Extension(self.0.map(|x| AB::Expr::zero() - x))
}

// Adds an extension field element
pub fn add<AB: SP1AirBuilder<Var = V>>(self, rhs: &Self) -> Extension<AB::Expr>
pub fn add<AB: SP1AirBuilder<Expr = E>>(self, rhs: &Self) -> Extension<AB::Expr>

Check failure on line 43 in core/src/air/extension.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

method `add` can be confused for the standard trait method `std::ops::Add::add`
where
V: Add<V, Output = AB::Expr> + Copy,
E: Add<E, Output = AB::Expr>,
{
let mut elements = Vec::new();

for (e1, e2) in self.0.into_iter().zip_eq(rhs.0.into_iter()) {
for (e1, e2) in self.0.into_iter().zip_eq(rhs.0.clone().into_iter()) {
elements.push(e1 + e2);
}

Extension(elements.try_into().unwrap())
}

// Subtracts an extension field element
pub fn sub<AB: SP1AirBuilder<Expr = E>>(self, rhs: &Self) -> Extension<AB::Expr>

Check failure on line 57 in core/src/air/extension.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

method `sub` can be confused for the standard trait method `std::ops::Sub::sub`
where
E: Add<E, Output = AB::Expr>,
{
let mut elements = Vec::new();

for (e1, e2) in self.0.into_iter().zip_eq(rhs.0.clone().into_iter()) {
elements.push(e1 - e2);
}

Extension(elements.try_into().unwrap())
}

// Multiplies an extension field element
pub fn mul<AB: SP1AirBuilder<Var = V>>(self, rhs: &Self) -> Extension<AB::Expr>
pub fn mul<AB: SP1AirBuilder<Expr = E>>(self, rhs: &Self) -> Extension<AB::Expr>

Check failure on line 71 in core/src/air/extension.rs

View workflow job for this annotation

GitHub Actions / Formatting & Clippy

method `mul` can be confused for the standard trait method `std::ops::Mul::mul`
where
V: Mul<V, Output = AB::Expr> + Copy,
E: Mul<E, Output = AB::Expr>,
{
let mut elements = Vec::new();

for (e1, e2) in self.0.into_iter().zip_eq(rhs.0.into_iter()) {
for (e1, e2) in self.0.into_iter().zip_eq(rhs.0.clone().into_iter()) {
elements.push(e1 * e2);
}

Extension(elements.try_into().unwrap())
}

pub fn as_base_slice(&self) -> &[V] {
pub fn as_base_slice(&self) -> &[E] {
&self.0
}
}

impl<V> Extension<V> {
// Converts a field element with var base elements to one with expr base elements.
pub fn from_var<AB: SP1AirBuilder<Var = V>>(self) -> Extension<AB::Expr>
where
V: Into<AB::Expr>,
{
Extension(self.0.map(|x| x.into()))
}
}

impl<F> From<BinomialExtensionField<F, 4>> for Extension<F>
where
F: Field,
Expand Down
25 changes: 13 additions & 12 deletions core/src/operations/div_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
//!
use core::borrow::Borrow;
use core::borrow::BorrowMut;
use p3_air::AirBuilder;
use p3_field::extension::BinomialExtensionField;
use p3_field::extension::BinomiallyExtendable;
use p3_field::AbstractField;
use p3_field::Field;
use sp1_derive::AlignedBorrow;
use std::mem::size_of;

use crate::air::Extension;
use crate::air::SP1AirBuilder;
use crate::air::DEGREE;

use super::IsEqualExtOperation;

/// A set of columns needed to compute whether the given word is 0.
#[derive(AlignedBorrow, Default, Debug, Clone, Copy)]
#[repr(C)]
pub struct DivExtOperation<T> {
pub is_equal: IsEqualExtOperation<T>,

/// Result is the quotient
pub result: Extension<T>,
}
Expand All @@ -30,6 +31,10 @@ impl<F: BinomiallyExtendable<DEGREE>> DivExtOperation<F> {
) -> BinomialExtensionField<F, DEGREE> {
let result = a / b;
self.result = result.into();

let product = b * result;
self.is_equal.populate(a, product);

result
}

Expand All @@ -39,16 +44,12 @@ impl<F: BinomiallyExtendable<DEGREE>> DivExtOperation<F> {
b: Extension<AB::Expr>,
cols: DivExtOperation<AB::Var>,
is_real: AB::Expr,
) {
) where
AB::F: BinomiallyExtendable<DEGREE>,
{
builder.assert_bool(is_real.clone());

let product = b.mul(&cols.result);
builder.when(is_real.clone()).assert_eq(product, a);

// If the result is 1, then the input is 0.
builder
.when(is_real.clone())
.when(cols.result)
.assert_zero(a.clone());
let product = b.mul::<AB>(&cols.result.from_var::<AB>());
IsEqualExtOperation::<AB::F>::eval(builder, a, product, cols.is_equal, is_real.clone());
}
}
6 changes: 4 additions & 2 deletions core/src/operations/is_equal_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ impl<F: BinomiallyExtendable<DEGREE>> IsEqualExtOperation<F> {
b: Extension<AB::Expr>,
cols: IsEqualExtOperation<AB::Var>,
is_real: AB::Expr,
) {
) where
AB::F: BinomiallyExtendable<DEGREE>,
{
builder.assert_bool(is_real.clone());

// Calculate differences.
let diff = a.sub(b);
let diff = a.sub::<AB>(&b);

// Check if the difference is 0.
IsZeroExtOperation::<AB::F>::eval(builder, diff, cols.is_diff_zero, is_real.clone());
Expand Down

0 comments on commit e4c93c5

Please sign in to comment.