From 391e3a621c7c7ae3cfcd960f9749f52e53aad3f7 Mon Sep 17 00:00:00 2001 From: Kevaundray Wedderburn Date: Tue, 24 Sep 2024 00:32:35 +0100 Subject: [PATCH 1/6] add batch_inverse_scratch_pad --- cryptography/bls12_381/src/batch_inversion.rs | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/cryptography/bls12_381/src/batch_inversion.rs b/cryptography/bls12_381/src/batch_inversion.rs index 3ba32321..8d67de98 100644 --- a/cryptography/bls12_381/src/batch_inversion.rs +++ b/cryptography/bls12_381/src/batch_inversion.rs @@ -1,20 +1,34 @@ /// Given a vector of field elements {v_i}, compute the vector {v_i^(-1)} +/// +/// Panics if any of the elements are zero pub fn batch_inverse(v: &mut [F]) { - // Montgomery’s Trick and Fast Implementation of Masked AES + let mut scratch_pad = Vec::with_capacity(v.len()); + batch_inverse_scratch_pad(v, &mut scratch_pad); +} + +/// Given a vector of field elements {v_i}, compute the vector {v_i^(-1)} +/// +/// A scratchpad is used to avoid excessive allocations in the case that this method is +/// called repeatedly. +/// +/// Panics if any of the elements are zero +pub fn batch_inverse_scratch_pad(v: &mut [F], scratchpad: &mut Vec) { + // Montgomery's Trick and Fast Implementation of Masked AES // Genelle, Prouff and Quisquater // Section 3.2 // but with an optimization to multiply every element in the returned vector by coeff + // Clear the scratchpad and ensure it has enough capacity + scratchpad.clear(); + scratchpad.reserve(v.len()); + // First pass: compute [a, ab, abc, ...] - let mut prod = Vec::with_capacity(v.len()); let mut tmp = F::ONE; - for f in v.iter().filter(|f| !f.is_zero_vartime()) { + for f in v.iter() { tmp.mul_assign(f); - prod.push(tmp); + scratchpad.push(tmp); } - assert_eq!(prod.len(), v.len(), "inversion by zero is not allowed"); - // Invert `tmp`. tmp = tmp .invert() @@ -25,14 +39,12 @@ pub fn batch_inverse(v: &mut [F]) { .iter_mut() // Backwards .rev() - // Ignore normalized elements - .filter(|f| !f.is_zero_vartime()) // Backwards, skip last element, fill in one for last term. - .zip(prod.into_iter().rev().skip(1).chain(Some(F::ONE))) + .zip(scratchpad.iter().rev().skip(1).chain(Some(&F::ONE))) { // tmp := tmp * f; f := tmp * s = 1/f let new_tmp = tmp * *f; - *f = tmp * s; + *f = tmp * *s; tmp = new_tmp; } } From 9904aedddb10586d38a946b047052adcd6879153 Mon Sep 17 00:00:00 2001 From: Kevaundray Wedderburn Date: Tue, 24 Sep 2024 11:16:38 +0100 Subject: [PATCH 2/6] update to macos latests --- .github/workflows/release-node-bindings.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release-node-bindings.yml b/.github/workflows/release-node-bindings.yml index ffe31cb6..97efeda7 100644 --- a/.github/workflows/release-node-bindings.yml +++ b/.github/workflows/release-node-bindings.yml @@ -37,7 +37,7 @@ jobs: fail-fast: false matrix: settings: - - host: ubuntu-latest + - host: macos-latest target: x86_64-apple-darwin - host: ubuntu-latest target: aarch64-apple-darwin From b8f0b73a51d285dc53491dedda6c1ded1901c7d7 Mon Sep 17 00:00:00 2001 From: Kevaundray Wedderburn Date: Tue, 24 Sep 2024 11:23:01 +0100 Subject: [PATCH 3/6] update to 0.14 zig --- .github/workflows/release-node-bindings.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release-node-bindings.yml b/.github/workflows/release-node-bindings.yml index 97efeda7..d25ed9ad 100644 --- a/.github/workflows/release-node-bindings.yml +++ b/.github/workflows/release-node-bindings.yml @@ -37,7 +37,7 @@ jobs: fail-fast: false matrix: settings: - - host: macos-latest + - host: ubuntu-latest target: x86_64-apple-darwin - host: ubuntu-latest target: aarch64-apple-darwin @@ -77,7 +77,7 @@ jobs: - name: Setup Zig uses: goto-bus-stop/setup-zig@v2 with: - version: 0.13.0 + version: 0.14.0 - name: Install Binstall uses: cargo-bins/cargo-binstall@main From 3c5fea5ff55e2b9a0f71baa8bce0c868e7a498cb Mon Sep 17 00:00:00 2001 From: Kevaundray Wedderburn Date: Tue, 24 Sep 2024 11:25:54 +0100 Subject: [PATCH 4/6] 0.13 zig not available - pin zigbuild --- .github/workflows/release-node-bindings.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release-node-bindings.yml b/.github/workflows/release-node-bindings.yml index d25ed9ad..1c4ec643 100644 --- a/.github/workflows/release-node-bindings.yml +++ b/.github/workflows/release-node-bindings.yml @@ -77,13 +77,13 @@ jobs: - name: Setup Zig uses: goto-bus-stop/setup-zig@v2 with: - version: 0.14.0 + version: 0.13.0 - name: Install Binstall uses: cargo-bins/cargo-binstall@main - name: Install cargo-zigbuild - run: cargo binstall cargo-zigbuild -y + run: cargo binstall cargo-zigbuild@0.19.1 -y - name: Install cargo-xwin (Windows on Linux only) if: contains(matrix.settings.target, 'windows') From 563e217a8aa71a6c001d50c088c7504416d9b62a Mon Sep 17 00:00:00 2001 From: Kevaundray Wedderburn Date: Tue, 24 Sep 2024 11:34:49 +0100 Subject: [PATCH 5/6] downgrade to 1.80.0 --- .github/workflows/release-node-bindings.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release-node-bindings.yml b/.github/workflows/release-node-bindings.yml index 1c4ec643..e283399e 100644 --- a/.github/workflows/release-node-bindings.yml +++ b/.github/workflows/release-node-bindings.yml @@ -63,7 +63,7 @@ jobs: - name: Install Rust uses: dtolnay/rust-toolchain@master with: - toolchain: stable + toolchain: 1.80.0 targets: ${{ matrix.settings.target }} # llvm-preview-tools are needed for xwin, because we are compiling assembly (blst) From 7d190e1cbea8612111c042aa00e95058854de4d6 Mon Sep 17 00:00:00 2001 From: Kevaundray Wedderburn Date: Tue, 24 Sep 2024 11:37:21 +0100 Subject: [PATCH 6/6] do not pin to 0.19.1 --- .github/workflows/release-node-bindings.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release-node-bindings.yml b/.github/workflows/release-node-bindings.yml index e283399e..5bb32578 100644 --- a/.github/workflows/release-node-bindings.yml +++ b/.github/workflows/release-node-bindings.yml @@ -83,7 +83,7 @@ jobs: uses: cargo-bins/cargo-binstall@main - name: Install cargo-zigbuild - run: cargo binstall cargo-zigbuild@0.19.1 -y + run: cargo binstall cargo-zigbuild -y - name: Install cargo-xwin (Windows on Linux only) if: contains(matrix.settings.target, 'windows')