Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions common/src/contract/cryptography/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ pub mod types;
mod pairings_bn254;
mod polynomial_eval;
mod transcript;
mod turbo_plonk;

pub const fn cryptography_libraries() -> &'static str {
concat!(
crate::TURBOPLONK_LIBRARY!(),
crate::TYPES_LIBRARY!(),
crate::PAIRINGSBN254_LIBRARY!(),
crate::POLYNOMIALEVAL_LIBRARY!(),
Expand Down
75 changes: 37 additions & 38 deletions common/src/contract/cryptography/pairings_bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ macro_rules! PAIRINGSBN254_LIBRARY {
* @dev Provides some basic methods to compute bilinear pairings, construct group elements and misc numerical methods
*/
library Bn254Crypto {
uint256 constant p_mod = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
uint256 constant r_mod = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 constant p_mod =
21888242871839275222246405745257275088696311157297823662689037894645226208583;
uint256 constant r_mod =
21888242871839275222246405745257275088548364400416034343698204186575808495617;

// Perform a modular exponentiation. This method is ideal for small exponents (~64 bits or less), as
// it is cheaper than using the pow precompile
Expand All @@ -24,8 +26,11 @@ library Bn254Crypto {

assembly {
let endpoint := add(exponent, 0x01)
for {} lt(count, endpoint) { count := add(count, count) }
{
for {

} lt(count, endpoint) {
count := add(count, count)
} {
if and(exponent, count) {
result := mulmod(result, input, modulus)
}
Expand All @@ -36,8 +41,7 @@ library Bn254Crypto {
return result;
}

function invert(uint256 fr) internal view returns (uint256)
{
function invert(uint256 fr) internal view returns (uint256) {
uint256 output;
bool success;
uint256 p = r_mod;
Expand Down Expand Up @@ -70,11 +74,12 @@ library Bn254Crypto {
return Types.G1Point(xValue, yValue);
}

function new_g2(uint256 x0, uint256 x1, uint256 y0, uint256 y1)
internal
pure
returns (Types.G2Point memory)
{
function new_g2(
uint256 x0,
uint256 x1,
uint256 y0,
uint256 y1
) internal pure returns (Types.G2Point memory) {
return Types.G2Point(x0, x1, y0, y1);
}

Expand All @@ -83,15 +88,15 @@ library Bn254Crypto {
}

function P2() internal pure returns (Types.G2Point memory) {
return Types.G2Point({
x0: 0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2,
x1: 0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed,
y0: 0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b,
y1: 0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa
});
return
Types.G2Point({
x0: 0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2,
x1: 0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed,
y0: 0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b,
y1: 0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa
});
}


/// Evaluate the following pairing product:
/// e(a1, a2).e(-b1, b2) == 1
function pairingProd2(
Expand Down Expand Up @@ -119,28 +124,21 @@ library Bn254Crypto {
mstore(add(mPtr, 0x120), mload(add(b2, 0x20)))
mstore(add(mPtr, 0x140), mload(add(b2, 0x40)))
mstore(add(mPtr, 0x160), mload(add(b2, 0x60)))
success := staticcall(
gas(),
8,
mPtr,
0x180,
0x00,
0x20
)
success := staticcall(gas(), 8, mPtr, 0x180, 0x00, 0x20)
out := mload(0x00)
}
require(success, "Pairing check failed!");
return (out != 0);
}

/**
* validate the following:
* x != 0
* y != 0
* x < p
* y < p
* y^2 = x^3 + 3 mod p
*/
* validate the following:
* x != 0
* y != 0
* x < p
* y < p
* y^2 = x^3 + 3 mod p
*/
function validateG1Point(Types.G1Point memory point) internal pure {
bool is_well_formed;
uint256 p = p_mod;
Expand All @@ -149,15 +147,16 @@ library Bn254Crypto {
let y := mload(add(point, 0x20))

is_well_formed := and(
and(
and(lt(x, p), lt(y, p)),
not(or(iszero(x), iszero(y)))
),
and(and(lt(x, p), lt(y, p)), not(or(iszero(x), iszero(y)))),
eq(mulmod(y, y, p), addmod(mulmod(x, mulmod(x, x, p), p), 3, p))
)
}
require(is_well_formed, "Bn254: G1 point not on curve, or is malformed");
require(
is_well_formed,
"Bn254: G1 point not on curve, or is malformed"
);
}
}

"# };
}
Loading