Skip to content

Commit

Permalink
feat: port to no_std env
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Jun 21, 2024
1 parent 5f4a3ab commit 3d3f8db
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
- name: Build
run: cargo build --verbose --features=cache
- name: Run tests
run: cargo test --verbose --features=cache
run: cargo test --verbose --features=kzg-test
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const-chunks = "0.3.0"
bls12_381 = "0.8.0"

[features]
default = ["std"]
default = []
serde = ["dep:serde"]
std = ["hex/std", "serde/std"]
kzg-test = ['std', 'cache']
std = ["dep:serde", "serde/std"]
cache = []

[[bin]]
Expand Down
20 changes: 17 additions & 3 deletions src/dtypes.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::enums::KzgError;
use crate::hex_to_bytes;

use alloc::string::ToString;

macro_rules! define_bytes_type {
($name:ident, $size:expr) => {
#[derive(Debug, Clone)]
pub struct $name([u8; $size]);

impl $name {
#[sp1_derive::cycle_tracker]
pub fn from_slice(slice: &[u8]) -> Result<Self, KzgError> {
if slice.len() != $size {
return Err(KzgError::InvalidBytesLength(
Expand All @@ -19,14 +20,12 @@ macro_rules! define_bytes_type {
Ok($name(bytes))
}

#[sp1_derive::cycle_tracker]
pub fn from_hex(hex_str: &str) -> Result<Self, KzgError> {
Self::from_slice(&hex_to_bytes(hex_str).unwrap())
}
}

impl Into<[u8; $size]> for $name {
#[sp1_derive::cycle_tracker]
fn into(self) -> [u8; $size] {
self.0
}
Expand All @@ -36,3 +35,18 @@ macro_rules! define_bytes_type {

define_bytes_type!(Bytes32, 32);
define_bytes_type!(Bytes48, 48);

#[cfg(test)]
mod tests {
#[test]
fn test_bytes32() {
let bytes = crate::dtypes::Bytes32::from_slice(&[0u8; 32]).unwrap();
assert_eq!(bytes.0.len(), 32);
}

#[test]
fn test_bytes48() {
let bytes = crate::dtypes::Bytes48::from_slice(&[0u8; 48]).unwrap();
assert_eq!(bytes.0.len(), 48);
}
}
2 changes: 2 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::string::String;

#[derive(Debug, Clone)]
pub enum KzgError {
/// The supplied data is invalid in some way.
Expand Down
49 changes: 44 additions & 5 deletions src/kzg_proof.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::dtypes::*;
use crate::enums::KzgError;
use crate::trusted_setup::KzgSettings;
use alloc::vec::Vec;

use alloc::{string::ToString, vec::Vec};
use bls12_381::{pairing, G1Affine, G2Affine, Scalar};

// #[sp1_derive::cycle_tracker]
fn safe_g1_affine_from_bytes(bytes: &Bytes48) -> Result<G1Affine, KzgError> {
let g1 = G1Affine::from_compressed(&(bytes.clone().into()));
if g1.is_none().into() {
Expand All @@ -15,7 +15,6 @@ fn safe_g1_affine_from_bytes(bytes: &Bytes48) -> Result<G1Affine, KzgError> {
Ok(g1.unwrap())
}

// #[sp1_derive::cycle_tracker]
fn safe_scalar_affine_from_bytes(bytes: &Bytes32) -> Result<Scalar, KzgError> {
let lendian: [u8; 32] = Into::<[u8; 32]>::into(bytes.clone())
.iter()
Expand All @@ -36,7 +35,6 @@ fn safe_scalar_affine_from_bytes(bytes: &Bytes32) -> Result<Scalar, KzgError> {
pub struct KzgProof {}

impl KzgProof {
#[sp1_derive::cycle_tracker]
pub fn verify_kzg_proof(
commitment_bytes: &Bytes48,
z_bytes: &Bytes32,
Expand Down Expand Up @@ -82,13 +80,54 @@ impl KzgProof {
}
}

#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
use crate::{test_format::Test, KzgProof, KzgSettings};
use super::*;
use serde_derive::Deserialize;
use std::{fs, path::PathBuf};

const VERIFY_KZG_PROOF_TESTS: &str = "tests/verify_kzg_proof/*/*";

#[derive(Deserialize)]
pub struct Input<'a> {
commitment: &'a str,
z: &'a str,
y: &'a str,
proof: &'a str,
}

impl Input<'_> {
pub fn get_commitment(&self) -> Result<Bytes48, KzgError> {
Bytes48::from_hex(self.commitment)
}

pub fn get_z(&self) -> Result<Bytes32, KzgError> {
Bytes32::from_hex(self.z)
}

pub fn get_y(&self) -> Result<Bytes32, KzgError> {
Bytes32::from_hex(self.y)
}

pub fn get_proof(&self) -> Result<Bytes48, KzgError> {
Bytes48::from_hex(self.proof)
}
}

#[derive(Deserialize)]
pub struct Test<'a> {
#[serde(borrow)]
pub input: Input<'a>,
output: Option<bool>,
}

impl Test<'_> {
pub fn get_output(&self) -> Option<bool> {
self.output
}
}

#[test]
#[cfg(feature = "cache")]
fn test_verify_kzg_proof() {
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
#[macro_use]
extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

pub mod consts;
pub mod dtypes;
pub mod enums;
pub mod kzg_proof;
pub mod test_format;
pub mod trusted_setup;

use alloc::vec::Vec;
pub use consts::*;
pub use dtypes::*;
pub use kzg_proof::KzgProof;
Expand Down
43 changes: 0 additions & 43 deletions src/test_format.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/trusted_setup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use core::{mem::transmute, slice};

use alloc::{
string::{String, ToString},
vec::Vec,
};
use bls12_381::{G1Affine, G2Affine};

use crate::{
Expand Down Expand Up @@ -51,7 +55,6 @@ pub struct KzgSettingsOwned {

impl KzgSettings {
#[cfg(feature = "cache")]
#[sp1_derive::cycle_tracker]
pub fn load_trusted_setup_file() -> Result<Self, KzgError> {
Ok(get_kzg_settings())
}
Expand Down Expand Up @@ -124,7 +127,6 @@ pub fn load_trusted_setup_file_brute() -> Result<KzgSettingsOwned, KzgError> {
})
}

#[sp1_derive::cycle_tracker]
fn bit_reversal_permutation(g1_points: &[G1Affine]) -> Result<[G1Affine; NUM_G1_POINTS], KzgError> {
let n = g1_points.len();
assert!(n.is_power_of_two(), "n must be a power of 2");
Expand All @@ -141,14 +143,12 @@ fn bit_reversal_permutation(g1_points: &[G1Affine]) -> Result<[G1Affine; NUM_G1_
Ok(bit_reversed_permutation)
}

#[sp1_derive::cycle_tracker]
fn pairings_verify(a1: G1Affine, a2: G2Affine, b1: G1Affine, b2: G2Affine) -> bool {
let pairing1 = bls12_381::pairing(&a1, &a2);
let pairing2 = bls12_381::pairing(&b1, &b2);
pairing1 == pairing2
}

#[sp1_derive::cycle_tracker]
fn is_trusted_setup_in_lagrange_form(
g1_points: &[G1Affine],
g2_points: &[G2Affine],
Expand Down

0 comments on commit 3d3f8db

Please sign in to comment.