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

Smiller coti/mpc core address #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions contracts/examples/ConfidentialNFTExample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ contract ConfidentialNFTExample is

function mint(
address to,
ctUint64[] calldata _itTokenURI,
bytes[] calldata _itSignature
itString calldata itTokenURI
) public onlyOwner {
uint256 tokenId = _totalSupply;

ConfidentialERC721._mint(to, tokenId);

ConfidentialERC721URIStorage._setTokenURI(msg.sender, tokenId, _itTokenURI, _itSignature);
ConfidentialERC721URIStorage._setTokenURI(msg.sender, tokenId, itTokenURI);

_totalSupply += 1;

Expand Down
42 changes: 11 additions & 31 deletions contracts/examples/DataOnChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ contract DataOnChain {
ctUint64 private ctNetworkSomeEncryptedValue;
ctUint64 private ctNetworkSomeEncryptedValueEncryptedInput;
ctUint64 private ctUserArithmeticResult;
ctUint64[] private ctUserSomeEncryptedStringEncryptedInput;
ctUint64[] private ctNetworkSomeEncryptedStringEncryptedInput;
ctUint64[] private ctUserSomeEncryptedStringValue;
ctString private ctUserSomeEncryptedStringEncryptedInput;
ctString private ctNetworkSomeEncryptedStringEncryptedInput;
ctString private ctUserSomeEncryptedStringValue;



Expand All @@ -23,7 +23,7 @@ contract DataOnChain {

event UserEncryptedValue(address indexed _from, ctUint64 ctUserSomeEncryptedValue);

event UserEncryptedStringValue(address indexed _from, ctUint64[] ctUserSomeEncryptedStringValue);
event UserEncryptedStringValue(address indexed _from, ctString ctUserSomeEncryptedStringValue);


function getNetworkSomeEncryptedValue() external view returns (ctUint64 ctSomeEncryptedValue) {
Expand All @@ -46,7 +46,7 @@ contract DataOnChain {
return ctUserSomeEncryptedValueEncryptedInput;
}

function getUserSomeEncryptedStringEncryptedInput() external view returns (ctUint64[] memory ctSomeEncryptedValue) {
function getUserSomeEncryptedStringEncryptedInput() external view returns (ctString memory ctSomeEncryptedValue) {
return ctUserSomeEncryptedStringEncryptedInput;
}

Expand All @@ -64,23 +64,10 @@ contract DataOnChain {
ctNetworkSomeEncryptedValueEncryptedInput = MpcCore.offBoard(gtNetworkSomeEncryptedValue); // saves it as cipher text (by network aes key)
}

function setSomeEncryptedStringEncryptedInput(ctUint64[] calldata _itInputString, bytes[] calldata _itSignature) external {
gtUint64[] memory _encryptedValueGt = new gtUint64[](_itInputString.length);
function setSomeEncryptedStringEncryptedInput(itString calldata _itInputString) external {
gtString memory _encryptedValueGt = MpcCore.validateCiphertext(_itInputString);

itUint64 memory it;

for (uint256 i = 0; i < _itInputString.length; ++i) {
it.ciphertext = _itInputString[i];
it.signature = _itSignature[i];

_encryptedValueGt[i] = MpcCore.validateCiphertext(it);
}

ctUint64[] memory tmp = new ctUint64[](_itInputString.length);
for (uint256 i = 0; i < _encryptedValueGt.length; ++i) {
tmp[i] = MpcCore.offBoard(_encryptedValueGt[i]);
}
ctNetworkSomeEncryptedStringEncryptedInput = tmp;
ctNetworkSomeEncryptedStringEncryptedInput = MpcCore.offBoard(_encryptedValueGt);
}

function setUserSomeEncryptedValue() external {
Expand All @@ -96,16 +83,9 @@ contract DataOnChain {
}

function setUserSomeEncryptedStringEncryptedInput() external {
gtUint64[] memory userGt = new gtUint64[](ctNetworkSomeEncryptedStringEncryptedInput.length);

for (uint256 i = 0; i < ctNetworkSomeEncryptedStringEncryptedInput.length; ++i) {
userGt[i] = MpcCore.onBoard(ctNetworkSomeEncryptedStringEncryptedInput[i]);
}
ctUint64[] memory tmp = new ctUint64[](userGt.length);
for (uint256 i = 0; i < userGt.length; ++i) {
tmp[i] = MpcCore.offBoardToUser(userGt[i], msg.sender);
}
ctUserSomeEncryptedStringEncryptedInput = tmp;
gtString memory userGt = MpcCore.onBoard(ctNetworkSomeEncryptedStringEncryptedInput);

ctUserSomeEncryptedStringEncryptedInput = MpcCore.offBoardToUser(userGt, msg.sender);

emit UserEncryptedStringValue(msg.sender, ctUserSomeEncryptedStringEncryptedInput);
}
Expand Down
65 changes: 63 additions & 2 deletions contracts/examples/TestMpcCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ pragma solidity ^0.8.19;
import "../lib/MpcCore.sol";

contract TestMpcCore {

// Encrypted string variables

ctString private userEncryptedString;

ctString private networkEncryptedString;

string public plaintext;
string public plaintextString;

bool public isEqual;

// Encrypted address variables

ctAddress public userEncryptedAddress;

ctAddress public networkEncryptedAddress;

address public plaintextAddress;


// Encrypted string functions

function setUserEncryptedString(itString calldata it_) public {
gtString memory gt_ = MpcCore.validateCiphertext(it_);

Expand All @@ -31,7 +45,7 @@ contract TestMpcCore {
function decryptNetworkEncryptedString() public {
gtString memory gt_ = MpcCore.onBoard(networkEncryptedString);

plaintext = MpcCore.decrypt(gt_);
plaintextString = MpcCore.decrypt(gt_);
}

function setPublicString(string calldata str) public {
Expand Down Expand Up @@ -60,4 +74,51 @@ contract TestMpcCore {

userEncryptedString = MpcCore.offBoardToUser(gt_, msg.sender);
}

// Encrypted address function

function setUserEncryptedAddress(itAddress calldata it_) public {
gtAddress memory gt_ = MpcCore.validateCiphertext(it_);

userEncryptedAddress = MpcCore.offBoardToUser(gt_, msg.sender);
}

function setNetworkEncryptedAddress(itAddress calldata it_) public {
gtAddress memory gt_ = MpcCore.validateCiphertext(it_);

networkEncryptedAddress = MpcCore.offBoard(gt_);
}

function decryptNetworkEncryptedAddress() public {
gtAddress memory gt_ = MpcCore.onBoard(networkEncryptedAddress);

plaintextAddress = MpcCore.decrypt(gt_);
}

function setPublicAddress(address addr) public {
gtAddress memory gt_ = MpcCore.setPublicAddress(addr);

userEncryptedAddress = MpcCore.offBoardToUser(gt_, msg.sender);
}

function setIsEqual(itAddress calldata a_, itAddress calldata b_, bool useEq) public {
gtAddress memory a = MpcCore.validateCiphertext(a_);
gtAddress memory b = MpcCore.validateCiphertext(b_);

gtBool isEqual_;

if (useEq) {
isEqual_ = MpcCore.eq(a, b);
} else {
isEqual_ = MpcCore.not(MpcCore.ne(a, b));
}

isEqual = MpcCore.decrypt(isEqual_);
}

function setRandomAddress() public {
gtAddress memory gt_ = MpcCore.randAddress();

userEncryptedAddress = MpcCore.offBoardToUser(gt_, msg.sender);
}
}
128 changes: 128 additions & 0 deletions contracts/lib/MpcCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ struct gtString {
gtUint64[] value;
}

struct gtAddress {
gtUint64 gt1; // bytes 1 - 8
gtUint64 gt2; // bytes 9 - 16
gtUint32 gt3; // bytes 17 - 20
}

type ctBool is uint256;
type ctUint8 is uint256;
type ctUint16 is uint256;
Expand All @@ -26,6 +32,12 @@ struct ctString {
ctUint64[] value;
}

struct ctAddress {
ctUint64 ct1; // bytes 1 - 8
ctUint64 ct2; // bytes 9 - 16
ctUint32 ct3; // bytes 17 - 20
}

struct itBool {
ctBool ciphertext;
bytes signature;
Expand All @@ -50,6 +62,12 @@ struct itString {
ctString ciphertext;
bytes[] signature;
}
struct itAddress {
ctAddress ciphertext;
bytes signature1;
bytes signature2;
bytes signature3;
}

struct utBool {
ctBool ciphertext;
Expand All @@ -75,6 +93,10 @@ struct utString {
ctString ciphertext;
ctString userCiphertext;
}
struct utAddress {
ctAddress ciphertext;
ctAddress userCiphertext;
}


import "./MpcInterface.sol";
Expand Down Expand Up @@ -991,6 +1013,112 @@ library MpcCore {



// ========== Address operations ===========

function validateCiphertext(itAddress memory input) internal returns (gtAddress memory) {
gtAddress memory gt_;

itUint64 memory it1_;

it1_.ciphertext = input.ciphertext.ct1;
it1_.signature = input.signature1;
gt_.gt1 = validateCiphertext(it1_);

it1_.ciphertext = input.ciphertext.ct2;
it1_.signature = input.signature2;
gt_.gt2 = validateCiphertext(it1_);

itUint32 memory it2_ = itUint32(input.ciphertext.ct3, input.signature3);
gt_.gt3 = validateCiphertext(it2_);

return gt_;
}

function onBoard(ctAddress memory ct) internal returns (gtAddress memory) {
gtAddress memory gt_;

gt_.gt1 = onBoard(ct.ct1);
gt_.gt2 = onBoard(ct.ct2);
gt_.gt3 = onBoard(ct.ct3);

return gt_;
}

function offBoard(gtAddress memory pt) internal returns (ctAddress memory) {
ctAddress memory ct_;

ct_.ct1 = offBoard(pt.gt1);
ct_.ct2 = offBoard(pt.gt2);
ct_.ct3 = offBoard(pt.gt3);

return ct_;
}

function offBoardToUser(gtAddress memory pt, address addr) internal returns (ctAddress memory) {
ctAddress memory ct_;

ct_.ct1 = offBoardToUser(pt.gt1, addr);
ct_.ct2 = offBoardToUser(pt.gt2, addr);
ct_.ct3 = offBoardToUser(pt.gt3, addr);

return ct_;
}

function offBoardCombined(gtAddress memory pt, address addr) internal returns (utAddress memory ut) {
ut.ciphertext = offBoard(pt);
ut.userCiphertext = offBoardToUser(pt, addr);
}

function setPublicAddress(address pt) internal returns (gtAddress memory) {
gtAddress memory result_;

result_.gt1 = setPublic64(uint64(bytes8(bytes20(pt))));
result_.gt2 = setPublic64(uint64(bytes8(bytes20(pt) << 64)));
result_.gt3 = setPublic32(uint32(bytes4(bytes20(pt) << 128)));

return result_;
}

function randAddress() internal returns (gtAddress memory) {
gtAddress memory result_;

result_.gt1 = rand64();
result_.gt2 = rand64();
result_.gt3 = rand32();

return result_;
}

function decrypt(gtAddress memory ct) internal returns (address){
bytes20 result_;

result_ |= bytes20(bytes8(decrypt(ct.gt1)));
result_ |= bytes20(bytes8(decrypt(ct.gt2))) >> 64;
result_ |= bytes20(bytes4(decrypt(ct.gt3))) >> 128;

return address(result_);
}

function eq(gtAddress memory a, gtAddress memory b) internal returns (gtBool) {
gtBool result_ = eq(a.gt1, b.gt1);

result_ = and(result_, eq(a.gt2, b.gt2));
result_ = and(result_, eq(a.gt3, b.gt3));

return result_;
}

function ne(gtAddress memory a, gtAddress memory b) internal returns (gtBool) {
gtBool result_ = ne(a.gt1, b.gt1);

result_ = or(result_, ne(a.gt2, b.gt2));
result_ = or(result_, ne(a.gt3, b.gt3));

return result_;
}



// =========== Operations with LHS_PUBLIC parameter ===========
// =========== 8 bit operations ==============

Expand Down
Loading