gnark-plonky2-verifier/poseidon/poseidon.go
poseidon/poseidon.goRaw
1package poseidon
2
3import (
4 . "gnark-ed25519/field"
5
6 "github.com/consensys/gnark/frontend"
7)
8
9const HALF_N_FULL_ROUNDS = 4
10const N_FULL_ROUNDS_TOTAL = 2 * HALF_N_FULL_ROUNDS
11const N_PARTIAL_ROUNDS = 22
12const N_ROUNDS = N_FULL_ROUNDS_TOTAL + N_PARTIAL_ROUNDS
13const MAX_WIDTH = 12
14const WIDTH = 12
15const SPONGE_WIDTH = 12
16const SPONGE_RATE = 8
17
18type PoseidonState = [WIDTH]F
19type PoseidonChip struct {
20 api frontend.API
21 field frontend.API
22}
23
24func NewPoseidonChip(api frontend.API, field frontend.API) *PoseidonChip {
25 return &PoseidonChip{api: api, field: field}
26}
27
28func (c *PoseidonChip) Poseidon(input PoseidonState) PoseidonState {
29 state := input
30 roundCounter := 0
31 state = c.fullRounds(state, &roundCounter)
32 state = c.partialRounds(state, &roundCounter)
33 state = c.fullRounds(state, &roundCounter)
34 return state
35}
36
37func (c *PoseidonChip) HashNToMNoPad(input []F, nbOutputs int) []F {
38 var state PoseidonState
39
40 for i := 0; i < len(input); i += SPONGE_RATE {
41 for j := 0; j < SPONGE_RATE; j++ {
42 if i+j < len(input) {
43 state[j] = input[i+j]
44 }
45 }
46 state = c.Poseidon(state)
47 }
48
49 var outputs []F
50
51 for {
52 for i := 0; i < SPONGE_RATE; i++ {
53 outputs = append(outputs, state[i])
54 if len(outputs) == nbOutputs {
55 return outputs
56 }
57 }
58 state = c.Poseidon(state)
59 }
60}
61
62func (c *PoseidonChip) HashNoPad(input []F) Hash {
63 var hash Hash
64 copy(hash[:], c.HashNToMNoPad(input, 4))
65 return hash
66}
67
68func (c *PoseidonChip) fullRounds(state PoseidonState, roundCounter *int) PoseidonState {
69 for i := 0; i < HALF_N_FULL_ROUNDS; i++ {
70 state = c.constantLayer(state, roundCounter)
71 state = c.sBoxLayer(state)
72 state = c.mdsLayer(state)
73 *roundCounter += 1
74 }
75 return state
76}
77
78func (c *PoseidonChip) partialRounds(state PoseidonState, roundCounter *int) PoseidonState {
79 state = c.partialFirstConstantLayer(state)
80 state = c.mdsPartialLayerInit(state)
81
82 for i := 0; i < N_PARTIAL_ROUNDS; i++ {
83 state[0] = c.sBoxMonomial(state[0])
84 state[0] = c.field.Add(state[0], FAST_PARTIAL_ROUND_CONSTANTS[i]).(F)
85 state = c.mdsPartialLayerFast(state, i)
86 }
87
88 *roundCounter += N_PARTIAL_ROUNDS
89
90 return state
91}
92
93func (c *PoseidonChip) constantLayer(state PoseidonState, roundCounter *int) PoseidonState {
94 for i := 0; i < 12; i++ {
95 if i < WIDTH {
96 roundConstant := NewFieldElement(ALL_ROUND_CONSTANTS[i+WIDTH*(*roundCounter)])
97 state[i] = c.field.Add(state[i], roundConstant).(F)
98 }
99 }
100 return state
101}
102
103func (c *PoseidonChip) sBoxLayer(state PoseidonState) PoseidonState {
104 for i := 0; i < 12; i++ {
105 if i < WIDTH {
106 state[i] = c.sBoxMonomial(state[i])
107 }
108 }
109 return state
110}
111
112func (c *PoseidonChip) sBoxMonomial(x F) F {
113 x2 := c.field.Mul(x, x)
114 x4 := c.field.Mul(x2, x2)
115 x3 := c.field.Mul(x2, x)
116 return c.field.Mul(x3, x4).(F)
117}
118
119func (c *PoseidonChip) mdsRowShf(r int, v [WIDTH]frontend.Variable) frontend.Variable {
120 res := frontend.Variable(0)
121
122 for i := 0; i < 12; i++ {
123 if i < WIDTH {
124 res1 := c.api.Mul(v[(i+r)%WIDTH], frontend.Variable(MDS_MATRIX_CIRC[i]))
125 res = c.api.Add(res, res1)
126 }
127 }
128
129 res = c.api.Add(res, c.api.Mul(v[r], MDS_MATRIX_DIAG[r]))
130 return res
131}
132
133func (c *PoseidonChip) mdsLayer(state_ PoseidonState) PoseidonState {
134 var result PoseidonState
135 for i := 0; i < WIDTH; i++ {
136 result[i] = NewFieldElement(0)
137 }
138
139 var state [WIDTH]frontend.Variable
140 for i := 0; i < WIDTH; i++ {
141 state[i] = c.api.FromBinary(c.field.ToBinary(state_[i])...)
142 }
143
144 for r := 0; r < 12; r++ {
145 if r < WIDTH {
146 sum := c.mdsRowShf(r, state)
147 bits := c.api.ToBinary(sum)
148 result[r] = c.field.FromBinary(bits).(F)
149 }
150 }
151
152 return result
153}
154
155func (c *PoseidonChip) partialFirstConstantLayer(state PoseidonState) PoseidonState {
156 for i := 0; i < 12; i++ {
157 if i < WIDTH {
158 state[i] = c.field.Add(state[i], NewFieldElement(FAST_PARTIAL_FIRST_ROUND_CONSTANT[i])).(F)
159 }
160 }
161 return state
162}
163
164func (c *PoseidonChip) mdsPartialLayerInit(state PoseidonState) PoseidonState {
165 var result PoseidonState
166 for i := 0; i < 12; i++ {
167 result[i] = NewFieldElement(0)
168 }
169
170 result[0] = state[0]
171
172 for r := 1; r < 12; r++ {
173 if r < WIDTH {
174 for d := 1; d < 12; d++ {
175 if d < WIDTH {
176 t := NewFieldElement(FAST_PARTIAL_ROUND_INITIAL_MATRIX[r-1][d-1])
177 result[d] = c.field.Add(result[d], c.field.Mul(state[r], t)).(F)
178 }
179 }
180 }
181 }
182
183 return result
184}
185
186func (c *PoseidonChip) mdsPartialLayerFast(state PoseidonState, r int) PoseidonState {
187 dSum := frontend.Variable(0)
188 for i := 1; i < 12; i++ {
189 if i < WIDTH {
190 t := frontend.Variable(FAST_PARTIAL_ROUND_W_HATS[r][i-1])
191 si := c.api.FromBinary(c.field.ToBinary(state[i])...)
192 dSum = c.api.Add(dSum, c.api.Mul(si, t))
193 }
194 }
195
196 s0 := c.api.FromBinary(c.field.ToBinary(state[0])...)
197 mds0to0 := frontend.Variable(MDS_MATRIX_CIRC[0] + MDS_MATRIX_DIAG[0])
198 dSum = c.api.Add(dSum, c.api.Mul(s0, mds0to0))
199 d := c.field.FromBinary(c.api.ToBinary(dSum))
200
201 var result PoseidonState
202 for i := 0; i < WIDTH; i++ {
203 result[i] = NewFieldElement(0)
204 }
205
206 result[0] = d.(F)
207
208 for i := 1; i < 12; i++ {
209 if i < WIDTH {
210 t := NewFieldElement(FAST_PARTIAL_ROUND_VS[r][i-1])
211 result[i] = c.field.Add(state[i], c.field.Mul(state[0], t)).(F)
212 }
213 }
214
215 return result
216}