[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"))] #[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()),

View File

@ -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()),

View File

@ -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)
} }
} }
} }

View File

@ -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)]

View File

@ -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)
} }
} }