Skip to content

Commit

Permalink
Merge pull request #30 from chainwayxyz/ekrem/bisection-presign
Browse files Browse the repository at this point in the history
Ekrem/bisection presign
  • Loading branch information
ekrembal authored Dec 9, 2023
2 parents e630b4f + b713ee9 commit 9f9fc3d
Show file tree
Hide file tree
Showing 11 changed files with 1,433 additions and 123 deletions.
27 changes: 26 additions & 1 deletion src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct Actor {
pub public_key: XOnlyPublicKey,
pub address: Address,
challenge_preimages: Vec<Vec<PreimageValue>>,
challenge_hashes: Vec<Vec<HashValue>>,
signatures: Vec<Signature>,
}

impl Default for Actor {
Expand All @@ -42,6 +44,8 @@ impl Actor {
public_key: xonly,
address,
challenge_preimages: Vec::new(),
challenge_hashes: Vec::new(),
signatures: Vec::new(),
}
}

Expand Down Expand Up @@ -72,7 +76,7 @@ impl Actor {
}

pub fn generate_challenge_hashes(&mut self, num_gates: usize) -> Vec<HashValue> {
let mut challenge_hashes = Vec::new();
let mut challenge_hashes: Vec<HashValue> = Vec::new();
let mut rng = rand::thread_rng();
let mut preimages = Vec::new();
for _ in 0..num_gates {
Expand All @@ -81,8 +85,29 @@ impl Actor {
challenge_hashes.push(sha256::Hash::hash(&preimage).to_byte_array());
}
self.challenge_preimages.push(preimages);
self.challenge_hashes.push(challenge_hashes.clone());
challenge_hashes
}

pub fn add_challenge_hashes(&mut self, challenge_hashes: Vec<HashValue>) {
self.challenge_hashes.push(challenge_hashes);
}

pub fn get_challenge_hashes(&self, index: usize) -> Vec<HashValue> {
self.challenge_hashes[index].clone()
}

pub fn get_challenge_preimage(&self, index: usize, gate_num: usize) -> PreimageValue {
self.challenge_preimages[index][gate_num]
}

pub fn add_signature(&mut self, signature: Signature) {
self.signatures.push(signature);
}

pub fn get_signature(&self, index: usize) -> Signature {
self.signatures[index]
}
}

#[cfg(test)]
Expand Down
40 changes: 22 additions & 18 deletions src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::iter::zip;
use std::rc::Rc;

use std::sync::{Arc, Mutex};

use crate::wire::HashTuple;

Expand All @@ -15,13 +15,13 @@ use crate::{
pub struct Circuit {
pub input_sizes: Vec<usize>,
pub output_sizes: Vec<usize>,
pub gates: Vec<Box<dyn GateTrait>>,
pub wires: Vec<Rc<RefCell<Wire>>>,
pub gates: Vec<Box<dyn GateTrait + Send>>,
pub wires: Vec<Arc<Mutex<Wire>>>,
}

impl Default for Circuit {
fn default() -> Self {
Self::from_bristol("bristol/test.txt")
Self::from_bristol("bristol/test.txt", None)
}
}

Expand All @@ -46,7 +46,7 @@ impl Circuit {
combined_inputs.extend(a);
}
for (i, value) in combined_inputs.iter().enumerate() {
self.wires[i].try_borrow_mut().unwrap().selector = Some(*value);
self.wires[i].lock().unwrap().selector = Some(*value);
}
for gate in self.gates.as_mut_slice() {
gate.evaluate();
Expand All @@ -57,7 +57,7 @@ impl Circuit {
for os in self.output_sizes.clone() {
let mut output_vec = Vec::new();
for i in output_index..(output_index + os) {
let value = self.wires[i].try_borrow_mut().unwrap().selector.unwrap();
let value = self.wires[i].lock().unwrap().selector.unwrap();
output_vec.push(value);
}
output_index += os;
Expand All @@ -70,18 +70,18 @@ impl Circuit {
self.wires
.iter()
.map(|wire_rcref| {
let wire = wire_rcref.try_borrow_mut().unwrap();
let wire = wire_rcref.lock().unwrap();
wire.get_hash_pair()
})
.collect::<Vec<HashTuple>>()
}

pub fn from_bristol(file: &str) -> Self {
pub fn from_bristol(file: &str, wire_hashes: Option<Vec<HashTuple>>) -> Self {
let mut nog: usize = 0; // number of gates
let mut now: usize = 0; // number of wires
let mut input_sizes = Vec::<usize>::new();
let mut output_sizes = Vec::<usize>::new();
let mut gates = Vec::<Box<dyn GateTrait>>::new();
let mut gates = Vec::<Box<dyn GateTrait + Send>>::new();
let mut wire_indices = BTreeMap::new();

for (i, line) in read_lines(file).unwrap().enumerate() {
Expand All @@ -91,8 +91,12 @@ impl Circuit {
nog = words.next().unwrap().parse().unwrap();
now = words.next().unwrap().parse().unwrap();
for i in 0..now {
let wire = Wire::new(i);
wire_indices.insert(i, Rc::new(RefCell::new(wire)));
let wire = if let Some(wire_hashes) = wire_hashes.clone() {
Wire::new_with_hash_pair(i, wire_hashes[i])
} else {
Wire::new(i)
};
wire_indices.insert(i, Arc::new(Mutex::new(wire)));
}
} else if i == 1 {
let mut words = line_str.split_whitespace();
Expand Down Expand Up @@ -174,7 +178,7 @@ impl Circuit {
wires: wire_indices
.values()
.cloned()
.collect::<Vec<Rc<RefCell<Wire>>>>(),
.collect::<Vec<Arc<Mutex<Wire>>>>(),
};
}
}
Expand All @@ -201,13 +205,13 @@ mod tests {

#[test]
fn test_bristol() {
let circuit = Circuit::from_bristol("bristol/add.txt");
let circuit = Circuit::from_bristol("bristol/add.txt", None);
assert!(circuit.output_sizes[0] == 64);
}

#[test]
fn test_add_circuit() {
let mut circuit = Circuit::from_bristol("bristol/add.txt");
let mut circuit = Circuit::from_bristol("bristol/add.txt", None);
let a1 = 633;
let a2 = 15;
let b1 = number_to_bool_array(a1, 64);
Expand All @@ -220,7 +224,7 @@ mod tests {

#[test]
fn test_challenge_tree() {
let circuit = Circuit::from_bristol("bristol/test.txt");
let circuit = Circuit::from_bristol("bristol/test.txt", None);
let prover = Actor::new();
let mut verifier = Actor::new();
let secp = Secp256k1::new();
Expand All @@ -232,10 +236,10 @@ mod tests {
&circuit,
prover.public_key,
verifier.public_key,
challenge_hashes,
&challenge_hashes,
);
for wire_rcref in circuit.wires.iter() {
let wire = wire_rcref.try_borrow_mut().unwrap();
let wire = wire_rcref.lock().unwrap();
let script =
generate_anti_contradiction_script(wire.get_hash_pair(), verifier.public_key);
let ctrl_block = tree_info
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: Moce bisection size, etc. to config
Loading

0 comments on commit 9f9fc3d

Please sign in to comment.