pso-poseidon 0.4.0

BN254 Poseidon (Circom-compatible) and Poseidon2 (Barretenberg/noir-compatible) hash implementations
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
name: CI

# Triggers
#   push:main         full pipeline incl. cog auto-bump + publish + github-release
#   push:tags:["v*"]  release-only path; skip lint/test/build, run publish + github-release
#                     against the named tag
#   pull_request      lint/test/build/cargo-deny only — no release work
#   schedule          nightly cargo-deny at 04:00 UTC to surface fresh advisories
#   workflow_dispatch maintainer-named (re-)release; empty `tag` input runs the normal
#                     main pipeline

on:
  push:
    branches: [ main ]
    tags: [ "v*" ]
  pull_request:
    branches: [ main ]
  schedule:
    - cron: '0 4 * * *'
  workflow_dispatch:
    inputs:
      tag:
        description: >-
          Existing tag to (re-)release (e.g. "v0.2.0"). Empty input
          runs the normal main pipeline.
        required: false
        type: string

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  # ----------------------------------------------------------------
  # Mode prelude. Every release-aware job's `if:` reads `release_tag`.
  # Non-empty when this run is a release-only path (tag push or
  # workflow_dispatch with `tag` input). Empty otherwise — the
  # normal "ran on main" / "PR" pipeline.
  # ----------------------------------------------------------------
  resolve:
    name: Resolve release tag
    runs-on: ubuntu-latest
    outputs:
      release_tag: ${{ steps.r.outputs.release_tag }}
    steps:
      - id: r
        run: |
          set -euo pipefail
          tag=""
          case "${{ github.event_name }}" in
            push)
              if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
                tag="${{ github.ref_name }}"
              fi
              ;;
            workflow_dispatch)
              tag='${{ inputs.tag }}'
              ;;
          esac
          echo "release_tag=${tag}"
          echo "release_tag=${tag}" >> "$GITHUB_OUTPUT"

  # Supply-chain enforcement. Runs on every PR + nightly cron @ 04:00 UTC.
  # Independent of the rest of the pipeline so a fresh advisory surfacing
  # nightly is visible without rerunning lint/test.
  cargo-deny:
    name: cargo-deny
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: EmbarkStudios/cargo-deny-action@v2
        with:
          command: check all

  commitlint:
    name: Commit lint (conventional-commits)
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: wagoid/commitlint-github-action@v6

  lint:
    name: Lint and Format Check
    runs-on: ubuntu-latest
    needs: [resolve]
    if: needs.resolve.outputs.release_tag == ''
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: rustfmt, clippy, cargo
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-stable-lint-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-stable-lint-

      - name: Check code formatting
        run: cargo fmt --all -- --check

      - name: Run clippy (code smells check)
        run: |
          cargo clippy --version
          # Run clippy with practical code smell checks
          # Focus on catching critical issues: debug macros, unsafe patterns, etc.
          cargo clippy --all-targets --all-features -- -D warnings \
            -D clippy::dbg_macro \
            -D clippy::print_stdout \
            -D clippy::print_stderr \
            -D clippy::todo \
            -D clippy::unimplemented \
            -D clippy::panic \
            -D clippy::exit \
            -D clippy::cast_lossless \
            -D clippy::cast_possible_truncation \
            -D clippy::cast_possible_wrap \
            -D clippy::cast_precision_loss \
            -D clippy::cast_sign_loss \
            -D clippy::clone_on_ref_ptr \
            -D clippy::empty_enums \
            -D clippy::enum_glob_use \
            -D clippy::if_not_else \
            -D clippy::mut_mut \
            -D clippy::non_ascii_literal \
            -D clippy::single_match_else \
            -D clippy::string_add \
            -D clippy::string_add_assign \
            -D clippy::string_lit_as_bytes \
            -D clippy::unnecessary_unwrap \
            -D clippy::unused_self \
            -D clippy::useless_let_if_seq \
            -A clippy::module_name_repetitions \
            -A clippy::must_use_candidate \
            -A clippy::missing_errors_doc \
            -A clippy::missing_panics_doc \
            -A clippy::too_many_lines \
            -A clippy::similar_names \
            -A clippy::inline_always \
            -A clippy::unwrap_used \
            -A clippy::expect_used \
            -A clippy::panic \
            -A unused_imports \
            -A unused_macros \
            -A dead_code

      - name: Check documentation builds
        run: cargo doc --no-deps --all-features --document-private-items

  test:
    name: Test
    runs-on: ubuntu-latest
    needs: [resolve, lint]
    if: needs.resolve.outputs.release_tag == ''
    strategy:
      matrix:
        rust:
          - stable
          - beta
          - nightly
        include:
          - rust: nightly
            allow_failure: true
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: rustfmt, clippy, cargo
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ matrix.rust }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-${{ matrix.rust }}-

      - name: Check formatting
        run: cargo fmt --all -- --check
        continue-on-error: ${{ matrix.rust == 'nightly' }}

      - name: Run clippy
        run: |
          cargo clippy --version
          cargo clippy --all-targets --all-features -- -D warnings \
            -D clippy::dbg_macro \
            -D clippy::print_stdout \
            -D clippy::print_stderr \
            -A clippy::unwrap_used \
            -A clippy::expect_used \
            -A clippy::panic
        continue-on-error: ${{ matrix.rust == 'nightly' }}

      - name: Run tests
        run: cargo test --verbose --all-features

      - name: Build
        run: cargo build --verbose --release --all-features

  build:
    name: Build
    runs-on: ${{ matrix.os }}
    needs: [resolve, lint, test]
    if: needs.resolve.outputs.release_tag == ''
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: rustfmt, clippy, cargo
          toolchain: ${{ matrix.rust }}
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-stable-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-stable-
      - name: Build
        run: cargo build --verbose --release --all-features

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: build-${{ matrix.os }}
          path: target/release/
          if-no-files-found: ignore

  # ----------------------------------------------------------------
  # Version bump + tag. Runs after every check on `push: main` (PR
  # runs and tag-push runs skip). Cog inspects the conventional-
  # commit log since the last tag and creates a `chore(version):
  # vX.Y.Z` commit + matching tag if `feat/fix/breaking` commits
  # have landed.
  # ----------------------------------------------------------------
  tag:
    name: Bump version & tag
    runs-on: ubuntu-latest
    needs: [resolve, lint, test, build]
    if: |
      needs.resolve.outputs.release_tag == '' &&
      github.event_name == 'push' &&
      github.ref == 'refs/heads/main' &&
      !startsWith(github.event.head_commit.message, 'chore(version):') &&
      !contains(github.event.head_commit.message, '[skip ci]')
    outputs:
      tag: ${{ steps.bump.outputs.tag }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Configure git identity
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

      - name: Cocogitto bump
        uses: cocogitto/cocogitto-action@v4
        with:
          command: bump
          args: --auto
          git-user: "github-actions[bot]"
          git-user-email: "41898282+github-actions[bot]@users.noreply.github.com"

      - name: Resolve created tag (if any)
        id: bump
        run: |
          set -euo pipefail
          tag="$(git tag --points-at HEAD | grep '^v' | head -n1 || true)"
          echo "tag=${tag}"
          echo "tag=${tag}" >> "$GITHUB_OUTPUT"

  # ----------------------------------------------------------------
  # Publish (cog flow). Fires on (a) a fresh cog-pushed tag in the
  # run above, or (b) a manual tag push / workflow_dispatch with
  # `tag` input. The "no new bump" case (tag job runs but creates no
  # tag) cleanly short-circuits via the `tag.outputs.tag != ''` guard.
  # ----------------------------------------------------------------
  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: [resolve, tag]
    if: |
      always() &&
      (
        (needs.tag.result == 'success' && needs.tag.outputs.tag != '') ||
        needs.resolve.outputs.release_tag != ''
      )
    steps:
      - uses: actions/checkout@v4
        with:
          # Check out the freshly-created tag (or the manually-named
          # one) so cargo publish ships the bumped Cargo.toml the
          # `tag` job's cog run wrote, not the pre-bump HEAD.
          ref: ${{ needs.tag.outputs.tag || needs.resolve.outputs.release_tag }}
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: cargo

      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          cargo publish --verbose --token "${CARGO_REGISTRY_TOKEN}"

      # Re-create the .crate locally. `cargo publish` cleans up
      # target/package/*.crate after a successful upload; `cargo
      # package --no-verify` is fast (skips the re-build) and
      # deterministic given the same source tree, so the resulting
      # bytes match what crates.io just received.
      - name: Re-package .crate for artifact upload
        if: success()
        run: cargo package --no-verify

      # Upload the byte-identical `.crate` as a workflow artifact so
      # the `github-release` job can attach it to the GH Release
      # alongside the crates.io copy. Signing in a follow-up commit
      # will produce one sigstore signature that verifies both the
      # GH-Release-attached and crates.io copies.
      - name: Upload .crate as workflow artifact
        if: success()
        uses: actions/upload-artifact@v4
        with:
          name: crate-tarball
          path: target/package/*.crate
          if-no-files-found: error
          retention-days: 1

  # ----------------------------------------------------------------
  # GitHub release. Attaches the byte-identical `.crate` (from the
  # `publish` job's workflow artifact) and a SHA256SUMS file
  # alongside the auto-generated notes. The crates.io upload remains
  # the canonical install channel; the GH Release adds an
  # alternate-checksummed copy for downstream consumers that prefer
  # GitHub-hosted artifacts (and is the signing pipeline's anchor in
  # a follow-up commit).
  # ----------------------------------------------------------------
  github-release:
    name: Create GitHub release
    runs-on: ubuntu-latest
    needs: [resolve, tag, publish]
    if: |
      always() &&
      (
        (needs.tag.result == 'success' && needs.tag.outputs.tag != '') ||
        needs.resolve.outputs.release_tag != ''
      )
    # id-token + attestations widen this job's scope so cosign keyless +
    # actions/attest-build-provenance can mint an OIDC token and write to
    # the GitHub attestation store. Workflow-level perms stay narrower.
    permissions:
      contents: write
      id-token: write
      attestations: write
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.tag.outputs.tag || needs.resolve.outputs.release_tag }}

      - name: Download .crate artifact
        if: needs.publish.result == 'success'
        uses: actions/download-artifact@v4
        with:
          name: crate-tarball
          path: dist/

      - name: Generate SHA256SUMS
        if: needs.publish.result == 'success'
        run: |
          set -euo pipefail
          cd dist
          shasum -a 256 *.crate > SHA256SUMS
          echo "---"
          cat SHA256SUMS

      - name: Install cosign
        if: needs.publish.result == 'success'
        uses: sigstore/cosign-installer@7e8b541eb2e61bf99390e1afd4be13a184e9ebc5 # pin-target: v3

      # Keyless OIDC sign-blob over every staged artifact (.crate +
      # SHA256SUMS). Each gets a sibling `.sig` + `.pem` (the Fulcio-
      # issued ephemeral cert). Skips files that are themselves a sig
      # or cert so a re-run is idempotent.
      - name: Sign release artifacts (cosign keyless)
        if: needs.publish.result == 'success'
        run: |
          set -euo pipefail
          cd dist
          for f in *; do
            [ -f "$f" ] || continue
            case "$f" in *.sig|*.pem) continue ;; esac
            echo "Signing $f"
            cosign sign-blob --yes \
              --output-signature "${f}.sig" \
              --output-certificate "${f}.pem" \
              "$f"
          done
          ls -la

      # SLSA v1.0 build provenance for the .crate + SHA256SUMS. Stored
      # in the GitHub attestation store (queryable via `gh attestation
      # verify`); the local DSSE bundle is also captured for users who
      # want offline verification via `cosign verify-blob-attestation`.
      - name: Generate SLSA build provenance
        if: needs.publish.result == 'success'
        uses: actions/attest-build-provenance@e8998f949152b193b063cb0ec769d69d929409be # pin-target: v2
        with:
          subject-path: |
            dist/*.crate
            dist/SHA256SUMS

      - name: Compose release body
        env:
          TAG: ${{ needs.tag.outputs.tag || needs.resolve.outputs.release_tag }}
          PUBLISH_RESULT: ${{ needs.publish.result }}
        run: |
          set -euo pipefail
          {
            echo "## Install"
            echo
            echo "    cargo add pso-poseidon@${TAG#v}"
            echo
            echo "## Artifacts"
            echo
            echo "- \`pso-poseidon-${TAG#v}.crate\` — byte-identical to the crates.io upload."
            echo "- \`SHA256SUMS\` — SHA-256 of the .crate."
            echo "- \`*.sig\` + \`*.pem\` — sigstore cosign keyless signature + Fulcio cert per artifact."
            echo
            echo "## Verification"
            echo
            echo "See [SECURITY.md](https://github.com/psonet/pso-poseidon/blob/main/SECURITY.md) for the verify recipe."
            echo
            echo "## CI"
            echo
            echo "- crates.io publish: ${PUBLISH_RESULT}"
          } > RELEASE_BODY.md
          cat RELEASE_BODY.md

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ needs.tag.outputs.tag || needs.resolve.outputs.release_tag }}
          name: ${{ needs.tag.outputs.tag || needs.resolve.outputs.release_tag }}
          body_path: RELEASE_BODY.md
          generate_release_notes: true
          fail_on_unmatched_files: false
          files: |
            dist/*.crate
            dist/*.sig
            dist/*.pem
            dist/SHA256SUMS

  # ----------------------------------------------------------------
  # verify-release: post-publish smoke test. Re-downloads every asset
  # of the release we just cut and runs `cosign verify-blob` against
  # each one. Hard-fails the workflow on any bad signature — this is
  # the regression test for both the action SHA pins and the cert-
  # identity regex below. A typo in the regex silently accepts any
  # sigstore-signed blob, so iterate carefully when porting to other
  # repos.
  # ----------------------------------------------------------------
  verify-release:
    name: Verify signed release
    runs-on: ubuntu-latest
    needs: [resolve, tag, publish, github-release]
    if: |
      always() &&
      needs.github-release.result == 'success' &&
      needs.publish.result == 'success' &&
      (
        (needs.tag.result == 'success' && needs.tag.outputs.tag != '') ||
        needs.resolve.outputs.release_tag != ''
      )
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4

      - name: Install cosign
        uses: sigstore/cosign-installer@7e8b541eb2e61bf99390e1afd4be13a184e9ebc5 # pin-target: v3

      - name: Download release assets
        env:
          TAG: ${{ needs.tag.outputs.tag || needs.resolve.outputs.release_tag }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set -euo pipefail
          mkdir -p verify
          gh release download "$TAG" --dir verify --repo "${{ github.repository }}"
          ls -la verify/

      - name: Verify cosign signatures
        env:
          TAG: ${{ needs.tag.outputs.tag || needs.resolve.outputs.release_tag }}
        run: |
          set -euo pipefail
          cd verify
          # Identity regex pins the signature to a tag-triggered run of
          # THIS repo's ci.yml workflow. A signature minted on any other
          # workflow path, on a branch ref, or by another repo fails.
          IDENTITY_RE='^https://github\.com/psonet/pso-poseidon/\.github/workflows/ci\.yml@refs/(heads/main|tags/v[0-9]+\.[0-9]+\.[0-9]+)$'
          OIDC_ISSUER='https://token.actions.githubusercontent.com'
          failed=0
          shopt -s nullglob
          for f in *; do
            [ -f "$f" ] || continue
            case "$f" in *.sig|*.pem) continue ;; esac
            if [ ! -f "${f}.sig" ] || [ ! -f "${f}.pem" ]; then
              echo "::error::missing ${f}.sig or ${f}.pem alongside ${f}"
              failed=1
              continue
            fi
            echo "Verifying $f"
            if cosign verify-blob \
                --certificate "${f}.pem" \
                --signature "${f}.sig" \
                --certificate-identity-regexp "$IDENTITY_RE" \
                --certificate-oidc-issuer "$OIDC_ISSUER" \
                "$f"; then
              echo "  OK $f"
            else
              echo "::error::cosign verify-blob FAILED for $f"
              failed=1
            fi
          done
          if [ "$failed" -ne 0 ]; then
            echo "::error::one or more release artifacts failed signature verification"
            exit 1
          fi
          echo "All artifacts verified."