From 14a0b460e79449eb299a46dc6cdde797909253e1 Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 5 Dec 2023 20:28:06 +0300 Subject: [PATCH] feat: add TranscriptVar trait (#42) * feat: add TranscriptVar trait * Update src/transcript/poseidon.rs Co-authored-by: arnaucube --------- Co-authored-by: arnaucube --- src/transcript/mod.rs | 16 ++++++++++++++++ src/transcript/poseidon.rs | 18 +++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/transcript/mod.rs b/src/transcript/mod.rs index 51f6eb7..0e22a11 100644 --- a/src/transcript/mod.rs +++ b/src/transcript/mod.rs @@ -1,5 +1,8 @@ use crate::Error; use ark_ec::CurveGroup; +use ark_ff::PrimeField; +use ark_r1cs_std::{boolean::Boolean, fields::fp::FpVar}; +use ark_relations::r1cs::{ConstraintSystemRef, SynthesisError}; use ark_std::fmt::Debug; pub mod poseidon; @@ -16,3 +19,16 @@ pub trait Transcript { fn get_challenge_nbits(&mut self, nbits: usize) -> Vec; fn get_challenges(&mut self, n: usize) -> Vec; } + +pub trait TranscriptVar { + type TranscriptVarConfig: Debug; + + fn new(cs: ConstraintSystemRef, poseidon_config: &Self::TranscriptVarConfig) -> Self; + fn absorb(&mut self, v: FpVar) -> Result<(), SynthesisError>; + fn absorb_vec(&mut self, v: &[FpVar]) -> Result<(), SynthesisError>; + fn get_challenge(&mut self) -> Result, SynthesisError>; + /// returns the bit representation of the challenge, we use its output in-circuit for the + /// `GC.scalar_mul_le` method. + fn get_challenge_nbits(&mut self, nbits: usize) -> Result>, SynthesisError>; + fn get_challenges(&mut self, n: usize) -> Result>, SynthesisError>; +} diff --git a/src/transcript/poseidon.rs b/src/transcript/poseidon.rs index efb5082..7050f47 100644 --- a/src/transcript/poseidon.rs +++ b/src/transcript/poseidon.rs @@ -12,6 +12,8 @@ use ark_std::{One, Zero}; use crate::transcript::Transcript; use crate::Error; +use super::TranscriptVar; + /// PoseidonTranscript implements the Transcript trait using the Poseidon hash pub struct PoseidonTranscript where @@ -84,18 +86,20 @@ fn prepare_point(p: &C) -> Result, Error> { pub struct PoseidonTranscriptVar { sponge: PoseidonSpongeVar, } -impl PoseidonTranscriptVar { - pub fn new(cs: ConstraintSystemRef, poseidon_config: &PoseidonConfig) -> Self { +impl TranscriptVar for PoseidonTranscriptVar { + type TranscriptVarConfig = PoseidonConfig; + + fn new(cs: ConstraintSystemRef, poseidon_config: &Self::TranscriptVarConfig) -> Self { let sponge = PoseidonSpongeVar::::new(cs, poseidon_config); Self { sponge } } - pub fn absorb(&mut self, v: FpVar) -> Result<(), SynthesisError> { + fn absorb(&mut self, v: FpVar) -> Result<(), SynthesisError> { self.sponge.absorb(&v) } - pub fn absorb_vec(&mut self, v: &[FpVar]) -> Result<(), SynthesisError> { + fn absorb_vec(&mut self, v: &[FpVar]) -> Result<(), SynthesisError> { self.sponge.absorb(&v) } - pub fn get_challenge(&mut self) -> Result, SynthesisError> { + fn get_challenge(&mut self) -> Result, SynthesisError> { let c = self.sponge.squeeze_field_elements(1)?; self.sponge.absorb(&c[0])?; Ok(c[0].clone()) @@ -103,10 +107,10 @@ impl PoseidonTranscriptVar { /// returns the bit representation of the challenge, we use its output in-circuit for the /// `GC.scalar_mul_le` method. - pub fn get_challenge_nbits(&mut self, nbits: usize) -> Result>, SynthesisError> { + fn get_challenge_nbits(&mut self, nbits: usize) -> Result>, SynthesisError> { self.sponge.squeeze_bits(nbits) } - pub fn get_challenges(&mut self, n: usize) -> Result>, SynthesisError> { + fn get_challenges(&mut self, n: usize) -> Result>, SynthesisError> { let c = self.sponge.squeeze_field_elements(n)?; self.sponge.absorb(&c)?; Ok(c)