sonobe/folding-schemes/src/transcript/mod.rs
folding-schemes/src/transcript/mod.rsRaw
1use ark_crypto_primitives::sponge::{constraints::CryptographicSpongeVar, CryptographicSponge};
2use ark_ec::CurveGroup;
3use ark_ff::PrimeField;
4use ark_r1cs_std::{
5 boolean::Boolean, fields::fp::FpVar, groups::CurveVar, ToConstraintFieldGadget,
6};
7use ark_relations::r1cs::SynthesisError;
8
9pub mod poseidon;
10
11/// An interface for objects that can be absorbed by a `Transcript`.
12///
13/// Matches `Absorb` in `ark-crypto-primitives`.
14pub trait AbsorbNonNative<F: PrimeField> {
15 /// Converts the object into field elements that can be absorbed by a `Transcript`.
16 /// Append the list to `dest`
17 fn to_native_sponge_field_elements(&self, dest: &mut Vec<F>);
18
19 /// Converts the object into field elements that can be absorbed by a `Transcript`.
20 /// Return the list as `Vec`
21 fn to_native_sponge_field_elements_as_vec(&self) -> Vec<F> {
22 let mut result = Vec::new();
23 self.to_native_sponge_field_elements(&mut result);
24 result
25 }
26}
27
28/// An interface for objects that can be absorbed by a `TranscriptVar` whose constraint field
29/// is `F`.
30///
31/// Matches `AbsorbGadget` in `ark-crypto-primitives`.
32pub trait AbsorbNonNativeGadget<F: PrimeField> {
33 /// Converts the object into field elements that can be absorbed by a `TranscriptVar`.
34 fn to_native_sponge_field_elements(&self) -> Result<Vec<FpVar<F>>, SynthesisError>;
35}
36
37pub trait Transcript<F: PrimeField>: CryptographicSponge {
38 /// `absorb_point` is for absorbing points whose `BaseField` is the field of
39 /// the sponge, i.e., the type `C` of these points should satisfy
40 /// `C::BaseField = F`.
41 ///
42 /// If the sponge field `F` is `C::ScalarField`, call `absorb_nonnative`
43 /// instead.
44 fn absorb_point<C: CurveGroup<BaseField = F>>(&mut self, v: &C);
45 /// `absorb_nonnative` is for structs that contain non-native (field or
46 /// group) elements, including:
47 ///
48 /// - A field element of type `T: PrimeField` that will be absorbed into a
49 /// sponge that operates in another field `F != T`.
50 /// - A group element of type `C: CurveGroup` that will be absorbed into a
51 /// sponge that operates in another field `F != C::BaseField`, e.g.,
52 /// `F = C::ScalarField`.
53 /// - A `CommittedInstance` on the secondary curve (used for CycleFold) that
54 /// will be absorbed into a sponge that operates in the (scalar field of
55 /// the) primary curve.
56 ///
57 /// Note that although a `CommittedInstance` for `AugmentedFCircuit` on
58 /// the primary curve also contains non-native elements, we still regard
59 /// it as native, because the sponge is on the same curve.
60 fn absorb_nonnative<V: AbsorbNonNative<F>>(&mut self, v: &V);
61
62 fn get_challenge(&mut self) -> F;
63 /// get_challenge_nbits returns a field element of size nbits
64 fn get_challenge_nbits(&mut self, nbits: usize) -> Vec<bool>;
65 fn get_challenges(&mut self, n: usize) -> Vec<F>;
66}
67
68pub trait TranscriptVar<F: PrimeField, S: CryptographicSponge>:
69 CryptographicSpongeVar<F, S>
70{
71 /// `absorb_point` is for absorbing points whose `BaseField` is the field of
72 /// the sponge, i.e., the type `C` of these points should satisfy
73 /// `C::BaseField = F`.
74 ///
75 /// If the sponge field `F` is `C::ScalarField`, call `absorb_nonnative`
76 /// instead.
77 fn absorb_point<C: CurveGroup<BaseField = F>, GC: CurveVar<C, F> + ToConstraintFieldGadget<F>>(
78 &mut self,
79 v: &GC,
80 ) -> Result<(), SynthesisError>;
81 /// `absorb_nonnative` is for structs that contain non-native (field or
82 /// group) elements, including:
83 ///
84 /// - A field element of type `T: PrimeField` that will be absorbed into a
85 /// sponge that operates in another field `F != T`.
86 /// - A group element of type `C: CurveGroup` that will be absorbed into a
87 /// sponge that operates in another field `F != C::BaseField`, e.g.,
88 /// `F = C::ScalarField`.
89 /// - A `CommittedInstance` on the secondary curve (used for CycleFold) that
90 /// will be absorbed into a sponge that operates in the (scalar field of
91 /// the) primary curve.
92 ///
93 /// Note that although a `CommittedInstance` for `AugmentedFCircuit` on
94 /// the primary curve also contains non-native elements, we still regard
95 /// it as native, because the sponge is on the same curve.
96 fn absorb_nonnative<V: AbsorbNonNativeGadget<F>>(
97 &mut self,
98 v: &V,
99 ) -> Result<(), SynthesisError>;
100
101 fn get_challenge(&mut self) -> Result<FpVar<F>, SynthesisError>;
102 /// returns the bit representation of the challenge, we use its output in-circuit for the
103 /// `GC.scalar_mul_le` method.
104 fn get_challenge_nbits(&mut self, nbits: usize) -> Result<Vec<Boolean<F>>, SynthesisError>;
105 fn get_challenges(&mut self, n: usize) -> Result<Vec<FpVar<F>>, SynthesisError>;
106}