Skip to content

Commit

Permalink
Works on zk
Browse files Browse the repository at this point in the history
  • Loading branch information
cmester0 committed Mar 14, 2024
1 parent 7047ec9 commit 5ec5a9f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ovn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ hacspec-concordium-derive = { path = "../concordium-derive" } # 4d4b024b547a1f12

quickcheck = "1"
quickcheck_macros = "1"
bls12_381 = "0.8"

[dev-dependencies]
criterion = "0.4"
rand = "0.8"
bls12_381 = "*"

[features]
hax_compilation = []
13 changes: 13 additions & 0 deletions ovn/src/ovn_zk_secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ impl MGroup for Point {
}

}

use bls12_381::*;

impl MGroup for Gt {
fn pow (p: Self,exp: Self::Scalar) -> Self {
p * exp
}

fn hash(inp: Vec<Self>) -> Self::Scalar {
return Self::Scalar::ONE // TODO
}

}
55 changes: 44 additions & 11 deletions ovn/tests/ovn_zk_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use rand_core::{RngCore, *};
// use quickcheck::RngCore;
use hacspec_bip_340::{GroupTrait::*, Point, *};

use rand::rngs::StdRng;
use rand::random;
use rand::rngs::StdRng;

#[cfg(test)]
pub fn group_test<G: MGroup>() {
Expand All @@ -38,6 +38,11 @@ pub fn secp256k1_group_correctness() {
group_test::<Point>()
}

// #[test]
// pub fn bls12_381_real_group_correctness() {
// group_test::<Gt>()
// }

#[cfg(test)]
pub fn schnorr_zkp_correctness<G: MGroup>() -> bool {
let random_r: <G as Group>::Scalar = <G as Group>::Scalar::random(rand::thread_rng());
Expand All @@ -58,6 +63,11 @@ pub fn schnorr_zkp_secp256k1_correctness() {
// .quickcheck(schnorr_zkp_correctness::<Point> as fn() -> bool)
}

#[test]
pub fn bls12_381_real_schnorr_correctness() {
assert!(schnorr_zkp_correctness::<Gt>())
}

#[cfg(test)]
pub fn or_zkp_correctness<G: MGroup>(v: bool) -> bool {
let random_w: <G as Group>::Scalar = <G as Group>::Scalar::random(rand::thread_rng());
Expand All @@ -74,12 +84,19 @@ pub fn or_zkp_correctness<G: MGroup>(v: bool) -> bool {

#[test]
// TODO: Fix inverse opeation, should make this test parse
pub fn or_zkp_secp256k1_correctness() {
pub fn or_zkp_secp256k1() {
QuickCheck::new()
.tests(10)
.quickcheck(or_zkp_correctness::<Point> as fn(bool) -> bool)
}

#[test]
pub fn or_zkp_bls12_381_real() {
QuickCheck::new()
.tests(10)
.quickcheck(or_zkp_correctness::<Gt> as fn(bool) -> bool)
}

#[cfg(test)]
pub fn sum_to_zero<G: MGroup, const n: usize>() {
let mut xis: [G::Scalar; n] = [G::Scalar::ZERO; n];
Expand Down Expand Up @@ -109,9 +126,16 @@ pub fn sum_to_zero_secp256k1() {
sum_to_zero::<Point, 55>()
}

#[test]
pub fn sum_to_zero_bls12_381_real() {
sum_to_zero::<Point, 55>()
}


#[cfg(test)]
pub fn test_params_of_group<G: MGroup, A: HasActions>()
where G::Scalar: hacspec_concordium::Serial + hacspec_concordium::Deserial,
where
G::Scalar: hacspec_concordium::Serial + hacspec_concordium::Deserial,
{
// Setup the context
let mut ctx = hacspec_concordium::test_infrastructure::ReceiveContextTest::empty();
Expand All @@ -122,13 +146,14 @@ pub fn test_params_of_group<G: MGroup, A: HasActions>()
};
let parameter_bytes = to_bytes(&parameter);
let ctx_params = ctx.clone().set_parameter(&parameter_bytes);
let param_back : Result<RegisterParam::<G::Scalar>, ParseError> = ctx_params.parameter_cursor().get();
let param_back: Result<RegisterParam<G::Scalar>, ParseError> =
ctx_params.parameter_cursor().get();
assert!(param_back.is_ok());

let wu_param = param_back.unwrap();
assert_eq!(wu_param.rp_i,parameter.rp_i);
assert_eq!(wu_param.rp_xi,parameter.rp_xi);
assert_eq!(wu_param.rp_zkp_random,parameter.rp_zkp_random);
assert_eq!(wu_param.rp_i, parameter.rp_i);
assert_eq!(wu_param.rp_xi, parameter.rp_xi);
assert_eq!(wu_param.rp_zkp_random, parameter.rp_zkp_random);
}

#[test]
Expand Down Expand Up @@ -178,8 +203,7 @@ where
};
let parameter_bytes = to_bytes(&parameter);
(_, state) =
commit_to_vote::<G, n, A>(ctx.clone().set_parameter(&parameter_bytes), state)
.unwrap();
commit_to_vote::<G, n, A>(ctx.clone().set_parameter(&parameter_bytes), state).unwrap();
}

for i in 0..n {
Expand All @@ -199,7 +223,8 @@ where
let parameter = TallyParameter {};
let parameter_bytes = to_bytes(&parameter);

(_, state) = tally_votes::<G, n, A>(ctx.clone().set_parameter(&parameter_bytes), state).unwrap();
(_, state) =
tally_votes::<G, n, A>(ctx.clone().set_parameter(&parameter_bytes), state).unwrap();

let mut count = 0u32;
for v in votes {
Expand All @@ -213,7 +238,8 @@ where
}

#[cfg(test)]
fn randomized_full_test<G: MGroup, const n: usize> () -> bool where
fn randomized_full_test<G: MGroup, const n: usize>() -> bool
where
G::Scalar: hacspec_concordium::Serial + hacspec_concordium::Deserial,
{
let mut votes: [bool; n] = [false; n];
Expand Down Expand Up @@ -267,3 +293,10 @@ fn test_full_secp256k1() {
.tests(1)
.quickcheck(randomized_full_test::<Point, 15> as fn() -> bool)
}

// #[test]
// fn test_full_bls12_381_real() {
// QuickCheck::new()
// .tests(1)
// .quickcheck(randomized_full_test::<Gt, 15> as fn() -> bool)
// }

0 comments on commit 5ec5a9f

Please sign in to comment.