Skip to content

Commit 8d3d356

Browse files
authored
Merge pull request #30 from anoma/xuyang/update_resource_logic_circuit
integrate the encryption into resource logic circuit
2 parents 2fd763e + 5a85141 commit 8d3d356

File tree

3 files changed

+140
-10
lines changed

3 files changed

+140
-10
lines changed

Diff for: native/cairo_vm/trivial_resource_logic.json

+1-1
Large diffs are not rendered by default.

Diff for: native/cairo_vm/trivial_resource_logic.juvix

+138-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ type LogicResult :=
2323
self_resource_id : Field;
2424
-- The merkle root of resources
2525
root : Field;
26-
};
26+
cipher_text_elem0 : Field;
27+
cipher_text_elem1 : Field;
28+
cipher_text_elem2 : Field;
29+
cipher_text_elem3 : Field;
30+
cipher_text_elem4 : Field;
31+
cipher_text_elem5 : Field;
32+
cipher_text_elem6 : Field;
33+
cipher_text_elem7 : Field;
34+
cipher_text_elem8 : Field;
35+
cipher_text_elem9 : Field;
36+
mac : Field;
37+
pk_x : Field;
38+
pk_y : Field;
39+
nonce : Field;
40+
};
2741

2842
check_merkle (current_root : Field) : Pair Field Bool -> Field
2943
| (node, is_left) :=
@@ -41,6 +55,103 @@ check_merkle_path
4155
| [] := cur
4256
| (p :: ps) := check_merkle_path (check_merkle cur p) ps;
4357

58+
type EncryptionResult :=
59+
mkEncryptionResult@{
60+
cipher_text_elem0 : Field;
61+
cipher_text_elem1 : Field;
62+
cipher_text_elem2 : Field;
63+
cipher_text_elem3 : Field;
64+
cipher_text_elem4 : Field;
65+
cipher_text_elem5 : Field;
66+
cipher_text_elem6 : Field;
67+
cipher_text_elem7 : Field;
68+
cipher_text_elem8 : Field;
69+
cipher_text_elem9 : Field;
70+
mac : Field;
71+
sender_pk_x : Field;
72+
sender_pk_y : Field;
73+
nonce : Field
74+
};
75+
76+
type Cipher :=
77+
mkCipher@{
78+
cipher_text : List Field;
79+
cur_state : Field
80+
};
81+
82+
update_poseidon_state (cur_msg secret_key_x : Field) (cipher : Cipher) : Cipher :=
83+
let
84+
new_state := Cipher.cur_state cipher + cur_msg;
85+
new_text := new_state :: Cipher.cipher_text cipher;
86+
in mkCipher@{
87+
cipher_text := new_text;
88+
cur_state := poseidonHash2 new_state secret_key_x
89+
};
90+
91+
generate_cipher (poseidon_state : Field) (secret_key_x : Field) (plaintext : List Field) : Cipher :=
92+
let
93+
go (cipher : Cipher) : List Field -> Cipher
94+
| [] := cipher@Cipher{cipher_text := reverse (Cipher.cipher_text cipher)}
95+
| (m :: ms) := go (update_poseidon_state m secret_key_x cipher) ms;
96+
in go
97+
mkCipher@{
98+
cipher_text := [];
99+
cur_state := poseidon_state
100+
}
101+
plaintext;
102+
103+
encryption
104+
(messages : List Field)
105+
(pk_x : Field)
106+
(pk_y : Field)
107+
(sk : Field)
108+
(nonce : Field)
109+
: EncryptionResult :=
110+
111+
let
112+
-- Generate encryption key
113+
pk := Ec.mkPoint pk_x pk_y;
114+
secret_key := Ec.mul sk pk;
115+
116+
-- PLAINTEXT_NUM := 10;
117+
118+
-- TODO: Pad the messages here or outside of the circuit?
119+
plaintext := messages;
120+
121+
-- Init poseidon state
122+
secret_key_x := Ec.Point.x secret_key;
123+
poseidon_state := poseidonHashList [secret_key_x; Ec.Point.y secret_key; nonce; 10];
124+
125+
-- Generate cipher
126+
final_cipher := generate_cipher poseidon_state secret_key_x plaintext;
127+
128+
-- Get MAC
129+
mac := Cipher.cur_state final_cipher;
130+
131+
-- Generate sender's pk
132+
generator := Ec.mkPoint Ec.StarkCurve.GEN_X Ec.StarkCurve.GEN_Y;
133+
sender_pk := Ec.mul sk generator;
134+
135+
in case Cipher.cipher_text final_cipher of
136+
| [elem0; elem1; elem2; elem3; elem4; elem5; elem6; elem7; elem8; elem9] :=
137+
mkEncryptionResult@{
138+
cipher_text_elem0 := elem0;
139+
cipher_text_elem1 := elem1;
140+
cipher_text_elem2 := elem2;
141+
cipher_text_elem3 := elem3;
142+
cipher_text_elem4 := elem4;
143+
cipher_text_elem5 := elem5;
144+
cipher_text_elem6 := elem6;
145+
cipher_text_elem7 := elem7;
146+
cipher_text_elem8 := elem8;
147+
cipher_text_elem9 := elem9;
148+
mac;
149+
sender_pk_x := Ec.Point.x sender_pk;
150+
sender_pk_y := Ec.Point.y sender_pk;
151+
nonce
152+
}
153+
| _ := mkEncryptionResult 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
154+
44155
main
45156
(self_resource : Resource)
46157
(resource_nf_key : Field)
@@ -51,14 +162,14 @@ main
51162
let
52163
generated_npk : Field := poseidonHash2 resource_nf_key 0;
53164

54-
is_output_resource := case merkle_path of
165+
is_output_resource := case merkle_path of
55166
-- merkle_path can not be empty
56167
| nil := true
57168
| (_, is_left) :: t := is_left;
58169

59170
-- Actual npk
60-
actual_npk :=
61-
if
171+
actual_npk :=
172+
if
62173
| is_output_resource := Resource.npk self_resource
63174
| else := generated_npk;
64175

@@ -104,14 +215,33 @@ main
104215
poseidonHashList
105216
[actual_npk; Resource.nonce self_resource; resource_psi; resource_cm];
106217

107-
self_resource_id_ :=
108-
if
218+
self_resource_id_ :=
219+
if
109220
| is_output_resource := resource_cm
110221
| else := resource_nullifier_;
111-
112-
root_ := check_merkle_path self_resource_id_ merkle_path
113222

223+
root_ := check_merkle_path self_resource_id_ merkle_path;
224+
225+
-- Encryption
226+
messages := [Resource.logic self_resource; Resource.label self_resource; Resource.quantity self_resource; Resource.data self_resource ; resource_eph_field ; Resource.nonce self_resource ; Resource.npk self_resource ; Resource.rseed self_resource ; 0 ; 0 ];
227+
228+
cihper := encryption messages Ec.StarkCurve.GEN_X Ec.StarkCurve.GEN_Y 1 1;
229+
-- cihper_ := encryption [Resource.logic self_resource; Resource.label self_resource; Resource.quantity self_resource; Resource.data self_resource; Resource.eph self_resource; Resource.nonce self_resource; Resource.npk self_resource; Resource.rseed self_resource; 0; 0 ] Ec.StarkCurve.GEN_X Ec.StarkCurve.GEN_Y 1 1;
114230
in mkResult@{
115231
self_resource_id := self_resource_id_;
116232
root := root_;
233+
cipher_text_elem0 := EncryptionResult.cipher_text_elem0 cihper;
234+
cipher_text_elem1 := EncryptionResult.cipher_text_elem1 cihper;
235+
cipher_text_elem2 := EncryptionResult.cipher_text_elem2 cihper;
236+
cipher_text_elem3 := EncryptionResult.cipher_text_elem3 cihper;
237+
cipher_text_elem4 := EncryptionResult.cipher_text_elem4 cihper;
238+
cipher_text_elem5 := EncryptionResult.cipher_text_elem5 cihper;
239+
cipher_text_elem6 := EncryptionResult.cipher_text_elem6 cihper;
240+
cipher_text_elem7 := EncryptionResult.cipher_text_elem7 cihper;
241+
cipher_text_elem8 := EncryptionResult.cipher_text_elem8 cihper;
242+
cipher_text_elem9 := EncryptionResult.cipher_text_elem9 cihper;
243+
mac := EncryptionResult.mac cihper;
244+
pk_x := EncryptionResult.sender_pk_x cihper;
245+
pk_y := EncryptionResult.sender_pk_y cihper;
246+
nonce := EncryptionResult.nonce cihper;
117247
};

Diff for: native/cairo_vm/trivial_resource_logic_input.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"self_resource": {
3-
"logic" : "0x6de91eadc72a84989a824b25f16b1b3566556013025c8cedaddf2dd2c95ef6a",
3+
"logic" : "0x373bb1d37414c2edf111cf2f9f076517da99d38e44cdd716ca2ad00a07731e5",
44
"label" : "0x12",
55
"quantity" : "0x13",
66
"data" : "0x14",

0 commit comments

Comments
 (0)