2021-06-04 23:05:58 -07:00
|
|
|
use super::EccInstructions;
|
|
|
|
use crate::circuit::gadget::utilities::{copy, CellValue, Var};
|
2021-06-11 21:32:31 -07:00
|
|
|
use crate::constants::{self, OrchardFixedBasesFull, ValueCommitV};
|
|
|
|
use arrayvec::ArrayVec;
|
2021-06-11 15:29:05 -07:00
|
|
|
|
|
|
|
use group::prime::PrimeCurveAffine;
|
2021-06-04 23:05:58 -07:00
|
|
|
use halo2::{
|
|
|
|
circuit::{Chip, Layouter},
|
|
|
|
plonk::{Advice, Column, ConstraintSystem, Error, Fixed, Permutation, Selector},
|
|
|
|
};
|
2021-06-11 15:29:05 -07:00
|
|
|
use pasta_curves::{arithmetic::CurveAffine, pallas};
|
2021-06-04 23:05:58 -07:00
|
|
|
|
2021-06-04 23:13:56 -07:00
|
|
|
pub(super) mod add;
|
2021-06-04 23:11:40 -07:00
|
|
|
pub(super) mod add_incomplete;
|
2021-06-04 23:05:58 -07:00
|
|
|
// pub(super) mod mul;
|
|
|
|
// pub(super) mod mul_fixed;
|
2021-06-04 23:08:51 -07:00
|
|
|
pub(super) mod witness_point;
|
2021-06-04 23:05:58 -07:00
|
|
|
// pub(super) mod witness_scalar_fixed;
|
|
|
|
|
|
|
|
/// A curve point represented in affine (x, y) coordinates. Each coordinate is
|
|
|
|
/// assigned to a cell.
|
|
|
|
#[derive(Clone, Debug)]
|
2021-06-11 15:29:05 -07:00
|
|
|
pub struct EccPoint {
|
2021-06-04 23:05:58 -07:00
|
|
|
/// x-coordinate
|
2021-06-11 15:29:05 -07:00
|
|
|
x: CellValue<pallas::Base>,
|
2021-06-04 23:05:58 -07:00
|
|
|
/// y-coordinate
|
2021-06-11 15:29:05 -07:00
|
|
|
y: CellValue<pallas::Base>,
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
impl EccPoint {
|
2021-06-18 23:59:09 -07:00
|
|
|
/// Constructs a point from its coordinates, without checking they are on the curve.
|
|
|
|
///
|
|
|
|
/// This is an internal API that we only use where we know we have a valid curve point
|
|
|
|
/// (specifically inside Sinsemilla).
|
|
|
|
pub(in crate::circuit::gadget) fn from_coordinates_unchecked(
|
|
|
|
x: CellValue<pallas::Base>,
|
|
|
|
y: CellValue<pallas::Base>,
|
|
|
|
) -> Self {
|
|
|
|
EccPoint { x, y }
|
|
|
|
}
|
|
|
|
|
2021-06-04 23:05:58 -07:00
|
|
|
/// Returns the value of this curve point, if known.
|
2021-06-11 15:29:05 -07:00
|
|
|
pub fn point(&self) -> Option<pallas::Affine> {
|
2021-06-04 23:05:58 -07:00
|
|
|
match (self.x.value(), self.y.value()) {
|
|
|
|
(Some(x), Some(y)) => {
|
2021-06-11 15:29:05 -07:00
|
|
|
if x == pallas::Base::zero() && y == pallas::Base::zero() {
|
|
|
|
Some(pallas::Affine::identity())
|
2021-06-04 23:05:58 -07:00
|
|
|
} else {
|
2021-06-11 15:29:05 -07:00
|
|
|
Some(pallas::Affine::from_xy(x, y).unwrap())
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 20:24:12 -07:00
|
|
|
/// The cell containing the affine short-Weierstrass x-coordinate,
|
|
|
|
/// or 0 for the zero point.
|
2021-06-11 15:29:05 -07:00
|
|
|
pub fn x(&self) -> CellValue<pallas::Base> {
|
2021-06-10 20:24:12 -07:00
|
|
|
self.x
|
|
|
|
}
|
|
|
|
/// The cell containing the affine short-Weierstrass y-coordinate,
|
|
|
|
/// or 0 for the zero point.
|
2021-06-11 15:29:05 -07:00
|
|
|
pub fn y(&self) -> CellValue<pallas::Base> {
|
2021-06-10 20:24:12 -07:00
|
|
|
self.y
|
|
|
|
}
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Configuration for the ECC chip
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
#[allow(non_snake_case)]
|
2021-06-11 15:29:05 -07:00
|
|
|
pub struct EccConfig {
|
2021-06-04 23:05:58 -07:00
|
|
|
/// Advice columns needed by instructions in the ECC chip.
|
|
|
|
pub advices: [Column<Advice>; 10],
|
|
|
|
|
|
|
|
/// Coefficients of interpolation polynomials for x-coordinates (used in fixed-base scalar multiplication)
|
|
|
|
pub lagrange_coeffs: [Column<Fixed>; constants::H],
|
|
|
|
/// Fixed z such that y + z = u^2 some square, and -y + z is a non-square. (Used in fixed-base scalar multiplication)
|
|
|
|
pub fixed_z: Column<Fixed>,
|
|
|
|
|
|
|
|
/// Incomplete addition
|
|
|
|
pub q_add_incomplete: Selector,
|
|
|
|
/// Complete addition
|
|
|
|
pub q_add: Selector,
|
|
|
|
/// Variable-base scalar multiplication (hi half)
|
|
|
|
pub q_mul_hi: Selector,
|
|
|
|
/// Variable-base scalar multiplication (lo half)
|
|
|
|
pub q_mul_lo: Selector,
|
|
|
|
/// Selector used in scalar decomposition for variable-base scalar mul
|
|
|
|
pub q_mul_decompose_var: Selector,
|
|
|
|
/// Variable-base scalar multiplication (final scalar)
|
|
|
|
pub q_mul_complete: Selector,
|
|
|
|
/// Fixed-base full-width scalar multiplication
|
|
|
|
pub q_mul_fixed: Selector,
|
|
|
|
/// Fixed-base signed short scalar multiplication
|
|
|
|
pub q_mul_fixed_short: Selector,
|
|
|
|
/// Witness point
|
|
|
|
pub q_point: Selector,
|
|
|
|
/// Witness full-width scalar for fixed-base scalar mul
|
|
|
|
pub q_scalar_fixed: Selector,
|
|
|
|
/// Witness signed short scalar for full-width fixed-base scalar mul
|
|
|
|
pub q_scalar_fixed_short: Selector,
|
|
|
|
/// Permutation
|
|
|
|
pub perm: Permutation,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A chip implementing EccInstructions
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2021-06-11 15:29:05 -07:00
|
|
|
pub struct EccChip {
|
|
|
|
config: EccConfig,
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
impl Chip<pallas::Base> for EccChip {
|
|
|
|
type Config = EccConfig;
|
2021-06-04 23:05:58 -07:00
|
|
|
type Loaded = ();
|
|
|
|
|
|
|
|
fn config(&self) -> &Self::Config {
|
|
|
|
&self.config
|
|
|
|
}
|
|
|
|
|
|
|
|
fn loaded(&self) -> &Self::Loaded {
|
|
|
|
&()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
impl EccChip {
|
|
|
|
pub fn construct(config: <Self as Chip<pallas::Base>>::Config) -> Self {
|
2021-06-10 10:09:04 -07:00
|
|
|
Self { config }
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub fn configure(
|
2021-06-11 15:29:05 -07:00
|
|
|
meta: &mut ConstraintSystem<pallas::Base>,
|
2021-06-04 23:05:58 -07:00
|
|
|
advices: [Column<Advice>; 10],
|
|
|
|
perm: Permutation,
|
2021-06-11 15:29:05 -07:00
|
|
|
) -> <Self as Chip<pallas::Base>>::Config {
|
|
|
|
let config = EccConfig {
|
2021-06-04 23:05:58 -07:00
|
|
|
advices,
|
|
|
|
lagrange_coeffs: [
|
|
|
|
meta.fixed_column(),
|
|
|
|
meta.fixed_column(),
|
|
|
|
meta.fixed_column(),
|
|
|
|
meta.fixed_column(),
|
|
|
|
meta.fixed_column(),
|
|
|
|
meta.fixed_column(),
|
|
|
|
meta.fixed_column(),
|
|
|
|
meta.fixed_column(),
|
|
|
|
],
|
|
|
|
fixed_z: meta.fixed_column(),
|
|
|
|
q_add_incomplete: meta.selector(),
|
|
|
|
q_add: meta.selector(),
|
|
|
|
q_mul_hi: meta.selector(),
|
|
|
|
q_mul_lo: meta.selector(),
|
|
|
|
q_mul_decompose_var: meta.selector(),
|
|
|
|
q_mul_complete: meta.selector(),
|
|
|
|
q_mul_fixed: meta.selector(),
|
|
|
|
q_mul_fixed_short: meta.selector(),
|
|
|
|
q_point: meta.selector(),
|
|
|
|
q_scalar_fixed: meta.selector(),
|
|
|
|
q_scalar_fixed_short: meta.selector(),
|
|
|
|
perm,
|
|
|
|
};
|
|
|
|
|
2021-06-04 23:08:51 -07:00
|
|
|
// Create witness point gate
|
|
|
|
{
|
2021-06-11 15:29:05 -07:00
|
|
|
let config: witness_point::Config = (&config).into();
|
2021-06-10 10:09:04 -07:00
|
|
|
config.create_gate(meta);
|
2021-06-04 23:08:51 -07:00
|
|
|
}
|
2021-06-04 23:05:58 -07:00
|
|
|
|
2021-06-04 23:11:40 -07:00
|
|
|
// Create incomplete point addition gate
|
|
|
|
{
|
2021-06-11 15:29:05 -07:00
|
|
|
let config: add_incomplete::Config = (&config).into();
|
2021-06-04 23:11:40 -07:00
|
|
|
config.create_gate(meta);
|
|
|
|
}
|
|
|
|
|
2021-06-04 23:13:56 -07:00
|
|
|
// Create complete point addition gate
|
|
|
|
{
|
2021-06-11 15:29:05 -07:00
|
|
|
let add_config: add::Config = (&config).into();
|
2021-06-04 23:13:56 -07:00
|
|
|
add_config.create_gate(meta);
|
|
|
|
}
|
|
|
|
|
2021-06-04 23:05:58 -07:00
|
|
|
config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 21:32:31 -07:00
|
|
|
/// A full-width scalar used for fixed-base scalar multiplication.
|
|
|
|
/// This is decomposed in chunks of `window_width` bits in little-endian order.
|
|
|
|
/// For example, if `window_width` = 3, we will have [k_0, k_1, ..., k_n]
|
|
|
|
/// where `scalar = k_0 + k_1 * (2^3) + ... + k_n * (2^3)^n` and each `k_i` is
|
|
|
|
/// in the range [0..2^3).
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct EccScalarFixed {
|
|
|
|
value: Option<pallas::Scalar>,
|
|
|
|
windows: ArrayVec<CellValue<pallas::Base>, { constants::NUM_WINDOWS }>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A signed short scalar used for fixed-base scalar multiplication.
|
|
|
|
/// This is decomposed in chunks of `window_width` bits in little-endian order.
|
|
|
|
/// For example, if `window_width` = 3, we will have [k_0, k_1, ..., k_n]
|
|
|
|
/// where `scalar = k_0 + k_1 * (2^3) + ... + k_n * (2^3)^n` and each `k_i` is
|
|
|
|
/// in the range [0..2^3).
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct EccScalarFixedShort {
|
|
|
|
magnitude: Option<pallas::Scalar>,
|
|
|
|
sign: CellValue<pallas::Base>,
|
|
|
|
windows: ArrayVec<CellValue<pallas::Base>, { constants::NUM_WINDOWS_SHORT }>,
|
|
|
|
}
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
impl EccInstructions<pallas::Affine> for EccChip {
|
2021-06-11 21:32:31 -07:00
|
|
|
type ScalarFixed = EccScalarFixed;
|
|
|
|
type ScalarFixedShort = EccScalarFixedShort;
|
|
|
|
type ScalarVar = CellValue<pallas::Base>;
|
2021-06-11 15:29:05 -07:00
|
|
|
type Point = EccPoint;
|
|
|
|
type X = CellValue<pallas::Base>;
|
2021-06-11 21:32:31 -07:00
|
|
|
type FixedPoints = OrchardFixedBasesFull;
|
|
|
|
type FixedPointsShort = ValueCommitV;
|
2021-06-04 23:05:58 -07:00
|
|
|
|
2021-06-13 06:26:30 -07:00
|
|
|
fn constrain_equal(
|
|
|
|
&self,
|
|
|
|
layouter: &mut impl Layouter<pallas::Base>,
|
|
|
|
a: &Self::Point,
|
|
|
|
b: &Self::Point,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let config = self.config().clone();
|
|
|
|
layouter.assign_region(
|
|
|
|
|| "constrain equal",
|
|
|
|
|mut region| {
|
|
|
|
// Constrain x-coordinates
|
|
|
|
region.constrain_equal(&config.perm, a.x().cell(), b.x().cell())?;
|
|
|
|
// Constrain x-coordinates
|
|
|
|
region.constrain_equal(&config.perm, a.y().cell(), b.y().cell())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-04 23:05:58 -07:00
|
|
|
fn witness_scalar_var(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
_layouter: &mut impl Layouter<pallas::Base>,
|
|
|
|
_value: Option<pallas::Base>,
|
2021-06-04 23:05:58 -07:00
|
|
|
) -> Result<Self::ScalarVar, Error> {
|
2021-06-10 10:09:04 -07:00
|
|
|
todo!()
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn witness_scalar_fixed(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
_layouter: &mut impl Layouter<pallas::Base>,
|
|
|
|
_value: Option<pallas::Scalar>,
|
2021-06-04 23:05:58 -07:00
|
|
|
) -> Result<Self::ScalarFixed, Error> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn witness_scalar_fixed_short(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
_layouter: &mut impl Layouter<pallas::Base>,
|
|
|
|
_value: Option<pallas::Scalar>,
|
2021-06-04 23:05:58 -07:00
|
|
|
) -> Result<Self::ScalarFixedShort, Error> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn witness_point(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
layouter: &mut impl Layouter<pallas::Base>,
|
|
|
|
value: Option<pallas::Affine>,
|
2021-06-04 23:05:58 -07:00
|
|
|
) -> Result<Self::Point, Error> {
|
2021-06-11 15:29:05 -07:00
|
|
|
let config: witness_point::Config = self.config().into();
|
2021-06-04 23:08:51 -07:00
|
|
|
layouter.assign_region(
|
|
|
|
|| "witness point",
|
|
|
|
|mut region| config.assign_region(value, 0, &mut region),
|
|
|
|
)
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_p(point: &Self::Point) -> &Self::X {
|
|
|
|
&point.x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_incomplete(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
layouter: &mut impl Layouter<pallas::Base>,
|
2021-06-04 23:05:58 -07:00
|
|
|
a: &Self::Point,
|
|
|
|
b: &Self::Point,
|
|
|
|
) -> Result<Self::Point, Error> {
|
2021-06-11 15:29:05 -07:00
|
|
|
let config: add_incomplete::Config = self.config().into();
|
2021-06-04 23:11:40 -07:00
|
|
|
layouter.assign_region(
|
|
|
|
|| "incomplete point addition",
|
|
|
|
|mut region| config.assign_region(a, b, 0, &mut region),
|
|
|
|
)
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
layouter: &mut impl Layouter<pallas::Base>,
|
2021-06-04 23:05:58 -07:00
|
|
|
a: &Self::Point,
|
|
|
|
b: &Self::Point,
|
|
|
|
) -> Result<Self::Point, Error> {
|
2021-06-11 15:29:05 -07:00
|
|
|
let config: add::Config = self.config().into();
|
2021-06-04 23:13:56 -07:00
|
|
|
layouter.assign_region(
|
|
|
|
|| "complete point addition",
|
|
|
|
|mut region| config.assign_region(a, b, 0, &mut region),
|
|
|
|
)
|
2021-06-04 23:05:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mul(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
_layouter: &mut impl Layouter<pallas::Base>,
|
2021-06-10 10:09:04 -07:00
|
|
|
_scalar: &Self::ScalarVar,
|
|
|
|
_base: &Self::Point,
|
2021-06-04 23:05:58 -07:00
|
|
|
) -> Result<Self::Point, Error> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mul_fixed(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
_layouter: &mut impl Layouter<pallas::Base>,
|
2021-06-10 10:09:04 -07:00
|
|
|
_scalar: &Self::ScalarFixed,
|
|
|
|
_base: &Self::FixedPoints,
|
2021-06-04 23:05:58 -07:00
|
|
|
) -> Result<Self::Point, Error> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mul_fixed_short(
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
_layouter: &mut impl Layouter<pallas::Base>,
|
2021-06-10 10:09:04 -07:00
|
|
|
_scalar: &Self::ScalarFixedShort,
|
|
|
|
_base: &Self::FixedPointsShort,
|
2021-06-04 23:05:58 -07:00
|
|
|
) -> Result<Self::Point, Error> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|