halo2_base/poseidon/
mod.rs

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
use crate::{
    gates::{RangeChip, RangeInstructions},
    poseidon::hasher::{spec::OptimizedPoseidonSpec, PoseidonHasher},
    safe_types::{FixLenBytes, VarLenBytes, VarLenBytesVec},
    utils::{BigPrimeField, ScalarField},
    AssignedValue, Context,
};

use itertools::Itertools;

/// Module for Poseidon hasher
pub mod hasher;

/// Chip for Poseidon hash.
pub struct PoseidonChip<'a, F: ScalarField, const T: usize, const RATE: usize> {
    range_chip: &'a RangeChip<F>,
    hasher: PoseidonHasher<F, T, RATE>,
}

impl<'a, F: ScalarField, const T: usize, const RATE: usize> PoseidonChip<'a, F, T, RATE> {
    /// Create a new PoseidonChip.
    pub fn new(
        ctx: &mut Context<F>,
        spec: OptimizedPoseidonSpec<F, T, RATE>,
        range_chip: &'a RangeChip<F>,
    ) -> Self {
        let mut hasher = PoseidonHasher::new(spec);
        hasher.initialize_consts(ctx, range_chip.gate());
        Self { range_chip, hasher }
    }
}

/// Trait for Poseidon instructions
pub trait PoseidonInstructions<F: ScalarField> {
    /// Return hash of a [VarLenBytes]
    fn hash_var_len_bytes<const MAX_LEN: usize>(
        &self,
        ctx: &mut Context<F>,
        inputs: &VarLenBytes<F, MAX_LEN>,
    ) -> AssignedValue<F>
    where
        F: BigPrimeField;

    /// Return hash of a [VarLenBytesVec]
    fn hash_var_len_bytes_vec(
        &self,
        ctx: &mut Context<F>,
        inputs: &VarLenBytesVec<F>,
    ) -> AssignedValue<F>
    where
        F: BigPrimeField;

    /// Return hash of a [FixLenBytes]
    fn hash_fix_len_bytes<const MAX_LEN: usize>(
        &self,
        ctx: &mut Context<F>,
        inputs: &FixLenBytes<F, MAX_LEN>,
    ) -> AssignedValue<F>
    where
        F: BigPrimeField;
}

impl<'a, F: ScalarField, const T: usize, const RATE: usize> PoseidonInstructions<F>
    for PoseidonChip<'a, F, T, RATE>
{
    fn hash_var_len_bytes<const MAX_LEN: usize>(
        &self,
        ctx: &mut Context<F>,
        inputs: &VarLenBytes<F, MAX_LEN>,
    ) -> AssignedValue<F>
    where
        F: BigPrimeField,
    {
        let inputs_len = inputs.len();
        self.hasher.hash_var_len_array(
            ctx,
            self.range_chip,
            inputs.bytes().map(|sb| *sb.as_ref()).as_ref(),
            *inputs_len,
        )
    }

    fn hash_var_len_bytes_vec(
        &self,
        ctx: &mut Context<F>,
        inputs: &VarLenBytesVec<F>,
    ) -> AssignedValue<F>
    where
        F: BigPrimeField,
    {
        let inputs_len = inputs.len();
        self.hasher.hash_var_len_array(
            ctx,
            self.range_chip,
            &inputs.bytes().iter().map(|sb| *sb.as_ref()).collect_vec(),
            *inputs_len,
        )
    }

    fn hash_fix_len_bytes<const MAX_LEN: usize>(
        &self,
        ctx: &mut Context<F>,
        inputs: &FixLenBytes<F, MAX_LEN>,
    ) -> AssignedValue<F>
    where
        F: BigPrimeField,
    {
        self.hasher.hash_fix_len_array(
            ctx,
            self.range_chip.gate(),
            inputs.bytes().map(|sb| *sb.as_ref()).as_ref(),
        )
    }
}