#![allow(dead_code)]
use super::Error;
use super::ed::{self, EcPointJson};
use crate::tss::bigint::BigUintDec;
use purecrypto::ec::edwards25519::hazmat::{EdwardsPoint, Scalar};
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)]
pub struct Key {
#[serde(rename = "Xi")]
pub xi: BigUintDec,
#[serde(rename = "ShareID")]
pub share_id: BigUintDec,
#[serde(rename = "Ks")]
pub ks: Vec<BigUintDec>,
#[serde(rename = "BigXj")]
pub big_xj: Vec<EcPointJson>,
#[serde(rename = "EDDSAPub")]
pub eddsa_pub: EcPointJson,
}
impl Key {
pub fn from_json(s: &str) -> Result<Key, Error> {
Ok(serde_json::from_str(s)?)
}
pub fn to_json(&self) -> Result<String, Error> {
Ok(serde_json::to_string(self)?)
}
pub(crate) fn xi_scalar(&self) -> Scalar {
ed::scalar_from_be(self.xi.as_be_bytes())
}
pub(crate) fn ks_scalars(&self) -> Vec<Scalar> {
self.ks
.iter()
.map(|k| ed::scalar_from_be(k.as_be_bytes()))
.collect()
}
pub(crate) fn big_xj_points(&self) -> Option<Vec<EdwardsPoint>> {
self.big_xj.iter().map(ed::point_from_json).collect()
}
pub(crate) fn eddsa_pub_point(&self) -> Option<EdwardsPoint> {
ed::point_from_json(&self.eddsa_pub)
}
pub fn validate_basic(&self) -> Result<(), Error> {
if self.ks.len() != self.big_xj.len() {
return Err(Error::Validation(
"key: Ks and BigXj length mismatch".into(),
));
}
for p in self.big_xj.iter().chain(std::iter::once(&self.eddsa_pub)) {
if p.curve != ed::CURVE_NAME {
return Err(Error::Validation(format!(
"key: unexpected curve {}",
p.curve
)));
}
}
if self.eddsa_pub_point().is_none() || self.big_xj_points().is_none() {
return Err(Error::Validation("key: a point is off-curve".into()));
}
Ok(())
}
}