nova-snark 0.68.0

High-speed recursive arguments from folding schemes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use crate::frontend::gadgets::poseidon::{
  hash_type::HashType,
  poseidon_inner::{Arity, Poseidon, PoseidonConstants},
  sponge::api::{IOPattern, InnerSpongeAPI},
  PoseidonError, Strength,
};
use ff::PrimeField;
use std::collections::VecDeque;

// General information on sponge construction: https://keccak.team/files/CSF-0.1.pdf

/*
A sponge can be instantiated in either simplex or duplex mode. Once instantiated, a sponge's mode never changes.

At any time, a sponge is operating in one of two directions: squeezing or absorbing. All sponges are initialized in the
absorbing direction. The number of absorbed field elements is incremented each time an element is absorbed and
decremented each time an element is squeezed. In duplex mode, the count of currently absorbed elements can never
decrease below zero, so only as many elements as have been absorbed can be squeezed at any time. In simplex mode, there
is no limit on the number of elements that can be squeezed, once absorption is complete.

In simplex mode, absorbing and squeezing cannot be interleaved. First all elements are absorbed, then all needed
elements are squeezed. At most the number of elements which were absorbed can be squeezed. Elements must be absorbed in
chunks of R (rate). After every R chunks have been absorbed, the state is permuted. After the final element has been
absorbed, any needed padding is added, and the final permutation (or two -- if required by padding) is performed. Then
groups of R field elements are squeezed, and the state is permuted after each group of R elements has been squeezed.
After squeezing is complete, a simplex sponge is exhausted, and no further absorption is possible.

In duplex mode, absorbing and squeezing can be interleaved. The state is permuted after every R elements have been
absorbed. This makes R elements available to be squeezed. If elements remain to be squeezed when the state is permuted,
remaining unsqueezed elements are queued. Otherwise they would be lost when permuting.

*/

/// Mode of the sponge
#[derive(Clone, Copy)]
pub enum Mode {
  /// Simplex mode
  Simplex,
}

#[derive(Clone, Copy)]
pub enum Direction {
  Absorbing,
  Squeezing,
}

/// Poseidon sponge
pub struct Sponge<'a, F: PrimeField, A: Arity<F>> {
  absorbed: usize,
  squeezed: usize,
  /// Poseidon state
  pub state: Poseidon<'a, F, A>,
  mode: Mode,
  direction: Direction,
  squeeze_pos: usize,
  queue: VecDeque<F>,
  pattern: IOPattern,
  io_count: usize,
}

/// Sponge trait
pub trait SpongeTrait<'a, F: PrimeField, A: Arity<F>>
where
  Self: Sized,
{
  /// Accumulator type
  type Acc;
  /// Value type
  type Elt;
  /// Error type
  type Error;

  /// Create a new sponge with the given constants and mode
  fn new_with_constants(constants: &'a PoseidonConstants<F, A>, mode: Mode) -> Self;

  /// Return API constants
  fn api_constants(strength: Strength) -> PoseidonConstants<F, A> {
    PoseidonConstants::new_with_strength_and_type(strength, HashType::Sponge)
  }

  /// Return the mode of the sponge
  fn mode(&self) -> Mode;
  /// Return the direction of the sponge
  fn direction(&self) -> Direction;
  /// Set the direction of the sponge
  fn set_direction(&mut self, direction: Direction);
  /// Return the number of absorbed elements
  fn absorbed(&self) -> usize;
  /// Return the number of squeezed elements
  fn squeezed(&self) -> usize;
  /// Set the number of squeezed elements
  fn set_squeezed(&mut self, squeezed: usize);
  /// Return the squeeze position
  fn squeeze_pos(&self) -> usize;
  /// Set the squeeze position
  fn set_squeeze_pos(&mut self, squeeze_pos: usize);
  /// Return the absorb position
  fn absorb_pos(&self) -> usize;
  /// Set the absorb position
  fn set_absorb_pos(&mut self, pos: usize);

  /// Return the element at the given index
  fn element(&self, index: usize) -> Self::Elt;
  /// Set the element at the given index
  fn set_element(&mut self, index: usize, elt: Self::Elt);

  /// Return whether the sponge is in simplex mode
  fn is_simplex(&self) -> bool {
    match self.mode() {
      Mode::Simplex => true,
    }
  }
  /// Return whether the sponge is in duplex mode
  fn is_duplex(&self) -> bool {
    match self.mode() {
      Mode::Simplex => false,
    }
  }

  /// Return whether the sponge is absorbing
  fn is_absorbing(&self) -> bool {
    match self.direction() {
      Direction::Absorbing => true,
      Direction::Squeezing => false,
    }
  }

  /// Return the number of available elements
  fn available(&self) -> usize {
    self.absorbed() - self.squeezed()
  }

  /// Return the rate of the sponge
  fn rate(&self) -> usize;

  /// Return the capacity of the sponge
  fn capacity(&self) -> usize;

  /// Return the size of the sponge
  fn size(&self) -> usize;

  /// Return the total size of the sponge
  fn total_size(&self) -> usize {
    assert!(self.is_simplex());
    match self.constants().hash_type {
      HashType::ConstantLength(l) => l,
      HashType::VariableLength => unimplemented!(),
      _ => A::to_usize(),
    }
  }

  /// Return the constants of the sponge
  fn constants(&self) -> &PoseidonConstants<F, A>;

  /// Return whether the sponge can squeeze without permuting
  fn can_squeeze_without_permuting(&self) -> bool {
    self.squeeze_pos() < self.size() - self.capacity()
  }

  /// Permute the sponge
  fn permute(&mut self, acc: &mut Self::Acc) -> Result<(), Self::Error> {
    // NOTE: this will apply any needed padding in the partially-absorbed case.
    // However, padding should only be applied when no more elements will be absorbed.
    // A duplex sponge should never apply padding implicitly, and a simplex sponge should only do so when it is
    // about to apply its final permutation.
    let unpermuted = self.absorb_pos();
    let needs_padding = self.is_absorbing() && unpermuted < self.rate();

    if needs_padding {
      match self.mode() {
        Mode::Simplex => {
          let final_permutation = self.squeezed() % self.total_size() <= self.rate();
          assert!(
            final_permutation,
            "Simplex sponge may only pad before final permutation"
          );
          self.pad();
        }
      }
    }

    self.permute_state(acc)?;
    self.set_absorb_pos(0);
    self.set_squeeze_pos(0);
    Ok(())
  }

  /// permutate the sponge state
  fn pad(&mut self);

  /// Permute the sponge state
  fn permute_state(&mut self, acc: &mut Self::Acc) -> Result<(), Self::Error>;

  /// Ensure the sponge is squeezing
  fn ensure_squeezing(&mut self, acc: &mut Self::Acc) -> Result<(), Self::Error> {
    match self.direction() {
      Direction::Squeezing => (),
      Direction::Absorbing => {
        match self.mode() {
          Mode::Simplex => {
            let done_squeezing_previous = self.squeeze_pos() >= self.rate();
            let partially_absorbed = self.absorb_pos() > 0;

            if done_squeezing_previous || partially_absorbed {
              self.permute(acc)?;
            }
          }
        }
        self.set_direction(Direction::Squeezing);
      }
    }
    Ok(())
  }

  /// Squeeze Aux
  fn squeeze_aux(&mut self) -> Self::Elt;

  /// Perform a squeeze operation
  fn squeeze(&mut self, acc: &mut Self::Acc) -> Result<Option<Self::Elt>, Self::Error> {
    self.ensure_squeezing(acc)?;

    if self.is_duplex() && self.available() == 0 {
      // What has not yet been absorbed cannot be squeezed.
      return Ok(None);
    };

    self.set_squeezed(self.squeezed() + 1);

    if let Some(queued) = self.dequeue() {
      return Ok(Some(queued));
    }

    if !self.can_squeeze_without_permuting() && self.is_simplex() {
      self.permute(acc)?;
    }

    let squeezed = self.squeeze_aux();

    Ok(Some(squeezed))
  }

  /// Dequeue an element
  fn dequeue(&mut self) -> Option<Self::Elt>;
}

impl<'a, F: PrimeField, A: Arity<F>> SpongeTrait<'a, F, A> for Sponge<'a, F, A> {
  type Acc = ();
  type Elt = F;
  type Error = PoseidonError;

  fn new_with_constants(constants: &'a PoseidonConstants<F, A>, mode: Mode) -> Self {
    let poseidon = Poseidon::new(constants);

    Self {
      mode,
      direction: Direction::Absorbing,
      state: poseidon,
      absorbed: 0,
      squeezed: 0,
      squeeze_pos: 0,
      queue: VecDeque::with_capacity(A::to_usize()),
      pattern: IOPattern(Vec::new()),
      io_count: 0,
    }
  }

  fn mode(&self) -> Mode {
    self.mode
  }
  fn direction(&self) -> Direction {
    self.direction
  }
  fn set_direction(&mut self, direction: Direction) {
    self.direction = direction;
  }
  fn absorbed(&self) -> usize {
    self.absorbed
  }

  fn squeezed(&self) -> usize {
    self.squeezed
  }
  fn set_squeezed(&mut self, squeezed: usize) {
    self.squeezed = squeezed;
  }
  fn squeeze_pos(&self) -> usize {
    self.squeeze_pos
  }
  fn set_squeeze_pos(&mut self, squeeze_pos: usize) {
    self.squeeze_pos = squeeze_pos;
  }
  fn absorb_pos(&self) -> usize {
    self.state.pos - 1
  }
  fn set_absorb_pos(&mut self, pos: usize) {
    self.state.pos = pos + 1;
  }

  fn element(&self, index: usize) -> Self::Elt {
    self.state.elements[index]
  }
  fn set_element(&mut self, index: usize, elt: Self::Elt) {
    self.state.elements[index] = elt;
  }

  fn rate(&self) -> usize {
    A::to_usize()
  }

  fn capacity(&self) -> usize {
    1
  }

  fn size(&self) -> usize {
    self.state.constants.width()
  }

  fn constants(&self) -> &PoseidonConstants<F, A> {
    self.state.constants
  }

  fn pad(&mut self) {
    self.state.apply_padding();
  }

  fn permute_state(&mut self, _acc: &mut Self::Acc) -> Result<(), Self::Error> {
    self.state.hash();
    Ok(())
  }

  fn dequeue(&mut self) -> Option<Self::Elt> {
    self.queue.pop_front()
  }

  fn squeeze_aux(&mut self) -> Self::Elt {
    let squeezed = self.element(SpongeTrait::squeeze_pos(self) + SpongeTrait::capacity(self));
    SpongeTrait::set_squeeze_pos(self, SpongeTrait::squeeze_pos(self) + 1);

    squeezed
  }
}

impl<F: PrimeField, A: Arity<F>> Iterator for Sponge<'_, F, A> {
  type Item = F;

  fn next(&mut self) -> Option<F> {
    self.squeeze(&mut ()).unwrap_or(None)
  }

  fn size_hint(&self) -> (usize, Option<usize>) {
    match self.mode {
      Mode::Simplex => (0, None),
    }
  }
}

impl<F: PrimeField, A: Arity<F>> InnerSpongeAPI<F, A> for Sponge<'_, F, A> {
  type Acc = ();
  type Value = F;

  fn initialize_capacity(&mut self, tag: u128, _: &mut ()) {
    let mut repr = F::Repr::default();
    repr.as_mut()[..16].copy_from_slice(&tag.to_le_bytes());

    let f = F::from_repr(repr).unwrap();
    self.set_element(0, f);
  }

  fn read_rate_element(&mut self, offset: usize) -> F {
    self.element(offset + SpongeTrait::capacity(self))
  }
  fn add_rate_element(&mut self, offset: usize, x: &F) {
    self.set_element(offset + SpongeTrait::capacity(self), *x);
  }
  fn permute(&mut self, acc: &mut ()) {
    SpongeTrait::permute(self, acc).unwrap();
  }

  // Supplemental methods needed for a generic implementation.

  fn zero() -> F {
    F::ZERO
  }

  fn rate(&self) -> usize {
    SpongeTrait::rate(self)
  }
  fn absorb_pos(&self) -> usize {
    SpongeTrait::absorb_pos(self)
  }
  fn squeeze_pos(&self) -> usize {
    SpongeTrait::squeeze_pos(self)
  }
  fn set_absorb_pos(&mut self, pos: usize) {
    SpongeTrait::set_absorb_pos(self, pos);
  }
  fn set_squeeze_pos(&mut self, pos: usize) {
    SpongeTrait::set_squeeze_pos(self, pos);
  }
  fn add(a: F, b: &F) -> F {
    a + b
  }

  fn pattern(&self) -> &IOPattern {
    &self.pattern
  }

  fn set_pattern(&mut self, pattern: IOPattern) {
    self.pattern = pattern
  }

  fn increment_io_count(&mut self) -> usize {
    let old_count = self.io_count;
    self.io_count += 1;
    old_count
  }
}