[zk-token-sdk] use canonical decoding for scalars (#28870)

use canonical decoding for scalars
This commit is contained in:
samkim-crypto 2022-11-18 15:11:06 +09:00 committed by GitHub
parent e5551e5294
commit f1e7ffba0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 11 deletions

View File

@ -101,7 +101,7 @@ mod target_arch {
#[cfg(not(target_os = "solana"))]
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 result = &scalar * &point;
@ -114,8 +114,13 @@ mod target_arch {
type Point = 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(
scalars.iter().map(Scalar::from),
scalars,
points
.iter()
.map(|point| EdwardsPoint::try_from(point).ok()),

View File

@ -101,7 +101,7 @@ mod target_arch {
#[cfg(not(target_os = "solana"))]
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 result = &scalar * &point;
@ -114,8 +114,13 @@ mod target_arch {
type Point = 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(
scalars.iter().map(Scalar::from),
scalars,
points
.iter()
.map(|point| RistrettoPoint::try_from(point).ok()),

View File

@ -6,7 +6,7 @@ pub struct PodScalar(pub [u8; 32]);
#[cfg(not(target_os = "solana"))]
mod target_arch {
use {super::*, curve25519_dalek::scalar::Scalar};
use {super::*, crate::curve25519::errors::Curve25519Error, curve25519_dalek::scalar::Scalar};
impl From<&Scalar> for PodScalar {
fn from(scalar: &Scalar) -> Self {
@ -14,9 +14,11 @@ mod target_arch {
}
}
impl From<&PodScalar> for Scalar {
fn from(pod: &PodScalar) -> Self {
Scalar::from_bits(pod.0)
impl TryFrom<&PodScalar> for Scalar {
type Error = Curve25519Error;
fn try_from(pod: &PodScalar) -> Result<Self, Self::Error> {
Scalar::from_canonical_bytes(pod.0).ok_or(Curve25519Error::PodConversion)
}
}
}

View File

@ -28,6 +28,8 @@ pub enum ProofError {
Decryption,
#[error("invalid ciphertext data")]
CiphertextDeserialization,
#[error("invalid scalar data")]
ScalarDeserialization,
}
#[derive(Error, Clone, Debug, Eq, PartialEq)]

View File

@ -82,9 +82,11 @@ mod target_arch {
}
}
impl From<PodScalar> for Scalar {
fn from(pod: PodScalar) -> Self {
Scalar::from_bits(pod.0)
impl TryFrom<PodScalar> for Scalar {
type Error = ProofError;
fn try_from(pod: PodScalar) -> Result<Self, Self::Error> {
Scalar::from_canonical_bytes(pod.0).ok_or(ProofError::CiphertextDeserialization)
}
}