Skip to content

Commit

Permalink
Update sha256 coprocessor discarded bits (#1127)
Browse files Browse the repository at this point in the history
* Update sha256 coprocessor discarded bits

Previously, the pallas curve had a capacity of 254 bits. The new bn256
curve has a capacity of 253 bits. This makes the pack_bits gadget used by
sha256 coprocessor drop the three most significant bits of the output
instead of just two.

This commit updates the coprocessor's evaluate_simple implementation to
set the correct number of bits to zero based on the field used to avoid a
panic caused by a mismatch between the synthesize and evaluate outputs.

* clippy

---------

Co-authored-by: Arthur Paulino <arthurleonardo.ap@gmail.com>
  • Loading branch information
cpacia and arthurpaulino authored Feb 21, 2024
1 parent 5458a53 commit ce2f33a
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/coprocessor/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ fn compute_sha256<F: LurkField, T: Tag>(n: usize, z_ptrs: &[ZPtr<T, F>]) -> F {

hasher.update(input);
let mut bytes = hasher.finalize();
bytes.reverse();
let l = bytes.len();
// Discard the two most significant bits.
bytes[l - 1] &= 0b00111111;

// The pack_bits gadget used by the synthesize_sha256 function
// sets the n most significant bits of the hash output to zero,
// where n is 256 minus the field's capacity. We do the same
// here to match the output.
discard_bits::<F>(&mut bytes);
bytes.reverse();
F::from_bytes(&bytes).unwrap()
}

Expand Down Expand Up @@ -127,6 +129,20 @@ impl<F: LurkField> Sha256Coprocessor<F> {
}
}

// Retains the Scalar::CAPACITY last bits of a big-endian input
fn discard_bits<Scalar: LurkField>(bytes: &mut [u8]) {
let bits_to_zero = 256 - Scalar::CAPACITY as usize;
let full_bytes_to_zero = bits_to_zero / 8;
let partial_bits_to_zero = bits_to_zero % 8;

bytes[..full_bytes_to_zero].iter_mut().for_each(|b| *b = 0);

if partial_bits_to_zero > 0 {
let mask = 0xFF >> partial_bits_to_zero;
bytes[full_bytes_to_zero] &= mask;
}
}

#[derive(Clone, Debug, Coproc, Serialize, Deserialize)]
pub enum Sha256Coproc<F: LurkField> {
SC(Sha256Coprocessor<F>),
Expand Down

1 comment on commit ce2f33a

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Fibonacci GPU benchmark.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
32 vCPUs
125 GB RAM
Workflow run: https://github.com/lurk-lab/lurk-rs/actions/runs/7986793880

Benchmark Results

LEM Fibonacci Prove - rc = 100

ref=5458a5308d6a370a09ba6543aab3a1a3d7883c1a ref=ce2f33aab56e8fa0a6666e417f5a2ac6a2ab6c77
num-100 1.45 s (✅ 1.00x) 1.45 s (✅ 1.00x faster)
num-200 2.78 s (✅ 1.00x) 2.78 s (✅ 1.00x faster)

LEM Fibonacci Prove - rc = 600

ref=5458a5308d6a370a09ba6543aab3a1a3d7883c1a ref=ce2f33aab56e8fa0a6666e417f5a2ac6a2ab6c77
num-100 1.83 s (✅ 1.00x) 1.82 s (✅ 1.01x faster)
num-200 3.02 s (✅ 1.00x) 3.02 s (✅ 1.00x faster)

Made with criterion-table

Please sign in to comment.