Skip to content

Commit

Permalink
fix:aggregate podr2 batch proof verification result (#418)
Browse files Browse the repository at this point in the history
* chores:delete or add some log

* feat:add request_aggregate_signature

* add some debug log
  • Loading branch information
democ98 authored Dec 2, 2024
1 parent 8668e90 commit ca093c4
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 103 deletions.
148 changes: 83 additions & 65 deletions crates/bloom-filter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
extern crate alloc;

use alloc::boxed::Box;
use alloc::vec::Vec;
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::RuntimeDebug;
Expand All @@ -12,78 +13,95 @@ pub struct BloomFilter(pub [u64; 256]);

#[derive(Debug)]
pub enum BloomError {
InsertError,
DeleteError,
Overflow,
BinaryError,
InsertError,
DeleteError,
Overflow,
BinaryError,
LengthError,
}

impl Default for BloomFilter {
fn default() -> Self {
let value: [u64; 256] = [0u64; 256];
BloomFilter(value)
}
fn default() -> Self {
let value: [u64; 256] = [0u64; 256];
BloomFilter(value)
}
}

impl BloomFilter {
pub fn insert(&mut self, elem: [u8; 256]) -> Result<(), BloomError> {
let mut index: usize = 0;
for value in elem {
if value != 1 && value != 0 {
return Err(BloomError::InsertError);
}
self.0[index] = self.0[index] + value as u64;
index = index + 1;
}

Ok(())
}

pub fn delete(&mut self, elem: [u8; 256]) -> Result<(), BloomError> {
let mut index: usize = 0;
for value in elem {
if value != 1 && value != 0 {
return Err(BloomError::DeleteError);
}
self.0[index] = self.0[index] - value as u64;
index = index + 1;
}

Ok(())
}
impl TryFrom<Vec<u64>> for BloomFilter {
type Error = BloomError;

fn try_from(value: Vec<u64>) -> Result<Self, Self::Error> {
if value.len() != 256 {
return Err(BloomError::LengthError);
}

let array: [u64; 256] = match value.try_into() {
Ok(arr) => arr,
Err(_) => return Err(BloomError::BinaryError),
};

Ok(BloomFilter(array))
}
}

impl BloomFilter {
pub fn insert(&mut self, elem: [u8; 256]) -> Result<(), BloomError> {
let mut index: usize = 0;
for value in elem {
if value != 1 && value != 0 {
return Err(BloomError::InsertError);
}
self.0[index] = self.0[index] + value as u64;
index = index + 1;
}

Ok(())
}

pub fn delete(&mut self, elem: [u8; 256]) -> Result<(), BloomError> {
let mut index: usize = 0;
for value in elem {
if value != 1 && value != 0 {
return Err(BloomError::DeleteError);
}
self.0[index] = self.0[index] - value as u64;
index = index + 1;
}

Ok(())
}
}

pub fn binary(data: [u8; 64]) -> Result<Box<[u8; 256]>, BloomError> {
let mut elem: Box<[u8; 256]> = Box::new([0u8; 256]);
let mut index: usize = 0;
for value in data.iter() {
let binary = match value {
b'0' => [0, 0, 0, 0],
b'1' => [0, 0, 0, 1],
b'2' => [0, 0, 1, 0],
b'3' => [0, 0, 1, 1],
b'4' => [0, 1, 0, 0],
b'5' => [0, 1, 0, 1],
b'6' => [0, 1, 1, 0],
b'7' => [0, 1, 1, 1],
b'8' => [1, 0, 0, 0],
b'9' => [1, 0, 0, 1],
b'a' => [1, 0, 1, 0],
b'b' => [1, 0, 1, 1],
b'c' => [1, 1, 0, 0],
b'd' => [1, 1, 0, 1],
b'e' => [1, 1, 1, 0],
b'f' => [1, 1, 1, 1],
_ => return Err(BloomError::BinaryError),
};

elem[index * 4] = binary[0];
elem[index * 4 + 1] = binary[1];
elem[index * 4 + 2] = binary[2];
elem[index * 4 + 3] = binary[3];

index = index + 1;
}
Ok(elem)
let mut elem: Box<[u8; 256]> = Box::new([0u8; 256]);
let mut index: usize = 0;
for value in data.iter() {
let binary = match value {
b'0' => [0, 0, 0, 0],
b'1' => [0, 0, 0, 1],
b'2' => [0, 0, 1, 0],
b'3' => [0, 0, 1, 1],
b'4' => [0, 1, 0, 0],
b'5' => [0, 1, 0, 1],
b'6' => [0, 1, 1, 0],
b'7' => [0, 1, 1, 1],
b'8' => [1, 0, 0, 0],
b'9' => [1, 0, 0, 1],
b'a' => [1, 0, 1, 0],
b'b' => [1, 0, 1, 1],
b'c' => [1, 1, 0, 0],
b'd' => [1, 1, 0, 1],
b'e' => [1, 1, 1, 0],
b'f' => [1, 1, 1, 1],
_ => return Err(BloomError::BinaryError),
};

elem[index * 4] = binary[0];
elem[index * 4 + 1] = binary[1];
elem[index * 4 + 2] = binary[2];
elem[index * 4 + 3] = binary[3];

index = index + 1;
}
Ok(elem)
}
27 changes: 22 additions & 5 deletions crates/ces-pdp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl Keys {
"The size of file {:?} is {}, which cannot be divisible by {} blocks",
name, file_size, n_blocks
)),
})
});
};

let each_chunks_size = file_size / n_blocks;
Expand All @@ -218,10 +218,11 @@ impl Keys {
let _ = f.seek(SeekFrom::Start(offset));
match f.read_exact(&mut chunks) {
Ok(_) => {},
Err(e) =>
Err(e) => {
return Err(PDPError {
error_code: FailCode::InternalError(format!("Fail in read file :{:?}", e.to_string())),
}),
})
},
};

let tx = tx.clone();
Expand Down Expand Up @@ -284,7 +285,7 @@ impl Keys {
"The size of file {:?} is {}, which cannot be divisible by {} blocks",
name, file_size, n_blocks
)),
})
});
};

let each_chunks_size = file_size / n_blocks;
Expand Down Expand Up @@ -422,6 +423,22 @@ impl Keys {
Ok(sigma.to_string())
}

pub fn aggr_append_proof(&self, mut aggr_sigma: String, mut sub_sigma: String) -> Result<String, PDPError> {
let n = num_bigint::BigUint::from_bytes_be(&self.pkey.n().to_bytes_be());
if aggr_sigma == "".to_string() {
aggr_sigma = "1".to_string()
}
let mut sigma = num_bigint::BigUint::from_str(&aggr_sigma)
.map_err(|e| PDPError { error_code: FailCode::InternalError(e.to_string()) })?;

let mut sub_sigma = num_bigint::BigUint::from_str(&aggr_sigma)
.map_err(|e| PDPError { error_code: FailCode::InternalError(e.to_string()) })?;
sigma = sigma * sub_sigma;
sigma = sigma.mod_floor(&n);

Ok(sigma.to_string())
}

pub fn verify(
&self,
u: String,
Expand Down Expand Up @@ -579,7 +596,7 @@ pub fn gen_chall(n: u64) -> Vec<QElement> {
q.v = v.to_bytes_be();
challenge[index] = q;
index += 1;
break
break;
}
}
}
Expand Down
30 changes: 26 additions & 4 deletions crates/cestory/api/proto/podr2-api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ message EchoMessage {

service Podr2VerifierApi {
rpc request_batch_verify(RequestBatchVerify) returns (ResponseBatchVerify) {}
rpc request_aggregate_signature(RequestAggregateSignature) returns (ResponseAggregateSignature) {}
}

message Tag {
Expand Down Expand Up @@ -54,12 +55,12 @@ message GenTagMsg {
bytes u_sig = 2;
bytes signature = 3;
}

message RequestBatchVerify {
message Qslice {
message Qslice {
repeated uint32 random_index_list = 1;
repeated bytes random_list = 2;
}
}

message RequestBatchVerify {
message BatchVerifyParam {
repeated string names = 1;
repeated string us = 2;
Expand All @@ -70,6 +71,7 @@ message RequestBatchVerify {
Qslice qslices = 2;
repeated bytes u_sigs = 3;
bytes miner_id = 4;
repeated uint64 service_bloom_filter = 5;
}

message ResponseBatchVerify {
Expand All @@ -79,6 +81,26 @@ message ResponseBatchVerify {
bytes signature = 4;
}


message RequestAggregateSignature {
message VerifyInServiceFileStructure {
bytes miner_id = 1;
bool result =2;
string sigma = 3;
repeated uint64 service_bloom_filter = 4;
bytes signature = 5;
}
repeated VerifyInServiceFileStructure verify_inservice_file_history = 1;
Qslice qslices = 2;
}

message ResponseAggregateSignature {
bytes tee_account_id = 1;
bytes signature = 2;
}



enum StatusCode {
Success = 0;
Processing = 1;
Expand Down
14 changes: 7 additions & 7 deletions crates/cestory/src/light_validation/justification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ impl<Block: BlockT> GrandpaJustification<Block> {
NumberFor<Block>: finality_grandpa::BlockNumberOps,
{
let mut justification = GrandpaJustification::<Block>::decode(&mut &*encoded).map_err(|e| {
log::error!(
"decode justification error:{:?}, block:{:?}, input(len:{}):{}",
e,
finalized_target.1,
encoded.len(),
hex::encode(encoded)
);
// log::error!(
// "decode justification error:{:?}, block:{:?}, input(len:{}):{}",
// e,
// finalized_target.1,
// encoded.len(),
// hex::encode(encoded)
// );
ClientError::JustificationDecode
})?;
justification.grandpa_note_stalled = grandpa_note_stalled;
Expand Down
Loading

0 comments on commit ca093c4

Please sign in to comment.