Skip to content
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

zkCRA PR into merge-create-loan #378

Open
wants to merge 159 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
159 commits
Select commit Hold shift + click to select a range
7fc20ca
Merge branch 'develop' into feature/merge-create-loan
elclandestin0 Jun 3, 2021
dfffc22
data provider
elclandestin0 Jun 4, 2021
7c513d1
merge
elclandestin0 Jun 4, 2021
eee923c
fix compilation errors
elclandestin0 Jun 4, 2021
95eb4d5
some changes to dp
elclandestin0 Jun 4, 2021
1ccb5a4
Skelly for ZK CRA
tpscrpt Jun 4, 2021
031cbc3
Borrow function logic done, need to create loan and add configuratio…
tpscrpt Jun 4, 2021
d427b2d
Add snarks lib
tpscrpt Jun 4, 2021
cb26dae
Add documentation and fix using collateralAmount instead of collatera…
tpscrpt Jun 4, 2021
e08f40f
Into one file
tpscrpt Jun 4, 2021
c6eb931
Sample handler
tpscrpt Jun 4, 2021
b206dea
Updaets
tpscrpt Jun 4, 2021
07252f2
More clarity on market unctions
tpscrpt Jun 4, 2021
059c03c
zkCRA test template
elclandestin0 Jun 4, 2021
0d6dcd3
Save
tpscrpt Jun 5, 2021
3b40eea
attempt at fixing comp. errors
elclandestin0 Jun 7, 2021
3f3ed56
Fix compilation errors
tpscrpt Jun 7, 2021
6443352
packed variables
elclandestin0 Jun 8, 2021
08f7020
update test
elclandestin0 Jun 8, 2021
7a0c7c4
got witness, output & proof
elclandestin0 Jun 8, 2021
8c2e977
set config admins
elclandestin0 Jun 9, 2021
3cc58ab
tests almost done
elclandestin0 Jun 10, 2021
e76a85b
update tests
elclandestin0 Jun 10, 2021
3025c2f
ting
tpscrpt Jun 10, 2021
23162c0
Updates
tpscrpt Jun 10, 2021
aa3fe79
data being submitted onchain
elclandestin0 Jun 11, 2021
dadfc00
update tests
elclandestin0 Jun 11, 2021
26992f1
Keys
tpscrpt Jun 11, 2021
8776bb9
Snark proof verifying
tpscrpt Jun 11, 2021
cb7ffd1
YE
tpscrpt Jun 11, 2021
5728e29
Faster tests
tpscrpt Jun 12, 2021
be9bd3d
Progress
tpscrpt Jun 12, 2021
58efe8b
Packing properly hopefully
tpscrpt Jun 12, 2021
fca10a3
Working zk cra
tpscrpt Jun 12, 2021
9d94bfb
fix void signer error
elclandestin0 Jun 12, 2021
c83a425
Merge branch 'develop' into feature/zk-cra
elclandestin0 Jun 21, 2021
fce6a8b
zkcra.json stored in ipfs
elclandestin0 Jun 23, 2021
1f1ee5f
good scores bad scores
elclandestin0 Jun 23, 2021
5044652
ipfs successfully returning source
elclandestin0 Jun 23, 2021
cc8dccf
zkcra create loan changes
elclandestin0 Jun 24, 2021
fa6c4c6
fix errors
elclandestin0 Jun 24, 2021
5c79e38
merge fix
elclandestin0 Jun 24, 2021
40e8a65
market interest calc
elclandestin0 Jun 24, 2021
892e58a
moved init market admins to create loan facet
elclandestin0 Jun 25, 2021
ec8187b
reworked setting provider info
elclandestin0 Jun 25, 2021
c5da9b5
modifyign tests
elclandestin0 Jun 27, 2021
e584254
zkcra working
elclandestin0 Jun 28, 2021
3765885
zkcra updated tests
elclandestin0 Jun 28, 2021
3ab1604
fixing signaturedata datatype
elclandestin0 Jun 28, 2021
ee8c659
require statement
elclandestin0 Jun 28, 2021
e72d8ba
updated w/ noah's comments
elclandestin0 Jun 29, 2021
e9651ab
unused imports fix
elclandestin0 Jun 29, 2021
43e8f23
Revert "unused imports fix"
elclandestin0 Jun 29, 2021
f9b9f51
para programming w/ Noah
elclandestin0 Jun 29, 2021
70aac4e
process request library
elclandestin0 Jun 29, 2021
e8426f0
sample market handler
elclandestin0 Jun 30, 2021
e1e8058
fixed dir structure
elclandestin0 Jun 30, 2021
cbe2e82
ready for deployment
elclandestin0 Jun 30, 2021
90382a7
deployed process request library and teller market handler
elclandestin0 Jun 30, 2021
bfdb832
change to public function
elclandestin0 Jul 1, 2021
020d131
create provider factory
elclandestin0 Jul 1, 2021
1a489ce
deployed provider factory
elclandestin0 Jul 1, 2021
4e9d18c
signer successfully set
elclandestin0 Jul 1, 2021
d6a73af
provider set in market
elclandestin0 Jul 1, 2021
0a35fd5
commitments = number of signatures required
elclandestin0 Jul 2, 2021
55e7edf
end to end testing with good score done
elclandestin0 Jul 2, 2021
35d182b
provider creation with args
elclandestin0 Jul 2, 2021
3a70f50
added provider to request
elclandestin0 Jul 2, 2021
c6bdbf7
end to end testing for bad score completed
elclandestin0 Jul 2, 2021
84f4c23
removed console logs
elclandestin0 Jul 2, 2021
3831705
changed teller interest value
elclandestin0 Jul 5, 2021
82ffaf2
removed market lib
elclandestin0 Jul 5, 2021
0a53985
fix bad score test
elclandestin0 Jul 5, 2021
c6c50a2
update
elclandestin0 Jul 5, 2021
b9d8bc9
market handler updated with new loan calculator
elclandestin0 Jul 5, 2021
0a60b7a
removed useless require statement
elclandestin0 Jul 5, 2021
20aefc5
states file
elclandestin0 Jul 6, 2021
d9aec33
retrieve state's rate
elclandestin0 Jul 6, 2021
2df3405
fix bn error
elclandestin0 Jul 7, 2021
af9c5fa
fix market handling rounding error
elclandestin0 Jul 8, 2021
04087de
Merge pull request #421 from teller-protocol/zk-cra/test-flow
elclandestin0 Jul 9, 2021
996c352
data provider
elclandestin0 Jun 4, 2021
302ea5b
some changes to dp
elclandestin0 Jun 4, 2021
619181e
Skelly for ZK CRA
tpscrpt Jun 4, 2021
3b0459c
Borrow function logic done, need to create loan and add configuratio…
tpscrpt Jun 4, 2021
c724a92
Add snarks lib
tpscrpt Jun 4, 2021
28a7010
Add documentation and fix using collateralAmount instead of collatera…
tpscrpt Jun 4, 2021
34eb446
Into one file
tpscrpt Jun 4, 2021
6a7a0ee
Sample handler
tpscrpt Jun 4, 2021
da73525
Updaets
tpscrpt Jun 4, 2021
3cd3cc2
More clarity on market unctions
tpscrpt Jun 4, 2021
7409780
zkCRA test template
elclandestin0 Jun 4, 2021
402f868
Save
tpscrpt Jun 5, 2021
61faa69
attempt at fixing comp. errors
elclandestin0 Jun 7, 2021
ddbd7ac
Fix compilation errors
tpscrpt Jun 7, 2021
1648bbc
packed variables
elclandestin0 Jun 8, 2021
6bb792e
update test
elclandestin0 Jun 8, 2021
ebcbefb
got witness, output & proof
elclandestin0 Jun 8, 2021
464f372
set config admins
elclandestin0 Jun 9, 2021
f24b050
tests almost done
elclandestin0 Jun 10, 2021
023284a
update tests
elclandestin0 Jun 10, 2021
22080ea
ting
tpscrpt Jun 10, 2021
02dcfa7
Updates
tpscrpt Jun 10, 2021
94be52a
data being submitted onchain
elclandestin0 Jun 11, 2021
b0e6cf5
update tests
elclandestin0 Jun 11, 2021
7307e4d
Keys
tpscrpt Jun 11, 2021
ee9d83d
Snark proof verifying
tpscrpt Jun 11, 2021
1165739
YE
tpscrpt Jun 11, 2021
a37e0fe
Faster tests
tpscrpt Jun 12, 2021
7b6e983
Progress
tpscrpt Jun 12, 2021
3fe5a75
Packing properly hopefully
tpscrpt Jun 12, 2021
66700fa
Working zk cra
tpscrpt Jun 12, 2021
9596241
fix void signer error
elclandestin0 Jun 12, 2021
0aae7a7
zkcra.json stored in ipfs
elclandestin0 Jun 23, 2021
bd6766f
good scores bad scores
elclandestin0 Jun 23, 2021
dbab14a
ipfs successfully returning source
elclandestin0 Jun 23, 2021
b730f9f
zkcra create loan changes
elclandestin0 Jun 24, 2021
4cf9167
fix errors
elclandestin0 Jun 24, 2021
7d71c35
merge fix
elclandestin0 Jun 24, 2021
b7baa43
market interest calc
elclandestin0 Jun 24, 2021
addc0ff
moved init market admins to create loan facet
elclandestin0 Jun 25, 2021
d1fab5e
reworked setting provider info
elclandestin0 Jun 25, 2021
babd58a
modifyign tests
elclandestin0 Jun 27, 2021
b870397
zkcra working
elclandestin0 Jun 28, 2021
268d5c5
zkcra updated tests
elclandestin0 Jun 28, 2021
aae851a
fixing signaturedata datatype
elclandestin0 Jun 28, 2021
420bded
require statement
elclandestin0 Jun 28, 2021
a77ccd9
updated w/ noah's comments
elclandestin0 Jun 29, 2021
852f1ee
Revert "unused imports fix"
elclandestin0 Jun 29, 2021
29b2792
para programming w/ Noah
elclandestin0 Jun 29, 2021
94ac369
process request library
elclandestin0 Jun 29, 2021
fb49df4
sample market handler
elclandestin0 Jun 30, 2021
3587f25
fixed dir structure
elclandestin0 Jun 30, 2021
addb731
ready for deployment
elclandestin0 Jun 30, 2021
6959c2b
deployed process request library and teller market handler
elclandestin0 Jun 30, 2021
d03861b
change to public function
elclandestin0 Jul 1, 2021
4990170
create provider factory
elclandestin0 Jul 1, 2021
a199943
deployed provider factory
elclandestin0 Jul 1, 2021
735c85c
signer successfully set
elclandestin0 Jul 1, 2021
8b14b53
provider set in market
elclandestin0 Jul 1, 2021
f301a6d
commitments = number of signatures required
elclandestin0 Jul 2, 2021
96bef47
end to end testing with good score done
elclandestin0 Jul 2, 2021
351ae5d
provider creation with args
elclandestin0 Jul 2, 2021
805ae56
added provider to request
elclandestin0 Jul 2, 2021
0aa513b
end to end testing for bad score completed
elclandestin0 Jul 2, 2021
c9040de
removed console logs
elclandestin0 Jul 2, 2021
ebef161
changed teller interest value
elclandestin0 Jul 5, 2021
13def46
removed market lib
elclandestin0 Jul 5, 2021
9e2ee4e
fix bad score test
elclandestin0 Jul 5, 2021
688bb99
update
elclandestin0 Jul 5, 2021
50a1f36
market handler updated with new loan calculator
elclandestin0 Jul 5, 2021
c30bc65
removed useless require statement
elclandestin0 Jul 5, 2021
784171e
states file
elclandestin0 Jul 6, 2021
47d4887
retrieve state's rate
elclandestin0 Jul 6, 2021
c6b2a29
fix bn error
elclandestin0 Jul 7, 2021
aeabdd3
fix market handling rounding error
elclandestin0 Jul 8, 2021
6114b1e
created new loan facets
elclandestin0 Jul 29, 2021
ce38b12
Merge remote-tracking branch 'origin/zk-cra/market-handler' into zk-c…
elclandestin0 Jul 29, 2021
b395cd6
Merge pull request #434 from teller-protocol/zk-cra/market-handler
elclandestin0 Jul 29, 2021
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: 1 addition & 1 deletion contracts/escrow/dapps/DappMods.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract contract DappMods {
);
_;
}

/**
* @notice checks if the respective loan escrow is secure
* @param loanID uint256 of the respective loan escrow
Expand Down
2 changes: 1 addition & 1 deletion contracts/lending/libraries/LendingLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ library LendingLib {
function tToken(address asset) internal view returns (ITToken tToken_) {
tToken_ = s().tTokens[asset];
}
}
}
1 change: 0 additions & 1 deletion contracts/lending/ttoken/ITToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ abstract contract ITToken is ERC20Upgradeable, RolesFacet {
external
virtual
returns (uint16 ratio_);


/**
* @notice Called by the Teller Diamond contract when a loan has been taken out and requires funds.
Expand Down
4 changes: 3 additions & 1 deletion contracts/lending/ttoken/TToken_V1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ contract TToken_V1 is ITToken {

/**
* @notice it retrives the value in the underlying tokens
*
* @param amount the amount of underlying
* @param rate the rate the underlying is exchanging at
* @return value_ the value of the underlying
*/
function _valueInUnderlying(uint256 amount, uint256 rate)
internal
Expand Down
35 changes: 30 additions & 5 deletions contracts/market/CreateLoanFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
ReentryMods
} from "../contexts2/access-control/reentry/ReentryMods.sol";
import { RolesMods } from "../contexts2/access-control/roles/RolesMods.sol";
import { AUTHORIZED } from "../shared/roles.sol";
import { AUTHORIZED, ADMIN } from "../shared/roles.sol";
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

// Libraries
import { LibLoans } from "./libraries/LibLoans.sol";
import { LibEscrow } from "../escrow/libraries/LibEscrow.sol";
import { LibCollateral } from "./libraries/LibCollateral.sol";
import { LibConsensus } from "./libraries/LibConsensus.sol";
import { MarketLib } from "./libraries/MarketLib.sol";
import { LendingLib } from "../lending/libraries/LendingLib.sol";
import {
PlatformSettingsLib
Expand All @@ -31,6 +31,10 @@ import {
} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import { NumbersLib } from "../shared/libraries/NumbersLib.sol";
import { NFTLib } from "../nft/libraries/NFTLib.sol";
import { Verifier } from "./cra/verifier.sol";
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import { MarketLib } from "./libraries/MarketLib.sol";
import { ProcessRequestLib } from "./cra/ProcessRequestLib.sol";

// Interfaces
import { ILoansEscrow } from "../escrow/escrow/ILoansEscrow.sol";
Expand All @@ -48,7 +52,9 @@ import {
LoanStatus,
LoanTerms,
Loan,
MarketStorageLib
MarketStorageLib,
Signature,
DataProviderSignature
} from "../storage/market.sol";
import { AppStorageLib } from "../storage/app.sol";

Expand Down Expand Up @@ -81,6 +87,26 @@ contract CreateLoanFacet is RolesMods, ReentryMods, PausableMods {
loan.duration = request.request.duration;
}

// used for testing our zkcra function
function initializeMarketAdmins() external authorized(ADMIN, msg.sender) {
// setting market admin
// MarketLib.m(bytes32(0)).admin[msg.sender] = true;
// // setting providers admin
// MarketLib.p(bytes32(0)).admin[msg.sender] = true;
// MarketLib.p(bytes32(uint256(1))).admin[msg.sender] = true;
// MarketLib.p(bytes32(uint256(2))).admin[msg.sender] = true;
}

function setProviderInformation(
bytes32 providerId,
uint32 maxAge,
address signer,
bool signerValue
) external {
// MarketLib.setProviderSigner(providerId, signer, signerValue);
// MarketLib.setProviderMaxAge(providerId, maxAge);
}

/**
* @notice Creates a loan with the loan request and NFTs without any collateral
* @param request Struct of the protocol loan request
Expand Down Expand Up @@ -208,10 +234,9 @@ library CreateLoanLib {
request.request.duration,
"Teller: max loan duration exceeded"
);

// Get consensus values from request
(uint16 interestRate, uint16 collateralRatio, uint256 maxLoanAmount) =
LibConsensus.processLoanTerms(request);
ProcessRequestLib.processMarketRequest(request);

// Perform loan value checks
require(
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion contracts/market/LoanDataFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ contract LoanDataFacet {
/**
* @notice it returns the total escrow tokens held by an escrow
* @param loanID the identifier of the respective escrow's loan
* @return tokens_ the tokens held at the escrow loan
* @return tokens_ the tokens held at the escrow loan
*/
function getEscrowTokens(uint256 loanID)
external
Expand Down
32 changes: 32 additions & 0 deletions contracts/market/ProviderFactoryFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

import { DataProvider } from "./cra/DataProvider.sol";

contract ProviderFactoryFacet {
address public admin;
DataProvider[] public providers;

modifier onlyAdmin() {
require(admin == msg.sender, "Only the admin can call this!");
_;
}

// whichever address that deploys the provider factory facet is the admin of the factory
constructor() {
admin = msg.sender;
}

/**
* @notice it creates a new provider. whichever address that creates the new provider is the
* admin of the said provider.
*/
function createProvider() public {
DataProvider provider = new DataProvider(msg.sender);
providers.push(provider);
}

function getProviders() public view returns (DataProvider[] memory) {
return providers;
}
}
37 changes: 37 additions & 0 deletions contracts/market/cra/DataProvider.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

contract DataProvider {
mapping(address => bool) public admins;
mapping(address => bool) public signers;

modifier onlyAdmin() {
require(admins[msg.sender], "Teller: not admin");
_;
}

constructor(address initAdmin) {
admins[initAdmin] = true;
}

/**
* @notice it sets the admin of a provider
* @param signerAddress the address of the new or existing signer
* @param signerValue the bool value for the admin
*/
function setSigner(address signerAddress, bool signerValue)
public
onlyAdmin
{
signers[signerAddress] = signerValue;
}

/**
* @notice it sets the admin of a provider
* @param adminAddress the address of the new or existing admin
* @param adminValue the bool value for the admin
*/
function setAdmin(address adminAddress, bool adminValue) public onlyAdmin {
admins[adminAddress] = adminValue;
}
}
155 changes: 155 additions & 0 deletions contracts/market/cra/ProcessRequestLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {
LoanRequest,
DataProviderSignature,
Signature
} from "../../storage/market.sol";
import { MarketHandler } from "../cra/market-handler/MarketHandler.sol";
import { LibLoans } from "../libraries/LibLoans.sol";
import { Verifier } from "../cra/verifier.sol";
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import { DataProvider } from "./DataProvider.sol";

library ProcessRequestLib {
/**
* @notice it uses our request to verify the returned proof and witness with each other,
* verifies our signature data with our respective data providers, then retrieves our interest rate,
* collateral ratio and max loan amount
* @param request contains all the needed data to do the above
* @return interestRate the rate of the loan
* @return collateralRatio the collateral ratio required for the loan, if any
* @return maxLoanAmount the max loan amount the user is entitled to
*/
function processMarketRequest(LoanRequest memory request)
public
returns (
uint16 interestRate,
uint16 collateralRatio,
uint256 maxLoanAmount
)
{
MarketHandler marketHandler =
MarketHandler(request.marketHandlerAddress);
// Overwrite the first snark witness item with the on-chain identifier
// for the loan (msg.sender ^ nonce). This forces the CRA to have been
// run with the proper identifier.
request.snarkWitnesses[0] =
uint256(uint160(msg.sender)) ^
LibLoans.s().borrowerLoans[msg.sender].length;

// Verify the snark proof.
require(
Verifier.verifyTx(request.snarkProof, request.snarkWitnesses),
"Proof not verified"
);

// signatures length
uint8 signaturesLength = marketHandler.numberOfSignaturesRequired();

// get variable amount of commitments from market handler
bytes32[] memory commitments = new bytes32[](signaturesLength);

// constructing our commitments to verify with our signature data
for (uint8 i = 0; i < commitments.length; i++) {
for (uint8 j = 0; j < 8; j++) {
commitments[i] =
(commitments[i] << 32) ^
bytes32(request.snarkWitnesses[2 + i * 8 + j]);
}
commitments[i] ^= bytes32(
request.dataProviderSignatures[i].signedAt
);
}

// equate this require statement to amount of commitments from market handler
require(
request.dataProviderSignatures.length == 3,
"Must have 3 providers!"
);

// Verify that the commitment signatures are valid and that the data
// is not too old for the market's liking.
_verifySignatures(
commitments,
request.dataProviderSignatures,
request.marketHandlerAddress,
request.providers
);

// The second witness item (after identifier) is the market
// score
uint256 marketScore = uint256(request.snarkWitnesses[1]);
require(marketScore > 5, "Teller: market score not high enough");

// Let the market handle the loan request and disperse the loan.

// create default teller market handler
// pass it the marketId and return max loan amount, collateral ratio, interest rate
// upper and lower bound for loan amount, interest rate and collateral ratio depending on
// market id
(interestRate, collateralRatio, maxLoanAmount) = marketHandler.handler(
marketScore,
request
);
return (interestRate, collateralRatio, maxLoanAmount);
}

function _verifySignatures(
bytes32[] memory commitments,
DataProviderSignature[] memory signatureData,
address marketHandlerAddress,
address[] memory providers
) private {
MarketHandler marketHandler = MarketHandler(marketHandlerAddress);
for (uint256 i = 0; i < commitments.length; i++) {
address providerAddress = providers[i];
require(
signatureData[i].signedAt > block.timestamp - 5 days,
"Signed at less than max age"
);
require(
marketHandler.usedCommitments(commitments[i]) == false,
"Teller: commitment already used"
);
marketHandler.addCommitment(commitments[i]);

_validateSignature(
signatureData[i].signature,
commitments[i],
providerAddress
);
}
}

/**
* @notice It validates whether a signature is valid or not.
* @param signature signature to validate.
* @param commitment used to recover the signer.
* @param providerAddress the provider address to check for the recovered signer.
*/
function _validateSignature(
Signature memory signature,
bytes32 commitment,
address providerAddress
) private view {
address recoveredSigner =
ECDSA.recover(
keccak256(
abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
uint256(commitment)
)
),
signature.v,
signature.r,
signature.s
);
DataProvider provider = DataProvider(providerAddress);
require(
provider.signers(recoveredSigner),
"Teller: not valid signature"
);
}
}
37 changes: 37 additions & 0 deletions contracts/market/cra/cra.zok
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import "hashes/sha256/256bitPadded.zok" as sha256
Copy link
Member

Choose a reason for hiding this comment

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

do we still need this file locally for testing? if possible lets upload to IPFS so that we can just use that


/*
Zero-Knowledge CRA. Each market configures up to 4 data providers used to
create their market score. Markets also configure the weight of each value
given for the user by the data provider. Each data provider gives the user
a score between 0 and 4,294,967,295 along with a secret. These two are
combined to create a commitment, which is also signed by the data provider
and verified on-chain.

@param private u32[4][8] data - Array of 4 uint256's. The first 32 bits of
each value represents the user's score given by a specific data provider.
The last 224 bits represent a secret value used to create a commitment hash.
Data providers sign the value sha256(value, secret) ^ timestamp, and that
signature is verified on-chain.

@param public field identifier - user address ^ user borrow nonce. This
param is used to prevent replaying proofs on-chain.

@param public u32[4] weights - Array of 4 uint32's. Each value represents
a weight given by the market for the data provider at that index. If
data[i][0] (value) == 10 and weights[i] == 5, then market score += 50.

@return field MARKET_SCORE - uint256 accumulated markets score.
@return u32[4][8] commitments - Array of the 4 commitments.
*/
def main(private u32[3][8] data, public field identifier) -> (u32, u32[3][8]):
u32[3][8] commitments = data
u32 MARKET_SCORE = 0
u32 MASK = 0x0000000a

for u32 i in 0..3 do
MARKET_SCORE = MARKET_SCORE + data[i][0] & MASK
commitments[i] = sha256(data[i])
endfor

return MARKET_SCORE,commitments
Loading