Skip to content

chore(crypto): Clean up BIP340 signature processing - #1233

Merged
randombit merged 3 commits into
masterfrom
jack/no-need-to-flip
Oct 2, 2024
Merged

randombit merged 3 commits into
masterfrom
jack/no-need-to-flip

Conversation

@randombit

Copy link
Copy Markdown
Contributor

A somewhat confusing k256 API led me to believe it did not correctly handle the situation where the y coordinate of the public key was odd. This is not the case, and the code to explicitly flip the public or private keys during signature generation/verification is not required.

A somewhat confusing k256 API led me to believe it did not correctly
handle the situation where the y coordinate of the public key was odd.
This is not the case, and the code to explicitly flip the public or
private keys during signature generation/verification is not required.
@randombit
randombit requested a review from a team August 29, 2024 20:29
@randombit randombit changed the title chore(crypto): Clean up BIP340 signature generation chore(crypto): Clean up BIP340 signature processing Aug 29, 2024
@github-actions github-actions Bot added the chore label Aug 29, 2024
Comment thread rs/crypto/secp256k1/src/lib.rs Resolved
@randombit
randombit enabled auto-merge October 2, 2024 14:19
@randombit
randombit added this pull request to the merge queue Oct 2, 2024
Merged via the queue into master with commit 9fe63e2 Oct 2, 2024
@randombit
randombit deleted the jack/no-need-to-flip branch October 2, 2024 20:14
AleDema pushed a commit to AleDema/ic that referenced this pull request Nov 18, 2026
…#12155)

Upgrades `aspect_bazel_lib` from 2.19.3 to
[2.22.5](https://github.com/bazel-contrib/bazel-lib/releases/tag/v2.22.5),
its latest release. 2.x is the last `aspect_bazel_lib` line: from 3.0 on
the module is published as `bazel_lib`, which is already in our graph at
3.2.2 (through `llvm`, `rules_qemu`, `rules_distroless`,
`rules_foreign_cc`, `tar.bzl` and `yq.bzl`).

The bump comes with a small patch to `aspect_bazel_lib`'s
`MODULE.bazel`. Without it, `aspect_bazel_lib`'s own toolchains would
take over the toolchain types that it now aliases from `bazel_lib` and
`tar.bzl` (since 2.21.1 and 2.22.0). Then hermetic-llvm would assemble
its toolchain with `aspect_bazel_lib`'s tools instead of `bazel_lib`'s,
and every C/C++/Rust build would break as soon as `bazel_lib` >= 3.8.0
enters the graph (details below). With the patch, the build graph of the
release artifacts resolves the same toolchains as on master, and the
release artifacts are byte-identical.

## Changes

- **`MODULE.bazel`**: version bump, plus a `single_version_override`
that applies the patch below and pins 2.22.5. The pin is needed because
Bazel applies a patch to a module's `MODULE.bazel` to every version of
the module that it visits during resolution, and `rules_oci` 2.0.0 asks
for 2.7.2, where the patch doesn't apply.
- The pin also holds `aspect_bazel_lib` at 2.22.5 when another module
asks for a newer version, and Bazel doesn't warn about that. The comment
above the override says so and points to `bazel mod explain
aspect_bazel_lib --verbose`, which marks the requests that the pin
overrides, e.g. `[email protected] # (was 2.7.2, cause
single_version_override)` under `rules_oci` 2.0.0. Nothing asks for more
than 2.22.5 today. In our graph, `rules_oci` 2.0.0 asks for 2.7.2 and
`jq.bzl` 0.1.0 for 2.8.1. Among the latest releases of our direct
dependencies, the highest request is 2.21.1 (`rules_oci` 2.3.5).
- BCR's `aspect_bazel_lib/2.22.5` carries only its usual release patches
(version stamp, Go dev dependencies).
- 2.22.5 adds a dependency on `bazel_lib` 3.0.0 and asks for `tar.bzl`
0.5.1, `bazel_skylib` 1.8.1 and `stardoc` 0.7.1. We already resolve
`bazel_lib` 3.2.2, `tar.bzl` 0.10.4, `bazel_skylib` 1.9.0 and `stardoc`
0.7.2, so no other module's resolved version changes.
- **`bazel/aspect_bazel_lib_aliased_toolchains.patch`** (new): removes 5
of the 9 `register_toolchains` entries from `aspect_bazel_lib`'s
`MODULE.bazel`, those of the five toolchain types that 2.21.1 and 2.22.0
turned into aliases (`copy_directory`, `copy_to_directory`, `coreutils`,
`expand_template` and `tar`). jq, yq, zstd and bats are still
`aspect_bazel_lib`'s own toolchain types and keep their registrations.
The patch applies to the BCR source with no offset or fuzz.

## Why the patch


[bazel-contrib/bazel-lib#1153](bazel-contrib/bazel-lib#1153)
(2.21.1) turned the `tar` toolchain type into an alias of `tar.bzl`'s.
[bazel-contrib/bazel-lib#1216](bazel-contrib/bazel-lib#1216)
(2.22.0) turned the `copy_directory`, `copy_to_directory`, `coreutils`
and `expand_template` toolchain types into aliases of `bazel_lib`'s, so
that rules from both modules create identical actions. But
`aspect_bazel_lib` still registers its own toolchains for all five. It
is a direct dependency of the root module, while `bazel_lib` and
`tar.bzl` are transitive ones, so its registrations come first and win
for every rule that requests one of the shared types. With only the
version bump, `bazel cquery` shows two switches:

- **Release artifacts:** hermetic-llvm assembles its resource directory,
crt objects and the libc++/libc++abi/libunwind/glibc search directories
with `bazel_lib`'s `copy_file` and `copy_to_directory`. These 22
`CopyFile` and 86 `CopyToDirectory` actions (across configurations) are
the only users of these toolchains in the build graph of the release
artifacts. They would switch from `bazel_lib` 3.2.2's tools to
`aspect_bazel_lib`'s, e.g. from uutils coreutils 0.5.0 to 0.1.0.
- **Test and Rosetta images:** the `tar.bzl` and `rules_distroless`
targets would switch from `tar.bzl`'s bsdtar
(hermeticbuild/bsdtar-prebuilt v3.8.1-3) to `aspect_bazel_lib`'s
(aspect-build/bsdtar-prebuilt v3.8.1). These are
`//rs/tests:{sh,awk,ca_certificates,dpkg_status,passwd,ubuntu_packages_tar}`
and the `passwd` layers in `//rs/rosetta-api/{icp,icrc1}` and
`//rs/tests/httpbin-rs`. For `sh`, `awk` and the `passwd` targets,
`tar.bzl`'s `tar` writes the `mtree` with `bazel_lib`'s
`expand_template`, and those actions would switch to
`aspect_bazel_lib`'s `expand_template` binary as well.

That alone would be harmless today. But `bazel_lib` 3.8.0
([bazel-contrib/bazel-lib#1310](bazel-contrib/bazel-lib#1310))
passes `copy_to_directory`'s arguments in a params file, which
`aspect_bazel_lib`'s binary doesn't accept. So once any module brings in
`bazel_lib` >= 3.8.0, every one of those `CopyToDirectory` actions
fails, and with them every C/C++/Rust build. This is
[bazel-contrib/bazel-lib#1312](bazel-contrib/bazel-lib#1312).
The latest releases of our direct dependencies ask for at most
`bazel_lib` 3.2.2, so this would only bite later.

Upstream fixed dfinity#1312 on its 2.x branch with
[bazel-contrib/bazel-lib#1313](bazel-contrib/bazel-lib#1313),
merged on 2026-11-14. No release has it yet: 2.22.5 is still the latest
2.x release. dfinity#1313 removes only the `copy_directory`,
`copy_to_directory` and `coreutils` registrations, which covers the
switch in the release artifacts but not the one in the images. This
patch also removes the `expand_template` and `bsd_tar` registrations.
That keeps the image targets above on the toolchains they use on master,
and all five aliased types resolve the same way.

With the patch, each aliased type resolves to the toolchains of the
module that defines it:

- The build graph of the release artifacts resolves exactly the
toolchains it does on master.
- The `tar.bzl` and `rules_distroless` targets above keep `tar.bzl`'s
bsdtar and `bazel_lib`'s `expand_template`, as on master.
- `rules_oci` 2.0.0's `oci_image` and `oci_load`, and our
`expand_template`, ask for `aspect_bazel_lib`'s alias types. So they now
get `tar.bzl`'s bsdtar and `bazel_lib`'s coreutils (0.5.0) and
`expand_template`. On master they get `aspect_bazel_lib`'s bsdtar 3.7.9
and coreutils 0.0.27.
- Both sets of toolchains provide the same fields (`tarinfo.binary`,
`coreutils_info.bin`, `expand_template_info.bin`,
`copy_to_directory_info.bin`). The `expand_template`, `copy_directory`
and `copy_to_directory` Go tools of `aspect_bazel_lib` 2.22.5 and
`bazel_lib` 3.2.2 are built from the same `main.go` files. Only their
shared helper package differs: `bazel_lib`'s also tries reflink copies
on Linux. The outputs are byte-identical (see Verification).

## Upstream changes that touch our setup

- Toolchain aliases: `tar`
([dfinity#1153](bazel-contrib/bazel-lib#1153), 2.21.1),
and `copy_directory`, `copy_to_directory`, `coreutils` and
`expand_template`
([dfinity#1216](bazel-contrib/bazel-lib#1216), 2.22.0).
See above.
- `aspect_bazel_lib`'s own bsdtar moves from 3.7.9 to 3.8.1
([dfinity#1126](bazel-contrib/bazel-lib#1126), 2.20.0),
and its coreutils from 0.0.27 to 0.1.0
([dfinity#1136](bazel-contrib/bazel-lib#1136), 2.21.0).
With the patch, nothing in our build uses these two any more.
- `expand_template` now adds the runfiles of its `data` to its own
([dfinity#1171](bazel-contrib/bazel-lib#1171), 2.21.2).
Our two `expand_template` targets,
`//rs/rosetta-api/{icp,icrc1}:stamped`, have no `data`.
- `copy_directory`, `copy_to_directory`, `copy_file` and `copy_to_bin`
now forward to `bazel_lib`'s implementations
([dfinity#1229](bazel-contrib/bazel-lib#1229),
[dfinity#1233](bazel-contrib/bazel-lib#1233), 2.22.4
and 2.22.5). We don't use them directly, only through `rules_oci`.
- Not relevant for us: jq stays 1.7, and zstd stays 1.5.6 (the same
binaries from a new URL). yq moves to 4.45.2, but nothing in our build
uses `aspect_bazel_lib`'s yq toolchain.

## Verification

All in the dev container:

- `bazel mod graph`: compared with master, only `aspect_bazel_lib`
changes (2.19.3 → 2.22.5). There are no new warnings; the existing
`rules_qemu` `use_repo` warning is unchanged.
- **After merging master**: dfinity#12150, dfinity#12152 and dfinity#12154 changed the
`bazel_skylib`, `rules_shell` and `buildifier_prebuilt` lines around
this PR's `aspect_bazel_lib` bump. The merge keeps master's versions of
those three and this PR's `aspect_bazel_lib` 2.22.5 with its comment and
override. Against master, `bazel mod graph` still changes only
`aspect_bazel_lib`. `bazel mod explain aspect_bazel_lib --verbose` lists
only the root module and `rules_oci` 2.0.0 (asking for 2.7.2), so no
module asks for a newer version that the pin would override. `bazel
build //... --nobuild` analyzes all 3389 targets, and `bazel run
//:buildifier` changes nothing.
- `bazel build //... --nobuild`: all 3387 targets analyze.
- Linter job:
`//pre-commit:{shfmt-check,ruff-check,buf-check,DFINITY-capitalization-check,do-not-merge-check,buildifier-check,gazelle-check,buf-breaking-check}`
pass. `buildifier-fix`, `shfmt-fix`, `ruff-fix` and `//:gazelle` leave
the tree clean.
- Toolchain resolution: for the release targets below, `bazel cquery
'deps(...)'` returns the same 110,935 configured targets as on master,
with the same toolchain targets. `aspect_bazel_lib` isn't among them.
Without the patch, the `coreutils` and `copy_to_directory` toolchains
switch to `aspect_bazel_lib`'s, as described above.
- **Release artifacts are byte-identical.** I built
`//publish/binaries:bundle`, `//publish/canisters:bundle` and the
unstamped IC-OS bundles that `ci/container/build-ic.sh --icos` builds
(GuestOS prod disk and update, GuestOS recovery update, HostOS prod
update, SetupOS prod). All 133 output files have the same SHA-256 as a
build of master at c712eb2. No sandboxed action ran: apart from 26
actions that run unsandboxed (`local`), every action was a cache hit.
- **The targets that use `aspect_bazel_lib`, or its toolchains through
`rules_oci`, produce byte-identical outputs.** I built these 21 targets,
which cover every kind of use, with 2.19.3 and with this PR in the same
output base. Both builds produce the same 77 files, and the 4 OCI layout
directories have the same structure and file modes:
  - `//rs/tests:{sh,awk}` (`tar`)
- `//rs/tests:{ca_certificates,dpkg_status,passwd,ubuntu_packages_tar}`
(`rules_distroless`)
- the OCI images and `oci_load` tarballs
`//rs/tests:{ubuntu_test_runtime_image_src,ubuntu_test_runtime.tar,static-file-server.tar,minica.tar}`,
`//rs/tests/httpbin-rs:{httpbin_image_src,httpbin.tar}`,
`//rs/rosetta-api/icp:{rosetta_image,rosetta_image.tar}` and
`//rs/rosetta-api/icrc1:{ic_icrc_rosetta_image,icrc_rosetta_image.tar}`
- `//rs/tests:colocate_uvm_config_image`, which packs the test-runtime
image tarball
- the Rosetta push scripts `//rs/rosetta-api/icp:push_rosetta_image` and
`//rs/rosetta-api/icrc1:push_ic_icrc_rosetta_image`
- their `:stamped` tag files, built both unstamped and with `--stamp
--embed_label=...` (which `rosetta-release.yml` passes); with stamping,
`expand_template` runs its toolchain's binary, now `bazel_lib`'s
- I also diffed the `bazel aquery` action keys of everything these
targets depend on, master against this PR. 58 of the 8,363 actions
change, all of them `rules_oci` actions whose tools are now
`bazel_lib`'s and `tar.bzl`'s: the 10 `OCIDescriptor` (`oci_image`) and
6 `OCITarball` (`oci_load`) actions, the 3 `CopyToDirectory` actions
that lay out pulled base images, and the template and runfiles actions
of their scripts. The images, tarballs and scripts they produce are
among the identical files above.
- Not built locally: the other `oci_load` tarballs of pulled images and
the other UVM config images under `//rs/tests` (e.g. `bitcoind.tar`,
`jaeger.tar`, `vector-with-log-fetcher.tar`, `ckbtc_uvm_config_image`).
They use the same rules as `static-file-server.tar`, `minica.tar` and
`colocate_uvm_config_image`.
- Not run locally: the 476 tests that depend on these targets are all
system tests under `//rs/tests`. CI runs them, along with the full build
and test suite.

The second commit changes only comments: the patch header and the
`MODULE.bazel` comment above the override. After it, I re-ran:

- The new patch applies with `patch -F0` (no offset or fuzz) to the
`MODULE.bazel` that the BCR serves for 2.22.5. The result has the same
SHA-256 as with the first commit's patch, and so does the
`aspect_bazel_lib` `MODULE.bazel` that Bazel fetched.
- `bazel mod graph` (depth 1 and full): the same output as before the
commit, with no new warnings. `bazel mod explain aspect_bazel_lib
--verbose` shows `[email protected] # (was 2.7.2, cause
single_version_override)` under `rules_oci` 2.0.0.
- I checked the pin's effect on newer requests in a scratch workspace
whose root module asks for `aspect_bazel_lib` 2.7.2 and depends on
`jq.bzl` 0.1.0, which asks for 2.8.1. With
`single_version_override(version = "2.7.2")`, Bazel resolves 2.7.2
without a warning, and `mod explain --verbose` shows `(was 2.8.1, cause
single_version_override)`. Without the override, it resolves 2.8.1 and
warns that the root module asks for 2.7.2.
- `bazel build //... --nobuild`: all 3387 targets analyze. The
Linter-job checks pass, and the autofixes plus `//:buildifier` leave the
tree clean.
- `bazel cquery 'deps(...)'` returns the same 110,935 configured targets
for the release targets, and the same 53,045 for the targets that use
the module, as before the commit. `bazel aquery`: all 8,363 action keys
in the deps of the 21 targets above are unchanged. So I didn't rebuild
the release artifacts for this commit.

## Follow-ups

- dfinity#12157 (`rules_oci` 2.3.5, which needs `aspect_bazel_lib` >= 2.21.1)
is stacked on this PR, so it lands after it.
- Once an `aspect_bazel_lib` 2.x release includes
bazel-contrib/bazel-lib#1313, this patch no longer applies (its hunk
fails against the 2.x branch). Then reduce it to the `expand_template`
and `bsd_tar` registrations, as the TODO in the patch says, and keep the
version pin. Dropping the patch instead would make the `tar` and
`expand_template` types resolve to `aspect_bazel_lib`'s bsdtar and
`expand_template`, including for the `tar.bzl` and `rules_distroless`
targets above.
- Possibly move our three direct uses of `aspect_bazel_lib`
(`expand_template` in the two Rosetta packages, `tar` in `//rs/tests`)
to `bazel_lib` and `tar.bzl`, its 3.x successors. `rules_oci` 2.x still
depends on `aspect_bazel_lib`, though.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants