From 448869295deaf8eb723107f4f26c7b12d3dc0e0c Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Thu, 13 Oct 2022 05:21:43 +0200 Subject: [PATCH 01/13] implement Zero and One traits for scalar --- Cargo.toml | 1 + src/lib.rs | 1 + src/scalar.rs | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index f32957c5..213356c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ serde = { version = "1.0", default-features = false, optional = true, features = packed_simd = { version = "0.3.4", package = "packed_simd_2", features = ["into_bits"], optional = true } zeroize = { version = ">=1, <1.4", default-features = false } fiat-crypto = { version = "0.1.6", optional = true} +num-traits = "0.2" [features] nightly = ["subtle/nightly"] diff --git a/src/lib.rs b/src/lib.rs index f33ffded..63c18b80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -264,6 +264,7 @@ extern crate packed_simd; extern crate byteorder; pub extern crate digest; extern crate rand_core; +extern crate num_traits; extern crate zeroize; #[cfg(any(feature = "fiat_u64_backend", feature = "fiat_u32_backend"))] diff --git a/src/scalar.rs b/src/scalar.rs index 00de7408..a6819005 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -149,6 +149,8 @@ use core::ops::{Add, AddAssign}; use core::ops::{Mul, MulAssign}; use core::ops::{Sub, SubAssign}; +use::num_traits::{Zero, One}; + #[allow(unused_imports)] use prelude::*; @@ -290,6 +292,24 @@ impl Index for Scalar { } } +impl Zero for Scalar { + fn zero() -> Self { + Scalar::zero() + } + fn is_zero(&self) -> bool { + self == &Scalar::zero() + } +} + +impl One for Scalar { + fn one() -> Self { + Scalar::one() + } + fn is_one(&self) -> bool { + self == &Scalar::one() + } +} + impl<'b> MulAssign<&'b Scalar> for Scalar { fn mul_assign(&mut self, _rhs: &'b Scalar) { *self = UnpackedScalar::mul(&self.unpack(), &_rhs.unpack()).pack(); From c14b03802d57746d8229c52aa9a240664da25670 Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Thu, 13 Oct 2022 06:08:24 +0200 Subject: [PATCH 02/13] add Div --- src/scalar.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/scalar.rs b/src/scalar.rs index a6819005..fa81c937 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -148,6 +148,7 @@ use core::ops::Neg; use core::ops::{Add, AddAssign}; use core::ops::{Mul, MulAssign}; use core::ops::{Sub, SubAssign}; +use core::ops::{Div, DivAssign}; use::num_traits::{Zero, One}; @@ -310,6 +311,13 @@ impl One for Scalar { } } +impl<'b> Div<&'b Scalar> for Scalar { + type Output = Scalar; + fn div(self, q: &Scalar) -> Self::Output { + Scalar::one() + } +} + impl<'b> MulAssign<&'b Scalar> for Scalar { fn mul_assign(&mut self, _rhs: &'b Scalar) { *self = UnpackedScalar::mul(&self.unpack(), &_rhs.unpack()).pack(); From fa13373953f2b2401d7224867620cc256993312b Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Thu, 13 Oct 2022 06:23:05 +0200 Subject: [PATCH 03/13] fix div signature --- src/scalar.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scalar.rs b/src/scalar.rs index fa81c937..5fd9c2e3 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -311,9 +311,9 @@ impl One for Scalar { } } -impl<'b> Div<&'b Scalar> for Scalar { +impl Div for Scalar { type Output = Scalar; - fn div(self, q: &Scalar) -> Self::Output { + fn div(self, q: Scalar) -> Self::Output { Scalar::one() } } From b0da3a3200a1648b2d5faca74847937a11f609bd Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Thu, 13 Oct 2022 07:48:50 +0200 Subject: [PATCH 04/13] add square and multiply using bitvec --- Cargo.toml | 1 + src/lib.rs | 1 + src/scalar.rs | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 213356c3..a1e876ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ packed_simd = { version = "0.3.4", package = "packed_simd_2", features = ["into_ zeroize = { version = ">=1, <1.4", default-features = false } fiat-crypto = { version = "0.1.6", optional = true} num-traits = "0.2" +bitvec = "1.0.1" [features] nightly = ["subtle/nightly"] diff --git a/src/lib.rs b/src/lib.rs index 63c18b80..fde263f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -265,6 +265,7 @@ extern crate byteorder; pub extern crate digest; extern crate rand_core; extern crate num_traits; +extern crate bitvec; extern crate zeroize; #[cfg(any(feature = "fiat_u64_backend", feature = "fiat_u32_backend"))] diff --git a/src/scalar.rs b/src/scalar.rs index 5fd9c2e3..80290d51 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -138,7 +138,6 @@ //! //! The resulting `Scalar` has exactly the specified bit pattern, //! **except for the highest bit, which will be set to 0**. - use core::borrow::Borrow; use core::cmp::{Eq, PartialEq}; use core::fmt::Debug; @@ -151,6 +150,7 @@ use core::ops::{Sub, SubAssign}; use core::ops::{Div, DivAssign}; use::num_traits::{Zero, One}; +use bitvec::prelude::*; #[allow(unused_imports)] use prelude::*; @@ -311,10 +311,39 @@ impl One for Scalar { } } +fn square_and_multiply(x: Scalar, n: Scalar) -> Scalar { + let mut ret = Scalar::zero(); + + let mut bitvec: Vec = Vec::new(); + for byte in n.as_bytes() { + let bits = byte.view_bits::(); + for bit in bits { + bitvec.push(*bit); + } + } + //println!("{:?}", bitvec); + let mut square = x; + //for (i,bit) in bitvec.iter().enumerate() { + for bit in bitvec { + //println!("bit {} square is {:?}", i, square); + if bit { + //println!("bit {} is {}", i, *bit); + //let old = ret; + ret += square; + //println!("{:?} +\n{:?} =\n{:?}", old.as_bytes(), square.as_bytes(), ret.as_bytes()); + } + square *= square; + } + //println!("{:?}", square); + ret +} + impl Div for Scalar { type Output = Scalar; fn div(self, q: Scalar) -> Self::Output { - Scalar::one() + let q1 = square_and_multiply(q, constants::BASEPOINT_ORDER - Scalar::from(2 as u8)); + println!("inverse of {:?} is {:?}", q, q1); + self * q1 } } From 3ef64bf3f8c7cff846a6b75580f4dc4a10001d8b Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Thu, 13 Oct 2022 08:00:11 +0200 Subject: [PATCH 05/13] add DivAssign --- src/scalar.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/scalar.rs b/src/scalar.rs index 80290d51..b8496b76 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -263,6 +263,12 @@ impl Scalar { s } + + /// Find the inverse module the group order of the passed scalar + pub fn inverse(&self) -> Self { + let exp = constants::BASEPOINT_ORDER - Scalar::from(2 as u8); + square_and_multiply(self, &exp) + } } impl Debug for Scalar { @@ -311,7 +317,7 @@ impl One for Scalar { } } -fn square_and_multiply(x: Scalar, n: Scalar) -> Scalar { +fn square_and_multiply(x: &Scalar, n: &Scalar) -> Scalar { let mut ret = Scalar::zero(); let mut bitvec: Vec = Vec::new(); @@ -322,7 +328,7 @@ fn square_and_multiply(x: Scalar, n: Scalar) -> Scalar { } } //println!("{:?}", bitvec); - let mut square = x; + let mut square = *x; //for (i,bit) in bitvec.iter().enumerate() { for bit in bitvec { //println!("bit {} square is {:?}", i, square); @@ -341,12 +347,20 @@ fn square_and_multiply(x: Scalar, n: Scalar) -> Scalar { impl Div for Scalar { type Output = Scalar; fn div(self, q: Scalar) -> Self::Output { - let q1 = square_and_multiply(q, constants::BASEPOINT_ORDER - Scalar::from(2 as u8)); + let q1 = q.inverse(); println!("inverse of {:?} is {:?}", q, q1); self * q1 } } +impl DivAssign for Scalar { + fn div_assign(&mut self, q: Scalar) { + let q1 = q.inverse(); + println!("inverse of {:?} is {:?}", q, q1); + *self = *self * q1; + } +} + impl<'b> MulAssign<&'b Scalar> for Scalar { fn mul_assign(&mut self, _rhs: &'b Scalar) { *self = UnpackedScalar::mul(&self.unpack(), &_rhs.unpack()).pack(); From d3cb3d160aa1a3a9f538a6820dc6bcc8a4e0f2b3 Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Thu, 13 Oct 2022 08:10:57 +0200 Subject: [PATCH 06/13] scalar already had an invert --- src/scalar.rs | 39 +++------------------------------------ 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/src/scalar.rs b/src/scalar.rs index b8496b76..eb76144e 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -150,7 +150,7 @@ use core::ops::{Sub, SubAssign}; use core::ops::{Div, DivAssign}; use::num_traits::{Zero, One}; -use bitvec::prelude::*; +//use bitvec::prelude::*; #[allow(unused_imports)] use prelude::*; @@ -263,12 +263,6 @@ impl Scalar { s } - - /// Find the inverse module the group order of the passed scalar - pub fn inverse(&self) -> Self { - let exp = constants::BASEPOINT_ORDER - Scalar::from(2 as u8); - square_and_multiply(self, &exp) - } } impl Debug for Scalar { @@ -317,37 +311,10 @@ impl One for Scalar { } } -fn square_and_multiply(x: &Scalar, n: &Scalar) -> Scalar { - let mut ret = Scalar::zero(); - - let mut bitvec: Vec = Vec::new(); - for byte in n.as_bytes() { - let bits = byte.view_bits::(); - for bit in bits { - bitvec.push(*bit); - } - } - //println!("{:?}", bitvec); - let mut square = *x; - //for (i,bit) in bitvec.iter().enumerate() { - for bit in bitvec { - //println!("bit {} square is {:?}", i, square); - if bit { - //println!("bit {} is {}", i, *bit); - //let old = ret; - ret += square; - //println!("{:?} +\n{:?} =\n{:?}", old.as_bytes(), square.as_bytes(), ret.as_bytes()); - } - square *= square; - } - //println!("{:?}", square); - ret -} - impl Div for Scalar { type Output = Scalar; fn div(self, q: Scalar) -> Self::Output { - let q1 = q.inverse(); + let q1 = q.invert(); println!("inverse of {:?} is {:?}", q, q1); self * q1 } @@ -355,7 +322,7 @@ impl Div for Scalar { impl DivAssign for Scalar { fn div_assign(&mut self, q: Scalar) { - let q1 = q.inverse(); + let q1 = q.invert(); println!("inverse of {:?} is {:?}", q, q1); *self = *self * q1; } From ba783569b9be69b52f857d7c0b8b6a7f99d509d6 Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Thu, 13 Oct 2022 08:54:04 +0200 Subject: [PATCH 07/13] remove unused imports and debugging println --- src/scalar.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/scalar.rs b/src/scalar.rs index eb76144e..76dc35be 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -150,7 +150,6 @@ use core::ops::{Sub, SubAssign}; use core::ops::{Div, DivAssign}; use::num_traits::{Zero, One}; -//use bitvec::prelude::*; #[allow(unused_imports)] use prelude::*; @@ -315,7 +314,6 @@ impl Div for Scalar { type Output = Scalar; fn div(self, q: Scalar) -> Self::Output { let q1 = q.invert(); - println!("inverse of {:?} is {:?}", q, q1); self * q1 } } @@ -323,7 +321,6 @@ impl Div for Scalar { impl DivAssign for Scalar { fn div_assign(&mut self, q: Scalar) { let q1 = q.invert(); - println!("inverse of {:?} is {:?}", q, q1); *self = *self * q1; } } From 11cf540934c36e52346169370766746439544b3d Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Thu, 13 Oct 2022 08:56:14 +0200 Subject: [PATCH 08/13] remove bitvec --- Cargo.toml | 1 - src/lib.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a1e876ed..213356c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,6 @@ packed_simd = { version = "0.3.4", package = "packed_simd_2", features = ["into_ zeroize = { version = ">=1, <1.4", default-features = false } fiat-crypto = { version = "0.1.6", optional = true} num-traits = "0.2" -bitvec = "1.0.1" [features] nightly = ["subtle/nightly"] diff --git a/src/lib.rs b/src/lib.rs index fde263f5..63c18b80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -265,7 +265,6 @@ extern crate byteorder; pub extern crate digest; extern crate rand_core; extern crate num_traits; -extern crate bitvec; extern crate zeroize; #[cfg(any(feature = "fiat_u64_backend", feature = "fiat_u32_backend"))] From 438b497a0c74dc0a7a35c38c28972395f2520d12 Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Fri, 14 Oct 2022 05:58:14 +0200 Subject: [PATCH 09/13] implement Display and (Partial)Ord --- Cargo.toml | 1 + src/lib.rs | 4 ++-- src/scalar.rs | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 213356c3..f7dcd140 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ packed_simd = { version = "0.3.4", package = "packed_simd_2", features = ["into_ zeroize = { version = ">=1, <1.4", default-features = false } fiat-crypto = { version = "0.1.6", optional = true} num-traits = "0.2" +hex = "0.4.3" [features] nightly = ["subtle/nightly"] diff --git a/src/lib.rs b/src/lib.rs index 63c18b80..38020939 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -266,6 +266,7 @@ pub extern crate digest; extern crate rand_core; extern crate num_traits; extern crate zeroize; +extern crate hex; #[cfg(any(feature = "fiat_u64_backend", feature = "fiat_u32_backend"))] extern crate fiat_crypto; @@ -273,8 +274,7 @@ extern crate fiat_crypto; // Used for traits related to constant-time code. extern crate subtle; -#[cfg(all(test, feature = "serde"))] -extern crate bincode; +#[cfg(all(test, feature = "serde"))]extern crate bincode; #[cfg(feature = "serde")] extern crate serde; diff --git a/src/scalar.rs b/src/scalar.rs index 76dc35be..e8dfede1 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -139,8 +139,8 @@ //! The resulting `Scalar` has exactly the specified bit pattern, //! **except for the highest bit, which will be set to 0**. use core::borrow::Borrow; -use core::cmp::{Eq, PartialEq}; -use core::fmt::Debug; +use core::cmp::{Eq, Ord, PartialEq}; +use core::fmt::{Debug, Display}; use core::iter::{Product, Sum}; use core::ops::Index; use core::ops::Neg; @@ -194,7 +194,7 @@ type UnpackedScalar = backend::serial::u32::scalar::Scalar29; /// The `Scalar` struct holds an integer \\(s < 2\^{255} \\) which /// represents an element of \\(\mathbb Z / \ell\\). -#[derive(Copy, Clone, Hash)] +#[derive(Copy, Clone, Hash, Ord, PartialOrd)] pub struct Scalar { /// `bytes` is a little-endian byte encoding of an integer representing a scalar modulo the /// group order. @@ -270,6 +270,15 @@ impl Debug for Scalar { } } +impl Display for Scalar { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + for byte in self.as_bytes() { + write!(f, "{:#02x}", byte)?; + } + Ok(()) + } +} + impl Eq for Scalar {} impl PartialEq for Scalar { fn eq(&self, other: &Self) -> bool { From 02d4afc558f6429de4c9aaa91a3f105698fc0830 Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Fri, 14 Oct 2022 06:02:53 +0200 Subject: [PATCH 10/13] use hex create to display Scalar --- src/scalar.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/scalar.rs b/src/scalar.rs index e8dfede1..f9e3abbf 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -272,9 +272,8 @@ impl Debug for Scalar { impl Display for Scalar { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - for byte in self.as_bytes() { - write!(f, "{:#02x}", byte)?; - } + let data = hex::encode(self.bytes); + write!(f, "{}", data)?; Ok(()) } } From 5a3e399c79e747f84b9b891f27e46b164d02ffcf Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Fri, 14 Oct 2022 06:06:47 +0200 Subject: [PATCH 11/13] fix typo in lib.rs --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 38020939..2b72678f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -274,7 +274,8 @@ extern crate fiat_crypto; // Used for traits related to constant-time code. extern crate subtle; -#[cfg(all(test, feature = "serde"))]extern crate bincode; +#[cfg(all(test, feature = "serde"))] +extern crate bincode; #[cfg(feature = "serde")] extern crate serde; From 816157e5873824c682b599d57ea26b2e7609398f Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Fri, 14 Oct 2022 11:31:54 +0200 Subject: [PATCH 12/13] implement Zero trait --- src/ristretto.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ristretto.rs b/src/ristretto.rs index b9e37343..73d4e7fd 100644 --- a/src/ristretto.rs +++ b/src/ristretto.rs @@ -165,6 +165,8 @@ use core::ops::{Add, Neg, Sub}; use core::ops::{AddAssign, SubAssign}; use core::ops::{Mul, MulAssign}; +use::num_traits::Zero; + use rand_core::{CryptoRng, RngCore}; use digest::generic_array::typenum::U64; @@ -422,6 +424,15 @@ impl<'de> Deserialize<'de> for CompressedRistretto { } } +impl Zero for RistrettoPoint { + fn zero() -> Self { + RistrettoPoint::identity() + } + fn is_zero(&self) -> bool { + self == &RistrettoPoint::identity() + } +} + // ------------------------------------------------------------------------ // Internal point representations // ------------------------------------------------------------------------ From 589d5e994129ac9cc400a7488b17de3f760096bf Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Fri, 14 Oct 2022 11:54:11 +0200 Subject: [PATCH 13/13] implement Display for RistrettoPoint --- src/ristretto.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ristretto.rs b/src/ristretto.rs index 73d4e7fd..b59e588e 100644 --- a/src/ristretto.rs +++ b/src/ristretto.rs @@ -159,7 +159,7 @@ //! https://ristretto.group/ use core::borrow::Borrow; -use core::fmt::Debug; +use core::fmt::{Debug, Display}; use core::iter::Sum; use core::ops::{Add, Neg, Sub}; use core::ops::{AddAssign, SubAssign}; @@ -433,6 +433,14 @@ impl Zero for RistrettoPoint { } } +impl Display for RistrettoPoint { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + let data = hex::encode(self.compress().as_bytes()); + write!(f, "{}", data)?; + Ok(()) + } +} + // ------------------------------------------------------------------------ // Internal point representations // ------------------------------------------------------------------------