2021-06-04 23:13:56 -07:00
|
|
|
|
use std::array;
|
|
|
|
|
|
|
|
|
|
use super::{copy, CellValue, EccConfig, EccPoint, Var};
|
|
|
|
|
use ff::Field;
|
|
|
|
|
use halo2::{
|
2021-06-11 15:29:05 -07:00
|
|
|
|
arithmetic::BatchInvert,
|
2021-06-04 23:13:56 -07:00
|
|
|
|
circuit::Region,
|
|
|
|
|
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Permutation, Selector},
|
|
|
|
|
poly::Rotation,
|
|
|
|
|
};
|
2021-06-11 15:29:05 -07:00
|
|
|
|
use pasta_curves::{arithmetic::FieldExt, pallas};
|
|
|
|
|
use std::collections::HashSet;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2021-06-11 15:29:05 -07:00
|
|
|
|
pub struct Config {
|
2021-06-04 23:13:56 -07:00
|
|
|
|
q_add: Selector,
|
|
|
|
|
// lambda
|
|
|
|
|
lambda: Column<Advice>,
|
|
|
|
|
// 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>,
|
|
|
|
|
// α = inv0(x_q - x_p)
|
|
|
|
|
alpha: Column<Advice>,
|
|
|
|
|
// β = inv0(x_p)
|
|
|
|
|
beta: Column<Advice>,
|
|
|
|
|
// γ = inv0(x_q)
|
|
|
|
|
gamma: Column<Advice>,
|
|
|
|
|
// δ = inv0(y_p + y_q) if x_q = x_p, 0 otherwise
|
|
|
|
|
delta: Column<Advice>,
|
|
|
|
|
// Permutation
|
|
|
|
|
perm: Permutation,
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
|
impl From<&EccConfig> for Config {
|
|
|
|
|
fn from(ecc_config: &EccConfig) -> Self {
|
2021-06-04 23:13:56 -07:00
|
|
|
|
Self {
|
|
|
|
|
q_add: ecc_config.q_add,
|
|
|
|
|
x_p: ecc_config.advices[0],
|
|
|
|
|
y_p: ecc_config.advices[1],
|
|
|
|
|
x_qr: ecc_config.advices[2],
|
|
|
|
|
y_qr: ecc_config.advices[3],
|
|
|
|
|
lambda: ecc_config.advices[4],
|
|
|
|
|
alpha: ecc_config.advices[5],
|
|
|
|
|
beta: ecc_config.advices[6],
|
|
|
|
|
gamma: ecc_config.advices[7],
|
|
|
|
|
delta: ecc_config.advices[8],
|
|
|
|
|
perm: ecc_config.perm.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
|
impl Config {
|
2021-06-04 23:13:56 -07:00
|
|
|
|
pub(crate) fn advice_columns(&self) -> HashSet<Column<Advice>> {
|
|
|
|
|
core::array::IntoIter::new([
|
|
|
|
|
self.x_p,
|
|
|
|
|
self.y_p,
|
|
|
|
|
self.x_qr,
|
|
|
|
|
self.y_qr,
|
|
|
|
|
self.lambda,
|
|
|
|
|
self.alpha,
|
|
|
|
|
self.beta,
|
|
|
|
|
self.gamma,
|
|
|
|
|
self.delta,
|
|
|
|
|
])
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
|
pub(crate) fn create_gate(&self, meta: &mut ConstraintSystem<pallas::Base>) {
|
2021-06-04 23:13:56 -07:00
|
|
|
|
meta.create_gate("complete addition gates", |meta| {
|
|
|
|
|
let q_add = meta.query_selector(self.q_add);
|
|
|
|
|
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());
|
|
|
|
|
let lambda = meta.query_advice(self.lambda, Rotation::cur());
|
|
|
|
|
|
|
|
|
|
// α = inv0(x_q - x_p)
|
|
|
|
|
let alpha = meta.query_advice(self.alpha, Rotation::cur());
|
|
|
|
|
// β = inv0(x_p)
|
|
|
|
|
let beta = meta.query_advice(self.beta, Rotation::cur());
|
|
|
|
|
// γ = inv0(x_q)
|
|
|
|
|
let gamma = meta.query_advice(self.gamma, Rotation::cur());
|
|
|
|
|
// δ = inv0(y_p + y_q) if x_q = x_p, 0 otherwise
|
|
|
|
|
let delta = meta.query_advice(self.delta, Rotation::cur());
|
|
|
|
|
|
|
|
|
|
// Useful composite expressions
|
|
|
|
|
// α ⋅(x_q - x_p)
|
|
|
|
|
let if_alpha = (x_q.clone() - x_p.clone()) * alpha;
|
|
|
|
|
// β ⋅ x_p
|
|
|
|
|
let if_beta = x_p.clone() * beta;
|
|
|
|
|
// γ ⋅ x_q
|
|
|
|
|
let if_gamma = x_q.clone() * gamma;
|
|
|
|
|
// δ ⋅(y_p + y_q)
|
|
|
|
|
let if_delta = (y_q.clone() + y_p.clone()) * delta;
|
|
|
|
|
|
|
|
|
|
// Useful constants
|
2021-06-11 15:29:05 -07:00
|
|
|
|
let one = Expression::Constant(pallas::Base::one());
|
|
|
|
|
let two = Expression::Constant(pallas::Base::from_u64(2));
|
|
|
|
|
let three = Expression::Constant(pallas::Base::from_u64(3));
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
// (x_q − x_p)⋅((x_q − x_p)⋅λ − (y_q−y_p)) = 0
|
|
|
|
|
let poly1 = {
|
|
|
|
|
let x_q_minus_x_p = x_q.clone() - x_p.clone(); // (x_q − x_p)
|
|
|
|
|
|
|
|
|
|
let y_q_minus_y_p = y_q.clone() - y_p.clone(); // (y_q − y_p)
|
|
|
|
|
let incomplete = x_q_minus_x_p.clone() * lambda.clone() - y_q_minus_y_p; // (x_q − x_p)⋅λ − (y_q−y_p)
|
|
|
|
|
|
|
|
|
|
// q_add ⋅(x_q − x_p)⋅((x_q − x_p)⋅λ − (y_q−y_p))
|
|
|
|
|
x_q_minus_x_p * incomplete
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// (1 - (x_q - x_p)⋅α)⋅(2y_p ⋅λ - 3x_p^2) = 0
|
|
|
|
|
let poly2 = {
|
2021-06-11 15:59:02 -07:00
|
|
|
|
let three_x_p_sq = three * x_p.clone().square(); // 3x_p^2
|
2021-06-04 23:13:56 -07:00
|
|
|
|
let two_y_p = two * y_p.clone(); // 2y_p
|
|
|
|
|
let tangent_line = two_y_p * lambda.clone() - three_x_p_sq; // (2y_p ⋅λ - 3x_p^2)
|
|
|
|
|
|
|
|
|
|
// q_add ⋅(1 - (x_q - x_p)⋅α)⋅(2y_p ⋅λ - 3x_p^2)
|
|
|
|
|
(one.clone() - if_alpha.clone()) * tangent_line
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// x_p⋅x_q⋅(x_q - x_p)⋅(λ^2 - x_p - x_q - x_r) = 0
|
2021-06-11 15:59:02 -07:00
|
|
|
|
let secant_line = lambda.clone().square() - x_p.clone() - x_q.clone() - x_r.clone(); // (λ^2 - x_p - x_q - x_r)
|
2021-06-04 23:13:56 -07:00
|
|
|
|
let poly3 = {
|
|
|
|
|
let x_q_minus_x_p = x_q.clone() - x_p.clone(); // (x_q - x_p)
|
|
|
|
|
|
|
|
|
|
// x_p⋅x_q⋅(x_q - x_p)⋅(λ^2 - x_p - x_q - x_r)
|
2021-06-11 15:59:02 -07:00
|
|
|
|
x_p.clone() * x_q.clone() * x_q_minus_x_p * secant_line.clone()
|
2021-06-04 23:13:56 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// x_p⋅x_q⋅(x_q - x_p)⋅(λ ⋅(x_p - x_r) - y_p - y_r) = 0
|
|
|
|
|
let poly4 = {
|
|
|
|
|
let x_q_minus_x_p = x_q.clone() - x_p.clone(); // (x_q - x_p)
|
|
|
|
|
let x_p_minus_x_r = x_p.clone() - x_r.clone(); // (x_p - x_r)
|
|
|
|
|
|
|
|
|
|
// x_p⋅x_q⋅(x_q - x_p)⋅(λ ⋅(x_p - x_r) - y_p - y_r)
|
|
|
|
|
x_p.clone()
|
|
|
|
|
* x_q.clone()
|
|
|
|
|
* x_q_minus_x_p
|
|
|
|
|
* (lambda.clone() * x_p_minus_x_r - y_p.clone() - y_r.clone())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// x_p⋅x_q⋅(y_q + y_p)⋅(λ^2 - x_p - x_q - x_r) = 0
|
|
|
|
|
let poly5 = {
|
|
|
|
|
let y_q_plus_y_p = y_q.clone() + y_p.clone(); // (y_q + y_p)
|
|
|
|
|
|
|
|
|
|
// x_p⋅x_q⋅(y_q + y_p)⋅(λ^2 - x_p - x_q - x_r)
|
2021-06-11 15:59:02 -07:00
|
|
|
|
x_p.clone() * x_q.clone() * y_q_plus_y_p * secant_line
|
2021-06-04 23:13:56 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// x_p⋅x_q⋅(y_q + y_p)⋅(λ ⋅(x_p - x_r) - y_p - y_r) = 0
|
|
|
|
|
let poly6 = {
|
|
|
|
|
let y_q_plus_y_p = y_q.clone() + y_p.clone(); // (y_q + y_p)
|
|
|
|
|
let x_p_minus_x_r = x_p.clone() - x_r.clone(); // (x_p - x_r)
|
|
|
|
|
|
|
|
|
|
// x_p⋅x_q⋅(y_q + y_p)⋅(λ ⋅(x_p - x_r) - y_p - y_r)
|
|
|
|
|
x_p.clone()
|
|
|
|
|
* x_q.clone()
|
|
|
|
|
* y_q_plus_y_p
|
|
|
|
|
* (lambda * x_p_minus_x_r - y_p.clone() - y_r.clone())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// (1 - x_p * β) * (x_r - x_q) = 0
|
|
|
|
|
let poly7 = (one.clone() - if_beta.clone()) * (x_r.clone() - x_q);
|
|
|
|
|
|
|
|
|
|
// (1 - x_p * β) * (y_r - y_q) = 0
|
|
|
|
|
let poly8 = (one.clone() - if_beta) * (y_r.clone() - y_q);
|
|
|
|
|
|
|
|
|
|
// (1 - x_q * γ) * (x_r - x_p) = 0
|
|
|
|
|
let poly9 = (one.clone() - if_gamma.clone()) * (x_r.clone() - x_p);
|
|
|
|
|
|
|
|
|
|
// (1 - x_q * γ) * (y_r - y_p) = 0
|
|
|
|
|
let poly10 = (one.clone() - if_gamma) * (y_r.clone() - y_p);
|
|
|
|
|
|
|
|
|
|
// ((1 - (x_q - x_p) * α - (y_q + y_p) * δ)) * x_r
|
|
|
|
|
let poly11 = (one.clone() - if_alpha.clone() - if_delta.clone()) * x_r;
|
|
|
|
|
|
|
|
|
|
// ((1 - (x_q - x_p) * α - (y_q + y_p) * δ)) * y_r
|
|
|
|
|
let poly12 = (one - if_alpha - if_delta) * y_r;
|
|
|
|
|
|
|
|
|
|
array::IntoIter::new([
|
|
|
|
|
poly1, poly2, poly3, poly4, poly5, poly6, poly7, poly8, poly9, poly10, poly11,
|
|
|
|
|
poly12,
|
|
|
|
|
])
|
|
|
|
|
.map(move |poly| q_add.clone() * poly)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 10:09:04 -07:00
|
|
|
|
pub(super) fn assign_region(
|
2021-06-04 23:13:56 -07:00
|
|
|
|
&self,
|
2021-06-11 15:29:05 -07:00
|
|
|
|
p: &EccPoint,
|
|
|
|
|
q: &EccPoint,
|
2021-06-04 23:13:56 -07:00
|
|
|
|
offset: usize,
|
2021-06-11 15:29:05 -07:00
|
|
|
|
region: &mut Region<'_, pallas::Base>,
|
|
|
|
|
) -> Result<EccPoint, Error> {
|
2021-06-04 23:13:56 -07:00
|
|
|
|
// Enable `q_add` selector
|
|
|
|
|
self.q_add.enable(region, offset)?;
|
|
|
|
|
|
|
|
|
|
// 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)?;
|
|
|
|
|
|
|
|
|
|
let (x_p, y_p) = (p.x.value(), p.y.value());
|
|
|
|
|
let (x_q, y_q) = (q.x.value(), q.y.value());
|
|
|
|
|
|
2021-06-10 21:05:21 -07:00
|
|
|
|
// [alpha, beta, gamma, delta]
|
|
|
|
|
// = [inv0(x_q - x_p), inv0(x_p), inv0(x_q), inv0(y_q + y_p)]
|
|
|
|
|
// where inv0(x) = 0 if x = 0, 1/x otherwise.
|
|
|
|
|
//
|
|
|
|
|
let (alpha, beta, gamma, delta) = {
|
|
|
|
|
let inverses = x_p
|
|
|
|
|
.zip(x_q)
|
|
|
|
|
.zip(y_p)
|
|
|
|
|
.zip(y_q)
|
|
|
|
|
.map(|(((x_p, x_q), y_p), y_q)| {
|
|
|
|
|
let alpha = x_q - x_p;
|
|
|
|
|
let beta = x_p;
|
|
|
|
|
let gamma = x_q;
|
|
|
|
|
let delta = y_q + y_p;
|
|
|
|
|
|
2021-06-11 15:36:26 -07:00
|
|
|
|
let mut inverses = [alpha, beta, gamma, delta];
|
2021-06-10 21:05:21 -07:00
|
|
|
|
inverses.batch_invert();
|
|
|
|
|
inverses
|
|
|
|
|
});
|
|
|
|
|
|
2021-06-11 15:36:26 -07:00
|
|
|
|
if let Some([alpha, beta, gamma, delta]) = inverses {
|
|
|
|
|
(Some(alpha), Some(beta), Some(gamma), Some(delta))
|
2021-06-10 21:05:21 -07:00
|
|
|
|
} else {
|
|
|
|
|
(None, None, None, None)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-04 23:13:56 -07:00
|
|
|
|
// Assign α = inv0(x_q - x_p)
|
|
|
|
|
region.assign_advice(
|
|
|
|
|
|| "α",
|
|
|
|
|
self.alpha,
|
|
|
|
|
offset,
|
|
|
|
|
|| alpha.ok_or(Error::SynthesisError),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Assign β = inv0(x_p)
|
|
|
|
|
region.assign_advice(
|
|
|
|
|
|| "β",
|
|
|
|
|
self.beta,
|
|
|
|
|
offset,
|
2021-06-10 21:05:21 -07:00
|
|
|
|
|| beta.ok_or(Error::SynthesisError),
|
2021-06-04 23:13:56 -07:00
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Assign γ = inv0(x_q)
|
|
|
|
|
region.assign_advice(
|
|
|
|
|
|| "γ",
|
|
|
|
|
self.gamma,
|
|
|
|
|
offset,
|
2021-06-10 21:05:21 -07:00
|
|
|
|
|| gamma.ok_or(Error::SynthesisError),
|
2021-06-04 23:13:56 -07:00
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Assign δ = inv0(y_q + y_p) if x_q = x_p, 0 otherwise
|
|
|
|
|
region.assign_advice(
|
|
|
|
|
|| "δ",
|
|
|
|
|
self.delta,
|
|
|
|
|
offset,
|
|
|
|
|
|| {
|
|
|
|
|
let x_p = x_p.ok_or(Error::SynthesisError)?;
|
|
|
|
|
let x_q = x_q.ok_or(Error::SynthesisError)?;
|
|
|
|
|
|
2021-06-11 15:36:26 -07:00
|
|
|
|
if x_q == x_p {
|
|
|
|
|
delta.ok_or(Error::SynthesisError)
|
2021-06-04 23:13:56 -07:00
|
|
|
|
} else {
|
2021-06-11 15:29:05 -07:00
|
|
|
|
Ok(pallas::Base::zero())
|
2021-06-11 15:36:26 -07:00
|
|
|
|
}
|
2021-06-04 23:13:56 -07:00
|
|
|
|
},
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::collapsible_else_if)]
|
|
|
|
|
// Assign lambda
|
2021-06-10 21:05:21 -07:00
|
|
|
|
let lambda =
|
|
|
|
|
x_p.zip(y_p)
|
|
|
|
|
.zip(x_q)
|
|
|
|
|
.zip(y_q)
|
|
|
|
|
.zip(alpha)
|
|
|
|
|
.map(|((((x_p, y_p), x_q), y_q), alpha)| {
|
|
|
|
|
if x_q != x_p {
|
|
|
|
|
// λ = (y_q - y_p)/(x_q - x_p)
|
|
|
|
|
// Here, alpha = inv0(x_q - x_p), which suffices since we
|
|
|
|
|
// know that x_q != x_p in this branch.
|
|
|
|
|
(y_q - y_p) * alpha
|
2021-06-04 23:13:56 -07:00
|
|
|
|
} else {
|
2021-06-11 15:29:05 -07:00
|
|
|
|
if y_p != pallas::Base::zero() {
|
2021-06-10 21:05:21 -07:00
|
|
|
|
// 3(x_p)^2
|
2021-06-11 15:59:02 -07:00
|
|
|
|
let three_x_p_sq = pallas::Base::from_u64(3) * x_p.square();
|
2021-06-10 21:05:21 -07:00
|
|
|
|
// 1 / 2(y_p)
|
2021-06-11 15:29:05 -07:00
|
|
|
|
let inv_two_y_p = y_p.invert().unwrap() * pallas::Base::TWO_INV;
|
2021-06-10 21:05:21 -07:00
|
|
|
|
// λ = 3(x_p)^2 / 2(y_p)
|
|
|
|
|
three_x_p_sq * inv_two_y_p
|
|
|
|
|
} else {
|
2021-06-11 15:29:05 -07:00
|
|
|
|
pallas::Base::zero()
|
2021-06-10 21:05:21 -07:00
|
|
|
|
}
|
2021-06-04 23:13:56 -07:00
|
|
|
|
}
|
2021-06-10 21:05:21 -07:00
|
|
|
|
});
|
2021-06-04 23:13:56 -07:00
|
|
|
|
region.assign_advice(
|
|
|
|
|
|| "λ",
|
|
|
|
|
self.lambda,
|
|
|
|
|
offset,
|
|
|
|
|
|| lambda.ok_or(Error::SynthesisError),
|
|
|
|
|
)?;
|
|
|
|
|
|
2021-06-11 15:59:02 -07:00
|
|
|
|
// Calculate (x_r, y_r)
|
|
|
|
|
let r =
|
2021-06-04 23:13:56 -07:00
|
|
|
|
x_p.zip(y_p)
|
|
|
|
|
.zip(x_q)
|
|
|
|
|
.zip(y_q)
|
|
|
|
|
.zip(lambda)
|
|
|
|
|
.map(|((((x_p, y_p), x_q), y_q), lambda)| {
|
2021-06-11 15:59:02 -07:00
|
|
|
|
{
|
|
|
|
|
if x_p == pallas::Base::zero() {
|
|
|
|
|
// 0 + Q = Q
|
|
|
|
|
(x_q, y_q)
|
|
|
|
|
} else if x_q == pallas::Base::zero() {
|
|
|
|
|
// P + 0 = P
|
|
|
|
|
(x_p, y_p)
|
|
|
|
|
} else if (x_q == x_p) && (y_q == -y_p) {
|
|
|
|
|
// P + (-P) maps to (0,0)
|
|
|
|
|
(pallas::Base::zero(), pallas::Base::zero())
|
|
|
|
|
} else {
|
|
|
|
|
// x_r = λ^2 - x_p - x_q
|
|
|
|
|
let x_r = lambda.square() - x_p - x_q;
|
|
|
|
|
// y_r = λ(x_p - x_r) - y_p
|
|
|
|
|
let y_r = lambda * (x_p - x_r) - y_p;
|
|
|
|
|
(x_r, y_r)
|
|
|
|
|
}
|
2021-06-04 23:13:56 -07:00
|
|
|
|
}
|
|
|
|
|
});
|
2021-06-11 15:59:02 -07:00
|
|
|
|
|
|
|
|
|
// Assign x_r
|
|
|
|
|
let x_r = r.map(|r| r.0);
|
2021-06-04 23:13:56 -07:00
|
|
|
|
let x_r_cell = region.assign_advice(
|
|
|
|
|
|| "x_r",
|
|
|
|
|
self.x_qr,
|
|
|
|
|
offset + 1,
|
|
|
|
|
|| x_r.ok_or(Error::SynthesisError),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
// Assign y_r
|
2021-06-11 15:59:02 -07:00
|
|
|
|
let y_r = r.map(|r| r.1);
|
2021-06-04 23:13:56 -07:00
|
|
|
|
let y_r_cell = region.assign_advice(
|
|
|
|
|
|| "y_r",
|
|
|
|
|
self.y_qr,
|
|
|
|
|
offset + 1,
|
|
|
|
|
|| y_r.ok_or(Error::SynthesisError),
|
|
|
|
|
)?;
|
|
|
|
|
|
2021-06-11 15:29:05 -07:00
|
|
|
|
let result = EccPoint {
|
|
|
|
|
x: CellValue::<pallas::Base>::new(x_r_cell, x_r),
|
|
|
|
|
y: CellValue::<pallas::Base>::new(y_r_cell, y_r),
|
2021-06-04 23:13:56 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
// Check that the correct sum is obtained.
|
|
|
|
|
{
|
|
|
|
|
use group::Curve;
|
|
|
|
|
|
|
|
|
|
let p = p.point();
|
|
|
|
|
let q = q.point();
|
|
|
|
|
let real_sum = p.zip(q).map(|(p, q)| p + q);
|
|
|
|
|
let result = result.point();
|
|
|
|
|
|
|
|
|
|
if let (Some(real_sum), Some(result)) = (real_sum, result) {
|
|
|
|
|
assert_eq!(real_sum.to_affine(), result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
pub mod tests {
|
2021-06-11 15:29:05 -07:00
|
|
|
|
use group::{prime::PrimeCurveAffine, Curve};
|
|
|
|
|
use halo2::{circuit::Layouter, plonk::Error};
|
|
|
|
|
use pasta_curves::{arithmetic::CurveExt, pallas};
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
use crate::circuit::gadget::ecc::{EccInstructions, Point};
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-06-11 15:29:05 -07:00
|
|
|
|
pub fn test_add<EccChip: EccInstructions<pallas::Affine> + Clone + Eq + std::fmt::Debug>(
|
2021-06-04 23:13:56 -07:00
|
|
|
|
chip: EccChip,
|
2021-06-11 15:29:05 -07:00
|
|
|
|
mut layouter: impl Layouter<pallas::Base>,
|
|
|
|
|
zero: &Point<pallas::Affine, EccChip>,
|
|
|
|
|
p_val: pallas::Affine,
|
|
|
|
|
p: &Point<pallas::Affine, EccChip>,
|
|
|
|
|
q_val: pallas::Affine,
|
|
|
|
|
q: &Point<pallas::Affine, EccChip>,
|
|
|
|
|
p_neg: &Point<pallas::Affine, EccChip>,
|
2021-06-04 23:13:56 -07:00
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
// Make sure P and Q are not the same point.
|
|
|
|
|
assert_ne!(p_val, q_val);
|
|
|
|
|
|
|
|
|
|
// Check complete addition P + (-P)
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add(layouter.namespace(|| "P + (-P)"), p_neg)?;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
// Check complete addition 𝒪 + 𝒪
|
2021-06-11 15:36:26 -07:00
|
|
|
|
zero.add(layouter.namespace(|| "𝒪 + 𝒪"), zero)?;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
// Check P + Q
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add(layouter.namespace(|| "P + Q"), q)?;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
// P + P
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add(layouter.namespace(|| "P + P"), p)?;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
// P + 𝒪
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add(layouter.namespace(|| "P + 𝒪"), zero)?;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
// 𝒪 + P
|
2021-06-11 15:36:26 -07:00
|
|
|
|
zero.add(layouter.namespace(|| "𝒪 + P"), p)?;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
// (x, y) + (ζx, y) should behave like normal P + Q.
|
|
|
|
|
let endo_p = p_val.to_curve().endo();
|
|
|
|
|
let endo_p = Point::new(
|
|
|
|
|
chip.clone(),
|
2021-06-11 15:36:26 -07:00
|
|
|
|
layouter.namespace(|| "endo(P)"),
|
2021-06-04 23:13:56 -07:00
|
|
|
|
Some(endo_p.to_affine()),
|
|
|
|
|
)?;
|
|
|
|
|
p.add(layouter.namespace(|| "P + endo(P)"), &endo_p)?;
|
|
|
|
|
|
|
|
|
|
// (x, y) + (ζx, -y) should also behave like normal P + Q.
|
|
|
|
|
let endo_p_neg = (-p_val).to_curve().endo();
|
|
|
|
|
let endo_p_neg = Point::new(
|
|
|
|
|
chip.clone(),
|
2021-06-11 15:36:26 -07:00
|
|
|
|
layouter.namespace(|| "endo(-P)"),
|
2021-06-04 23:13:56 -07:00
|
|
|
|
Some(endo_p_neg.to_affine()),
|
|
|
|
|
)?;
|
|
|
|
|
p.add(layouter.namespace(|| "P + endo(-P)"), &endo_p_neg)?;
|
|
|
|
|
|
|
|
|
|
// (x, y) + ((ζ^2)x, y)
|
|
|
|
|
let endo_2_p = p_val.to_curve().endo().endo();
|
|
|
|
|
let endo_2_p = Point::new(
|
|
|
|
|
chip.clone(),
|
2021-06-11 15:36:26 -07:00
|
|
|
|
layouter.namespace(|| "endo^2(P)"),
|
2021-06-04 23:13:56 -07:00
|
|
|
|
Some(endo_2_p.to_affine()),
|
|
|
|
|
)?;
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add(layouter.namespace(|| "P + endo^2(P)"), &endo_2_p)?;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
// (x, y) + ((ζ^2)x, -y)
|
|
|
|
|
let endo_2_p_neg = (-p_val).to_curve().endo().endo();
|
|
|
|
|
let endo_2_p_neg = Point::new(
|
|
|
|
|
chip,
|
2021-06-11 15:36:26 -07:00
|
|
|
|
layouter.namespace(|| "endo^2(-P)"),
|
2021-06-04 23:13:56 -07:00
|
|
|
|
Some(endo_2_p_neg.to_affine()),
|
|
|
|
|
)?;
|
2021-06-11 15:36:26 -07:00
|
|
|
|
p.add(layouter.namespace(|| "P + endo^2(-P)"), &endo_2_p_neg)?;
|
2021-06-04 23:13:56 -07:00
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|