2021-02-25 10:11:46 -08:00
|
|
|
//! Gadgets for elliptic curve operations.
|
|
|
|
|
2021-05-17 21:03:40 -07:00
|
|
|
use ff::Field;
|
2021-05-03 22:13:04 -07:00
|
|
|
use std::fmt::Debug;
|
2021-02-25 10:11:46 -08:00
|
|
|
|
|
|
|
use halo2::{
|
2021-05-17 21:03:40 -07:00
|
|
|
arithmetic::{CurveAffine, FieldExt},
|
2021-02-25 10:11:46 -08:00
|
|
|
circuit::{Chip, Layouter},
|
|
|
|
plonk::Error,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The set of circuit instructions required to use the ECC gadgets.
|
2021-04-26 08:17:08 -07:00
|
|
|
pub trait EccInstructions<C: CurveAffine>: Chip<C::Base> {
|
2021-05-03 22:13:04 -07:00
|
|
|
/// Variable representing an element of the elliptic curve's base field, that
|
|
|
|
/// is used as a scalar in variable-base scalar mul.
|
|
|
|
///
|
|
|
|
/// It is not true in general that a scalar field element fits in a curve's
|
|
|
|
/// base field, and in particular it is untrue for the Pallas curve, whose
|
|
|
|
/// scalar field `Fq` is larger than its base field `Fp`.
|
|
|
|
///
|
|
|
|
/// However, the only use of variable-base scalar mul in the Orchard protocol
|
|
|
|
/// is in deriving diversified addresses `[ivk] g_d`, and `ivk` is guaranteed
|
|
|
|
/// to be in the base field of the curve. (See non-normative notes in
|
|
|
|
/// https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents.)
|
|
|
|
type ScalarVar: Clone + Debug;
|
2021-05-18 01:07:40 -07:00
|
|
|
/// Variable representing a full-width element of the elliptic curve's
|
|
|
|
/// scalar field, to be used for fixed-base scalar mul.
|
2021-05-03 22:13:04 -07:00
|
|
|
type ScalarFixed: Clone + Debug;
|
2021-05-18 01:07:40 -07:00
|
|
|
/// Variable representing a signed short element of the elliptic curve's
|
|
|
|
/// scalar field, to be used for fixed-base scalar mul.
|
2021-05-17 21:03:40 -07:00
|
|
|
///
|
|
|
|
/// A `ScalarFixedShort` must be in the range [-(2^64 - 1), 2^64 - 1].
|
2021-05-03 22:13:04 -07:00
|
|
|
type ScalarFixedShort: Clone + Debug;
|
2021-02-25 10:11:46 -08:00
|
|
|
/// Variable representing an elliptic curve point.
|
2021-05-03 22:13:04 -07:00
|
|
|
type Point: Clone + Debug;
|
2021-05-18 01:07:40 -07:00
|
|
|
/// Variable representing the affine short Weierstrass x-coordinate of an
|
|
|
|
/// elliptic curve point.
|
2021-05-03 22:13:04 -07:00
|
|
|
type X: Clone + Debug;
|
2021-02-25 10:11:46 -08:00
|
|
|
/// Variable representing the set of fixed bases in the circuit.
|
2021-05-03 22:13:04 -07:00
|
|
|
type FixedPoints: Clone + Debug;
|
2021-05-18 01:07:40 -07:00
|
|
|
/// Variable representing the set of fixed bases to be used in scalar
|
|
|
|
/// multiplication with a short signed exponent.
|
2021-05-17 21:09:49 -07:00
|
|
|
type FixedPointsShort: Clone + Debug;
|
2021-02-25 10:11:46 -08:00
|
|
|
/// Variable representing a fixed elliptic curve point (constant in the circuit).
|
2021-05-03 22:13:04 -07:00
|
|
|
type FixedPoint: Clone + Debug;
|
2021-05-06 00:42:01 -07:00
|
|
|
/// Variable representing a fixed elliptic curve point (constant in the circuit)
|
|
|
|
/// to be used in scalar multiplication with a short signed exponent.
|
|
|
|
type FixedPointShort: Clone + Debug;
|
2021-05-03 22:13:04 -07:00
|
|
|
|
2021-05-18 01:07:40 -07:00
|
|
|
/// Witnesses the given base field element as a private input to the circuit
|
|
|
|
/// for variable-base scalar mul.
|
2021-05-03 22:13:04 -07:00
|
|
|
fn witness_scalar_var(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
|
|
|
value: Option<C::Base>,
|
|
|
|
) -> Result<Self::ScalarVar, Error>;
|
2021-02-25 10:11:46 -08:00
|
|
|
|
2021-05-18 01:07:40 -07:00
|
|
|
/// Witnesses the given full-width scalar as a private input to the circuit
|
|
|
|
/// for fixed-base scalar mul.
|
2021-04-26 08:17:08 -07:00
|
|
|
fn witness_scalar_fixed(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
|
|
|
value: Option<C::Scalar>,
|
|
|
|
) -> Result<Self::ScalarFixed, Error>;
|
|
|
|
|
2021-05-18 01:07:40 -07:00
|
|
|
/// Witnesses the given signed short scalar as a private input to the circuit
|
|
|
|
/// for fixed-base scalar mul.
|
2021-04-26 08:17:08 -07:00
|
|
|
fn witness_scalar_fixed_short(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
2021-02-25 10:11:46 -08:00
|
|
|
value: Option<C::Scalar>,
|
2021-04-26 08:17:08 -07:00
|
|
|
) -> Result<Self::ScalarFixedShort, Error>;
|
2021-02-25 10:11:46 -08:00
|
|
|
|
|
|
|
/// Witnesses the given point as a private input to the circuit.
|
|
|
|
fn witness_point(
|
2021-04-26 08:17:08 -07:00
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
2021-02-25 10:11:46 -08:00
|
|
|
value: Option<C>,
|
|
|
|
) -> Result<Self::Point, Error>;
|
|
|
|
|
2021-04-26 08:17:08 -07:00
|
|
|
/// Extracts the x-coordinate of a point.
|
|
|
|
fn extract_p(point: &Self::Point) -> &Self::X;
|
|
|
|
|
2021-05-03 22:13:04 -07:00
|
|
|
/// Returns a fixed point that had been previously loaded into the circuit.
|
|
|
|
/// The pre-loaded cells are used to set up equality constraints in other
|
|
|
|
/// parts of the circuit where the fixed base is used.
|
2021-04-26 08:17:08 -07:00
|
|
|
fn get_fixed(&self, fixed_points: Self::FixedPoints) -> Result<Self::FixedPoint, Error>;
|
2021-02-25 10:11:46 -08:00
|
|
|
|
2021-05-06 00:42:01 -07:00
|
|
|
/// Returns a fixed point to be used in scalar multiplication with a signed
|
|
|
|
/// short exponent.
|
|
|
|
fn get_fixed_short(
|
|
|
|
&self,
|
2021-05-17 21:09:49 -07:00
|
|
|
fixed_points: Self::FixedPointsShort,
|
2021-05-06 00:42:01 -07:00
|
|
|
) -> Result<Self::FixedPointShort, Error>;
|
|
|
|
|
2021-05-03 22:13:04 -07:00
|
|
|
/// Performs incomplete point addition, returning `a + b`.
|
2021-05-17 21:26:59 -07:00
|
|
|
///
|
|
|
|
/// This returns an error in exceptional cases.
|
2021-05-03 22:13:04 -07:00
|
|
|
fn add_incomplete(
|
2021-04-26 08:17:08 -07:00
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
2021-02-25 10:11:46 -08:00
|
|
|
a: &Self::Point,
|
|
|
|
b: &Self::Point,
|
|
|
|
) -> Result<Self::Point, Error>;
|
|
|
|
|
2021-04-26 08:17:08 -07:00
|
|
|
/// Performs complete point addition, returning `a + b`.
|
2021-05-03 22:13:04 -07:00
|
|
|
fn add(
|
2021-04-26 08:17:08 -07:00
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
|
|
|
a: &Self::Point,
|
|
|
|
b: &Self::Point,
|
|
|
|
) -> Result<Self::Point, Error>;
|
2021-02-25 10:11:46 -08:00
|
|
|
|
|
|
|
/// Performs variable-base scalar multiplication, returning `[scalar] base`.
|
|
|
|
fn mul(
|
2021-04-26 08:17:08 -07:00
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
2021-05-03 22:13:04 -07:00
|
|
|
scalar: &Self::ScalarVar,
|
2021-02-25 10:11:46 -08:00
|
|
|
base: &Self::Point,
|
|
|
|
) -> Result<Self::Point, Error>;
|
|
|
|
|
2021-04-26 08:17:08 -07:00
|
|
|
/// Performs fixed-base scalar multiplication using a full-width scalar, returning `[scalar] base`.
|
2021-02-25 10:11:46 -08:00
|
|
|
fn mul_fixed(
|
2021-04-26 08:17:08 -07:00
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
|
|
|
scalar: &Self::ScalarFixed,
|
|
|
|
base: &Self::FixedPoint,
|
|
|
|
) -> Result<Self::Point, Error>;
|
|
|
|
|
|
|
|
/// Performs fixed-base scalar multiplication using a short signed scalar, returning `[scalar] base`.
|
|
|
|
fn mul_fixed_short(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
|
|
|
scalar: &Self::ScalarFixedShort,
|
2021-05-06 00:42:01 -07:00
|
|
|
base: &Self::FixedPointShort,
|
2021-02-25 10:11:46 -08:00
|
|
|
) -> Result<Self::Point, Error>;
|
|
|
|
}
|
|
|
|
|
2021-05-03 22:13:04 -07:00
|
|
|
/// An element of the given elliptic curve's base field, that is used as a scalar
|
|
|
|
/// in variable-base scalar mul.
|
|
|
|
///
|
|
|
|
/// It is not true in general that a scalar field element fits in a curve's
|
|
|
|
/// base field, and in particular it is untrue for the Pallas curve, whose
|
|
|
|
/// scalar field `Fq` is larger than its base field `Fp`.
|
|
|
|
///
|
|
|
|
/// However, the only use of variable-base scalar mul in the Orchard protocol
|
|
|
|
/// is in deriving diversified addresses `[ivk] g_d`, and `ivk` is guaranteed
|
|
|
|
/// to be in the base field of the curve. (See non-normative notes in
|
|
|
|
/// https://zips.z.cash/protocol/nu5.pdf#orchardkeycomponents.)
|
|
|
|
#[derive(Debug)]
|
2021-05-18 01:12:06 -07:00
|
|
|
pub struct ScalarVar<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> {
|
2021-05-03 22:13:04 -07:00
|
|
|
chip: EccChip,
|
|
|
|
inner: EccChip::ScalarVar,
|
|
|
|
}
|
|
|
|
|
2021-05-18 01:12:06 -07:00
|
|
|
impl<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> ScalarVar<C, EccChip> {
|
2021-05-03 22:13:04 -07:00
|
|
|
/// Constructs a new ScalarVar with the given value.
|
|
|
|
pub fn new(
|
|
|
|
chip: EccChip,
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
value: Option<C::Base>,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
chip.witness_scalar_var(&mut layouter, value)
|
|
|
|
.map(|inner| ScalarVar { chip, inner })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:17:08 -07:00
|
|
|
/// A full-width element of the given elliptic curve's scalar field, to be used for fixed-base scalar mul.
|
2021-02-25 10:11:46 -08:00
|
|
|
#[derive(Debug)]
|
2021-05-18 01:12:06 -07:00
|
|
|
pub struct ScalarFixed<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> {
|
2021-04-26 08:17:08 -07:00
|
|
|
chip: EccChip,
|
|
|
|
inner: EccChip::ScalarFixed,
|
2021-02-25 10:11:46 -08:00
|
|
|
}
|
|
|
|
|
2021-05-18 01:12:06 -07:00
|
|
|
impl<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> ScalarFixed<C, EccChip> {
|
2021-04-26 08:17:08 -07:00
|
|
|
/// Constructs a new ScalarFixed with the given value.
|
|
|
|
pub fn new(
|
|
|
|
chip: EccChip,
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
value: Option<C::Scalar>,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
chip.witness_scalar_fixed(&mut layouter, value)
|
2021-05-03 22:13:04 -07:00
|
|
|
.map(|inner| ScalarFixed { chip, inner })
|
2021-04-26 08:17:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A signed short element of the given elliptic curve's scalar field, to be used for fixed-base scalar mul.
|
|
|
|
#[derive(Debug)]
|
2021-05-18 01:12:06 -07:00
|
|
|
pub struct ScalarFixedShort<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> {
|
2021-04-26 08:17:08 -07:00
|
|
|
chip: EccChip,
|
|
|
|
inner: EccChip::ScalarFixedShort,
|
|
|
|
}
|
|
|
|
|
2021-05-18 01:12:06 -07:00
|
|
|
impl<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq>
|
|
|
|
ScalarFixedShort<C, EccChip>
|
|
|
|
{
|
2021-04-26 08:17:08 -07:00
|
|
|
/// Constructs a new ScalarFixedShort with the given value.
|
2021-05-17 21:03:40 -07:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// The short scalar must be in the range [-(2^64 - 1), (2^64 - 1)].
|
2021-02-25 10:11:46 -08:00
|
|
|
pub fn new(
|
2021-04-26 08:17:08 -07:00
|
|
|
chip: EccChip,
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
2021-02-25 10:11:46 -08:00
|
|
|
value: Option<C::Scalar>,
|
|
|
|
) -> Result<Self, Error> {
|
2021-05-17 21:03:40 -07:00
|
|
|
// Check that the scalar is in the range [-(2^64 - 1), (2^64 - 1)]
|
|
|
|
if let Some(value) = value {
|
|
|
|
let mut sign = C::Scalar::one();
|
|
|
|
|
|
|
|
// T = (p-1) / 2
|
|
|
|
let t = (C::Scalar::zero() - C::Scalar::one()) * C::Scalar::TWO_INV;
|
|
|
|
|
|
|
|
if value > t {
|
|
|
|
sign = -sign;
|
|
|
|
}
|
|
|
|
let magnitude = value * sign;
|
|
|
|
assert!(magnitude < C::Scalar::from_u128(1 << 64));
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:17:08 -07:00
|
|
|
chip.witness_scalar_fixed_short(&mut layouter, value)
|
2021-05-03 22:13:04 -07:00
|
|
|
.map(|inner| ScalarFixedShort { chip, inner })
|
2021-02-25 10:11:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An elliptic curve point over the given curve.
|
|
|
|
#[derive(Debug)]
|
2021-05-18 01:12:06 -07:00
|
|
|
pub struct Point<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> {
|
2021-04-26 08:17:08 -07:00
|
|
|
chip: EccChip,
|
2021-02-25 10:11:46 -08:00
|
|
|
inner: EccChip::Point,
|
|
|
|
}
|
|
|
|
|
2021-05-18 01:12:06 -07:00
|
|
|
impl<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> Point<C, EccChip> {
|
2021-02-25 10:11:46 -08:00
|
|
|
/// Constructs a new point with the given value.
|
2021-04-26 08:17:08 -07:00
|
|
|
pub fn new(
|
2021-05-03 22:13:04 -07:00
|
|
|
chip: EccChip,
|
2021-04-26 08:17:08 -07:00
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
value: Option<C>,
|
|
|
|
) -> Result<Self, Error> {
|
2021-05-03 22:13:04 -07:00
|
|
|
let point = chip.witness_point(&mut layouter, value);
|
|
|
|
point.map(|inner| Point { chip, inner })
|
2021-02-25 10:11:46 -08:00
|
|
|
}
|
|
|
|
|
2021-04-26 08:17:08 -07:00
|
|
|
/// Extracts the x-coordinate of a point.
|
|
|
|
pub fn extract_p(&self) -> X<C, EccChip> {
|
|
|
|
X::from_inner(self.chip.clone(), EccChip::extract_p(&self.inner).clone())
|
2021-02-25 10:11:46 -08:00
|
|
|
}
|
|
|
|
|
2021-04-26 08:17:08 -07:00
|
|
|
/// Wraps the given point (obtained directly from an instruction) in a gadget.
|
|
|
|
pub fn from_inner(chip: EccChip, inner: EccChip::Point) -> Self {
|
|
|
|
Point { chip, inner }
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:50:50 -07:00
|
|
|
/// Returns `self + other` using complete addition.
|
2021-04-26 08:17:08 -07:00
|
|
|
pub fn add(&self, mut layouter: impl Layouter<C::Base>, other: &Self) -> Result<Self, Error> {
|
2021-05-18 01:12:06 -07:00
|
|
|
assert_eq!(self.chip, other.chip);
|
2021-04-26 08:17:08 -07:00
|
|
|
self.chip
|
|
|
|
.add(&mut layouter, &self.inner, &other.inner)
|
|
|
|
.map(|inner| Point {
|
|
|
|
chip: self.chip.clone(),
|
|
|
|
inner,
|
|
|
|
})
|
2021-02-25 10:11:46 -08:00
|
|
|
}
|
|
|
|
|
2021-05-05 21:50:50 -07:00
|
|
|
/// Returns `self + other` using incomplete addition.
|
|
|
|
pub fn add_incomplete(
|
|
|
|
&self,
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
other: &Self,
|
|
|
|
) -> Result<Self, Error> {
|
2021-05-18 01:12:06 -07:00
|
|
|
assert_eq!(self.chip, other.chip);
|
2021-05-05 21:50:50 -07:00
|
|
|
self.chip
|
|
|
|
.add_incomplete(&mut layouter, &self.inner, &other.inner)
|
|
|
|
.map(|inner| Point {
|
|
|
|
chip: self.chip.clone(),
|
|
|
|
inner,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:11:46 -08:00
|
|
|
/// Returns `[by] self`.
|
2021-05-03 22:13:04 -07:00
|
|
|
pub fn mul(
|
|
|
|
&self,
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
by: &ScalarVar<C, EccChip>,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
assert_eq!(format!("{:?}", self.chip), format!("{:?}", by.chip));
|
2021-04-26 08:17:08 -07:00
|
|
|
self.chip
|
2021-05-03 22:13:04 -07:00
|
|
|
.mul(&mut layouter, &by.inner, &self.inner)
|
2021-04-26 08:17:08 -07:00
|
|
|
.map(|inner| Point {
|
|
|
|
chip: self.chip.clone(),
|
|
|
|
inner,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The x-coordinate of an elliptic curve point over the given curve.
|
|
|
|
#[derive(Debug)]
|
2021-05-18 01:12:06 -07:00
|
|
|
pub struct X<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> {
|
2021-04-26 08:17:08 -07:00
|
|
|
chip: EccChip,
|
|
|
|
inner: EccChip::X,
|
|
|
|
}
|
|
|
|
|
2021-05-18 01:12:06 -07:00
|
|
|
impl<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> X<C, EccChip> {
|
2021-04-26 08:17:08 -07:00
|
|
|
/// Wraps the given x-coordinate (obtained directly from an instruction) in a gadget.
|
|
|
|
pub fn from_inner(chip: EccChip, inner: EccChip::X) -> Self {
|
|
|
|
X { chip, inner }
|
2021-02-25 10:11:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A constant elliptic curve point over the given curve, for which scalar multiplication
|
|
|
|
/// is more efficient.
|
2021-04-26 08:17:08 -07:00
|
|
|
#[derive(Clone, Debug)]
|
2021-05-18 01:12:06 -07:00
|
|
|
pub struct FixedPoint<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> {
|
2021-04-26 08:17:08 -07:00
|
|
|
chip: EccChip,
|
2021-02-25 10:11:46 -08:00
|
|
|
inner: EccChip::FixedPoint,
|
|
|
|
}
|
|
|
|
|
2021-05-18 01:12:06 -07:00
|
|
|
impl<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> FixedPoint<C, EccChip> {
|
2021-02-25 10:11:46 -08:00
|
|
|
/// Gets a reference to the specified fixed point in the circuit.
|
2021-04-26 08:17:08 -07:00
|
|
|
pub fn get(chip: EccChip, point: EccChip::FixedPoints) -> Result<Self, Error> {
|
2021-05-03 22:13:04 -07:00
|
|
|
chip.get_fixed(point)
|
|
|
|
.map(|inner| FixedPoint { chip, inner })
|
2021-02-25 10:11:46 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `[by] self`.
|
|
|
|
pub fn mul(
|
|
|
|
&self,
|
2021-04-26 08:17:08 -07:00
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
by: &ScalarFixed<C, EccChip>,
|
2021-02-25 10:11:46 -08:00
|
|
|
) -> Result<Point<C, EccChip>, Error> {
|
2021-05-03 22:13:04 -07:00
|
|
|
assert_eq!(format!("{:?}", self.chip), format!("{:?}", by.chip));
|
2021-04-26 08:17:08 -07:00
|
|
|
self.chip
|
|
|
|
.mul_fixed(&mut layouter, &by.inner, &self.inner)
|
|
|
|
.map(|inner| Point {
|
|
|
|
chip: self.chip.clone(),
|
|
|
|
inner,
|
|
|
|
})
|
2021-02-25 10:11:46 -08:00
|
|
|
}
|
|
|
|
}
|
2021-05-03 22:13:04 -07:00
|
|
|
|
|
|
|
/// A constant elliptic curve point over the given curve, used in scalar multiplication
|
|
|
|
/// with a short signed exponent
|
|
|
|
#[derive(Clone, Debug)]
|
2021-05-18 01:12:06 -07:00
|
|
|
pub struct FixedPointShort<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> {
|
2021-05-03 22:13:04 -07:00
|
|
|
chip: EccChip,
|
2021-05-06 00:42:01 -07:00
|
|
|
inner: EccChip::FixedPointShort,
|
2021-05-03 22:13:04 -07:00
|
|
|
}
|
|
|
|
|
2021-05-18 01:12:06 -07:00
|
|
|
impl<C: CurveAffine, EccChip: EccInstructions<C> + Clone + Debug + Eq> FixedPointShort<C, EccChip> {
|
2021-05-03 22:13:04 -07:00
|
|
|
/// Gets a reference to the specified fixed point in the circuit.
|
2021-05-17 21:09:49 -07:00
|
|
|
pub fn get(chip: EccChip, point: EccChip::FixedPointsShort) -> Result<Self, Error> {
|
2021-05-06 00:42:01 -07:00
|
|
|
chip.get_fixed_short(point)
|
2021-05-03 22:13:04 -07:00
|
|
|
.map(|inner| FixedPointShort { chip, inner })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `[by] self`.
|
|
|
|
pub fn mul(
|
|
|
|
&self,
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
by: &ScalarFixedShort<C, EccChip>,
|
|
|
|
) -> Result<Point<C, EccChip>, Error> {
|
|
|
|
assert_eq!(format!("{:?}", self.chip), format!("{:?}", by.chip));
|
|
|
|
self.chip
|
|
|
|
.mul_fixed_short(&mut layouter, &by.inner, &self.inner)
|
|
|
|
.map(|inner| Point {
|
|
|
|
chip: self.chip.clone(),
|
|
|
|
inner,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|