-
Notifications
You must be signed in to change notification settings - Fork 38
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
elclandestin0
wants to merge
159
commits into
develop
Choose a base branch
from
feature/zk-cra
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 dfffc22
data provider
elclandestin0 7c513d1
merge
elclandestin0 eee923c
fix compilation errors
elclandestin0 95eb4d5
some changes to dp
elclandestin0 1ccb5a4
Skelly for ZK CRA
tpscrpt 031cbc3
Borrow function logic done, need to create loan and add configuratio…
tpscrpt d427b2d
Add snarks lib
tpscrpt cb26dae
Add documentation and fix using collateralAmount instead of collatera…
tpscrpt e08f40f
Into one file
tpscrpt c6eb931
Sample handler
tpscrpt b206dea
Updaets
tpscrpt 07252f2
More clarity on market unctions
tpscrpt 059c03c
zkCRA test template
elclandestin0 0d6dcd3
Save
tpscrpt 3b40eea
attempt at fixing comp. errors
elclandestin0 3f3ed56
Fix compilation errors
tpscrpt 6443352
packed variables
elclandestin0 08f7020
update test
elclandestin0 7a0c7c4
got witness, output & proof
elclandestin0 8c2e977
set config admins
elclandestin0 3cc58ab
tests almost done
elclandestin0 e76a85b
update tests
elclandestin0 3025c2f
ting
tpscrpt 23162c0
Updates
tpscrpt aa3fe79
data being submitted onchain
elclandestin0 dadfc00
update tests
elclandestin0 26992f1
Keys
tpscrpt 8776bb9
Snark proof verifying
tpscrpt cb7ffd1
YE
tpscrpt 5728e29
Faster tests
tpscrpt be9bd3d
Progress
tpscrpt 58efe8b
Packing properly hopefully
tpscrpt fca10a3
Working zk cra
tpscrpt 9d94bfb
fix void signer error
elclandestin0 c83a425
Merge branch 'develop' into feature/zk-cra
elclandestin0 fce6a8b
zkcra.json stored in ipfs
elclandestin0 1f1ee5f
good scores bad scores
elclandestin0 5044652
ipfs successfully returning source
elclandestin0 cc8dccf
zkcra create loan changes
elclandestin0 fa6c4c6
fix errors
elclandestin0 5c79e38
merge fix
elclandestin0 40e8a65
market interest calc
elclandestin0 892e58a
moved init market admins to create loan facet
elclandestin0 ec8187b
reworked setting provider info
elclandestin0 c5da9b5
modifyign tests
elclandestin0 e584254
zkcra working
elclandestin0 3765885
zkcra updated tests
elclandestin0 3ab1604
fixing signaturedata datatype
elclandestin0 ee8c659
require statement
elclandestin0 e72d8ba
updated w/ noah's comments
elclandestin0 e9651ab
unused imports fix
elclandestin0 43e8f23
Revert "unused imports fix"
elclandestin0 f9b9f51
para programming w/ Noah
elclandestin0 70aac4e
process request library
elclandestin0 e8426f0
sample market handler
elclandestin0 e1e8058
fixed dir structure
elclandestin0 cbe2e82
ready for deployment
elclandestin0 90382a7
deployed process request library and teller market handler
elclandestin0 bfdb832
change to public function
elclandestin0 020d131
create provider factory
elclandestin0 1a489ce
deployed provider factory
elclandestin0 4e9d18c
signer successfully set
elclandestin0 d6a73af
provider set in market
elclandestin0 0a35fd5
commitments = number of signatures required
elclandestin0 55e7edf
end to end testing with good score done
elclandestin0 35d182b
provider creation with args
elclandestin0 3a70f50
added provider to request
elclandestin0 c6bdbf7
end to end testing for bad score completed
elclandestin0 84f4c23
removed console logs
elclandestin0 3831705
changed teller interest value
elclandestin0 82ffaf2
removed market lib
elclandestin0 0a53985
fix bad score test
elclandestin0 c6c50a2
update
elclandestin0 b9d8bc9
market handler updated with new loan calculator
elclandestin0 0a60b7a
removed useless require statement
elclandestin0 20aefc5
states file
elclandestin0 d9aec33
retrieve state's rate
elclandestin0 2df3405
fix bn error
elclandestin0 af9c5fa
fix market handling rounding error
elclandestin0 04087de
Merge pull request #421 from teller-protocol/zk-cra/test-flow
elclandestin0 996c352
data provider
elclandestin0 302ea5b
some changes to dp
elclandestin0 619181e
Skelly for ZK CRA
tpscrpt 3b0459c
Borrow function logic done, need to create loan and add configuratio…
tpscrpt c724a92
Add snarks lib
tpscrpt 28a7010
Add documentation and fix using collateralAmount instead of collatera…
tpscrpt 34eb446
Into one file
tpscrpt 6a7a0ee
Sample handler
tpscrpt da73525
Updaets
tpscrpt 3cd3cc2
More clarity on market unctions
tpscrpt 7409780
zkCRA test template
elclandestin0 402f868
Save
tpscrpt 61faa69
attempt at fixing comp. errors
elclandestin0 ddbd7ac
Fix compilation errors
tpscrpt 1648bbc
packed variables
elclandestin0 6bb792e
update test
elclandestin0 ebcbefb
got witness, output & proof
elclandestin0 464f372
set config admins
elclandestin0 f24b050
tests almost done
elclandestin0 023284a
update tests
elclandestin0 22080ea
ting
tpscrpt 02dcfa7
Updates
tpscrpt 94be52a
data being submitted onchain
elclandestin0 b0e6cf5
update tests
elclandestin0 7307e4d
Keys
tpscrpt ee9d83d
Snark proof verifying
tpscrpt 1165739
YE
tpscrpt a37e0fe
Faster tests
tpscrpt 7b6e983
Progress
tpscrpt 3fe5a75
Packing properly hopefully
tpscrpt 66700fa
Working zk cra
tpscrpt 9596241
fix void signer error
elclandestin0 0aae7a7
zkcra.json stored in ipfs
elclandestin0 bd6766f
good scores bad scores
elclandestin0 dbab14a
ipfs successfully returning source
elclandestin0 b730f9f
zkcra create loan changes
elclandestin0 4cf9167
fix errors
elclandestin0 7d71c35
merge fix
elclandestin0 b7baa43
market interest calc
elclandestin0 addc0ff
moved init market admins to create loan facet
elclandestin0 d1fab5e
reworked setting provider info
elclandestin0 babd58a
modifyign tests
elclandestin0 b870397
zkcra working
elclandestin0 268d5c5
zkcra updated tests
elclandestin0 aae851a
fixing signaturedata datatype
elclandestin0 420bded
require statement
elclandestin0 a77ccd9
updated w/ noah's comments
elclandestin0 852f1ee
Revert "unused imports fix"
elclandestin0 29b2792
para programming w/ Noah
elclandestin0 94ac369
process request library
elclandestin0 fb49df4
sample market handler
elclandestin0 3587f25
fixed dir structure
elclandestin0 addb731
ready for deployment
elclandestin0 6959c2b
deployed process request library and teller market handler
elclandestin0 d03861b
change to public function
elclandestin0 4990170
create provider factory
elclandestin0 a199943
deployed provider factory
elclandestin0 735c85c
signer successfully set
elclandestin0 8b14b53
provider set in market
elclandestin0 f301a6d
commitments = number of signatures required
elclandestin0 96bef47
end to end testing with good score done
elclandestin0 351ae5d
provider creation with args
elclandestin0 805ae56
added provider to request
elclandestin0 0aa513b
end to end testing for bad score completed
elclandestin0 c9040de
removed console logs
elclandestin0 ebef161
changed teller interest value
elclandestin0 13def46
removed market lib
elclandestin0 9e2ee4e
fix bad score test
elclandestin0 688bb99
update
elclandestin0 50a1f36
market handler updated with new loan calculator
elclandestin0 c30bc65
removed useless require statement
elclandestin0 784171e
states file
elclandestin0 47d4887
retrieve state's rate
elclandestin0 c6b2a29
fix bn error
elclandestin0 aeabdd3
fix market handling rounding error
elclandestin0 6114b1e
created new loan facets
elclandestin0 ce38b12
Merge remote-tracking branch 'origin/zk-cra/market-handler' into zk-c…
elclandestin0 b395cd6
Merge pull request #434 from teller-protocol/zk-cra/market-handler
elclandestin0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import "hashes/sha256/256bitPadded.zok" as sha256 | ||
|
||
/* | ||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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