2021-04-19 15:02:59 -07:00
|
|
|
|
use std::ops::Add;
|
|
|
|
|
|
2021-04-21 16:59:55 -07:00
|
|
|
|
use group::Group;
|
|
|
|
|
use pasta_curves::pallas;
|
2021-04-19 15:02:59 -07:00
|
|
|
|
use subtle::{ConstantTimeEq, CtOption};
|
|
|
|
|
|
|
|
|
|
/// P ∪ {⊥}
|
|
|
|
|
///
|
|
|
|
|
/// Simulated incomplete addition built over complete addition.
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
|
pub(super) struct IncompletePoint(CtOption<pallas::Point>);
|
|
|
|
|
|
|
|
|
|
impl From<pallas::Point> for IncompletePoint {
|
|
|
|
|
fn from(p: pallas::Point) -> Self {
|
|
|
|
|
IncompletePoint(CtOption::new(p, 1.into()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<IncompletePoint> for CtOption<pallas::Point> {
|
|
|
|
|
fn from(p: IncompletePoint) -> Self {
|
|
|
|
|
p.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Add for IncompletePoint {
|
|
|
|
|
type Output = IncompletePoint;
|
|
|
|
|
|
|
|
|
|
fn add(self, rhs: Self) -> Self::Output {
|
|
|
|
|
// ⊥ ⊹ ⊥ = ⊥
|
|
|
|
|
// ⊥ ⊹ P = ⊥
|
|
|
|
|
IncompletePoint(self.0.and_then(|p| {
|
|
|
|
|
// P ⊹ ⊥ = ⊥
|
|
|
|
|
rhs.0.and_then(|q| {
|
|
|
|
|
// 0 ⊹ 0 = ⊥
|
|
|
|
|
// 0 ⊹ P = ⊥
|
2021-04-21 16:59:55 -07:00
|
|
|
|
// P ⊹ 0 = ⊥
|
|
|
|
|
// (x, y) ⊹ (x', y') = ⊥ if x == x'
|
|
|
|
|
// (x, y) ⊹ (x', y') = (x, y) + (x', y') if x != x'
|
|
|
|
|
CtOption::new(
|
|
|
|
|
p + q,
|
|
|
|
|
!(p.is_identity() | q.is_identity() | p.ct_eq(&q) | p.ct_eq(&-q)),
|
|
|
|
|
)
|
2021-04-19 15:02:59 -07:00
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Add<pallas::Point> for IncompletePoint {
|
|
|
|
|
type Output = IncompletePoint;
|
|
|
|
|
|
|
|
|
|
fn add(self, rhs: pallas::Point) -> Self::Output {
|
|
|
|
|
self + IncompletePoint(CtOption::new(rhs, 1.into()))
|
|
|
|
|
}
|
|
|
|
|
}
|