Skip to content

Commit

Permalink
chore: Sync to noir-lang/noir
Browse files Browse the repository at this point in the history
chore: add struct for each bigint modulus (AztecProtocol/aztec-packages#4422)
chore: switch noir pull to master branch (AztecProtocol/aztec-packages#4581)
  • Loading branch information
AztecBot committed Feb 15, 2024
1 parent 78ef013 commit eef542e
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 75 deletions.
329 changes: 302 additions & 27 deletions noir_stdlib/src/bigint.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ops::{Add, Sub, Mul, Div, Rem,};

use crate::ops::{Add, Sub, Mul, Div};
use crate::cmp::Eq;

global bn254_fq = [0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
0x5d, 0x58, 0x81, 0x81, 0xb6, 0x45, 0x50, 0xb8, 0x29, 0xa0, 0x31, 0xe1, 0x72, 0x4e, 0x64, 0x30];
Expand Down Expand Up @@ -36,46 +36,321 @@ impl BigInt {
#[builtin(bigint_from_le_bytes)]
fn from_le_bytes(bytes: [u8], modulus: [u8]) -> BigInt {}
#[builtin(bigint_to_le_bytes)]
pub fn to_le_bytes(self) -> [u8] {}
fn to_le_bytes(self) -> [u8] {}

pub fn bn254_fr_from_le_bytes(bytes: [u8]) -> BigInt {
BigInt::from_le_bytes(bytes, bn254_fr)
fn check_32_bytes(self: Self, other: BigInt) -> bool {
let bytes = self.to_le_bytes();
let o_bytes = other.to_le_bytes();
let mut result = true;
for i in 0..32{
result = result & (bytes[i] == o_bytes[i]);
}
result
}
pub fn bn254_fq_from_le_bytes(bytes: [u8]) -> BigInt {
BigInt::from_le_bytes(bytes, bn254_fq)
}


trait BigField {
fn from_le_bytes(bytes: [u8]) -> Self;
fn to_le_bytes(self) -> [u8];
}

struct Secpk1Fq {
inner: BigInt,
}

impl BigField for Secpk1Fq {
fn from_le_bytes(bytes: [u8]) -> Secpk1Fq {
Secpk1Fq {
inner: BigInt::from_le_bytes(bytes, secpk1_fq)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
pub fn secpk1_fq_from_le_bytes(bytes: [u8]) -> BigInt {
BigInt::from_le_bytes(bytes, secpk1_fq)
}

impl Add for Secpk1Fq {
fn add(self: Self, other: Secpk1Fq) -> Secpk1Fq {
Secpk1Fq {
inner: self.inner.bigint_add(other.inner)
}
}
pub fn secpk1_fr_from_le_bytes(bytes: [u8]) -> BigInt {
BigInt::from_le_bytes(bytes, secpk1_fr)
}
impl Sub for Secpk1Fq {
fn sub(self: Self, other: Secpk1Fq) -> Secpk1Fq {
Secpk1Fq {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Secpk1Fq {
fn mul(self: Self, other: Secpk1Fq) -> Secpk1Fq {
Secpk1Fq {
inner: self.inner.bigint_mul(other.inner)
}

impl Add for BigInt {
fn add(self: Self, other: BigInt) -> BigInt {
self.bigint_add(other)
}
}
impl Sub for BigInt {
fn sub(self: Self, other: BigInt) -> BigInt {
self.bigint_sub(other)
impl Div for Secpk1Fq {
fn div(self: Self, other: Secpk1Fq) -> Secpk1Fq {
Secpk1Fq {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Mul for BigInt {
fn mul(self: Self, other: BigInt) -> BigInt {
self.bigint_mul(other)
impl Eq for Secpk1Fq {
fn eq(self: Self, other: Secpk1Fq) -> bool {
self.inner.check_32_bytes(other.inner)
}
}
impl Div for BigInt {
fn div(self: Self, other: BigInt) -> BigInt {
self.bigint_div(other)

struct Secpk1Fr {
inner: BigInt,
}

impl BigField for Secpk1Fr {
fn from_le_bytes(bytes: [u8]) -> Secpk1Fr {
Secpk1Fr {
inner: BigInt::from_le_bytes(bytes, secpk1_fr)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}

impl Add for Secpk1Fr {
fn add(self: Self, other: Secpk1Fr) -> Secpk1Fr {
Secpk1Fr {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Rem for BigInt {
fn rem(self: Self, other: BigInt) -> BigInt {
let quotient = self.bigint_div(other);
self.bigint_sub(quotient.bigint_mul(other))
impl Sub for Secpk1Fr {
fn sub(self: Self, other: Secpk1Fr) -> Secpk1Fr {
Secpk1Fr {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Secpk1Fr {
fn mul(self: Self, other: Secpk1Fr) -> Secpk1Fr {
Secpk1Fr {
inner: self.inner.bigint_mul(other.inner)
}

}
}
impl Div for Secpk1Fr {
fn div(self: Self, other: Secpk1Fr) -> Secpk1Fr {
Secpk1Fr {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Secpk1Fr {
fn eq(self: Self, other: Secpk1Fr) -> bool {
self.inner.check_32_bytes(other.inner)
}
}

struct Bn254Fr {
inner: BigInt,
}

impl BigField for Bn254Fr {
fn from_le_bytes(bytes: [u8]) -> Bn254Fr {
Bn254Fr {
inner: BigInt::from_le_bytes(bytes, bn254_fr)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}

impl Add for Bn254Fr {
fn add(self: Self, other: Bn254Fr) -> Bn254Fr {
Bn254Fr {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Bn254Fr {
fn sub(self: Self, other: Bn254Fr) -> Bn254Fr {
Bn254Fr {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Bn254Fr {
fn mul(self: Self, other: Bn254Fr) -> Bn254Fr {
Bn254Fr {
inner: self.inner.bigint_mul(other.inner)
}

}
}
impl Div for Bn254Fr {
fn div(self: Self, other: Bn254Fr) -> Bn254Fr {
Bn254Fr {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Bn254Fr {
fn eq(self: Self, other: Bn254Fr) -> bool {
self.inner.check_32_bytes(other.inner)
}
}

struct Bn254Fq {
inner: BigInt,
}

impl BigField for Bn254Fq {
fn from_le_bytes(bytes: [u8]) -> Bn254Fq {
Bn254Fq {
inner: BigInt::from_le_bytes(bytes, bn254_fq)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}

impl Add for Bn254Fq {
fn add(self: Self, other: Bn254Fq) -> Bn254Fq {
Bn254Fq {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Bn254Fq {
fn sub(self: Self, other: Bn254Fq) -> Bn254Fq {
Bn254Fq {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Bn254Fq {
fn mul(self: Self, other: Bn254Fq) -> Bn254Fq {
Bn254Fq {
inner: self.inner.bigint_mul(other.inner)
}

}
}
impl Div for Bn254Fq {
fn div(self: Self, other: Bn254Fq) -> Bn254Fq {
Bn254Fq {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Bn254Fq {
fn eq(self: Self, other: Bn254Fq) -> bool {
self.inner.check_32_bytes(other.inner)
}
}

struct Secpr1Fq {
inner: BigInt,
}

impl BigField for Secpr1Fq {
fn from_le_bytes(bytes: [u8]) -> Secpr1Fq {
Secpr1Fq {
inner: BigInt::from_le_bytes(bytes, secpr1_fq)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}

impl Add for Secpr1Fq {
fn add(self: Self, other: Secpr1Fq) -> Secpr1Fq {
Secpr1Fq {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Secpr1Fq {
fn sub(self: Self, other: Secpr1Fq) -> Secpr1Fq {
Secpr1Fq {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Secpr1Fq {
fn mul(self: Self, other: Secpr1Fq) -> Secpr1Fq {
Secpr1Fq {
inner: self.inner.bigint_mul(other.inner)
}

}
}
impl Div for Secpr1Fq {
fn div(self: Self, other: Secpr1Fq) -> Secpr1Fq {
Secpr1Fq {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Secpr1Fq {
fn eq(self: Self, other: Secpr1Fq) -> bool {
self.inner.check_32_bytes(other.inner)
}
}

struct Secpr1Fr {
inner: BigInt,
}

impl BigField for Secpr1Fr {
fn from_le_bytes(bytes: [u8]) -> Secpr1Fr {
Secpr1Fr {
inner: BigInt::from_le_bytes(bytes, secpr1_fr)
}
}
fn to_le_bytes(self) -> [u8] {
self.inner.to_le_bytes()
}
}

impl Add for Secpr1Fr {
fn add(self: Self, other: Secpr1Fr) -> Secpr1Fr {
Secpr1Fr {
inner: self.inner.bigint_add(other.inner)
}
}
}
impl Sub for Secpr1Fr {
fn sub(self: Self, other: Secpr1Fr) -> Secpr1Fr {
Secpr1Fr {
inner: self.inner.bigint_sub(other.inner)
}
}
}
impl Mul for Secpr1Fr {
fn mul(self: Self, other: Secpr1Fr) -> Secpr1Fr {
Secpr1Fr {
inner: self.inner.bigint_mul(other.inner)
}

}
}
impl Div for Secpr1Fr {
fn div(self: Self, other: Secpr1Fr) -> Secpr1Fr {
Secpr1Fr {
inner: self.inner.bigint_div(other.inner)
}
}
}
impl Eq for Secpr1Fr {
fn eq(self: Self, other: Secpr1Fr) -> bool {
self.inner.check_32_bytes(other.inner)
}
}
2 changes: 1 addition & 1 deletion test_programs/execution_success/3_add/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ fn main(mut x: u32, y: u32, z: u32) {

x *= 8;
assert(x > 9);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit eef542e

Please sign in to comment.