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

Remove N_LIMBS from all generics #14

Merged
merged 7 commits into from
Apr 13, 2023
Merged
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
24 changes: 12 additions & 12 deletions starky/src/arithmetic/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
use super::*;
use crate::arithmetic::builder::ChipBuilder;
use crate::arithmetic::chip::{ChipParameters, TestStark};
use crate::arithmetic::register::{BitRegister, U16Array};
use crate::arithmetic::register::BitRegister;
use crate::arithmetic::trace::trace;
use crate::config::StarkConfig;
use crate::prover::prove;
Expand All @@ -210,10 +210,10 @@ mod tests {
pub struct BoolTest;

impl<F: RichField + Extendable<D>, const D: usize> ChipParameters<F, D> for BoolTest {
const NUM_ARITHMETIC_COLUMNS: usize = 3;
const NUM_FREE_COLUMNS: usize = 2;
const NUM_ARITHMETIC_COLUMNS: usize = 5;
const NUM_FREE_COLUMNS: usize = 5;

type Instruction = Selector<U16Array<1>>;
type Instruction = Selector<BitRegister>;
}

#[test]
Expand All @@ -230,7 +230,7 @@ mod tests {
let bit_zero = builder.alloc_local::<BitRegister>().unwrap();
builder.write_data(&bit_zero).unwrap();

let dummy = builder.alloc_local::<U16Array<1>>().unwrap();
let dummy = builder.alloc_local::<BitRegister>().unwrap();
builder.write_data(&dummy).unwrap();

let (chip, spec) = builder.build();
Expand All @@ -242,7 +242,7 @@ mod tests {
for i in 0..num_rows {
handle.write_data(i, bit_one, vec![F::ONE]).unwrap();
handle.write_data(i, bit_zero, vec![F::ZERO]).unwrap();
handle.write_data(i, dummy, vec![F::ZERO; 1]).unwrap();
handle.write_data(i, dummy, vec![F::ZERO]).unwrap();
}
drop(handle);

Expand Down Expand Up @@ -343,11 +343,11 @@ mod tests {
let bit = builder.alloc_local::<BitRegister>().unwrap();
builder.write_data(&bit).unwrap();

let x = builder.alloc_local::<U16Array<1>>().unwrap();
let x = builder.alloc_local::<BitRegister>().unwrap();
builder.write_data(&x).unwrap();
let y = builder.alloc_local::<U16Array<1>>().unwrap();
let y = builder.alloc_local::<BitRegister>().unwrap();
builder.write_data(&y).unwrap();
let result = builder.alloc_local::<U16Array<1>>().unwrap();
let result = builder.alloc_local::<BitRegister>().unwrap();

let sel = builder.selector(&bit, &x, &y, &result).unwrap();

Expand All @@ -359,9 +359,9 @@ mod tests {
let (handle, generator) = trace::<F, D>(spec);

for i in 0..num_rows {
let x_i = 4u16;
let y_i = 5u16;
let bit_i = i % 2 == 0;
let x_i = 0u16;
let y_i = 1u16;
let bit_i = if i % 2 == 0 { true } else { false };
handle.write_bit(i, bit_i, &bit).unwrap();
let res = if i % 2 == 0 { x_i } else { y_i };
handle
Expand Down
2 changes: 1 addition & 1 deletion starky/src/arithmetic/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ mod tests {
const NUM_ARITHMETIC_COLUMNS: usize = 0;
const NUM_FREE_COLUMNS: usize = 3 * 16;

type Instruction = FpMul<Fp25519Param, 16>;
type Instruction = FpMul<Fp25519Param>;
}

#[test]
Expand Down
123 changes: 61 additions & 62 deletions starky/src/arithmetic/ec/edwards/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,62 @@ use crate::arithmetic::trace::TraceHandle;
#[derive(Debug, Clone, Copy)]
#[allow(non_snake_case)]
#[allow(dead_code)]
pub struct EcAddData<E: EdwardsParameters<N_LIMBS>, const N_LIMBS: usize> {
P: AffinePointRegister<E, N_LIMBS>,
Q: AffinePointRegister<E, N_LIMBS>,
R: AffinePointRegister<E, N_LIMBS>,
XNUM: FpQuad<E::FieldParam, N_LIMBS>,
YNUM: FpQuad<E::FieldParam, N_LIMBS>,
PXPY: FpMul<E::FieldParam, N_LIMBS>,
QXQY: FpMul<E::FieldParam, N_LIMBS>,
PXPYQXQY: FpMul<E::FieldParam, N_LIMBS>,
DXY: FpMulConst<E::FieldParam, N_LIMBS>,
XDEN: Den<E::FieldParam, N_LIMBS>,
YDEN: Den<E::FieldParam, N_LIMBS>,
pub struct EcAddData<E: EdwardsParameters> {
P: AffinePointRegister<E>,
Q: AffinePointRegister<E>,
R: AffinePointRegister<E>,
XNUM: FpQuad<E::FieldParam>,
YNUM: FpQuad<E::FieldParam>,
PXPY: FpMul<E::FieldParam>,
QXQY: FpMul<E::FieldParam>,
PXPYQXQY: FpMul<E::FieldParam>,
DXY: FpMulConst<E::FieldParam>,
XDEN: Den<E::FieldParam>,
YDEN: Den<E::FieldParam>,
}

impl<E: EdwardsParameters<N_LIMBS>, const N_LIMBS: usize> EcAddData<E, N_LIMBS> {
impl<E: EdwardsParameters> EcAddData<E> {
pub const fn num_ed_add_witness_columns() -> usize {
2 * (FpQuad::<E::FieldParam, N_LIMBS>::num_quad_columns() - 4 * N_LIMBS)
+ 3 * (FpMul::<E::FieldParam, N_LIMBS>::num_mul_columns() - 2 * N_LIMBS)
+ 2 * (FpMulConst::<E::FieldParam, N_LIMBS>::num_mul_const_columns() - N_LIMBS)
+ 2 * (Den::<E::FieldParam, N_LIMBS>::num_den_columns() - 3 * N_LIMBS)
2 * (FpQuad::<E::FieldParam>::num_quad_columns() - 4 * E::FieldParam::NB_LIMBS)
+ 3 * (FpMul::<E::FieldParam>::num_mul_columns() - 2 * E::FieldParam::NB_LIMBS)
+ (FpMulConst::<E::FieldParam>::num_mul_const_columns() - E::FieldParam::NB_LIMBS)
+ 2 * (Den::<E::FieldParam>::num_den_columns() - 3 * E::FieldParam::NB_LIMBS)
}

pub const fn num_ed_add_columns() -> usize {
6 * N_LIMBS + Self::num_ed_add_witness_columns()
6 * E::FieldParam::NB_LIMBS + Self::num_ed_add_witness_columns()
}

pub const fn num_ed_double_columns() -> usize {
Self::num_ed_add_columns() - 3 * N_LIMBS
Self::num_ed_add_columns() - 3 * E::FieldParam::NB_LIMBS
}
}

pub trait FromEdwardsAdd<E: EdwardsParameters<N>, const N: usize>:
From<FpMul<E::FieldParam, N>>
+ From<FpQuad<E::FieldParam, N>>
+ From<FpMulConst<E::FieldParam, N>>
+ From<Den<E::FieldParam, N>>
pub trait FromEdwardsAdd<E: EdwardsParameters>:
From<FpMul<E::FieldParam>>
+ From<FpQuad<E::FieldParam>>
+ From<FpMulConst<E::FieldParam>>
+ From<Den<E::FieldParam>>
{
}

impl<L: ChipParameters<F, D>, F: RichField + Extendable<D>, const D: usize> ChipBuilder<L, F, D> {
#[allow(non_snake_case)]
pub fn ed_add<E: EdwardsParameters<N>, const N: usize>(
pub fn ed_add<E: EdwardsParameters>(
&mut self,
P: &AffinePointRegister<E, N>,
Q: &AffinePointRegister<E, N>,
result: &AffinePointRegister<E, N>,
) -> Result<EcAddData<E, N>>
P: &AffinePointRegister<E>,
Q: &AffinePointRegister<E>,
result: &AffinePointRegister<E>,
) -> Result<EcAddData<E>>
where
L::Instruction: FromEdwardsAdd<E, N>,
L::Instruction: FromEdwardsAdd<E>,
{
let x_num_result = self.alloc_local::<FieldRegister<E::FieldParam, N>>()?;
let y_num_result = self.alloc_local::<FieldRegister<E::FieldParam, N>>()?;
let px_py_result = self.alloc_local::<FieldRegister<E::FieldParam, N>>()?;
let qx_qy_result = self.alloc_local::<FieldRegister<E::FieldParam, N>>()?;
let all_xy_result = self.alloc_local::<FieldRegister<E::FieldParam, N>>()?;
let dxy_result = self.alloc_local::<FieldRegister<E::FieldParam, N>>()?;
let x_num_result = self.alloc_local::<FieldRegister<E::FieldParam>>()?;
let y_num_result = self.alloc_local::<FieldRegister<E::FieldParam>>()?;
let px_py_result = self.alloc_local::<FieldRegister<E::FieldParam>>()?;
let qx_qy_result = self.alloc_local::<FieldRegister<E::FieldParam>>()?;
let all_xy_result = self.alloc_local::<FieldRegister<E::FieldParam>>()?;
let dxy_result = self.alloc_local::<FieldRegister<E::FieldParam>>()?;

let x_num_ins = self.fpquad(&P.x, &Q.y, &Q.x, &P.y, &x_num_result)?;
let y_num_ins = self.fpquad(&P.y, &Q.y, &P.x, &Q.x, &y_num_result)?;
Expand Down Expand Up @@ -96,27 +96,27 @@ impl<L: ChipParameters<F, D>, F: RichField + Extendable<D>, const D: usize> Chip
}

#[allow(non_snake_case)]
pub fn ed_double<E: EdwardsParameters<N>, const N: usize>(
pub fn ed_double<E: EdwardsParameters>(
&mut self,
P: &AffinePointRegister<E, N>,
result: &AffinePointRegister<E, N>,
) -> Result<EcAddData<E, N>>
P: &AffinePointRegister<E>,
result: &AffinePointRegister<E>,
) -> Result<EcAddData<E>>
where
L::Instruction: FromEdwardsAdd<E, N>,
L::Instruction: FromEdwardsAdd<E>,
{
self.ed_add(P, P, result)
}
}

impl<F: RichField + Extendable<D>, const D: usize> TraceHandle<F, D> {
#[allow(non_snake_case)]
pub fn write_ed_add<E: EdwardsParameters<N_LIMBS>, const N_LIMBS: usize>(
pub fn write_ed_add<E: EdwardsParameters>(
&self,
row_index: usize,
P: &AffinePoint<E, N_LIMBS>,
Q: &AffinePoint<E, N_LIMBS>,
chip_data: EcAddData<E, N_LIMBS>,
) -> Result<AffinePoint<E, N_LIMBS>> {
P: &AffinePoint<E>,
Q: &AffinePoint<E>,
chip_data: EcAddData<E>,
) -> Result<AffinePoint<E>> {
let x_num = self.write_fpquad(row_index, &P.x, &Q.y, &Q.x, &P.y, chip_data.XNUM)?;
let y_num = self.write_fpquad(row_index, &P.y, &Q.y, &P.x, &Q.x, chip_data.YNUM)?;

Expand All @@ -133,12 +133,12 @@ impl<F: RichField + Extendable<D>, const D: usize> TraceHandle<F, D> {
}

#[allow(non_snake_case)]
pub fn write_ed_double<E: EdwardsParameters<N_LIMBS>, const N_LIMBS: usize>(
pub fn write_ed_double<E: EdwardsParameters>(
&self,
row_index: usize,
P: &AffinePoint<E, N_LIMBS>,
chip_data: EcAddData<E, N_LIMBS>,
) -> Result<AffinePoint<E, N_LIMBS>> {
P: &AffinePoint<E>,
chip_data: EcAddData<E>,
) -> Result<AffinePoint<E>> {
self.write_ed_add(row_index, P, P, chip_data)
}
}
Expand Down Expand Up @@ -173,11 +173,10 @@ mod tests {
pub struct EdAddTest;

impl<F: RichField + Extendable<D>, const D: usize> ChipParameters<F, D> for EdAddTest {
const NUM_ARITHMETIC_COLUMNS: usize =
EcAddData::<Ed25519Parameters, 16>::num_ed_add_columns();
const NUM_ARITHMETIC_COLUMNS: usize = EcAddData::<Ed25519Parameters>::num_ed_add_columns();
const NUM_FREE_COLUMNS: usize = 0;

type Instruction = EdWardsMicroInstruction<Ed25519Parameters, 16>;
type Instruction = EdWardsMicroInstruction<Ed25519Parameters>;
}

#[allow(non_snake_case)]
Expand All @@ -193,11 +192,11 @@ mod tests {
// build the stark
let mut builder = ChipBuilder::<EdAddTest, F, D>::new();

let P = builder.alloc_local_ec_point::<E, 16>().unwrap();
let Q = builder.alloc_local_ec_point::<E, 16>().unwrap();
let R = builder.alloc_local_ec_point::<E, 16>().unwrap();
let P = builder.alloc_local_ec_point::<E>().unwrap();
let Q = builder.alloc_local_ec_point::<E>().unwrap();
let R = builder.alloc_local_ec_point::<E>().unwrap();

let ed_data = builder.ed_add::<E, 16>(&P, &Q, &R).unwrap();
let ed_data = builder.ed_add::<E>(&P, &Q, &R).unwrap();
builder.write_ec_point(&P).unwrap();
builder.write_ec_point(&Q).unwrap();

Expand Down Expand Up @@ -303,10 +302,10 @@ mod tests {

impl<F: RichField + Extendable<D>, const D: usize> ChipParameters<F, D> for EdDoubleTest {
const NUM_ARITHMETIC_COLUMNS: usize =
EcAddData::<Ed25519Parameters, 16>::num_ed_double_columns();
EcAddData::<Ed25519Parameters>::num_ed_double_columns();
const NUM_FREE_COLUMNS: usize = 0;

type Instruction = EdWardsMicroInstruction<Ed25519Parameters, 16>;
type Instruction = EdWardsMicroInstruction<Ed25519Parameters>;
}

#[allow(non_snake_case)]
Expand All @@ -322,10 +321,10 @@ mod tests {
// build the stark
let mut builder = ChipBuilder::<EdAddTest, F, D>::new();

let P = builder.alloc_ec_point::<E, 16>().unwrap();
let R = builder.alloc_ec_point::<E, 16>().unwrap();
let P = builder.alloc_ec_point::<E>().unwrap();
let R = builder.alloc_ec_point::<E>().unwrap();

let ed_double_data = builder.ed_double::<E, 16>(&P, &R).unwrap();
let ed_double_data = builder.ed_double::<E>(&P, &R).unwrap();
builder.write_ec_point(&P).unwrap();

let (chip, spec) = builder.build();
Expand Down
42 changes: 21 additions & 21 deletions starky/src/arithmetic/ec/edwards/bigint_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::EdwardsParameters;
use crate::arithmetic::field::FieldParameters;
use crate::arithmetic::util::biguint_to_bits_le;

impl<E: EdwardsParameters<N>, const N: usize> AffinePoint<E, N> {
impl<E: EdwardsParameters> AffinePoint<E> {
pub fn neutral() -> Self {
Self::new(BigUint::from(0u32), BigUint::from(1u32))
}
Expand All @@ -26,10 +26,10 @@ impl<E: EdwardsParameters<N>, const N: usize> AffinePoint<E, N> {
}
}

impl<E: EdwardsParameters<N>, const N: usize> Add<&AffinePoint<E, N>> for &AffinePoint<E, N> {
type Output = AffinePoint<E, N>;
impl<E: EdwardsParameters> Add<&AffinePoint<E>> for &AffinePoint<E> {
type Output = AffinePoint<E>;

fn add(self, other: &AffinePoint<E, N>) -> AffinePoint<E, N> {
fn add(self, other: &AffinePoint<E>) -> AffinePoint<E> {
let p = E::FieldParam::modulus_biguint();
let x_3n = (&self.x * &other.y + &self.y * &other.x) % &p;
let y_3n = (&self.y * &other.y + &self.x * &other.x) % &p;
Expand All @@ -47,42 +47,42 @@ impl<E: EdwardsParameters<N>, const N: usize> Add<&AffinePoint<E, N>> for &Affin
}
}

impl<E: EdwardsParameters<N>, const N: usize> Add<AffinePoint<E, N>> for AffinePoint<E, N> {
type Output = AffinePoint<E, N>;
impl<E: EdwardsParameters> Add<AffinePoint<E>> for AffinePoint<E> {
type Output = AffinePoint<E>;

fn add(self, other: AffinePoint<E, N>) -> AffinePoint<E, N> {
fn add(self, other: AffinePoint<E>) -> AffinePoint<E> {
&self + &other
}
}

impl<E: EdwardsParameters<N>, const N: usize> Add<&AffinePoint<E, N>> for AffinePoint<E, N> {
type Output = AffinePoint<E, N>;
impl<E: EdwardsParameters> Add<&AffinePoint<E>> for AffinePoint<E> {
type Output = AffinePoint<E>;

fn add(self, other: &AffinePoint<E, N>) -> AffinePoint<E, N> {
fn add(self, other: &AffinePoint<E>) -> AffinePoint<E> {
&self + other
}
}

impl<E: EdwardsParameters<N>, const N: usize> Mul<&BigUint> for &AffinePoint<E, N> {
type Output = AffinePoint<E, N>;
impl<E: EdwardsParameters> Mul<&BigUint> for &AffinePoint<E> {
type Output = AffinePoint<E>;

fn mul(self, scalar: &BigUint) -> AffinePoint<E, N> {
fn mul(self, scalar: &BigUint) -> AffinePoint<E> {
self.scalar_mul(scalar)
}
}

impl<E: EdwardsParameters<N>, const N: usize> Mul<BigUint> for &AffinePoint<E, N> {
type Output = AffinePoint<E, N>;
impl<E: EdwardsParameters> Mul<BigUint> for &AffinePoint<E> {
type Output = AffinePoint<E>;

fn mul(self, scalar: BigUint) -> AffinePoint<E, N> {
fn mul(self, scalar: BigUint) -> AffinePoint<E> {
self.scalar_mul(&scalar)
}
}

impl<E: EdwardsParameters<N>, const N: usize> Mul<BigUint> for AffinePoint<E, N> {
type Output = AffinePoint<E, N>;
impl<E: EdwardsParameters> Mul<BigUint> for AffinePoint<E> {
type Output = AffinePoint<E>;

fn mul(self, scalar: BigUint) -> AffinePoint<E, N> {
fn mul(self, scalar: BigUint) -> AffinePoint<E> {
self.scalar_mul(&scalar)
}
}
Expand All @@ -100,7 +100,7 @@ mod tests {
#[test]
fn test_bigint_ed_add() {
type E = Ed25519Parameters;
let netural = AffinePoint::<E, 16>::neutral();
let netural = AffinePoint::<E>::neutral();
let base = E::generator();

assert_eq!(&base + &netural, base);
Expand All @@ -114,7 +114,7 @@ mod tests {
let base = E::generator();

let d = E::d_biguint();
let p = <E as EllipticCurveParameters<16>>::FieldParam::modulus_biguint();
let p = <E as EllipticCurveParameters>::FieldParam::modulus_biguint();
assert_eq!((d * 121666u32) % &p, (&p - 121665u32) % &p);

let mut rng = thread_rng();
Expand Down
Loading