2021-06-10 10:09:04 -07:00
|
|
|
|
use std::{array, marker::PhantomData};
|
2021-06-04 23:11:40 -07:00
|
|
|
|
|
|
|
|
|
use super::{copy, CellValue, EccConfig, EccPoint, Var};
|
|
|
|
|
use ff::Field;
|
|
|
|
|
use group::Curve;
|
|
|
|
|
use halo2::{
|
2021-06-10 10:09:04 -07:00
|
|
|
|
arithmetic::CurveAffine,
|
2021-06-04 23:11:40 -07:00
|
|
|
|
circuit::Region,
|
|
|
|
|
plonk::{Advice, Column, ConstraintSystem, Error, Permutation, Selector},
|
|
|
|
|
poly::Rotation,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2021-06-10 10:09:04 -07:00
|
|
|
|
pub struct Config<C: CurveAffine> {
|
2021-06-04 23:11:40 -07:00
|
|
|
|
q_add_incomplete: Selector,
|
|
|
|
|
// x-coordinate of P in P + Q = R
|
|
|
|
|
pub x_p: Column<Advice>,
|
|
|
|
|
// y-coordinate of P in P + Q = R
|
|
|
|
|
pub y_p: Column<Advice>,
|
|
|
|
|
// x-coordinate of Q or R in P + Q = R
|
|
|
|
|
pub x_qr: Column<Advice>,
|
|
|
|
|
// y-coordinate of Q or R in P + Q = R
|
|
|
|
|
pub y_qr: Column<Advice>,
|
|
|
|
|
// Permutation
|
|
|
|
|
perm: Permutation,
|
2021-06-10 10:09:04 -07:00
|
|
|
|
_marker: PhantomData<C>,
|
2021-06-04 23:11:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 10:09:04 -07:00
|
|
|
|
impl<C: CurveAffine> From<&EccConfig<C>> for Config<C> {
|
|
|
|
|
fn from(ecc_config: &EccConfig<C>) -> Self {
|
2021-06-04 23:11:40 -07:00
|
|
|
|
Self {
|
|
|
|
|
q_add_incomplete: ecc_config.q_add_incomplete,
|
|
|
|
|
x_p: ecc_config.advices[0],
|
|
|
|
|
y_p: ecc_config.advices[1],
|
|
|
|
|
x_qr: ecc_config.advices[2],
|
|
|
|
|
y_qr: ecc_config.advices[3],
|
|
|
|
|
perm: ecc_config.perm.clone(),
|
2021-06-10 10:09:04 -07:00
|
|
|
|
_marker: PhantomData,
|
2021-06-04 23:11:40 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 10:09:04 -07:00
|
|
|
|
impl<C: CurveAffine> Config<C> {
|
|
|
|
|
pub(super) fn create_gate(&self, meta: &mut ConstraintSystem<C::Base>) {
|
2021-06-04 23:11:40 -07:00
|
|
|
|
meta.create_gate("incomplete addition gates", |meta| {
|
|
|
|
|
let q_add_incomplete = meta.query_selector(self.q_add_incomplete);
|
|
|
|
|
let x_p = meta.query_advice(self.x_p, Rotation::cur());
|
|
|
|
|
let y_p = meta.query_advice(self.y_p, Rotation::cur());
|
|
|
|
|
let x_q = meta.query_advice(self.x_qr, Rotation::cur());
|
|
|
|
|
let y_q = meta.query_advice(self.y_qr, Rotation::cur());
|
|
|
|
|
let x_r = meta.query_advice(self.x_qr, Rotation::next());
|
|
|
|
|
let y_r = meta.query_advice(self.y_qr, Rotation::next());
|
|
|
|
|
|
|
|
|
|
// (x_r + x_q + x_p)⋅(x_p − x_q)^2 − (y_p − y_q)^2 = 0
|
|
|
|
|
let poly1 = {
|
|
|
|
|
(x_r.clone() + x_q.clone() + x_p.clone())
|
|
|
|
|
* (x_p.clone() - x_q.clone())
|
|
|
|
|
* (x_p.clone() - x_q.clone())
|
|
|
|
|
- (y_p.clone() - y_q.clone()) * (y_p.clone() - y_q.clone())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// (y_r + y_q)(x_p − x_q) − (y_p − y_q)(x_q − x_r) = 0
|
|
|
|
|
let poly2 = (y_r + y_q.clone()) * (x_p - x_q.clone()) - (y_p - y_q) * (x_q - x_r);
|
|
|
|
|
|
|
|
|
|
array::IntoIter::new([poly1, poly2]).map(move |poly| q_add_incomplete.clone() * poly)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 10:09:04 -07:00
|
|
|
|
pub(super) fn assign_region(
|
2021-06-04 23:11:40 -07:00
|
|
|
|
&self,
|
|
|
|
|
p: &EccPoint<C>,
|
|
|
|
|
q: &EccPoint<C>,
|
|
|
|
|
offset: usize,
|
|
|
|
|
region: &mut Region<'_, C::Base>,
|
|
|
|
|
) -> Result<EccPoint<C>, Error> {
|
|
|
|
|
// Enable `q_add_incomplete` selector
|
|
|
|
|
self.q_add_incomplete.enable(region, offset)?;
|
|
|
|
|
|
|
|
|
|
// Handle exceptional cases
|
|
|
|
|
let (x_p, y_p) = (p.x.value(), p.y.value());
|
|
|
|
|
let (x_q, y_q) = (q.x.value(), q.y.value());
|
|
|
|
|
x_p.zip(y_p)
|
|
|
|
|
.zip(x_q)
|
|
|
|
|
.zip(y_q)
|
|
|
|
|
.map(|(((x_p, y_p), x_q), y_q)| {
|
|
|
|
|
// P is point at infinity
|
|
|
|
|
if (x_p == C::Base::zero() && y_p == C::Base::zero())
|
|
|
|
|
// Q is point at infinity
|
|
|
|
|
|| (x_q == C::Base::zero() && y_q == C::Base::zero())
|
|
|
|
|
// x_p = x_q
|
|
|
|
|
|| (x_p == x_q)
|
|
|
|
|
{
|
|
|
|
|
Err(Error::SynthesisError)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.transpose()?;
|
|
|
|
|
|
|
|
|
|
// Copy point `p` into `x_p`, `y_p` columns
|
|
|
|
|
copy(region, || "x_p", self.x_p, offset, &p.x, &self.perm)?;
|
|
|
|
|
copy(region, || "y_p", self.y_p, offset, &p.y, &self.perm)?;
|
|
|
|
|
|
|
|
|
|
// Copy point `q` into `x_qr`, `y_qr` columns
|
|
|
|
|
copy(region, || "x_q", self.x_qr, offset, &q.x, &self.perm)?;
|
|
|
|
|
copy(region, || "y_q", self.y_qr, offset, &q.y, &self.perm)?;
|
|
|
|
|
|
|
|
|
|
// Compute the sum `P + Q = R`
|
|
|
|
|
let r = {
|
|
|
|
|
let p = p.point();
|
|
|
|
|
let q = q.point();
|
|
|
|
|
let r = p
|
|
|
|
|
.zip(q)
|
|
|
|
|
.map(|(p, q)| (p + q).to_affine().coordinates().unwrap());
|
|
|
|
|
let r_x = r.map(|r| *r.x());
|
|
|
|
|
let r_y = r.map(|r| *r.y());
|
|
|
|
|
|
|
|
|
|
(r_x, r_y)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Assign the sum to `x_qr`, `y_qr` columns in the next row
|
|
|
|
|
let x_r = r.0;
|
|
|
|
|
let x_r_var = region.assign_advice(
|
|
|
|
|
|| "x_r",
|
|
|
|
|
self.x_qr,
|
|
|
|
|
offset + 1,
|
|
|
|
|
|| x_r.ok_or(Error::SynthesisError),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let y_r = r.1;
|
|
|
|
|
let y_r_var = region.assign_advice(
|
|
|
|
|
|| "y_r",
|
|
|
|
|
self.y_qr,
|
|
|
|
|
offset + 1,
|
|
|
|
|
|| y_r.ok_or(Error::SynthesisError),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
let result = EccPoint::<C> {
|
|
|
|
|
x: CellValue::<C::Base>::new(x_r_var, x_r),
|
|
|
|
|
y: CellValue::<C::Base>::new(y_r_var, y_r),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
pub mod tests {
|
|
|
|
|
use halo2::{arithmetic::CurveAffine, circuit::Layouter, plonk::Error};
|
|
|
|
|
|
|
|
|
|
use crate::circuit::gadget::ecc::{EccInstructions, Point};
|
|
|
|
|
|
|
|
|
|
pub fn test_add_incomplete<
|
|
|
|
|
C: CurveAffine,
|
|
|
|
|
EccChip: EccInstructions<C> + Clone + Eq + std::fmt::Debug,
|
|
|
|
|
>(
|
|
|
|
|
mut layouter: impl Layouter<C::Base>,
|
|
|
|
|
zero: &Point<C, EccChip>,
|
|
|
|
|
p: &Point<C, EccChip>,
|
|
|
|
|
q: &Point<C, EccChip>,
|
|
|
|
|
p_neg: &Point<C, EccChip>,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
// P + Q
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add_incomplete(layouter.namespace(|| "P + Q"), q)?;
|
2021-06-04 23:11:40 -07:00
|
|
|
|
|
|
|
|
|
// P + P should return an error
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add_incomplete(layouter.namespace(|| "P + P"), p)
|
2021-06-04 23:11:40 -07:00
|
|
|
|
.expect_err("P + P should return an error");
|
|
|
|
|
|
|
|
|
|
// P + (-P) should return an error
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add_incomplete(layouter.namespace(|| "P + (-P)"), p_neg)
|
2021-06-04 23:11:40 -07:00
|
|
|
|
.expect_err("P + (-P) should return an error");
|
|
|
|
|
|
|
|
|
|
// P + 𝒪 should return an error
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add_incomplete(layouter.namespace(|| "P + 𝒪"), zero)
|
2021-06-04 23:11:40 -07:00
|
|
|
|
.expect_err("P + 0 should return an error");
|
|
|
|
|
|
|
|
|
|
// 𝒪 + P should return an error
|
2021-06-11 15:36:26 -07:00
|
|
|
|
zero.add_incomplete(layouter.namespace(|| "𝒪 + P"), p)
|
2021-06-04 23:11:40 -07:00
|
|
|
|
.expect_err("0 + P should return an error");
|
|
|
|
|
|
|
|
|
|
// 𝒪 + 𝒪 should return an error
|
2021-06-11 15:36:26 -07:00
|
|
|
|
zero.add_incomplete(layouter.namespace(|| "𝒪 + 𝒪"), zero)
|
2021-06-04 23:11:40 -07:00
|
|
|
|
.expect_err("𝒪 + 𝒪 should return an error");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|