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,
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-18 02:41:13 -07:00
|
|
|
|
use crate::circuit::gadget::utilities::CellValue;
|
|
|
|
|
|
2021-06-04 23:05:58 -07:00
|
|
|
|
pub mod chip;
|
|
|
|
|
|
2021-02-25 10:11:46 -08:00
|
|
|
|
/// 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-06-04 22:59:48 -07:00
|
|
|
|
/// Enumeration of the set of fixed bases to be used in full-width scalar mul.
|
2021-05-03 22:13:04 -07:00
|
|
|
|
type FixedPoints: Clone + Debug;
|
2021-06-04 22:59:48 -07:00
|
|
|
|
/// Enumeration of the set of fixed bases to be used in short signed scalar mul.
|
2021-05-17 21:09:49 -07:00
|
|
|
|
type FixedPointsShort: Clone + Debug;
|
2021-05-03 22:13:04 -07:00
|
|
|
|
|
2021-06-13 06:26:30 -07:00
|
|
|
|
/// Constrains point `a` to be equal in value to point `b`.
|
|
|
|
|
fn constrain_equal(
|
|
|
|
|
&self,
|
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
|
|
|
|
a: &Self::Point,
|
|
|
|
|
b: &Self::Point,
|
|
|
|
|
) -> Result<(), Error>;
|
|
|
|
|
|
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.
|
2021-06-10 20:18:52 -07:00
|
|
|
|
/// This maps the identity to (0, 0) in affine coordinates.
|
2021-02-25 10:11:46 -08:00
|
|
|
|
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
|
|
|
|
/// 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`.
|
2021-06-04 22:59:48 -07:00
|
|
|
|
/// Multiplication of the identity `[scalar] 𝒪 ` returns an error.
|
2021-02-25 10:11:46 -08:00
|
|
|
|
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,
|
2021-06-04 22:59:48 -07:00
|
|
|
|
base: &Self::FixedPoints,
|
2021-04-26 08:17:08 -07:00
|
|
|
|
) -> 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-06-04 22:59:48 -07:00
|
|
|
|
base: &Self::FixedPointsShort,
|
2021-02-25 10:11:46 -08:00
|
|
|
|
) -> Result<Self::Point, Error>;
|
2021-06-18 02:41:13 -07:00
|
|
|
|
|
|
|
|
|
/// Performs fixed-base scalar multiplication using a base field element as the scalar.
|
|
|
|
|
/// In the current implementation, this base field element must be output from another
|
|
|
|
|
/// instruction.
|
|
|
|
|
fn mul_fixed_base_field_elem(
|
|
|
|
|
&self,
|
|
|
|
|
layouter: &mut impl Layouter<C::Base>,
|
|
|
|
|
base_field_elem: CellValue<C::Base>,
|
|
|
|
|
base: &Self::FixedPoints,
|
|
|
|
|
) -> Result<Self::Point, Error>;
|
2021-02-25 10:11:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
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-06-04 22:59:48 -07:00
|
|
|
|
pub struct ScalarFixed<C: CurveAffine, EccChip>
|
|
|
|
|
where
|
|
|
|
|
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-06-04 22:59:48 -07:00
|
|
|
|
impl<C: CurveAffine, EccChip> ScalarFixed<C, EccChip>
|
|
|
|
|
where
|
|
|
|
|
EccChip: EccInstructions<C> + Clone + Debug + Eq,
|
|
|
|
|
{
|
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-06-04 22:59:48 -07:00
|
|
|
|
pub struct ScalarFixedShort<C: CurveAffine, EccChip>
|
|
|
|
|
where
|
|
|
|
|
EccChip: EccInstructions<C> + Clone + Debug + Eq,
|
|
|
|
|
{
|
2021-04-26 08:17:08 -07:00
|
|
|
|
chip: EccChip,
|
|
|
|
|
inner: EccChip::ScalarFixedShort,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 22:59:48 -07:00
|
|
|
|
impl<C: CurveAffine, EccChip> ScalarFixedShort<C, EccChip>
|
|
|
|
|
where
|
|
|
|
|
EccChip: EccInstructions<C> + Clone + Debug + Eq,
|
2021-05-18 01:12:06 -07:00
|
|
|
|
{
|
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.
|
2021-06-04 22:59:48 -07:00
|
|
|
|
#[derive(Copy, Clone, 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-06-13 06:26:30 -07:00
|
|
|
|
/// Constrains this point to be equal in value to another point.
|
|
|
|
|
pub fn constrain_equal(
|
|
|
|
|
&self,
|
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
|
other: &Self,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
self.chip
|
|
|
|
|
.constrain_equal(&mut layouter, &self.inner, &other.inner)
|
|
|
|
|
}
|
|
|
|
|
|
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> {
|
2021-05-18 02:14:13 -07:00
|
|
|
|
assert_eq!(self.chip, 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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 01:45:43 -07:00
|
|
|
|
/// The affine short Weierstrass x-coordinate of an elliptic curve point over the
|
|
|
|
|
/// given curve.
|
2021-04-26 08:17:08 -07:00
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 02:14:13 -07:00
|
|
|
|
/// A constant elliptic curve point over the given curve, for which window tables have
|
|
|
|
|
/// been provided to make scalar multiplication more efficient.
|
2021-04-26 08:17:08 -07:00
|
|
|
|
#[derive(Clone, Debug)]
|
2021-06-04 22:59:48 -07:00
|
|
|
|
pub struct FixedPoint<C: CurveAffine, EccChip>
|
|
|
|
|
where
|
|
|
|
|
EccChip: EccInstructions<C> + Clone + Debug + Eq,
|
|
|
|
|
{
|
2021-04-26 08:17:08 -07:00
|
|
|
|
chip: EccChip,
|
2021-06-04 22:59:48 -07:00
|
|
|
|
inner: EccChip::FixedPoints,
|
2021-02-25 10:11:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 22:59:48 -07:00
|
|
|
|
impl<C: CurveAffine, EccChip> FixedPoint<C, EccChip>
|
|
|
|
|
where
|
|
|
|
|
EccChip: EccInstructions<C> + Clone + Debug + Eq,
|
|
|
|
|
{
|
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-18 02:14:13 -07:00
|
|
|
|
assert_eq!(self.chip, 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-06-04 22:59:48 -07:00
|
|
|
|
|
2021-06-18 02:41:13 -07:00
|
|
|
|
/// Multiplies `self` using a value encoded in a base field element
|
|
|
|
|
/// as the scalar.
|
|
|
|
|
pub fn mul_base_field_elem(
|
|
|
|
|
&self,
|
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
|
by: CellValue<C::Base>,
|
|
|
|
|
) -> Result<Point<C, EccChip>, Error> {
|
|
|
|
|
self.chip
|
|
|
|
|
.mul_fixed_base_field_elem(&mut layouter, by, &self.inner)
|
|
|
|
|
.map(|inner| Point {
|
|
|
|
|
chip: self.chip.clone(),
|
|
|
|
|
inner,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 22:59:48 -07:00
|
|
|
|
/// Wraps the given fixed base (obtained directly from an instruction) in a gadget.
|
|
|
|
|
pub fn from_inner(chip: EccChip, inner: EccChip::FixedPoints) -> Self {
|
|
|
|
|
FixedPoint { chip, 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-06-04 22:59:48 -07:00
|
|
|
|
pub struct FixedPointShort<C: CurveAffine, EccChip>
|
|
|
|
|
where
|
|
|
|
|
EccChip: EccInstructions<C> + Clone + Debug + Eq,
|
|
|
|
|
{
|
2021-05-03 22:13:04 -07:00
|
|
|
|
chip: EccChip,
|
2021-06-04 22:59:48 -07:00
|
|
|
|
inner: EccChip::FixedPointsShort,
|
2021-05-03 22:13:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 22:59:48 -07:00
|
|
|
|
impl<C: CurveAffine, EccChip> FixedPointShort<C, EccChip>
|
|
|
|
|
where
|
|
|
|
|
EccChip: EccInstructions<C> + Clone + Debug + Eq,
|
|
|
|
|
{
|
2021-05-03 22:13:04 -07:00
|
|
|
|
/// Returns `[by] self`.
|
|
|
|
|
pub fn mul(
|
|
|
|
|
&self,
|
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
|
by: &ScalarFixedShort<C, EccChip>,
|
|
|
|
|
) -> Result<Point<C, EccChip>, Error> {
|
2021-05-18 02:14:13 -07:00
|
|
|
|
assert_eq!(self.chip, by.chip);
|
2021-05-03 22:13:04 -07:00
|
|
|
|
self.chip
|
|
|
|
|
.mul_fixed_short(&mut layouter, &by.inner, &self.inner)
|
|
|
|
|
.map(|inner| Point {
|
|
|
|
|
chip: self.chip.clone(),
|
|
|
|
|
inner,
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-06-04 22:59:48 -07:00
|
|
|
|
|
|
|
|
|
/// Wraps the given fixed base (obtained directly from an instruction) in a gadget.
|
|
|
|
|
pub fn from_inner(chip: EccChip, inner: EccChip::FixedPointsShort) -> Self {
|
|
|
|
|
FixedPointShort { chip, inner }
|
|
|
|
|
}
|
2021-05-03 22:13:04 -07:00
|
|
|
|
}
|
2021-06-10 10:07:56 -07:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2021-06-11 15:29:05 -07:00
|
|
|
|
use group::{prime::PrimeCurveAffine, Curve, Group};
|
|
|
|
|
|
2021-06-10 10:07:56 -07:00
|
|
|
|
use halo2::{
|
|
|
|
|
circuit::{layouter::SingleChipLayouter, Layouter},
|
|
|
|
|
dev::MockProver,
|
|
|
|
|
plonk::{Assignment, Circuit, ConstraintSystem, Error},
|
|
|
|
|
};
|
2021-06-11 15:29:05 -07:00
|
|
|
|
use pasta_curves::pallas;
|
2021-06-10 10:07:56 -07:00
|
|
|
|
|
|
|
|
|
use super::chip::{EccChip, EccConfig};
|
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
|
struct MyCircuit {}
|
2021-06-10 10:07:56 -07:00
|
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
2021-06-11 15:29:05 -07:00
|
|
|
|
impl Circuit<pallas::Base> for MyCircuit {
|
|
|
|
|
type Config = EccConfig;
|
2021-06-10 10:07:56 -07:00
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
|
fn configure(meta: &mut ConstraintSystem<pallas::Base>) -> Self::Config {
|
2021-06-10 10:07:56 -07:00
|
|
|
|
let advices = [
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
meta.advice_column(),
|
|
|
|
|
];
|
|
|
|
|
|
2021-06-13 06:06:34 -07:00
|
|
|
|
let lookup_table = meta.fixed_column();
|
2021-06-10 10:07:56 -07:00
|
|
|
|
let perm = meta.permutation(
|
|
|
|
|
&advices
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|advice| (*advice).into())
|
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
|
);
|
|
|
|
|
|
2021-06-13 06:06:34 -07:00
|
|
|
|
EccChip::configure(meta, advices, lookup_table, perm)
|
2021-06-10 10:07:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn synthesize(
|
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
|
cs: &mut impl Assignment<pallas::Base>,
|
2021-06-10 10:07:56 -07:00
|
|
|
|
config: Self::Config,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
let mut layouter = SingleChipLayouter::new(cs)?;
|
2021-06-13 06:06:34 -07:00
|
|
|
|
let chip = EccChip::construct(config.clone());
|
|
|
|
|
|
|
|
|
|
// Load 10-bit lookup table. In the Action circuit, this will be
|
|
|
|
|
// provided by the Sinsemilla chip.
|
|
|
|
|
config.lookup_config.load(&mut layouter)?;
|
2021-06-10 10:07:56 -07:00
|
|
|
|
|
|
|
|
|
// Generate a random point P
|
2021-06-11 15:29:05 -07:00
|
|
|
|
let p_val = pallas::Point::random(rand::rngs::OsRng).to_affine(); // P
|
2021-06-11 15:36:26 -07:00
|
|
|
|
let p = super::Point::new(chip.clone(), layouter.namespace(|| "P"), Some(p_val))?;
|
2021-06-10 10:07:56 -07:00
|
|
|
|
let p_neg = -p_val;
|
2021-06-11 15:36:26 -07:00
|
|
|
|
let p_neg = super::Point::new(chip.clone(), layouter.namespace(|| "-P"), Some(p_neg))?;
|
2021-06-10 10:07:56 -07:00
|
|
|
|
|
|
|
|
|
// Generate a random point Q
|
2021-06-11 15:29:05 -07:00
|
|
|
|
let q_val = pallas::Point::random(rand::rngs::OsRng).to_affine(); // Q
|
2021-06-11 15:36:26 -07:00
|
|
|
|
let q = super::Point::new(chip.clone(), layouter.namespace(|| "Q"), Some(q_val))?;
|
2021-06-10 10:07:56 -07:00
|
|
|
|
|
|
|
|
|
// Make sure P and Q are not the same point.
|
|
|
|
|
assert_ne!(p_val, q_val);
|
|
|
|
|
|
|
|
|
|
// Generate a (0,0) point to be used in other tests.
|
2021-06-10 20:18:52 -07:00
|
|
|
|
let zero = {
|
|
|
|
|
super::Point::new(
|
|
|
|
|
chip.clone(),
|
|
|
|
|
layouter.namespace(|| "identity"),
|
2021-06-11 15:29:05 -07:00
|
|
|
|
Some(pallas::Affine::identity()),
|
2021-06-10 20:18:52 -07:00
|
|
|
|
)?
|
|
|
|
|
};
|
2021-06-10 10:07:56 -07:00
|
|
|
|
|
|
|
|
|
// Test complete addition
|
|
|
|
|
{
|
|
|
|
|
super::chip::add::tests::test_add(
|
2021-06-13 09:19:21 -07:00
|
|
|
|
chip.clone(),
|
2021-06-10 10:07:56 -07:00
|
|
|
|
layouter.namespace(|| "complete addition"),
|
|
|
|
|
&zero,
|
|
|
|
|
p_val,
|
|
|
|
|
&p,
|
|
|
|
|
q_val,
|
|
|
|
|
&q,
|
|
|
|
|
&p_neg,
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test incomplete addition
|
|
|
|
|
{
|
|
|
|
|
super::chip::add_incomplete::tests::test_add_incomplete(
|
2021-06-13 18:56:34 -07:00
|
|
|
|
chip.clone(),
|
2021-06-10 10:07:56 -07:00
|
|
|
|
layouter.namespace(|| "incomplete addition"),
|
|
|
|
|
&zero,
|
2021-06-13 09:19:21 -07:00
|
|
|
|
p_val,
|
2021-06-10 10:07:56 -07:00
|
|
|
|
&p,
|
2021-06-13 09:19:21 -07:00
|
|
|
|
q_val,
|
2021-06-10 10:07:56 -07:00
|
|
|
|
&q,
|
|
|
|
|
&p_neg,
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 18:56:34 -07:00
|
|
|
|
// Test variable-base scalar multiplication
|
|
|
|
|
{
|
|
|
|
|
super::chip::mul::tests::test_mul(
|
|
|
|
|
chip.clone(),
|
|
|
|
|
layouter.namespace(|| "variable-base scalar mul"),
|
|
|
|
|
&zero,
|
|
|
|
|
&p,
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test full-width fixed-base scalar multiplication
|
|
|
|
|
{
|
|
|
|
|
super::chip::mul_fixed::full_width::tests::test_mul_fixed(
|
|
|
|
|
chip.clone(),
|
|
|
|
|
layouter.namespace(|| "full-width fixed-base scalar mul"),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test signed short fixed-base scalar multiplication
|
|
|
|
|
{
|
|
|
|
|
super::chip::mul_fixed::short::tests::test_mul_fixed_short(
|
2021-06-18 02:41:13 -07:00
|
|
|
|
chip.clone(),
|
2021-06-13 18:56:34 -07:00
|
|
|
|
layouter.namespace(|| "signed short fixed-base scalar mul"),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 02:41:13 -07:00
|
|
|
|
// Test fixed-base scalar multiplication with a base field element
|
|
|
|
|
{
|
|
|
|
|
super::chip::mul_fixed::base_field_elem::tests::test_mul_fixed_base_field(
|
|
|
|
|
chip,
|
|
|
|
|
layouter.namespace(|| "fixed-base scalar mul with base field element"),
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 10:07:56 -07:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn ecc() {
|
2021-06-18 02:41:13 -07:00
|
|
|
|
let k = 13;
|
2021-06-11 15:29:05 -07:00
|
|
|
|
let circuit = MyCircuit {};
|
2021-06-10 10:07:56 -07:00
|
|
|
|
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
|
|
|
|
|
assert_eq!(prover.verify(), Ok(()))
|
|
|
|
|
}
|
|
|
|
|
}
|