From 3193d67bce38a05fc928245faefc5554e81fa8bc Mon Sep 17 00:00:00 2001 From: Srinath Setty Date: Sat, 14 May 2022 12:16:22 +0530 Subject: [PATCH] delegate sampling of generators to trait implementors (#59) --- src/commitments.rs | 15 ++------------- src/pasta.rs | 39 +++++++++++++++++++++++---------------- src/traits.rs | 5 ++--- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/commitments.rs b/src/commitments.rs index 1de5a22..3574803 100644 --- a/src/commitments.rs +++ b/src/commitments.rs @@ -7,11 +7,8 @@ use core::{ marker::PhantomData, ops::{Add, AddAssign, Mul, MulAssign}, }; -use digest::{ExtendableOutput, Input}; use ff::Field; use merlin::Transcript; -use sha3::Shake256; -use std::io::Read; #[derive(Debug)] pub struct CommitGens { @@ -30,16 +27,8 @@ pub struct CompressedCommitment { } impl CommitGens { - pub fn new(label: &[u8], n: usize) -> Self { - let mut shake = Shake256::default(); - shake.input(label); - let mut reader = shake.xof_result(); - let mut gens: Vec = Vec::new(); - let mut uniform_bytes = [0u8; 64]; - for _ in 0..n { - reader.read_exact(&mut uniform_bytes).unwrap(); - gens.push(G::from_uniform_bytes(&uniform_bytes).unwrap()); - } + pub fn new(label: &'static [u8], n: usize) -> Self { + let gens = G::from_label(label, n); CommitGens { gens, diff --git a/src/pasta.rs b/src/pasta.rs index e9f30fb..e68b980 100644 --- a/src/pasta.rs +++ b/src/pasta.rs @@ -4,6 +4,7 @@ use crate::{ traits::{ChallengeTrait, CompressedGroup, Group}, }; use core::ops::Mul; +use digest::{ExtendableOutput, Input}; use ff::Field; use merlin::Transcript; use num_bigint::BigInt; @@ -16,6 +17,8 @@ use pasta_curves::{ }; use rand::SeedableRng; use rand_chacha::ChaCha20Rng; +use sha3::Shake256; +use std::io::Read; //////////////////////////////////////Pallas/////////////////////////////////////////////// @@ -55,16 +58,18 @@ impl Group for pallas::Point { PallasCompressedElementWrapper::new(self.to_bytes()) } - fn from_uniform_bytes(bytes: &[u8]) -> Option { - if bytes.len() != 64 { - None - } else { - let mut arr = [0; 32]; - arr.copy_from_slice(&bytes[0..32]); - + fn from_label(label: &'static [u8], n: usize) -> Vec { + let mut shake = Shake256::default(); + shake.input(label); + let mut reader = shake.xof_result(); + let mut gens: Vec = Vec::new(); + let mut uniform_bytes = [0u8; 32]; + for _ in 0..n { + reader.read_exact(&mut uniform_bytes).unwrap(); let hash = Ep::hash_to_curve("from_uniform_bytes"); - Some(hash(&arr).to_affine()) + gens.push(hash(&uniform_bytes).to_affine()); } + gens } fn to_coordinates(&self) -> (Self::Base, Self::Base, bool) { @@ -143,16 +148,18 @@ impl Group for vesta::Point { VestaCompressedElementWrapper::new(self.to_bytes()) } - fn from_uniform_bytes(bytes: &[u8]) -> Option { - if bytes.len() != 64 { - None - } else { - let mut arr = [0; 32]; - arr.copy_from_slice(&bytes[0..32]); - + fn from_label(label: &'static [u8], n: usize) -> Vec { + let mut shake = Shake256::default(); + shake.input(label); + let mut reader = shake.xof_result(); + let mut gens: Vec = Vec::new(); + let mut uniform_bytes = [0u8; 32]; + for _ in 0..n { + reader.read_exact(&mut uniform_bytes).unwrap(); let hash = Eq::hash_to_curve("from_uniform_bytes"); - Some(hash(&arr).to_affine()) + gens.push(hash(&uniform_bytes).to_affine()); } + gens } fn to_coordinates(&self) -> (Self::Base, Self::Base, bool) { diff --git a/src/traits.rs b/src/traits.rs index 7c7e70e..136124f 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -45,9 +45,8 @@ pub trait Group: /// Compresses the group element fn compress(&self) -> Self::CompressedGroupElement; - /// Attempts to create a group element from a sequence of bytes, - /// failing with a `None` if the supplied bytes do not encode the group element - fn from_uniform_bytes(bytes: &[u8]) -> Option; + /// Produce a vector of group elements using a static label + fn from_label(label: &'static [u8], n: usize) -> Vec; /// Returns the affine coordinates (x, y, infinty) for the point fn to_coordinates(&self) -> (Self::Base, Self::Base, bool);