Skip to content

Commit

Permalink
feat: added modular square root computation and fully constrained `de…
Browse files Browse the repository at this point in the history
…rive_from_seed` method (#32)

* got modular square root working. madness. also constrained derive from seed

part of the work required to get a constrained bigcurve::hash_to_curve working

* stashed nonsense to fix later

* fixed reduction parameter error

redc_param previously was only large enough to cover barrett reductions whose input was < 2^{modulus_bits * 2 + 2}

this was insufficient for elliptic curve arithmetic in bignum

redc_param is now large enough to cover barrett reduction inputs of at least 16 * modulus^2

additonally, library upgraded to not trigger compiler warnings for nargo 0.35.0

* format

* fix to pub traits

* added more thorough bignum tests

* typo

* nargo fmt

* removed redundant TODO
  • Loading branch information
zac-williamson authored Oct 17, 2024
1 parent c312ef7 commit 20e03b0
Show file tree
Hide file tree
Showing 3 changed files with 498 additions and 400 deletions.
13 changes: 11 additions & 2 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct BigNum<let N: u32, Params> {
**/
//
// trait BigNumParamsTrait<let N: u32, Params> where Params: RuntimeBigNumParamsTrait<N>, RuntimeBigNumInstance<N, Params>: RuntimeBigNumInstanceTrait<BigNum<N, Params>> {
trait BigNumParamsTrait<let N: u32> where Self: RuntimeBigNumParamsTrait<N> {
pub trait BigNumParamsTrait<let N: u32> where Self: RuntimeBigNumParamsTrait<N> {

fn get_instance() -> RuntimeBigNumInstance<N, Self> where Self: RuntimeBigNumParamsTrait<N>;// <N, Params>;

Expand All @@ -32,7 +32,7 @@ trait BigNumParamsTrait<let N: u32> where Self: RuntimeBigNumParamsTrait<N> {
fn has_multiplicative_inverse() -> bool { true }
}

trait BigNumTrait where BigNumTrait: std::ops::Add + std::ops::Sub + std::ops::Mul + std::ops::Div + std::ops::Eq + RuntimeBigNumTrait {
pub trait BigNumTrait where BigNumTrait: std::ops::Add + std::ops::Sub + std::ops::Mul + std::ops::Div + std::ops::Eq + RuntimeBigNumTrait {
// TODO: this crashes the compiler? v0.32
// fn default() -> Self { std::default::Default::default () }
fn from(limbs: [Field]) -> Self { RuntimeBigNumTrait::from(limbs) }
Expand All @@ -41,6 +41,7 @@ trait BigNumTrait where BigNumTrait: std::ops::Add + std::ops::Sub + std::ops::M
fn modulus() -> Self;
fn modulus_bits(self) -> u32;
fn num_limbs(self) -> u32;
fn derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self;
unconstrained fn __derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self;
unconstrained fn __pow(self, exponent: Self) -> Self;
unconstrained fn __neg(self) -> Self;
Expand Down Expand Up @@ -72,6 +73,7 @@ trait BigNumTrait where BigNumTrait: std::ops::Add + std::ops::Sub + std::ops::M
fn set_limb(&mut self, idx: u32, value: Field) { RuntimeBigNumTrait::set_limb(self, idx, value) }
fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self { RuntimeBigNumTrait::conditional_select(lhs, rhs, predicate) }
fn to_le_bytes<let X: u32>(self) -> [u8; X] { RuntimeBigNumTrait::to_le_bytes(self) }
unconstrained fn __tonelli_shanks_sqrt(self) -> std::option::Option<Self>;
}

impl<let N: u32, Params> BigNumTrait for BigNum<N, Params> where Params: BigNumParamsTrait<N> + RuntimeBigNumParamsTrait<N> {
Expand Down Expand Up @@ -117,6 +119,9 @@ impl<let N: u32, Params> BigNumTrait for BigNum<N, Params> where Params: BigNumP
Params::get_instance().__derive_from_seed(seed)
}

fn derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self {
Params::get_instance().derive_from_seed(seed)
}
unconstrained fn __neg(self) -> Self {
Params::get_instance().__neg(self)
}
Expand Down Expand Up @@ -265,6 +270,10 @@ impl<let N: u32, Params> BigNumTrait for BigNum<N, Params> where Params: BigNumP
fn umod(self, divisor: Self) -> Self {
Params::get_instance().umod(self, divisor)
}

unconstrained fn __tonelli_shanks_sqrt(self) -> std::option::Option<Self> {
Params::get_instance().__tonelli_shanks_sqrt(self)
}
}

impl<let N: u32, Params> BigNum<N, Params> where Params: BigNumParamsTrait<N> + RuntimeBigNumParamsTrait<N> {}
Expand Down
Loading

0 comments on commit 20e03b0

Please sign in to comment.