Initial MT and div improvements #30
8 changed files with 80 additions and 108 deletions
|
|
@ -12,9 +12,15 @@ serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
num = "0.4"
|
num = "0.4"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
|
threadpool = "1.8"
|
||||||
|
num_cpus = "1.16.0"
|
||||||
|
|
||||||
[source.crates-io]
|
[source.crates-io]
|
||||||
replace-with = "vendored-sources"
|
replace-with = "vendored-sources"
|
||||||
|
|
||||||
[source.vendored-sources]
|
[source.vendored-sources]
|
||||||
directory = "vendor"
|
directory = "vendor"
|
||||||
|
|
||||||
|
[profile.profiling]
|
||||||
|
inherits = "release"
|
||||||
|
debug = true
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ fn main() -> Result<()> {
|
||||||
let json = fs::read_to_string(path_to_workload).unwrap();
|
let json = fs::read_to_string(path_to_workload).unwrap();
|
||||||
let workload = kauma::utils::parse::parse_json(json)?;
|
let workload = kauma::utils::parse::parse_json(json)?;
|
||||||
|
|
||||||
let response = kauma::tasks::task_distrubute(&workload)?;
|
let response = kauma::tasks::task_distribute(&workload)?;
|
||||||
println!("{}", serde_json::to_string(&response)?);
|
println!("{}", serde_json::to_string(&response)?);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -185,16 +185,61 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn task_distrubute(testcases: &Testcases) -> Result<Responses> {
|
fn task_distribute_mt(testcases: &Testcases) -> Result<Responses> {
|
||||||
|
eprintln!("USING MULTITHREADED");
|
||||||
|
let mut responses: HashMap<String, Value> = HashMap::new();
|
||||||
|
let pool = threadpool::ThreadPool::default();
|
||||||
|
let (tx, rx) = std::sync::mpsc::channel();
|
||||||
|
for (key, testcase) in testcases.testcases.clone() {
|
||||||
|
let tx = tx.clone();
|
||||||
|
let testcase = testcase.clone();
|
||||||
|
pool.execute(move || {
|
||||||
|
tx.send((key, task_deploy(&testcase)))
|
||||||
|
.expect("could not send return value of thread to main thread")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..testcases.testcases.len() {
|
||||||
|
let result = match rx.recv_timeout(std::time::Duration::from_secs(60 * 5)) {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("! Job timed out: {e}");
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match result.1 {
|
||||||
|
Ok(v) => {
|
||||||
|
let _ = responses.insert(result.0, v);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("! failed to solve a challenge: {e:#}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Responses { responses })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task_distribute_st(testcases: &Testcases) -> Result<Responses> {
|
||||||
|
//eprintln!("USING SINGLETHREADED");
|
||||||
let mut responses: HashMap<String, Value> = HashMap::new();
|
let mut responses: HashMap<String, Value> = HashMap::new();
|
||||||
|
|
||||||
for (id, testcase) in &testcases.testcases {
|
for (id, testcase) in &testcases.testcases {
|
||||||
responses.insert(id.to_owned(), task_deploy(testcase).unwrap());
|
responses.insert(id.to_owned(), task_deploy(testcase).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Responses {
|
Ok(Responses { responses })
|
||||||
responses: responses,
|
}
|
||||||
})
|
|
||||||
|
pub fn task_distribute(testcases: &Testcases) -> Result<Responses> {
|
||||||
|
let cpus = num_cpus::get();
|
||||||
|
//TODO: Deactivate MT for now
|
||||||
|
if cpus > 10000000000 {
|
||||||
|
task_distribute_mt(testcases)
|
||||||
|
} else {
|
||||||
|
task_distribute_st(testcases)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
@ -227,7 +272,7 @@ mod tests {
|
||||||
let expected = json!({ "responses": { "b856d760-023d-4b00-bad2-15d2b6da22fe": {"block": "ARIAAAAAAAAAAAAAAAAAgA=="}}});
|
let expected = json!({ "responses": { "b856d760-023d-4b00-bad2-15d2b6da22fe": {"block": "ARIAAAAAAAAAAAAAAAAAgA=="}}});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -251,7 +296,7 @@ mod tests {
|
||||||
});
|
});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -266,7 +311,7 @@ mod tests {
|
||||||
let expected = json!({ "responses": { "b856d760-023d-4b00-bad2-15d2b6da22fe": {"product": "hSQAAAAAAAAAAAAAAAAAAA=="}}});
|
let expected = json!({ "responses": { "b856d760-023d-4b00-bad2-15d2b6da22fe": {"product": "hSQAAAAAAAAAAAAAAAAAAA=="}}});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -284,7 +329,7 @@ mod tests {
|
||||||
}});
|
}});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -304,7 +349,7 @@ mod tests {
|
||||||
}}});
|
}}});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -324,7 +369,7 @@ mod tests {
|
||||||
}}});
|
}}});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -342,7 +387,7 @@ mod tests {
|
||||||
}}});
|
}}});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -360,7 +405,7 @@ mod tests {
|
||||||
}}});
|
}}});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -378,7 +423,7 @@ mod tests {
|
||||||
}}});
|
}}});
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
serde_json::to_value(task_distrubute(&parsed)?).unwrap(),
|
serde_json::to_value(task_distribute(&parsed)?).unwrap(),
|
||||||
serde_json::to_value(expected).unwrap()
|
serde_json::to_value(expected).unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -119,10 +119,8 @@ pub fn gcm_encrypt_aes(
|
||||||
let mut counter: u32 = 1;
|
let mut counter: u32 = 1;
|
||||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||||
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
||||||
eprintln!("{:001X?}", nonce);
|
|
||||||
|
|
||||||
let auth_tag_xor = aes_128_encrypt(&key, &nonce)?;
|
let auth_tag_xor = aes_128_encrypt(&key, &nonce)?;
|
||||||
eprintln!("Y0 {:001X?}", auth_tag_xor);
|
|
||||||
|
|
||||||
let auth_key_h = aes_128_encrypt(&key, &0u128.to_be_bytes().to_vec())?;
|
let auth_key_h = aes_128_encrypt(&key, &0u128.to_be_bytes().to_vec())?;
|
||||||
|
|
||||||
|
|
@ -133,8 +131,6 @@ pub fn gcm_encrypt_aes(
|
||||||
nonce.drain(12..);
|
nonce.drain(12..);
|
||||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||||
|
|
||||||
eprintln!("{:001X?}", nonce);
|
|
||||||
|
|
||||||
let inter1 = aes_128_encrypt(&key, &nonce)?;
|
let inter1 = aes_128_encrypt(&key, &nonce)?;
|
||||||
|
|
||||||
let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
|
let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
|
||||||
|
|
@ -151,7 +147,6 @@ pub fn gcm_encrypt_aes(
|
||||||
&ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?,
|
&ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?,
|
||||||
auth_tag_xor,
|
auth_tag_xor,
|
||||||
)?;
|
)?;
|
||||||
eprintln!("aes auth tag: {:001X?}", &auth_tag);
|
|
||||||
|
|
||||||
Ok((ciphertext, auth_tag, l_field, auth_key_h))
|
Ok((ciphertext, auth_tag, l_field, auth_key_h))
|
||||||
}
|
}
|
||||||
|
|
@ -168,7 +163,6 @@ pub fn gcm_decrypt_aes(
|
||||||
let mut counter: u32 = 1;
|
let mut counter: u32 = 1;
|
||||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||||
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
||||||
eprintln!("{:001X?}", nonce);
|
|
||||||
|
|
||||||
let auth_tag_xor = aes_128_encrypt(&key, &nonce)?;
|
let auth_tag_xor = aes_128_encrypt(&key, &nonce)?;
|
||||||
|
|
||||||
|
|
@ -181,8 +175,6 @@ pub fn gcm_decrypt_aes(
|
||||||
nonce.drain(12..);
|
nonce.drain(12..);
|
||||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||||
|
|
||||||
eprintln!("{:001X?}", nonce);
|
|
||||||
|
|
||||||
let inter1 = aes_128_encrypt(&key, &nonce)?;
|
let inter1 = aes_128_encrypt(&key, &nonce)?;
|
||||||
|
|
||||||
let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
|
let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
|
||||||
|
|
@ -201,7 +193,6 @@ pub fn gcm_decrypt_aes(
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let valid = auth_tag == tag;
|
let valid = auth_tag == tag;
|
||||||
eprintln!("aes auth tag: {:001X?}", auth_tag);
|
|
||||||
|
|
||||||
Ok((plaintext, valid))
|
Ok((plaintext, valid))
|
||||||
}
|
}
|
||||||
|
|
@ -217,7 +208,6 @@ pub fn gcm_encrypt_sea(
|
||||||
let mut counter: u32 = 1;
|
let mut counter: u32 = 1;
|
||||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||||
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
||||||
eprintln!("{:001X?}", nonce);
|
|
||||||
|
|
||||||
let auth_tag_xor = sea_128_encrypt(&key, &nonce)?;
|
let auth_tag_xor = sea_128_encrypt(&key, &nonce)?;
|
||||||
|
|
||||||
|
|
@ -230,8 +220,6 @@ pub fn gcm_encrypt_sea(
|
||||||
nonce.drain(12..);
|
nonce.drain(12..);
|
||||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||||
|
|
||||||
eprintln!("{:001X?}", nonce);
|
|
||||||
|
|
||||||
let inter1 = sea_128_encrypt(&key, &nonce)?;
|
let inter1 = sea_128_encrypt(&key, &nonce)?;
|
||||||
|
|
||||||
let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
|
let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
|
||||||
|
|
@ -264,7 +252,6 @@ pub fn gcm_decrypt_sea(
|
||||||
let mut counter: u32 = 1;
|
let mut counter: u32 = 1;
|
||||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||||
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
//nonce.append(0u8.to_le_bytes().to_vec().as_mut());
|
||||||
eprintln!("Nonce 1: {:001X?}", nonce);
|
|
||||||
|
|
||||||
let auth_tag_xor = sea_128_encrypt(&key, &nonce)?;
|
let auth_tag_xor = sea_128_encrypt(&key, &nonce)?;
|
||||||
|
|
||||||
|
|
@ -272,17 +259,11 @@ pub fn gcm_decrypt_sea(
|
||||||
|
|
||||||
let plaintext_chunks: Vec<Vec<u8>> = ciphertext.chunks(16).map(|x| x.to_vec()).collect();
|
let plaintext_chunks: Vec<Vec<u8>> = ciphertext.chunks(16).map(|x| x.to_vec()).collect();
|
||||||
|
|
||||||
eprintln!("{:?}", plaintext_chunks);
|
|
||||||
|
|
||||||
counter = 2;
|
counter = 2;
|
||||||
for chunk in plaintext_chunks {
|
for chunk in plaintext_chunks {
|
||||||
eprintln!("Inside loop");
|
|
||||||
|
|
||||||
nonce.drain(12..);
|
nonce.drain(12..);
|
||||||
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
nonce.append(counter.to_be_bytes().to_vec().as_mut());
|
||||||
|
|
||||||
eprintln!("Nonce 2: {:001X?}", nonce);
|
|
||||||
|
|
||||||
let inter1 = sea_128_encrypt(&key, &nonce)?;
|
let inter1 = sea_128_encrypt(&key, &nonce)?;
|
||||||
|
|
||||||
let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
|
let mut inter2 = xor_bytes(&inter1, chunk.clone())?;
|
||||||
|
|
@ -295,15 +276,11 @@ pub fn gcm_decrypt_sea(
|
||||||
let mut c_len: Vec<u8> = ((plaintext.len() * 8) as u64).to_be_bytes().to_vec();
|
let mut c_len: Vec<u8> = ((plaintext.len() * 8) as u64).to_be_bytes().to_vec();
|
||||||
l_field.append(c_len.as_mut());
|
l_field.append(c_len.as_mut());
|
||||||
|
|
||||||
eprintln!("Ciphertext: {}", BASE64_STANDARD.encode(&ciphertext));
|
|
||||||
|
|
||||||
let auth_tag = xor_bytes(
|
let auth_tag = xor_bytes(
|
||||||
&ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?,
|
&ghash(auth_key_h.clone(), ad, ciphertext.clone(), l_field.clone())?,
|
||||||
auth_tag_xor,
|
auth_tag_xor,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
eprintln!("sea dec auth tag: {}", BASE64_STANDARD.encode(&auth_tag));
|
|
||||||
|
|
||||||
let valid = auth_tag == tag;
|
let valid = auth_tag == tag;
|
||||||
|
|
||||||
Ok((plaintext, valid))
|
Ok((plaintext, valid))
|
||||||
|
|
@ -317,10 +294,6 @@ pub fn ghash(
|
||||||
) -> Result<Vec<u8>> {
|
) -> Result<Vec<u8>> {
|
||||||
let output: Vec<u8> = vec![0; 16];
|
let output: Vec<u8> = vec![0; 16];
|
||||||
|
|
||||||
eprintln!("{:?}", ad.len() as u8);
|
|
||||||
eprintln!("{:?}", (ad.len() % 16) as u8);
|
|
||||||
eprintln!("{:001X?}", ad);
|
|
||||||
|
|
||||||
if ad.len() % 16 != 0 || ad.is_empty() {
|
if ad.len() % 16 != 0 || ad.is_empty() {
|
||||||
ad.append(vec![0u8; 16 - (ad.len() % 16)].as_mut());
|
ad.append(vec![0u8; 16 - (ad.len() % 16)].as_mut());
|
||||||
}
|
}
|
||||||
|
|
@ -329,20 +302,12 @@ pub fn ghash(
|
||||||
ciphertext.append(vec![0u8; 16 - (ciphertext.len() % 16)].as_mut());
|
ciphertext.append(vec![0u8; 16 - (ciphertext.len() % 16)].as_mut());
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("{:001X?}", ad);
|
|
||||||
eprintln!("{:001X?}", ciphertext);
|
|
||||||
|
|
||||||
let mut ad_chunks = ad.chunks(16);
|
let mut ad_chunks = ad.chunks(16);
|
||||||
|
|
||||||
eprintln!("Ad chunks before first next {:001X?}", ad_chunks);
|
|
||||||
|
|
||||||
let inter1 = xor_bytes(&output, ad_chunks.next().unwrap().to_vec())?;
|
let inter1 = xor_bytes(&output, ad_chunks.next().unwrap().to_vec())?;
|
||||||
let mut inter_loop = gfmul(&inter1, &auth_key_h, "gcm")?;
|
let mut inter_loop = gfmul(&inter1, &auth_key_h, "gcm")?;
|
||||||
eprintln!("Ad chunks after first next {:001X?}", ad_chunks);
|
|
||||||
|
|
||||||
for chunk in ad_chunks {
|
for chunk in ad_chunks {
|
||||||
eprintln!("Inside ad chunk loop");
|
|
||||||
eprintln!("Ad chunk in loop {:001X?}", chunk);
|
|
||||||
let inter2 = xor_bytes(&inter_loop, chunk.to_vec())?;
|
let inter2 = xor_bytes(&inter_loop, chunk.to_vec())?;
|
||||||
inter_loop = gfmul(&inter2, &auth_key_h, "gcm")?;
|
inter_loop = gfmul(&inter2, &auth_key_h, "gcm")?;
|
||||||
}
|
}
|
||||||
|
|
@ -357,8 +322,6 @@ pub fn ghash(
|
||||||
let inter4 = xor_bytes(&inter_loop, l_field)?;
|
let inter4 = xor_bytes(&inter_loop, l_field)?;
|
||||||
inter_loop = gfmul(&inter4, &auth_key_h, "gcm")?;
|
inter_loop = gfmul(&inter4, &auth_key_h, "gcm")?;
|
||||||
|
|
||||||
eprintln!("GHASH auth tag: {:001X?}", inter_loop);
|
|
||||||
|
|
||||||
Ok(inter_loop)
|
Ok(inter_loop)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,9 @@
|
||||||
use num::{BigUint, FromPrimitive, One};
|
use num::{BigUint, FromPrimitive, One};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
|
|
||||||
use super::poly::{gcd, Polynomial};
|
use super::poly::{gcd, Polynomial};
|
||||||
|
|
||||||
pub fn edf(f: Polynomial, d: u32) -> Vec<Polynomial> {
|
pub fn edf(f: Polynomial, d: u32) -> Vec<Polynomial> {
|
||||||
eprintln!("Starting edf");
|
|
||||||
|
|
||||||
let q = BigUint::pow(&BigUint::from_u8(2).unwrap(), 128);
|
let q = BigUint::pow(&BigUint::from_u8(2).unwrap(), 128);
|
||||||
let n: u32 = (f.degree() as u32) / (d);
|
let n: u32 = (f.degree() as u32) / (d);
|
||||||
let mut z: Vec<Polynomial> = vec![f.clone()];
|
let mut z: Vec<Polynomial> = vec![f.clone()];
|
||||||
|
|
@ -20,7 +17,6 @@ pub fn edf(f: Polynomial, d: u32) -> Vec<Polynomial> {
|
||||||
//eprintln!("h: {:02X?}", h);
|
//eprintln!("h: {:02X?}", h);
|
||||||
|
|
||||||
let exponent = (q.pow(d) - BigUint::one()) / BigUint::from_u8(3).unwrap();
|
let exponent = (q.pow(d) - BigUint::one()) / BigUint::from_u8(3).unwrap();
|
||||||
eprintln!("q before for {:0X?}", exponent);
|
|
||||||
|
|
||||||
let g = h.bpow_mod(exponent, &f) + Polynomial::one();
|
let g = h.bpow_mod(exponent, &f) + Polynomial::one();
|
||||||
//eprintln!("g before for {:0X?}", g);
|
//eprintln!("g before for {:0X?}", g);
|
||||||
|
|
@ -32,9 +28,7 @@ pub fn edf(f: Polynomial, d: u32) -> Vec<Polynomial> {
|
||||||
//eprintln!("Inside if");
|
//eprintln!("Inside if");
|
||||||
let j = gcd(&z[i], &g);
|
let j = gcd(&z[i], &g);
|
||||||
|
|
||||||
eprintln!("j: {:02X?}", j);
|
|
||||||
if j != one_cmp && j != z[i] {
|
if j != one_cmp && j != z[i] {
|
||||||
eprintln!("Working on Z");
|
|
||||||
let intemediate = z[i].div(&j).0;
|
let intemediate = z[i].div(&j).0;
|
||||||
z.remove(i);
|
z.remove(i);
|
||||||
z.push(j.clone());
|
z.push(j.clone());
|
||||||
|
|
|
||||||
|
|
@ -61,15 +61,12 @@ impl FieldElement {
|
||||||
let mut result: FieldElement = FieldElement::one();
|
let mut result: FieldElement = FieldElement::one();
|
||||||
|
|
||||||
if exponent == 1 {
|
if exponent == 1 {
|
||||||
eprintln!("special case 1: {:02X?}", self.clone());
|
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
if exponent == 0 {
|
if exponent == 0 {
|
||||||
let result = FieldElement::one();
|
let result = FieldElement::one();
|
||||||
|
|
||||||
eprintln!("Returned value is: {:02X?}", result);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,13 @@ pub struct Testcases {
|
||||||
pub testcases: HashMap<String, Testcase>,
|
pub testcases: HashMap<String, Testcase>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct Testcase {
|
pub struct Testcase {
|
||||||
pub action: String,
|
pub action: String,
|
||||||
pub arguments: Value,
|
pub arguments: Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct Responses {
|
pub struct Responses {
|
||||||
pub responses: HashMap<String, Value>,
|
pub responses: HashMap<String, Value>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,8 +68,6 @@ impl Polynomial {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
eprintln!("{:?}", c_array);
|
|
||||||
|
|
||||||
for coefficient in c_array {
|
for coefficient in c_array {
|
||||||
polynomial.push(FieldElement::new(
|
polynomial.push(FieldElement::new(
|
||||||
BASE64_STANDARD
|
BASE64_STANDARD
|
||||||
|
|
@ -95,8 +93,6 @@ impl Polynomial {
|
||||||
)]);
|
)]);
|
||||||
|
|
||||||
if exponent == 1 {
|
if exponent == 1 {
|
||||||
eprintln!("special case 1: {:02X?}", self.clone());
|
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,7 +101,6 @@ impl Polynomial {
|
||||||
polynomial_2_block(vec![0], "gcm").unwrap(),
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
)]);
|
)]);
|
||||||
|
|
||||||
eprintln!("Returned value is: {:02X?}", result);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,8 +152,6 @@ impl Polynomial {
|
||||||
)]);
|
)]);
|
||||||
|
|
||||||
if exponent == BigUint::one() {
|
if exponent == BigUint::one() {
|
||||||
eprintln!("special case 1: {:02X?}", self.clone().div(&modulus).1);
|
|
||||||
|
|
||||||
return self.div(&modulus).1;
|
return self.div(&modulus).1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,7 +160,6 @@ impl Polynomial {
|
||||||
polynomial_2_block(vec![0], "gcm").unwrap(),
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
)]);
|
)]);
|
||||||
|
|
||||||
eprintln!("Returned value is: {:02X?}", result);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -211,15 +203,12 @@ impl Polynomial {
|
||||||
)]);
|
)]);
|
||||||
|
|
||||||
if exponent == 1 {
|
if exponent == 1 {
|
||||||
eprintln!("special case 1: {:02X?}", self.clone().div(&modulus).1);
|
|
||||||
|
|
||||||
return self.div(&modulus).1;
|
return self.div(&modulus).1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if exponent == 0 {
|
if exponent == 0 {
|
||||||
let result = Polynomial::new(vec![FieldElement::one()]);
|
let result = Polynomial::new(vec![FieldElement::one()]);
|
||||||
|
|
||||||
eprintln!("Returned value is: {:02X?}", result);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -239,8 +228,6 @@ impl Polynomial {
|
||||||
exponent >>= 1;
|
exponent >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("result in powmod before reduction: {:02X?}", result);
|
|
||||||
|
|
||||||
while !result.polynomial.is_empty()
|
while !result.polynomial.is_empty()
|
||||||
&& result
|
&& result
|
||||||
.polynomial
|
.polynomial
|
||||||
|
|
@ -253,8 +240,6 @@ impl Polynomial {
|
||||||
result.polynomial.pop();
|
result.polynomial.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("result in powmod after reduction: {:02X?}", result);
|
|
||||||
|
|
||||||
if result.is_empty() {
|
if result.is_empty() {
|
||||||
result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
|
result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
|
||||||
}
|
}
|
||||||
|
|
@ -262,12 +247,7 @@ impl Polynomial {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns (quotient, remainder)
|
|
||||||
pub fn div(&self, rhs: &Self) -> (Self, Self) {
|
pub fn div(&self, rhs: &Self) -> (Self, Self) {
|
||||||
// Div by zero check ommitted since data is guaranteed to be non 0
|
|
||||||
|
|
||||||
//eprintln!("{:?}, {:?}", self.polynomial.len(), rhs.polynomial.len());
|
|
||||||
|
|
||||||
if self.polynomial.len() < rhs.polynomial.len() {
|
if self.polynomial.len() < rhs.polynomial.len() {
|
||||||
return (Polynomial::new(vec![FieldElement::zero()]), self.clone());
|
return (Polynomial::new(vec![FieldElement::zero()]), self.clone());
|
||||||
}
|
}
|
||||||
|
|
@ -285,24 +265,20 @@ impl Polynomial {
|
||||||
|
|
||||||
while remainder.polynomial.len() >= divisor.polynomial.len() {
|
while remainder.polynomial.len() >= divisor.polynomial.len() {
|
||||||
let deg_diff = remainder.polynomial.len() - divisor.polynomial.len();
|
let deg_diff = remainder.polynomial.len() - divisor.polynomial.len();
|
||||||
|
|
||||||
let leading_dividend = remainder.polynomial.last().unwrap();
|
let leading_dividend = remainder.polynomial.last().unwrap();
|
||||||
let leading_divisor = divisor.polynomial.last().unwrap();
|
let leading_divisor = divisor.polynomial.last().unwrap();
|
||||||
let quot_coeff = leading_dividend / leading_divisor;
|
let quot_coeff = leading_dividend / leading_divisor;
|
||||||
|
|
||||||
quotient_coeffs[deg_diff] = quot_coeff.clone();
|
quotient_coeffs[deg_diff] = quot_coeff.clone();
|
||||||
|
|
||||||
let mut subtrahend = vec![FieldElement::zero(); deg_diff];
|
let mut pos;
|
||||||
subtrahend.extend(
|
for (i, divisor_coeff) in divisor.polynomial.iter().enumerate() {
|
||||||
divisor
|
pos = deg_diff + i;
|
||||||
.polynomial
|
let a: &FieldElement = &remainder.polynomial[pos];
|
||||||
.iter()
|
let c: &FieldElement = "_coeff;
|
||||||
.map(|x| x.clone() * quot_coeff.clone()),
|
remainder.polynomial[pos] = a + &(divisor_coeff * c);
|
||||||
);
|
}
|
||||||
let subtrahend_poly = Polynomial::new(subtrahend);
|
|
||||||
|
|
||||||
remainder = remainder + subtrahend_poly;
|
|
||||||
|
|
||||||
|
// Remove trailing zeros
|
||||||
while !remainder.polynomial.is_empty()
|
while !remainder.polynomial.is_empty()
|
||||||
&& remainder
|
&& remainder
|
||||||
.polynomial
|
.polynomial
|
||||||
|
|
@ -316,9 +292,6 @@ impl Polynomial {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if remainder.is_empty() {
|
|
||||||
remainder = Polynomial::new(vec![FieldElement::zero()]);
|
|
||||||
}
|
|
||||||
(Polynomial::new(quotient_coeffs), remainder)
|
(Polynomial::new(quotient_coeffs), remainder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -515,12 +488,6 @@ impl PartialOrd for Polynomial {
|
||||||
for (field_a, field_b) in
|
for (field_a, field_b) in
|
||||||
self.as_ref().iter().rev().zip(other.as_ref().iter().rev())
|
self.as_ref().iter().rev().zip(other.as_ref().iter().rev())
|
||||||
{
|
{
|
||||||
eprintln!(
|
|
||||||
"Poly partord: {:02X?} {:02X?} ",
|
|
||||||
self.clone().to_c_array(),
|
|
||||||
other.clone().to_c_array()
|
|
||||||
);
|
|
||||||
|
|
||||||
match field_a
|
match field_a
|
||||||
//.reverse_bits()
|
//.reverse_bits()
|
||||||
.partial_cmp(&field_b)
|
.partial_cmp(&field_b)
|
||||||
|
|
@ -606,10 +573,10 @@ pub fn gfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u
|
||||||
let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec());
|
let mut red_poly_bytes: ByteArray = ByteArray(RED_POLY.to_be_bytes().to_vec());
|
||||||
//red_poly_bytes.0.push(0x01);
|
//red_poly_bytes.0.push(0x01);
|
||||||
|
|
||||||
let mut poly1: ByteArray = ByteArray(poly_a.to_owned());
|
let mut poly1: ByteArray = ByteArray(poly_a.to_vec());
|
||||||
//poly1.0.push(0x00);
|
//poly1.0.push(0x00);
|
||||||
|
|
||||||
let mut poly2: ByteArray = ByteArray(poly_b.to_owned());
|
let mut poly2: ByteArray = ByteArray(poly_b.to_vec());
|
||||||
//poly2.0.push(0x00);
|
//poly2.0.push(0x00);
|
||||||
|
|
||||||
if semantic == "gcm" {
|
if semantic == "gcm" {
|
||||||
|
|
@ -653,9 +620,9 @@ pub fn bgfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<
|
||||||
0x87, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x01,
|
0x87, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x01,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let mut poly1: BigUint = BigUint::from_le_bytes(&reverse_bits_in_bytevec(poly_a.to_owned()));
|
let mut poly1: BigUint = BigUint::from_le_bytes(poly_a);
|
||||||
|
|
||||||
let mut poly2: BigUint = BigUint::from_le_bytes(&reverse_bits_in_bytevec(poly_b.to_owned()));
|
let mut poly2: BigUint = BigUint::from_le_bytes(poly_b);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if semantic == "gcm" {
|
if semantic == "gcm" {
|
||||||
|
|
@ -691,7 +658,7 @@ pub fn bgfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Ok(reverse_bits_in_bytevec(result.to_bytes_le()))
|
Ok(result.to_bytes_le())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn convert_gcm_to_xex(gcm_poly: Vec<u8>) -> Result<Vec<u8>> {
|
pub fn convert_gcm_to_xex(gcm_poly: Vec<u8>) -> Result<Vec<u8>> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue