Skip to content

Commit

Permalink
chore: implement From trait on BigNum
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jan 15, 2025
1 parent 362d05f commit 5222a10
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
24 changes: 9 additions & 15 deletions src/bignum.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub trait BigNumTrait: Neg + Add + Sub + Mul + Div + Eq {
// fn default() -> Self { std::default::Default::default () }
pub fn new() -> Self;
pub fn one() -> Self;
pub fn from_field(field: Field) -> Self;
pub fn derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self;
pub unconstrained fn __derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self;
pub fn from_slice(limbs: [Field]) -> Self;
Expand Down Expand Up @@ -89,15 +88,15 @@ pub trait BigNumTrait: Neg + Add + Sub + Mul + Div + Eq {
pub fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self;
}

// impl<let N: u32, let MOD_BITS: u32, Params> std::convert::From<Field> for BigNum<N, MOD_BITS, Params>
// where
// Params: BigNumParamsGetter<N, MOD_BITS>,
// {
// fn from(input: Field) -> Self {
// let params = Params::get_params();
// Self { limbs: from_field::<N, MOD_BITS>(params, input) }
// }
// }
impl<let N: u32, let MOD_BITS: u32, Params> std::convert::From<Field> for BigNum<N, MOD_BITS, Params>
where
Params: BigNumParamsGetter<N, MOD_BITS>,
{
fn from(input: Field) -> Self {
let params = Params::get_params();
Self { limbs: from_field::<N, MOD_BITS>(params, input) }
}
}

impl<let N: u32, let MOD_BITS: u32, Params> Neg for BigNum<N, MOD_BITS, Params>
where
Expand All @@ -124,11 +123,6 @@ where
result
}

fn from_field(field: Field) -> Self {
let params = Params::get_params();
Self { limbs: from_field::<N, MOD_BITS>(params, field) }
}

fn derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self {
let params = Params::get_params();
Self { limbs: derive_from_seed::<_, MOD_BITS, _>(params, seed) }
Expand Down
6 changes: 3 additions & 3 deletions src/tests/bignum_test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,14 @@ fn test_expressions() {
#[test]
fn test_from_field_1_digit() {
let field: Field = 1;
let result = Fq::from_field(field);
let result = Fq::from(field);
assert(result == Fq::one());
}

#[test]
fn test_from_field_2_digits() {
let field: Field = 762576765071760201410184025311678064293966151975347778787092903729041075;
let result = Fq::from_field(field);
let result = Fq::from(field);
let expected: Fq =
BigNum { limbs: [0xe88ed97f8f707abd3fa65763c80eb3, 0x6e7d8b5586595aa1fb2ee04d5cb4f5, 0x0] };
assert(result == expected);
Expand All @@ -809,7 +809,7 @@ fn test_from_field_2_digits() {
#[test]
fn test_from_field_3_digits() {
let field: Field = -1;
let result = Fq::from_field(field);
let result = Fq::from(field);
let expected: Fq = BigNum {
limbs: [0x33e84879b9709143e1f593f0000000, 0x4e72e131a029b85045b68181585d28, 0x3064],
};
Expand Down

0 comments on commit 5222a10

Please sign in to comment.