Skip to content

Commit

Permalink
refactor: separate OffsetToBytes trait from transpose module (PROOF-8…
Browse files Browse the repository at this point in the history
…97) (#95)

# Rationale for this change
In an effort to support Blitzar's new `packed_msm` interface, we need to
make a number of updates to the Dory commitment computation. A future PR
will require the use of the `OffsetToBytes` trait. This PR separates the
trait into a different file to make future usage easier.

# What changes are included in this PR?
- The `OffsetToBytes` trait is moved to its own file.

# Are these changes tested?
Yes.
  • Loading branch information
jacobtrombetta authored Aug 12, 2024
1 parent 17b57a4 commit 5a554dd
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::{pairings, transpose, DoryCommitment, DoryProverPublicSetup, DoryScalar, G1Affine};
use crate::base::commitment::CommittableColumn;
use crate::{
base::commitment::CommittableColumn, proof_primitive::dory::offset_to_bytes::OffsetToBytes,
};
use ark_bls12_381::Fr;
use ark_ec::CurveGroup;
use ark_std::ops::Mul;
use blitzar::{compute::ElementP2, sequence::Sequence};
use rayon::prelude::*;
use zerocopy::AsBytes;

#[tracing::instrument(name = "get_offset_commits (gpu)", level = "debug", skip_all)]
fn get_offset_commits(
Expand Down Expand Up @@ -94,7 +95,7 @@ fn compute_dory_commitment_impl<'a, T>(
where
&'a T: Into<DoryScalar>,
&'a [T]: Into<Sequence<'a>>,
T: AsBytes + Copy + transpose::OffsetToBytes,
T: OffsetToBytes,
{
let num_columns = 1 << setup.sigma();
let data_size = std::mem::size_of::<T>();
Expand Down
1 change: 1 addition & 0 deletions crates/proof-of-sql/src/proof_primitive/dory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type DeferredGT = deferred_msm::DeferredMSM<GT, F>;
type DeferredG1 = deferred_msm::DeferredMSM<G1Affine, F>;
type DeferredG2 = deferred_msm::DeferredMSM<G2Affine, F>;

mod offset_to_bytes;
mod pairings;
mod transpose;

Expand Down
110 changes: 110 additions & 0 deletions crates/proof-of-sql/src/proof_primitive/dory/offset_to_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use super::F;
use zerocopy::AsBytes;

pub trait OffsetToBytes {
const IS_SIGNED: bool;
fn min_as_fr() -> F;
fn offset_to_bytes(&self) -> Vec<u8>;
}

impl OffsetToBytes for u8 {
const IS_SIGNED: bool = false;

fn min_as_fr() -> F {
F::from(0)
}

fn offset_to_bytes(&self) -> Vec<u8> {
vec![*self]
}
}

impl OffsetToBytes for i16 {
const IS_SIGNED: bool = true;

fn min_as_fr() -> F {
F::from(i16::MIN)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let shifted = self.wrapping_sub(i16::MIN);
shifted.to_le_bytes().to_vec()
}
}

impl OffsetToBytes for i32 {
const IS_SIGNED: bool = true;

fn min_as_fr() -> F {
F::from(i32::MIN)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let shifted = self.wrapping_sub(i32::MIN);
shifted.to_le_bytes().to_vec()
}
}

impl OffsetToBytes for i64 {
const IS_SIGNED: bool = true;

fn min_as_fr() -> F {
F::from(i64::MIN)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let shifted = self.wrapping_sub(i64::MIN);
shifted.to_le_bytes().to_vec()
}
}

impl OffsetToBytes for i128 {
const IS_SIGNED: bool = true;

fn min_as_fr() -> F {
F::from(i128::MIN)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let shifted = self.wrapping_sub(i128::MIN);
shifted.to_le_bytes().to_vec()
}
}

impl OffsetToBytes for bool {
const IS_SIGNED: bool = false;

fn min_as_fr() -> F {
F::from(false)
}

fn offset_to_bytes(&self) -> Vec<u8> {
vec![*self as u8]
}
}

impl OffsetToBytes for u64 {
const IS_SIGNED: bool = false;

fn min_as_fr() -> F {
F::from(0)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let bytes = self.to_le_bytes();
bytes.to_vec()
}
}

impl OffsetToBytes for [u64; 4] {
const IS_SIGNED: bool = false;

fn min_as_fr() -> F {
F::from(0)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let slice = self.as_bytes();
slice.to_vec()
}
}
114 changes: 3 additions & 111 deletions crates/proof-of-sql/src/proof_primitive/dory/transpose.rs
Original file line number Diff line number Diff line change
@@ -1,116 +1,7 @@
use ark_bls12_381::Fr;
use zerocopy::AsBytes;

pub trait OffsetToBytes {
const IS_SIGNED: bool;
fn min_as_fr() -> Fr;
fn offset_to_bytes(&self) -> Vec<u8>;
}

impl OffsetToBytes for u8 {
const IS_SIGNED: bool = false;

fn min_as_fr() -> Fr {
Fr::from(0)
}

fn offset_to_bytes(&self) -> Vec<u8> {
vec![*self]
}
}

impl OffsetToBytes for i16 {
const IS_SIGNED: bool = true;

fn min_as_fr() -> Fr {
Fr::from(i16::MIN)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let shifted = self.wrapping_sub(i16::MIN);
shifted.to_le_bytes().to_vec()
}
}

impl OffsetToBytes for i32 {
const IS_SIGNED: bool = true;

fn min_as_fr() -> Fr {
Fr::from(i32::MIN)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let shifted = self.wrapping_sub(i32::MIN);
shifted.to_le_bytes().to_vec()
}
}

impl OffsetToBytes for i64 {
const IS_SIGNED: bool = true;

fn min_as_fr() -> Fr {
Fr::from(i64::MIN)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let shifted = self.wrapping_sub(i64::MIN);
shifted.to_le_bytes().to_vec()
}
}

impl OffsetToBytes for i128 {
const IS_SIGNED: bool = true;

fn min_as_fr() -> Fr {
Fr::from(i128::MIN)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let shifted = self.wrapping_sub(i128::MIN);
shifted.to_le_bytes().to_vec()
}
}

impl OffsetToBytes for bool {
const IS_SIGNED: bool = false;

fn min_as_fr() -> Fr {
Fr::from(false)
}

fn offset_to_bytes(&self) -> Vec<u8> {
vec![*self as u8]
}
}

impl OffsetToBytes for u64 {
const IS_SIGNED: bool = false;

fn min_as_fr() -> Fr {
Fr::from(0)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let bytes = self.to_le_bytes();
bytes.to_vec()
}
}

impl OffsetToBytes for [u64; 4] {
const IS_SIGNED: bool = false;

fn min_as_fr() -> Fr {
Fr::from(0)
}

fn offset_to_bytes(&self) -> Vec<u8> {
let slice = self.as_bytes();
slice.to_vec()
}
}
use crate::proof_primitive::dory::offset_to_bytes::OffsetToBytes;

#[tracing::instrument(name = "transpose_for_fixed_msm (gpu)", level = "debug", skip_all)]
pub fn transpose_for_fixed_msm<T: AsBytes + Copy + OffsetToBytes>(
pub fn transpose_for_fixed_msm<T: OffsetToBytes>(
column: &[T],
offset: usize,
rows: usize,
Expand All @@ -134,6 +25,7 @@ pub fn transpose_for_fixed_msm<T: AsBytes + Copy + OffsetToBytes>(
#[cfg(test)]
mod tests {
use super::*;
use zerocopy::AsBytes;

#[test]
fn we_can_transpose_empty_column() {
Expand Down

0 comments on commit 5a554dd

Please sign in to comment.