arkworks-rs / r1cs-std

R1CS constraints for bits, fields, and elliptic curves
https://www.arkworks.rs
Apache License 2.0
133 stars 58 forks source link

Interchangeability of NonNativeFieldVar and FpVar for FieldVar #99

Open weikengchen opened 2 years ago

weikengchen commented 2 years ago

Summary

This issue is to discuss how to make NonNativeFieldVar a drop-in replacement for FieldVar in curve operations.

Problem Definition

One of the reasons that we implemented FieldVar for NonNativeFieldVar is to allow it to be a drop-in replacement of FpVar, so that one doesn't need to build a hierarchy of nonnative primitives.

This, however, is not covered by the current implementation, see: https://github.com/arkworks-rs/r1cs-std/blob/master/src/groups/curves/short_weierstrass/mod.rs#L44

#[derive(Derivative)]
#[derivative(Debug, Clone)]
#[must_use]
pub struct ProjectiveVar<
    P: SWModelParameters,
    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
> where
    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
{
    ...
}

It would be ideal, though, to lift this restriction. However, we want to make sure that we don't change the existing writing system---it would be highly inconvenient to use

pub struct ProjectiveVar<
    P: SWModelParameters,
    F: FieldVar<P::BaseField, CF>,
    CF: PrimeField,
> 

because it introduces one more param CF.

A possible workaround, though not favorable, is to define GeneralizedProjectVar<P, F, CF> and let Projective<P, F> = GeneralizedProjectVar<P, F, <P::BaseField as Field>::BasePrimeField>. This is not favored since nonnative field is not important to this level.

Proposal

We need to think about what would be the best way to do this.


For Admin Use

rubdos commented 1 month ago

What about:

pub struct ProjectiveVar<
    P: SWModelParameters,
    F: FieldVar<P::BaseField, CF>,
    CF: PrimeField = <P::BaseField as Field>::BasePrimeField,
>  {
}

... would that not suffice here? I suppose that too will sit in the way quite often though.