Add Fr::inverse

This commit is contained in:
Sean Bowe 2016-08-04 20:22:31 -06:00
parent 2d15e9df46
commit 977a8e8dab
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
4 changed files with 26 additions and 24 deletions

4
Cargo.lock generated
View File

@ -29,3 +29,7 @@ dependencies = [
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "dcb000abd6df9df4c637f75190297ebe56c1d7e66b56bbf3b4aa7aece15f61a2"
"checksum lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417"
"checksum libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "39dfaaa0f4da0f1a06876c5d94329d739ad0150868069cc235f1ddf80a0480e7"

24
snark/Cargo.lock generated
View File

@ -1,24 +0,0 @@
[root]
name = "snark"
version = "0.0.1"
dependencies = [
"gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gcc"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"

View File

@ -17,6 +17,7 @@ extern "C" {
fn libsnarkwrap_Fr_mul(a: *const Fr, b: *const Fr) -> Fr;
fn libsnarkwrap_Fr_sub(a: *const Fr, b: *const Fr) -> Fr;
fn libsnarkwrap_Fr_neg(a: *const Fr) -> Fr;
fn libsnarkwrap_Fr_inverse(a: *const Fr) -> Fr;
fn libsnarkwrap_Fr_is_zero(a: *const Fr) -> bool;
}
@ -33,6 +34,10 @@ impl Fr {
unsafe { libsnarkwrap_Fr_random() }
}
pub fn inverse(&self) -> Self {
unsafe { libsnarkwrap_Fr_inverse(self) }
}
pub fn is_zero(&self) -> bool {
unsafe { libsnarkwrap_Fr_is_zero(self) }
}
@ -105,6 +110,19 @@ impl Neg for Fr {
}
}
#[test]
fn test_inverses() {
super::initialize();
for _ in 0..50 {
let a = Fr::random();
assert!(a != Fr::zero());
let b = a.inverse();
assert!(a * b == Fr::one());
}
}
#[test]
fn test_basic_arith() {
super::initialize();

View File

@ -75,6 +75,10 @@ extern "C" curve_Fr libsnarkwrap_Fr_neg(const curve_Fr *a) {
return -(*a);
}
extern "C" curve_Fr libsnarkwrap_Fr_inverse(const curve_Fr *a) {
return a->inverse();
}
extern "C" bool libsnarkwrap_Fr_is_zero(const curve_Fr *a) {
return a->is_zero();
}