refactor: use jl projection from ring-math

This commit is contained in:
vimwitch
2024-10-17 17:00:07 -07:00
parent dbd4859486
commit a5e9ccc69f
3 changed files with 39 additions and 65 deletions

44
Cargo.lock generated
View File

@@ -230,9 +230,9 @@ checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "ashlang"
version = "0.1.2"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d7c53d5f9bd154ea4d9c0539fe7ec3f0c58360659cef9ca1052254ec1faafed"
checksum = "8a687cb475d4e86351f33d992fae6ff4961630d129b1859ea2fefa2600a9d9c4"
dependencies = [
"anyhow",
"camino",
@@ -345,9 +345,9 @@ checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3"
[[package]]
name = "cc"
version = "1.1.28"
version = "1.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e80e3b6a3ab07840e1cae9b0666a63970dc28e8ed5ffbcdacbfc760c281bfc1"
checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945"
dependencies = [
"shlex",
]
@@ -769,9 +769,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
version = "0.2.159"
version = "0.2.161"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
[[package]]
name = "libm"
@@ -835,9 +835,9 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pest"
version = "2.7.13"
version = "2.7.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdbef9d1d47087a895abd220ed25eb4ad973a5e26f6a4367b038c25e28dfc2d9"
checksum = "879952a81a83930934cbf1786752d6dedc3b1f29e8f8fb2ad1d0a36f377cf442"
dependencies = [
"memchr",
"thiserror",
@@ -846,9 +846,9 @@ dependencies = [
[[package]]
name = "pest_derive"
version = "2.7.13"
version = "2.7.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d3a6e3394ec80feb3b6393c725571754c6188490265c61aaf260810d6b95aa0"
checksum = "d214365f632b123a47fd913301e14c946c61d1c183ee245fa76eb752e59a02dd"
dependencies = [
"pest",
"pest_generator",
@@ -856,9 +856,9 @@ dependencies = [
[[package]]
name = "pest_generator"
version = "2.7.13"
version = "2.7.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94429506bde1ca69d1b5601962c73f4172ab4726571a59ea95931218cb0e930e"
checksum = "eb55586734301717aea2ac313f50b2eb8f60d2fc3dc01d190eefa2e625f60c4e"
dependencies = [
"pest",
"pest_meta",
@@ -869,9 +869,9 @@ dependencies = [
[[package]]
name = "pest_meta"
version = "2.7.13"
version = "2.7.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac8a071862e93690b6e34e9a5fb8e33ff3734473ac0245b27232222c4906a33f"
checksum = "b75da2a70cf4d9cb76833c990ac9cd3923c9a8905a8929789ce347c84564d03d"
dependencies = [
"once_cell",
"pest",
@@ -966,9 +966,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.87"
version = "1.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3e4daa0dcf6feba26f985457cdf104d4b4256fc5a09547140f3631bb076b19a"
checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9"
dependencies = [
"unicode-ident",
]
@@ -1073,9 +1073,9 @@ dependencies = [
[[package]]
name = "ring-math"
version = "0.1.2"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9ebb260975ce57fb0779b9b64811d9e32139ec6098b69a153cc9ed5659ca905"
checksum = "10f9dcc8b800c8685c7c565271d99a4032abec0fcf6da2b8c73b8d75df7c8efa"
dependencies = [
"anyhow",
"rand",
@@ -1099,9 +1099,9 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "scalarff"
version = "0.5.1"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "087fe17cda5f63e91dab4c22fcd6740ed92ede6752ee229f7769cbd55a33a0df"
checksum = "13a038b485b3d1515179274ec7b5715894f7a3c7435e930655688f293dc30793"
dependencies = [
"ark-bn254",
"ark-ff",
@@ -1152,9 +1152,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.128"
version = "1.0.129"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8"
checksum = "6dbcf9b78a125ee667ae19388837dd12294b858d101fdd393cb9d5501ef09eb2"
dependencies = [
"itoa",
"memchr",

View File

@@ -8,9 +8,9 @@ license = "MIT OR Apache-2.0"
[dependencies]
rand = "0.8.5"
ring-math = "0.1.2"
scalarff = "0.5.1"
ashlang = { version = "0.1.2", default-features = false }
ring-math = "0.2.0"
scalarff = "0.6.0"
ashlang = { version = "0.1.3", default-features = false }
#ashlang = { path = "../ashlang/ashlang", default-features = false }
anyhow = "1.0.89"

View File

@@ -3,53 +3,27 @@ mod test {
use ashlang::r1cs::parser::R1csParser;
use ashlang::rings::DilithiumPolynomialRingElement;
use ashlang::rings::DilithiumRingElement;
use rand::Rng;
use ring_math::Matrix2D;
use ring_math::PolynomialRingElement;
use ring_math::Vector;
use scalarff::BigUint;
use scalarff::FieldElement;
#[test]
fn johnson_lindenstrauss_projection() {
// generate a matrix of size 256x64
// this transforms a vector of length 64
// into a vector of length 256
let sample_jl = |input_dimension: usize,
projection_dimension: usize|
-> Matrix2D<DilithiumRingElement> {
let mut rng = rand::thread_rng();
let mut values = vec![];
// the matrix needs to be sampled randomly with
// each element being 0 with probabiltiy 1/2,
// 1 with probability 1/4 and -1 with probability 1/4
for _ in 0..(input_dimension * projection_dimension) {
let v = rng.gen_range(0..=3);
match v {
0 => values.push(DilithiumRingElement::one()),
1 => values.push(-DilithiumRingElement::one()),
_ => values.push(DilithiumRingElement::zero()),
}
}
Matrix2D {
dimensions: (projection_dimension, input_dimension),
values,
}
};
// iterate a few times and assert that we're always within the bounds
for _ in 0..1000 {
let d = 64;
let input = Vector::<DilithiumRingElement>::sample_uniform(d, &mut rand::thread_rng());
// this is the floored value of sqrt(128)
let root_128_approx = 11;
let projection_size = 256;
let projection = sample_jl(d, projection_size);
let out = projection * input.clone();
assert_eq!(out.len(), projection_size);
// we'll then check the l2 norm of the matrix multiplied
// by the input vector
// println!("{} {}", out.norm_l2(), root_128_approx * input.norm_l2());
assert!(out.norm_l2() < root_128_approx * input.norm_l2());
}
let d = 64;
let input = Vector::<DilithiumRingElement>::sample_uniform(d, &mut rand::thread_rng());
// this is the floored value of sqrt(128)
let root_128_approx = BigUint::from(11u32);
let projection_size = Matrix2D::<DilithiumRingElement>::JL_PROJECTION_SIZE;
// let projection = sample_jl(d, projection_size);
let projection = Matrix2D::sample_jl(d, &mut rand::thread_rng());
let out = projection * input.clone();
assert_eq!(out.len(), projection_size);
// we'll then check the l2 norm of the matrix multiplied
// by the input vector
// println!("{} {}", out.norm_l2(), root_128_approx * input.norm_l2());
assert!(out.norm_l2() < root_128_approx * input.norm_l2());
}
#[test]