Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
169 commits
Select commit Hold shift + click to select a range
ed7b345
1: Addition BigUint
ewynx Jun 27, 2022
e0e7733
2: Subtraction BigUint
ewynx Jun 28, 2022
ee0fc8a
Removed old TODO
ewynx Jun 28, 2022
1b367c4
Fix PR comment: make sure the edge case of 2 elements of length 1 for…
ewynx Jun 28, 2022
c081e2c
Prep for 3: Karatsuba multiplication.
ewynx Jun 29, 2022
e08ec14
Larger test works now, which can be used for a small Karatsuba example.
ewynx Jun 29, 2022
940cdd6
WIP Karatsuba just 1 level deep
ewynx Jun 29, 2022
73436f9
mod operation
man2706kum Jun 30, 2022
c45a666
1-Level Karatsuba for vector of length 2n finished up. This is not do…
ewynx Jun 30, 2022
fd3d655
Added some checks wrt working of the 1 level deep mult.
ewynx Jun 30, 2022
744da98
mod operation
man2706kum Jun 30, 2022
600fa3f
Merge branch 'modularArithmetic' of github.com:hashcloak/fuel-crypto …
ewynx Jun 30, 2022
4c0d80e
Updated comment
ewynx Jun 30, 2022
1fd2716
Worked on modular arithmetic.
ewynx Jun 30, 2022
713c325
This starts the arithmetic functionality finite field with p = 2^255-…
ewynx Jul 1, 2022
a83e6d4
Added mod_25519 with tests. Also improved the tests for carry_propagate.
ewynx Jul 1, 2022
807ff00
WIP addition mod p.
ewynx Jul 1, 2022
83352d5
changes in mod_25519
man2706kum Jul 2, 2022
d372382
subtraction and multiplication
man2706kum Jul 4, 2022
5056489
- Fixed the mod_25519 function (it was not reduces enough rounds in s…
ewynx Jul 4, 2022
ba82ad7
Merge branch 'edwards25519' of github.com:hashcloak/fuel-crypto into …
ewynx Jul 4, 2022
72066ef
Addition to merge
ewynx Jul 4, 2022
fb794d8
Scalar multiplication with a scalar with max 32 bits.
ewynx Jul 4, 2022
afdb84d
- Renamed mod_25519 to reduced, since it follows the 'reduced' functi…
ewynx Jul 5, 2022
58fe025
Added constant time equals function with tests.
ewynx Jul 5, 2022
78e4c50
Start on curve arithmetic
ewynx Jul 5, 2022
720273f
multiply64 changed, a*19 replaced by times19(a), subtraction & multip…
man2706kum Jul 5, 2022
8496a3a
overflow for fn multiply fixed
man2706kum Jul 5, 2022
64bb529
- Fixed multiplication of 2 field elements by fixing add64 and add_mu…
ewynx Jul 6, 2022
ac73088
fn inverse added, tests for fn square added
man2706kum Jul 6, 2022
da30518
Run the multiply test
ewynx Jul 6, 2022
20e7a71
Start on arithmetic for BLS12-381.
ewynx Jul 6, 2022
e05058d
- Renaming to be in line with blst impl
ewynx Jul 7, 2022
26ae8d6
The actual vec384 code.
ewynx Jul 7, 2022
4f19992
- Implemented sub_fp
ewynx Jul 8, 2022
a029fa1
montgomery reduction
man2706kum Jul 8, 2022
63b191b
- added the add and subtract for fp2 with tests
ewynx Jul 8, 2022
6fcc761
tests for montgomery not working
man2706kum Jul 11, 2022
2e03108
- Added a part of the function signatures from the blst that we shoul…
ewynx Jul 12, 2022
82af47b
- Implemented add_ mod_n, with tests that are equal to add_fp. More t…
ewynx Jul 13, 2022
f447d7e
montgomery removed
man2706kum Jul 13, 2022
05e0048
- Added mult_mon_n with tests.
ewynx Jul 13, 2022
6f95358
Formatted (forc fmt)
ewynx Jul 13, 2022
a809c69
Added sub_mod_n with tests
ewynx Jul 13, 2022
ec64c62
Reorganized and split up tests in multiple files for clearity
ewynx Jul 13, 2022
0f75136
- Renames the test_helpers file to tests_small_functions
ewynx Jul 13, 2022
0ef586a
redc_mont_n added(testing not done yet)
man2706kum Jul 14, 2022
bd4a93e
Better readability
ewynx Jul 14, 2022
e30af4c
Merge remote-tracking branch 'origin/bls12-381' into bls12-381
ewynx Jul 14, 2022
4d69f81
Testing field_element library through contract
ewynx Jul 14, 2022
2bad092
Making sure there is no recursion happening by accident in the contract
ewynx Jul 15, 2022
3046b37
overflow issue in redc_mont_n
man2706kum Jul 15, 2022
300fd42
Debugging mul_mont_n
ewynx Jul 15, 2022
5ecc807
mul_mont_n and redc_mont_n debugging
man2706kum Jul 18, 2022
3f6181c
- Tests go through script again for now, since contract testing doesn…
ewynx Jul 18, 2022
de6cc51
Added a test for the inverse function (which hasn't been tested yet).…
ewynx Jul 18, 2022
2076b2f
Added a test for the inverse function (which hasn't been tested yet).…
ewynx Jul 18, 2022
4504a82
Merge branch 'edwards25519' of github.com:hashcloak/fuel-crypto into …
ewynx Jul 18, 2022
cdf1986
Fixes for most of the warnings
ewynx Jul 19, 2022
2893b44
Cleanup & TODO added
ewynx Jul 19, 2022
e9e4d13
Small fixes
ewynx Jul 19, 2022
a48e7d0
few comments added
man2706kum Jul 19, 2022
9e987f9
bignum-lib removed
man2706kum Jul 19, 2022
9df07fb
bignum-lib removed
man2706kum Jul 19, 2022
0b7d36f
- Added mul_temp, the zkcrypto multiplication algorithm. Just to chec…
ewynx Jul 20, 2022
f7f4d8b
Temporary montgomery multiplication from nccgroup. To test similar to…
ewynx Jul 20, 2022
f3b9c79
Merge branch 'bls12-381' of https://github.com/hashcloak/fuel-crypto …
man2706kum Jul 20, 2022
beeb298
temp_fe_mont_mul works and added comments to test.
ewynx Jul 20, 2022
e999d8c
Trying to use temp_fe_mont_mul and the conversions as a temporary rep…
ewynx Jul 21, 2022
841f610
Merge branch 'bls12-381' of https://github.com/hashcloak/fuel-crypto …
man2706kum Jul 21, 2022
1216bca
Multiply and square tests through contracts. First steps.
ewynx Jul 21, 2022
cc7acbf
Moved subtraction tests
ewynx Jul 21, 2022
52bd896
Moved addition tests
ewynx Jul 21, 2022
4b35e1e
Converted reduction tests
ewynx Jul 21, 2022
4c2acc3
Converted helpers64 and scalar_mult to testing with Rust.
ewynx Jul 21, 2022
62b1941
Converted rshift tests to Rust
ewynx Jul 21, 2022
ac8906b
Updated README
ewynx Jul 21, 2022
a8ee378
Additions README
ewynx Jul 21, 2022
5f5d2a1
- Changed a line in the inverse implementation (field_element.sw)
ewynx Jul 21, 2022
701da35
Start on arithmetic for BLS12-381.
ewynx Jul 6, 2022
7401f18
- Renaming to be in line with blst impl
ewynx Jul 7, 2022
5a03247
The actual vec384 code.
ewynx Jul 7, 2022
dfe0024
- Implemented sub_fp
ewynx Jul 8, 2022
d9242ea
montgomery reduction
man2706kum Jul 8, 2022
6369569
- added the add and subtract for fp2 with tests
ewynx Jul 8, 2022
ac90c31
tests for montgomery not working
man2706kum Jul 11, 2022
a1a346e
- Added a part of the function signatures from the blst that we shoul…
ewynx Jul 12, 2022
abf1452
- Implemented add_ mod_n, with tests that are equal to add_fp. More t…
ewynx Jul 13, 2022
0cb248b
montgomery removed
man2706kum Jul 13, 2022
50213dc
- Added mult_mon_n with tests.
ewynx Jul 13, 2022
11d7039
Formatted (forc fmt)
ewynx Jul 13, 2022
a7c3cc3
Added sub_mod_n with tests
ewynx Jul 13, 2022
79627b6
Reorganized and split up tests in multiple files for clearity
ewynx Jul 13, 2022
070baf7
- Renames the test_helpers file to tests_small_functions
ewynx Jul 13, 2022
9d63ca7
Better readability
ewynx Jul 14, 2022
94eaae1
redc_mont_n added(testing not done yet)
man2706kum Jul 14, 2022
f1b365b
overflow issue in redc_mont_n
man2706kum Jul 15, 2022
743c249
Debugging mul_mont_n
ewynx Jul 15, 2022
cc3ad80
mul_mont_n and redc_mont_n debugging
man2706kum Jul 18, 2022
8ee7924
Small fixes
ewynx Jul 19, 2022
d235764
- Added mul_temp, the zkcrypto multiplication algorithm. Just to chec…
ewynx Jul 20, 2022
eee70db
Temporary montgomery multiplication from nccgroup. To test similar to…
ewynx Jul 20, 2022
dfeeea2
temp_fe_mont_mul works and added comments to test.
ewynx Jul 20, 2022
8b55d30
Trying to use temp_fe_mont_mul and the conversions as a temporary rep…
ewynx Jul 21, 2022
9edd86a
Merge branch 'bls12-381' of github.com:hashcloak/fuel-crypto into bls…
ewynx Jul 22, 2022
6685301
Fix rebase!
ewynx Jul 22, 2022
3020dc8
Start converting script tests to contract. Also renamed bls12-381 fol…
ewynx Jul 22, 2022
12a762d
Trying to import multiple libs from bls
ewynx Jul 22, 2022
45d2a74
- A single test for bls working through contract
ewynx Jul 22, 2022
0c00ce9
tests for subtraction, mul_by_x_fp and lshift_p added through contracts
man2706kum Jul 25, 2022
0df4a33
test for sub, mul_by_x_fp
man2706kum Jul 25, 2022
892b827
- Finished converting tests_add_fp
ewynx Jul 25, 2022
ae06f20
- Split up l_shift tests
ewynx Jul 26, 2022
f65f332
test for temp_fe_mont_mul goes outOfGas
man2706kum Jul 26, 2022
7c755ea
Converted tests for subfunctions in vect to contract testing.
ewynx Jul 28, 2022
8284a2f
Trying out to test with local fuel-core node.
ewynx Jul 28, 2022
e5bf863
rshift_mod_384 testing
man2706kum Jul 28, 2022
3ffddf1
harness file modification
man2706kum Jul 28, 2022
7e7ee9b
harness file modified
man2706kum Jul 28, 2022
51031ce
- Converted the temp_mul tests
ewynx Jul 28, 2022
a43b441
- Converted vect_fp2 tests to contract testing
ewynx Jul 28, 2022
03f5416
Making a start with new folders
ewynx Jul 29, 2022
43658ab
Added add and sub for Fp
ewynx Jul 29, 2022
25fb487
Added montgomery multiplication with test
ewynx Jul 29, 2022
0145059
Added squaring in Fp
ewynx Jul 29, 2022
3696970
This runs a local fuel-core node for contract testing. In version fue…
ewynx Jul 29, 2022
edc32c3
Adjusted tests for mul and square to align with zkcrypto tests. This …
ewynx Jul 30, 2022
e158f3d
- Started on Fp2 arithmetic
ewynx Jul 30, 2022
539750c
Square in Fp is working. Test in Rust added.
ewynx Aug 1, 2022
03ad0ea
This file is only for testing purposes. When testing through contract…
ewynx Aug 1, 2022
e804a32
Testing with custom gas limit
ewynx Aug 2, 2022
630a8fa
tests added for mul(failing), add, sub, neg for fp2
man2706kum Aug 2, 2022
29e3304
- Fp and Fp2 are implementing various traits; these have been added
ewynx Aug 2, 2022
951ab99
- Added Choice and CtOption. However, it's unclear how we can make th…
ewynx Aug 2, 2022
6a95611
add, sub, neg, mul_by_01 for fp6 and sum_of_products_6 in fp(testing …
man2706kum Aug 3, 2022
a8ec3d4
Compile fixes
ewynx Aug 3, 2022
1ddf3b4
To try to run something on the droplet.
ewynx Aug 3, 2022
9c78652
Create a new vector has changed in newer versions of Sway
ewynx Aug 3, 2022
f2c30ed
More fixes for newer Sway version
ewynx Aug 3, 2022
125d7e0
To test contract with custom gas limit in tests-bls
ewynx Aug 3, 2022
6eea130
- Commented out some functions which can't compile yet (heavy in fp6)
ewynx Aug 3, 2022
a08fd73
- Extract useful functions to utils.sw
ewynx Aug 3, 2022
a96872a
Fuel-rs to version 0.19 in tests_bls12_381. Works in testfile for fp,…
ewynx Aug 4, 2022
0b74ed1
- Start on scalar.sw
ewynx Aug 4, 2022
27e51f7
- Added functions to scalar.sw
ewynx Aug 4, 2022
78fbbd0
Causes error
ewynx Aug 4, 2022
6c7ef51
Improved imports
ewynx Aug 4, 2022
8b61e5d
Commented out functions that will give error
ewynx Aug 5, 2022
cd49a80
frobenius_map in fp2 & fp6
man2706kum Aug 5, 2022
2c3e6e3
error fixing
Aug 5, 2022
19f1e85
import fix
ewynx Aug 5, 2022
a6eb90b
Comment out the function that is not being able to compile yet (Inter…
ewynx Aug 5, 2022
fde03d2
Update fuels version in bls12-381 lib.
ewynx Aug 5, 2022
26f0b8e
- Renamed old bls and tests-bls folder for clarity. These will be rem…
ewynx Aug 6, 2022
1596b90
- Added comment where error Immediate18TooLarge pops up
ewynx Aug 9, 2022
6c7b479
- Fp2 multiplication fixed
ewynx Aug 9, 2022
919fc75
- Fixes in g1.sw
ewynx Aug 10, 2022
0b9cc5d
- Added ConstantTimeEq and impl for Fp
ewynx Aug 10, 2022
d9ca8df
- Fixed implementation for conditional_select
ewynx Aug 10, 2022
885e1ee
Trying to create the From trait
ewynx Aug 10, 2022
1d3801c
- Added not and conditional_select for Choice
ewynx Aug 11, 2022
2bfe21b
- fp6: added conditional_select and several trait implementations
ewynx Aug 11, 2022
af824c2
- Started with Fp12
ewynx Aug 12, 2022
1a117ae
- Added test lexicographically_largest_fp2 (subset of test from zkcry…
ewynx Aug 13, 2022
f37ba7f
Running forc test in tests_bls12_381 gives Immediate18TooLarge.
ewynx Aug 31, 2022
08ef32f
- Updated fuels
ewynx Sep 19, 2022
2d538fc
Rewrote some if branches and with that removed some TODOs.
ewynx Sep 22, 2022
e63759e
For Fp we can run add, sub, mul and sqrt at the same time. Adding the…
ewynx Sep 22, 2022
79c36f5
Old code removed.
ewynx Sep 22, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sw linguist-language=Rust
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

*/Cargo.lock
*/Forc.lock

*/out/*
*/target/*
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# fuel-crypto
Various Cryptographic Primitives in Sway for the Fuel VM

# Testing

## Spin Up a Fuel node
From [here](https://fuellabs.github.io/sway/v0.19.0/introduction/overview.html).
In a separate tab in your terminal, spin up a local Fuel node:


`fuel-core --db-type in-memory`

This starts a Fuel node with a volatile database that will be cleared when shut down (good for testing purposes).

Make sure `fuel-core` is up to date. This can be done with [fuelup](https://github.com/FuelLabs/fuelup). Also, make sure there's only 1 `fuel-core` installed (check this with `which -a fuel-core`).

## Edwards25519

The testing is done in Rust; the testfiles are in `/test_contract`. See reference in [Sway documentation](https://fuellabs.github.io/sway/v0.17.0/testing/testing-with-rust.html).

In `main.sw` the contract makes the connection with the library code in edward25519. This in turn can be tested in the `/test_contract/tests` folder.

To run tests for edwards25519 folder:
```
cd test_contract
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this

forc test
```

## BLS

To run tests for bls folder:
```
cd tests-bls
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this

forc test
```

## Running a script
Also to run a script a Fuel Node has to be spun up.

```
forc run --unsigned --pretty-print
```
2 changes: 2 additions & 0 deletions bls12_381/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
15 changes: 15 additions & 0 deletions bls12_381/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[project]
name = "bls12_381"
version = "0.1.0"
authors = ["Hashcloak"]
edition = "2021"
license = "Apache-2.0"

[dependencies]
fuels = { version = "0.23", features = ["fuel-core-lib"] }
tokio = { version = "1.12", features = ["rt", "macros"] }

[[test]]
harness = true
name = "integration_tests"
path = "tests/harness.rs"
7 changes: 7 additions & 0 deletions bls12_381/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Hashcloak"]
entry = "lib.sw"
license = "Apache-2.0"
name = "bls12_381"

[dependencies]
204 changes: 204 additions & 0 deletions bls12_381/src/choice.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
library choice;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since most of this code is from the Dalek-Cryptography subtle library, we should reproduce the license at the beginning of the file since they released the code under the 3-clause BSD license.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would maybe move this file to a utils folder since having a constant-time boolean implementation will be important for other cryptography primitives that we may implement in sway.


use core::num::*;
use std::{option::Option, u128::U128};
use core::ops::{Eq, BitwiseAnd, BitwiseOr, BitwiseXor};

/////////////// IMPORTANT<start> ///////////////

// All of this is coming from the dalek cryptograhpy project
// see https://github.com/dalek-cryptography/subtle/blob/main/src/lib.rs

/////////////// IMPORTANT<end> ///////////////

/// The `Choice` struct represents a choice for use in conditional assignment.
///
/// It is a wrapper around a `u8`, which should have the value either `1` (true)
/// or `0` (false).
pub struct Choice { c: u8 }

// Can't use name "From" because of collision with trait in U128 (even though not importing u128::*).
// This seems to be a bug in Sway, see discussion in Discord https://discord.com/channels/732892373507375164/734213700835082330/1007067117029433405
pub trait From {
fn from(input: u8) -> Self;
fn into(self) -> u8;
}

impl From for Choice {
fn from(input: u8) -> Self {
Choice { c: input }
}

fn into(self) -> u8 {
self.c
}
}

// If equals 1u8 => false, if 0u8 => true
pub fn opposite_choice_value(a: u8) -> bool {
asm(r1: a, r2) {
eq r2 r1 zero;
r2: bool
}
}

impl Choice {
pub fn unwrap_u8(self) -> u8 {
self.c
}

pub fn unwrap_as_bool(self) -> bool {
self.c == 1u8
}

pub fn from_bool(b: bool) -> Choice {
if b {
Choice{ c: 1u8}
} else {
Choice{ c: 0u8}
}
}
}

impl Choice {
pub fn not(self) -> Choice {
~Choice::from_bool(opposite_choice_value(self.c))
}
}

impl BitwiseXor for u8 {
fn binary_xor(self, other: Self) -> Self {
asm(r1: self, r2: other, r3) {
xor r3 r1 r2;
r3: u8
}
}
}

impl ConditionallySelectable for u8 {
fn conditional_select(a: u8, b: u8, choice: Choice) -> u32 {
let mask = wrapping_neg(choice.unwrap_u8());
b.binary_xor(mask & (a.binary_xor(b)))
}
}

impl ConditionallySelectable for Choice {
fn conditional_select(a: Self, b: Self, choice: Choice) -> Self {
~Choice::from(~u8::conditional_select(a.c, b.c, choice))
}
}

impl BitwiseAnd for u8 {
fn binary_and(self, other: Self) -> Self {
asm(r1: self, r2: other, r3) {
and r3 r1 r2;
r3: u8
}
}
}

impl BitwiseAnd for Choice {
fn binary_and(self, other: Self) -> Self {
~Choice::from(self.c & other.c)
}
}

impl BitwiseOr for u8 {
fn binary_or(self, other: Self) -> Self {
asm(r1: self, r2: other, r3) {
or r3 r1 r2;
r3: u8
}
}
}

impl BitwiseOr for Choice {
fn binary_or(self, other: Self) -> Self {
~Choice::from(self.c | other.c)
}
}


/// The `CtOption<T>` type represents an optional value similar to the
/// [`Option<T>`](core::option::Option) type but is intended for
/// use in constant time APIs.

pub struct CtOption<T> {
value: T,
is_some: Choice,
}

impl<T> CtOption<T> {
/// This method is used to construct a new `CtOption<T>` and takes
/// a value of type `T`, and a `Choice` that determines whether
/// the optional value should be `Some` or not. If `is_some` is
/// false, the value will still be stored but its value is never
/// exposed.
pub fn new(value: T, is_some: Choice) -> CtOption<T> {
CtOption {
value: value,
is_some: is_some,
}
}

pub fn new_from_bool(value: T, is_some: bool) -> CtOption<T> {
match is_some {
true => CtOption {value: value, is_some: Choice{ c: 1 },},
false => CtOption {value: value, is_some: Choice{ c: 0 },},
}
}

//To reference `is_some` this would have to go in a separate Impl
pub fn is_none(self) -> bool {
!self.is_some.unwrap_as_bool()
}

pub fn is_some(self) -> bool {
self.is_some.unwrap_as_bool()
}

pub fn unwrap(self) -> T {
self.value
}

// unwrap_or can't be implemented here..
// There is no type restriction possible on generics in Sway
// See https://discord.com/channels/732892373507375164/734213700835082330/1007097764242522273
}

pub trait ConditionallySelectable {
// Select a if choice == 1 or select b if choice == 0, in constant time.
fn conditional_select(a: Self, b: Self, choice: Choice) -> Self;
}

// From https://github.com/dalek-cryptography/subtle/blob/main/src/lib.rs
pub trait ConstantTimeEq {
fn ct_eq(self, other: Self) -> Choice;
}

fn add_wrap_64(a: u64, b :u64) -> u64 {
let a_128: U128 = ~U128::from(0, a);
let b_128: U128 = ~U128::from(0, b);
(a_128 + b_128).lower
}

pub fn wrapping_neg(a: u64) -> u64 {
add_wrap_64(~u64::max() - a, 1)
}

impl ConstantTimeEq for u64 {
fn ct_eq(self, other: u64) -> Choice {
// comments from reference impl
// x == 0 if and only if self == other
let x: u64 = self ^ other;

// If x == 0, then x and -x are both equal to zero;
// otherwise, one or both will have its high bit set.
let y: u64 = (x | wrapping_neg(x)) >> 63;

// Result is the opposite of the high bit (now shifted to low).
let res: u8 = y ^ (1u64);
~Choice::from(res)
}
}
103 changes: 103 additions & 0 deletions bls12_381/src/f12.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
library fp12;

dep fp6;

use fp6::Fp6;
use choice::{ConstantTimeEq};
use core::ops::{Eq, Add, Subtract, Multiply};

pub struct Fp12 {
c0: Fp6,
c1: Fp6,
}

impl ConditionallySelectable for Fp12 {
fn conditional_select(a: Self, b: Self, choice: Choice) -> Self {
Fp12 {
c0: ~Fp6::conditional_select(a.c0, b.c0, choice),
c1: ~Fp6::conditional_select(a.c1, b.c1, choice),
}
}
}

impl ConstantTimeEq for Fp12 {
fn ct_eq(self, other: Self) -> Choice {
self.c0.ct_eq(other.c0) & self.c1.ct_eq(other.c1)
}
}

impl Fp12 {
fn eq(self, other: Self) -> bool {
self.ct_eq(other).unwrap_as_bool()
}

pub fn zero() -> Self {
Fp12 {
c0: ~Fp6::zero(),
c1: ~Fp6::zero(),
}
}

pub fn one() -> Self {
Fp12 {
c0: ~Fp6::one(),
c1: ~Fp6::zero(),
}
}

fn from(f: Fp) -> Fp12 {
Fp12 {
c0: ~Fp6::from(f),
c1: ~Fp6::zero(),
}
}

fn from(f: Fp2) -> Fp12 {
Fp12 {
c0: ~Fp6::from(f),
c1: ~Fp6::zero(),
}
}

fn from(f: Fp6) -> Fp12 {
Fp12 {
c0: f,
c1: ~Fp6::zero(),
}
}

pub fn is_zero(self) -> Choice {
self.c0.is_zero().binary_and(self.c1.is_zero())
}

fn neg(self) -> Self {
Fp12 {
c0: self.c0.neg(),
c1: self.c1.neg(),
}
}
}

impl Eq for Fp12 {
fn eq(self, other: Self) -> bool {
self.eq(other)
}
}

impl Add for Fp12 {
fn add(self, rhs: Fp12) -> Self {
Fp12 {
c0: self.c0 + rhs.c0,
c1: self.c1 + rhs.c1,
}
}
}

impl Subtract for Fp12 {
fn sub(self, rhs: Fp12) -> Self {
Fp12 {
c0: self.c0 - rhs.c0,
c1: self.c1 - rhs.c1,
}
}
}
Loading