Skip to content

Commit

Permalink
wrote polynomial multiplication functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-liu801 committed Jul 26, 2023
1 parent a0a5855 commit cfec01b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
59 changes: 40 additions & 19 deletions sunscreen_runtime/src/debugger/data.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use petgraph::stable_graph::NodeIndex;
use petgraph::stable_graph::StableGraph;
use petgraph::visit::EdgeRef;
use petgraph::Direction::Incoming;
use seal_fhe::Modulus;
use seal_fhe::BfvEncryptionParametersBuilder;
use seal_fhe::Context;
use seal_fhe::Decryptor;
use seal_fhe::Modulus;
use seal_fhe::SecretKey;
use serde::{Deserialize, Serialize};
use sunscreen_compiler_common::Operation;
use sunscreen_compiler_common::Type;
use sunscreen_compiler_common::{EdgeInfo, NodeInfo};

use crate::InnerCiphertext;
use crate::PrivateKey;
use crate::SealData;

pub enum DebugNodeType {
Bfv(BfvNodeType),
Expand All @@ -27,7 +28,7 @@ pub struct BfvNodeType {
pub coefficients: Vec<Vec<u64>>,
pub multiplicative_depth: u64,
pub overflowed: Option<bool>,
pub noise_exceeded: Option<bool>
pub noise_exceeded: Option<bool>,
}

/**
Expand Down Expand Up @@ -68,14 +69,38 @@ where
*/
pub fn overflow_occurred<O>(
graph: &StableGraph<NodeInfo<O>, EdgeInfo>,
node: NodeIndex,) -> bool
where
O: Operation
node: NodeIndex,
p: u64,
sk: &SecretKey,
program_data: Vec<Option<SealData>>,
) -> bool
where
O: Operation,
{
let add_overflow = false;
let mul_overflow = false;
let mut add_overflow = true;
let mut mul_overflow = true;

add_overflow || mul_overflow
}

add_overflow | mul_overflow
fn polynomial_mult(a: &[u64], b: &[u64]) -> Vec<u64> {
let mut product = vec![0; a.len() + b.len() - 1];
for (i, &ai) in a.iter().enumerate() {
for (j, &bj) in b.iter().enumerate() {
product[i + j] += ai * bj;
}
}
product
}

fn polynomial_mult_mod(a: &[u64], b: &[u64], p: u64) -> Vec<u64> {
let mut product = vec![0; a.len() + b.len() - 1];
for (i, &ai) in a.iter().enumerate() {
for (j, &bj) in b.iter().enumerate() {
product[i + j] += (ai * bj) % p;
}
}
product
}

/**
Expand All @@ -95,13 +120,10 @@ pub fn decrypt_seal(inner_cipher: InnerCiphertext, sk: &SecretKey) -> Vec<Vec<u6
.map(|&num| Modulus::new(num).unwrap())
.collect::<Vec<_>>();
// Decrypt inner ciphertext
let encryption_params_builder =
BfvEncryptionParametersBuilder::new()
.set_coefficient_modulus(coeff_mod)
.set_plain_modulus_u64(inner_cipher.params.plain_modulus)
.set_poly_modulus_degree(
inner_cipher.params.lattice_dimension,
);
let encryption_params_builder = BfvEncryptionParametersBuilder::new()
.set_coefficient_modulus(coeff_mod)
.set_plain_modulus_u64(inner_cipher.params.plain_modulus)
.set_poly_modulus_degree(inner_cipher.params.lattice_dimension);
let encryption_params = encryption_params_builder.build().unwrap();
let ctx = Context::new(
&encryption_params,
Expand All @@ -110,8 +132,7 @@ pub fn decrypt_seal(inner_cipher: InnerCiphertext, sk: &SecretKey) -> Vec<Vec<u6
)
.expect("Failed to create context");

let decryptor =
Decryptor::new(&ctx, sk).expect("Failed to create decryptor");
let decryptor = Decryptor::new(&ctx, sk).expect("Failed to create decryptor");
let pt = decryptor.decrypt(&inner_cipher.data).unwrap();

for i in 0..pt.len() {
Expand All @@ -121,7 +142,7 @@ pub fn decrypt_seal(inner_cipher: InnerCiphertext, sk: &SecretKey) -> Vec<Vec<u6
}
}
}
coefficients
coefficients
}
#[derive(Clone, Serialize, Deserialize)]
pub struct ZkpNodeType {
Expand Down
14 changes: 10 additions & 4 deletions sunscreen_runtime/src/debugger/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use seal_fhe::{BfvEncryptionParametersBuilder, Context, Decryptor, Modulus};
use semver::Version;

use crate::{
debugger::{BfvNodeType, ZkpNodeType, DebugNodeType},
debugger::{get_mult_depth, get_sessions, overflow_occurred, decrypt_seal},
debugger::{decrypt_seal, get_mult_depth, get_sessions, overflow_occurred},
debugger::{BfvNodeType, DebugNodeType, ZkpNodeType},
Ciphertext, InnerCiphertext, InnerPlaintext, Plaintext, Runtime, SealData, Type, WithContext,
};
use petgraph::stable_graph::NodeIndex;
Expand Down Expand Up @@ -171,8 +171,14 @@ pub async fn get_node_data(
let multiplicative_depth: u64 =
get_mult_depth(stable_graph, NodeIndex::new(nodeid), 0);

let overflowed = overflow_occurred(stable_graph, NodeIndex::new(nodeid));

let overflowed = overflow_occurred(
stable_graph,
NodeIndex::new(nodeid),
pk.0.params.plain_modulus,
&pk.0.data,
curr_session.program_data.clone(),
);

let coefficients = decrypt_seal(sunscreen_ciphertext.inner, &pk.0.data);

// TODO: implement detection for overflow. Values overflow if two input operands have the same sign
Expand Down
10 changes: 3 additions & 7 deletions sunscreen_runtime/src/debugger/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn get_sessions() -> &'static Mutex<HashMap<String, Session>> {
*/
pub enum Session {
BfvSession(BfvSession),
ZkpSession(ZkpSession)
ZkpSession(ZkpSession),
}

impl From<BfvSession> for Session {
Expand All @@ -40,7 +40,6 @@ impl Session {
match self {
Self::BfvSession(s) => s,
_ => panic!("Called unwrap_bfv_session_mut on a non-BFV session"),

}
}

Expand Down Expand Up @@ -112,7 +111,7 @@ pub struct ZkpSession {
*/
// TODO: figure out how to refactor this
pub program_data: Vec<Option<SealData>>,

/**
* The source code of the ZKP program.
*/
Expand All @@ -123,10 +122,7 @@ impl ZkpSession {
/**
* Constructs a new `ZkpDebugInfo`.
*/
pub fn new(
graph: &CompilationResult<ZkpOperation>,
source_code: &str,
) -> Self {
pub fn new(graph: &CompilationResult<ZkpOperation>, source_code: &str) -> Self {
Self {
graph: graph.clone(),
program_data: vec![None; graph.node_count()],
Expand Down

0 comments on commit cfec01b

Please sign in to comment.