-
Notifications
You must be signed in to change notification settings - Fork 599
chore: add struct for each bigint modulus #4422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
906065e
c8e1e1e
76d4019
eb333a8
e950804
7b825d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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]; | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want this to also have Bounds for Add, Sub, Mul and Div?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you clarify what do you mean with 'Bounds'?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can create an issue to have BigField inheriting from the Add, Sub, Mul trait Bounds |
||
| 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) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,4 @@ fn main(mut x: u32, y: u32, z: u32) { | |
|
|
||
| x *= 8; | ||
| assert(x > 9); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.