Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
added an unconstrained power method
Browse files Browse the repository at this point in the history
  • Loading branch information
harshnambiar committed Sep 7, 2023
1 parent 8ee896d commit a75fcba
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# signed_int
The Noir repository for accessing Signed Integers by Resurgence Labs


# usage
To Use as a Dependency, add the following line to your project's Nargo.toml under [dependencies]:
signed_int = { tag = "v2.2.5", git = "https://github.com/resurgencelabs/signed_int" }
36 changes: 36 additions & 0 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ fn modSigned(s1: Signed, s2: Signed) -> Signed {
}
}


// Returns the Square of the Given Signed Integer
fn squareSigned(s: Signed) -> Signed {


Expand All @@ -132,6 +134,8 @@ fn squareSigned(s: Signed) -> Signed {

}


// Returns the Cube of the Given Signed Integer
fn cubeSigned(s: Signed) -> Signed {


Expand All @@ -143,6 +147,21 @@ fn cubeSigned(s: Signed) -> Signed {

}


// Obtain said Signed Integer raised to a given power. Unconstrained
unconstrained fn powerSigned(s: Signed, k: u32) -> Signed {
assert (k > 0);
let mut val = 1;
for i in 1..(k+1) {
val = val * s.value;
}
let sn = Signed {
sign: ((k % 2 == 0) | (s.sign)),
value: val,
};
sn
}

fn negativeSigned(s: Signed) -> Signed {
let sn = Signed{
sign: !s.sign,
Expand Down Expand Up @@ -421,3 +440,20 @@ fn test_zero_equivalence() {
let cmp = compareSigned(s1, s2);
assert (cmp == 0);
}


#[test]
fn test_power_positive() {
let s = toSigned(true, 2);
let p = powerSigned(s, 3);
assert (p.value == 8);
assert (p.sign);
}

#[test]
fn test_power_negative() {
let s = toSigned(false, 2);
let p = powerSigned(s, 5);
assert (p.value == 32);
assert (!p.sign);
}

0 comments on commit a75fcba

Please sign in to comment.