Skip to content

Commit

Permalink
chore: revamp the microchain API
Browse files Browse the repository at this point in the history
* Make the server completely passive. Instead of a meta command,
  it's now done via command line: `lurk microchain <addr>`
* Clients can spawn new microchains and get an ID back
* Clients can retrieve the genesis state by ID
* Clients can fetch the current state by ID
* Clients can provide proofs of state transition by ID
* Clients can verify the entire microchain by ID

Extra:
* Fix old hole in the DAG by memoizing the DAG of the current env
  before calling `intern_env`
* Simplify/optimize the implementation of the `def` meta command
  • Loading branch information
arthurpaulino committed Oct 10, 2024
1 parent de4f6c7 commit 51bb5d7
Show file tree
Hide file tree
Showing 12 changed files with 706 additions and 390 deletions.
44 changes: 24 additions & 20 deletions src/lurk/cli/comm_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use p3_field::{AbstractField, Field};
use p3_field::Field;
use serde::{Deserialize, Serialize};
use std::hash::Hash;

use crate::{
lair::chipset::Chipset,
Expand All @@ -11,14 +12,27 @@ use crate::{

use super::zdag::ZDag;

#[derive(Serialize, Deserialize)]
pub(crate) struct CommData<F: std::hash::Hash + Eq> {
pub(crate) secret: ZPtr<F>,
#[derive(Serialize, Deserialize, Clone)]
pub(crate) struct CommData<F: Hash + Eq> {
pub(crate) secret: [F; DIGEST_SIZE],
pub(crate) payload: ZPtr<F>,
pub(crate) zdag: ZDag<F>,
}

impl<F: std::hash::Hash + Eq + Default + Copy> CommData<F> {
impl<F: Field> CommData<F> {
pub(crate) fn hash<C: Chipset<F>>(
secret: &[F; DIGEST_SIZE],
payload: &ZPtr<F>,
zstore: &mut ZStore<F, C>,
) -> [F; DIGEST_SIZE] {
let mut preimg = [F::default(); HASH3_SIZE];
preimg[..DIGEST_SIZE].copy_from_slice(secret);
preimg[DIGEST_SIZE..].copy_from_slice(&payload.flatten());
zstore.hash3(preimg)
}
}

impl<F: Hash + Eq + Default + Copy> CommData<F> {
#[inline]
pub(crate) fn new<C: Chipset<F>>(
secret: ZPtr<F>,
Expand All @@ -29,43 +43,33 @@ impl<F: std::hash::Hash + Eq + Default + Copy> CommData<F> {
let mut zdag = ZDag::default();
zdag.populate_with_many([&secret, &payload], zstore);
Self {
secret,
secret: secret.digest,
payload,
zdag,
}
}

fn build_preimg(&self) -> [F; HASH3_SIZE]
where
F: AbstractField,
{
let mut preimg = [F::zero(); HASH3_SIZE];
preimg[..DIGEST_SIZE].copy_from_slice(&self.secret.digest);
preimg[DIGEST_SIZE..].copy_from_slice(&self.payload.flatten());
preimg
}

fn compute_hash<H: Chipset<F>>(&self, zstore: &mut ZStore<F, H>) -> [F; DIGEST_SIZE]
fn compute_digest<H: Chipset<F>>(&self, zstore: &mut ZStore<F, H>) -> [F; DIGEST_SIZE]
where
F: Field,
{
zstore.hash3(self.build_preimg())
Self::hash(&self.secret, &self.payload, zstore)
}

#[inline]
pub(crate) fn commit<C: Chipset<F>>(&self, zstore: &mut ZStore<F, C>) -> ZPtr<F>
where
F: Field,
{
ZPtr::comm(self.compute_hash(zstore))
ZPtr::comm(self.compute_digest(zstore))
}

#[inline]
pub(crate) fn populate_zstore<C: Chipset<F>>(self, zstore: &mut ZStore<F, C>)
where
F: Field,
{
let digest = self.compute_hash(zstore);
let digest = self.compute_digest(zstore);
zstore.intern_comm(digest);
self.zdag.populate_zstore(zstore);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lurk/cli/lurk_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{

use super::zdag::ZDag;

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
pub(crate) struct LurkData<F: std::hash::Hash + Eq> {
pub(crate) zptr: ZPtr<F>,
zdag: ZDag<F>,
Expand Down
Loading

0 comments on commit 51bb5d7

Please sign in to comment.