[zk-token-sdk] use canonical decoding for scalars (#28870)
use canonical decoding for scalars
This commit is contained in:
parent
e5551e5294
commit
f1e7ffba0c
|
@ -101,7 +101,7 @@ mod target_arch {
|
||||||
|
|
||||||
#[cfg(not(target_os = "solana"))]
|
#[cfg(not(target_os = "solana"))]
|
||||||
fn multiply(scalar: &PodScalar, point: &Self) -> Option<Self> {
|
fn multiply(scalar: &PodScalar, point: &Self) -> Option<Self> {
|
||||||
let scalar: Scalar = scalar.into();
|
let scalar: Scalar = scalar.try_into().ok()?;
|
||||||
let point: EdwardsPoint = point.try_into().ok()?;
|
let point: EdwardsPoint = point.try_into().ok()?;
|
||||||
|
|
||||||
let result = &scalar * &point;
|
let result = &scalar * &point;
|
||||||
|
@ -114,8 +114,13 @@ mod target_arch {
|
||||||
type Point = Self;
|
type Point = Self;
|
||||||
|
|
||||||
fn multiscalar_multiply(scalars: &[PodScalar], points: &[Self]) -> Option<Self> {
|
fn multiscalar_multiply(scalars: &[PodScalar], points: &[Self]) -> Option<Self> {
|
||||||
|
let scalars = scalars
|
||||||
|
.iter()
|
||||||
|
.map(|scalar| Scalar::try_from(scalar).ok())
|
||||||
|
.collect::<Option<Vec<_>>>()?;
|
||||||
|
|
||||||
EdwardsPoint::optional_multiscalar_mul(
|
EdwardsPoint::optional_multiscalar_mul(
|
||||||
scalars.iter().map(Scalar::from),
|
scalars,
|
||||||
points
|
points
|
||||||
.iter()
|
.iter()
|
||||||
.map(|point| EdwardsPoint::try_from(point).ok()),
|
.map(|point| EdwardsPoint::try_from(point).ok()),
|
||||||
|
|
|
@ -101,7 +101,7 @@ mod target_arch {
|
||||||
|
|
||||||
#[cfg(not(target_os = "solana"))]
|
#[cfg(not(target_os = "solana"))]
|
||||||
fn multiply(scalar: &PodScalar, point: &Self) -> Option<Self> {
|
fn multiply(scalar: &PodScalar, point: &Self) -> Option<Self> {
|
||||||
let scalar: Scalar = scalar.into();
|
let scalar: Scalar = scalar.try_into().ok()?;
|
||||||
let point: RistrettoPoint = point.try_into().ok()?;
|
let point: RistrettoPoint = point.try_into().ok()?;
|
||||||
|
|
||||||
let result = &scalar * &point;
|
let result = &scalar * &point;
|
||||||
|
@ -114,8 +114,13 @@ mod target_arch {
|
||||||
type Point = Self;
|
type Point = Self;
|
||||||
|
|
||||||
fn multiscalar_multiply(scalars: &[PodScalar], points: &[Self]) -> Option<Self> {
|
fn multiscalar_multiply(scalars: &[PodScalar], points: &[Self]) -> Option<Self> {
|
||||||
|
let scalars = scalars
|
||||||
|
.iter()
|
||||||
|
.map(|scalar| Scalar::try_from(scalar).ok())
|
||||||
|
.collect::<Option<Vec<_>>>()?;
|
||||||
|
|
||||||
RistrettoPoint::optional_multiscalar_mul(
|
RistrettoPoint::optional_multiscalar_mul(
|
||||||
scalars.iter().map(Scalar::from),
|
scalars,
|
||||||
points
|
points
|
||||||
.iter()
|
.iter()
|
||||||
.map(|point| RistrettoPoint::try_from(point).ok()),
|
.map(|point| RistrettoPoint::try_from(point).ok()),
|
||||||
|
|
|
@ -6,7 +6,7 @@ pub struct PodScalar(pub [u8; 32]);
|
||||||
|
|
||||||
#[cfg(not(target_os = "solana"))]
|
#[cfg(not(target_os = "solana"))]
|
||||||
mod target_arch {
|
mod target_arch {
|
||||||
use {super::*, curve25519_dalek::scalar::Scalar};
|
use {super::*, crate::curve25519::errors::Curve25519Error, curve25519_dalek::scalar::Scalar};
|
||||||
|
|
||||||
impl From<&Scalar> for PodScalar {
|
impl From<&Scalar> for PodScalar {
|
||||||
fn from(scalar: &Scalar) -> Self {
|
fn from(scalar: &Scalar) -> Self {
|
||||||
|
@ -14,9 +14,11 @@ mod target_arch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&PodScalar> for Scalar {
|
impl TryFrom<&PodScalar> for Scalar {
|
||||||
fn from(pod: &PodScalar) -> Self {
|
type Error = Curve25519Error;
|
||||||
Scalar::from_bits(pod.0)
|
|
||||||
|
fn try_from(pod: &PodScalar) -> Result<Self, Self::Error> {
|
||||||
|
Scalar::from_canonical_bytes(pod.0).ok_or(Curve25519Error::PodConversion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,8 @@ pub enum ProofError {
|
||||||
Decryption,
|
Decryption,
|
||||||
#[error("invalid ciphertext data")]
|
#[error("invalid ciphertext data")]
|
||||||
CiphertextDeserialization,
|
CiphertextDeserialization,
|
||||||
|
#[error("invalid scalar data")]
|
||||||
|
ScalarDeserialization,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Error, Clone, Debug, Eq, PartialEq)]
|
#[derive(Error, Clone, Debug, Eq, PartialEq)]
|
||||||
|
|
|
@ -82,9 +82,11 @@ mod target_arch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PodScalar> for Scalar {
|
impl TryFrom<PodScalar> for Scalar {
|
||||||
fn from(pod: PodScalar) -> Self {
|
type Error = ProofError;
|
||||||
Scalar::from_bits(pod.0)
|
|
||||||
|
fn try_from(pod: PodScalar) -> Result<Self, Self::Error> {
|
||||||
|
Scalar::from_canonical_bytes(pod.0).ok_or(ProofError::CiphertextDeserialization)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue