Oracle Program#
The Aleo Oracle program is designed to store information provided by the Attestation Reports generated in TEEs by the Oracle Notarization backend. The program can verify that the report is valid, and it was signed by an allowed key, and it was generated by a backend running a certain version of the software.
This page explains in detail the source code of the Aleo Oracle and how to use it.
Setting up local oracle for testing#
If you want to test your integration locally without interacting with a live network you can use your locally set up network and deploy your own oracle using source code below.
Requirements:
Steps to run local network:
-
Start 4 instances of validator nodes to start mining blocks locally. Open 4 terminals and run this command (1 per terminal):
-
Change the
owner
address in oracle source code to the address provided by the 1st instance of the validator node when you started it. -
Deploy oracle contract to the local network:
snarkos developer deploy official_oracle.aleo --private-key <privateKey> --query "http://0.0.0.0:3030" --path "./" --broadcast "http://0.0.0.0:3030/mainnet/transaction/broadcast" --priority-fee 0
Where
privateKey
is the key provided when you started the 1st instance of validator node -
Update oracle with
unique_id
,pcr_values
andpublic_key
's. You can getunique_id
andpublic_key
by requesting/info
endpoint of the SGX Oracle Notarization backend andpcr_values
withpublic_key
from/info
of the Nitro Oracle Notarization backendsnarkos developer execute official_oracle.aleo set_key '<signerPubKey>' '<true|false>' --private-key <privateKey> --query "http://0.0.0.0:3030" --broadcast "http://0.0.0.0:3030/mainnet/transaction/broadcast" snarkos developer execute official_oracle.aleo set_unique_id '{ chunk_1: <unique_id_1>, chunk_2: <unique_id_2> }' --private-key <privateKey> --query "http://0.0.0.0:3030" --broadcast "http://0.0.0.0:3030/mainnet/transaction/broadcast" snarkos developer execute official_oracle.aleo set_pcr_values '{ pcr_0_chunk_1: <pcr_0_chunk_1>, pcr_0_chunk_2: <pcr_0_chunk_2> ... pcr_2_chunk_3: <pcr_2_chunk_3> }' --private-key <privateKey> --query "http://0.0.0.0:3030" --broadcast "http://0.0.0.0:3030/mainnet/transaction/broadcast"
-
Oracle is set and ready to be used.
Aleo Oracle program#
Mainnet Oracle program#
official_oracle.aleo
You can find this program deployed in the mainnet by visiting the official Aleo Explorer or Aleo123 explorer.
Testnet Oracle program#
official_oracle_v2.aleo
You can find this program deployed in the testnet by visiting the official Aleo Explorer or Aleo123 explorer.
Tip
You may have seen and/or used official_oracle.aleo
program in testnet. That program is not supported anymore, and you
should use official_oracle_v2.aleo
instead. See the migration guide for more information.
Types#
UniqueID#
UniqueID
UniqueID
stores SGX unique ID for assertions on the source code that produced an attestation report.
PcrValues#
PcrValues
PcrValues
stores Nitro PCR values for assertions on the source code that produced an attestation report.
AttestedData#
AttestedData
AttestedData
is the type for storing the attested data in the blockchain. The struct includes an attestation timestamp.
TimestampedHash#
TimestampedHash
TimestampedHash
is a helper type for computing timestamped request hash for storing the timestamped attested data in the blockchain.
PositionData#
TimestampedHash
PositionData
provides information on how to extract certain data from the Nitro report such as data hash or PCR values.
Mappings#
sgx_unique_id#
sgx_unique_id
sgx_unique_id
mapping stores 32 bytes of SGX enclave's unique ID as a struct of 2 16-byte u128
formatted chunks under the 0u8
key. The program will allow reports only from the backends with this unique ID to make sure that report was produced by a backend running this specific version of source code. You can check which sgx_unique_id
Oracle Notarization backend is running by requesting the /info
endpoint of an SGX notarizer, which is currently hosted at sgx.aleooracle.xyz.
You can also run the Oracle Notarization backend yourself using reproducible builds and see the generated unique ID. It should match the sgx_unique_id
provided by other Oracle Notarization backends if they run the same code.
nitro_pcr_values#
nitro_pcr_values
nitro_pcr_values
mapping stores bytes of 3 PCR values (PCR_0, PCR_1 and PCR_2) from Nitro enclave as a struct of 9 16-byte u128
formatted chunks (3 chunks per pcr value) under the 0u8
key. The program will allow reports only from the backends with this PCR values to make sure that report was produced by a backend running this specific version of source code. You can check which nitro_pcr_values
Oracle Notarization backend is running by requesting the /info
endpoint of an Nitro notarizer, which is currently hosted at nitro.aleooracle.xyz.
You can also run the Oracle Notarization backend yourself using reproducible builds and see the generated PCR values. It should match the nitro_pcr_values
provided by other Oracle Notarization backends if they run the same code.
allowed_keys#
allowed_keys
allowed_keys
contains public keys generated by the Oracle Notarization backends which are used to verify the Schnorr signature of a report. Each time the Oracle Notarization backend starts it generates a key, which is used to create a signature. We store a list of keys from the backends that are verified and trusted, and reject all reports with untrusted keys.
sgx_attested_data#
sgx_attested_data
sgx_attested_data
stores the data acquired by an SGX notarizer with a timestamp from a specific request. You can always access the stored data using your Request Hash as a key or Timestamped Request Hash to get data the that was produced at a specific timestamp.
nitro_attested_data#
nitro_attested_data
nitro_attested_data
stores the data acquired by a Nitro notarizer with a timestamp from a specific request. You can always access the stored data using your Request Hash as a key or Timestamped Request Hash to get the data that was produced at a specific timestamp.
Transitions#
set_key#
set_key
This function is used by the owner of the program to add or remove allowed public keys that are used to verify Schnorr signature. The function sets the "allowed" state to the passed adddress.
set_unique_id#
set_unique_id
This function is used by the owner of the program to replace the old SGX enclave unique_id
with a new one. unique_id
is used to verify that a report was produced using a specific version of the source code. This function is used when the source code of Oracle Notarization backend is updated to make sure that the oracle program can be updated without re-deploying.
set_pcr_values#
set_pcr_values
This function is used by the owner of the program to replace the old Nitro enclave pcr_values
with a new ones. pcr_values
is used to verify that a report was produced using a specific version of the source code. This function is used when the source code of Oracle Notarization backend is updated to make sure that the oracle program can be updated using set_data_nitro
without re-deploying.
set_data_sgx#
set_data_sgx
This function is used to update the Oracle program with the new attested data from an SGX notarizer. It takes the report_data
that was included in the report, the report
itself, the Schnorr signature
of the provided report and the public_key
to verify this signature.
As a first step oracle verifies the report (see verify_sgx_report
) and its signature, then it creates the request_hash
(see get_request_hash
). This hash is going to be used as a key to store the provided attested_data
as the latest data produced by the attestation request. Then it computes a timestamped request hash using
the TimestampedHash
helper type and the Poseidon8 hash. That hash is going to be used as a key to store
the provided attested_data
as a historical data that is not going to be overwritten. You can read more about how the Request Hash
works here.
In the finalize step the Oracle verifies that the attestation report was signed by one of the approved keys, and that the uniquie_id
of the report is correct. Then it saves the attested_data
using a timestamped request hash as a key. Then it checks that the timestamp of the report creation is newer than the time of the last saved report to make sure the data is not outdated. And then it saves the attested_data
using the static request hash as a key.
set_data_nitro#
set_data_nitro
This function is used to update the Oracle program with the new attested data from a Nitro notarizer. It takes the report_data
that was included in the report, the report
itself, the Schnorr signature
of the provided report, the public_key
to verify this signature, the data_position
to extract the data hash from the report, pcr_0_position
, pcr_1_position
and pcr_2_position
to extract the PCR values
to verify that a report was produced using a specific version of the source code.
As a first step oracle extracts the data hash from the report and then verifies the report itself (see verify_nitro_report
) and its signature.
Then it creates the request_hash
(see get_request_hash
). This hash is going to be used as the key to store the provided attested_data
as the latest data produced by the attestation request. Then it computes a timestamped request hash using
the TimestampedHash
helper type and the Poseidon8 hash. That hash is going to be used as a key to store the provided attested_data
as a historical data
that is not going to be overwritten. You can read more about how the Request Hash
and the Timestamped Request Hash
works here.
Then it extracts the pcr_values
from the report to verify them later.
In the finalize step the Oracle verifies that the attestation report was signed by one of the approved keys, and that the pcr_values
of the report is correct. Then it saves the attested_data
using a timestamped request hash as a key. Then it checks that the timestamp of the report creation is newer than the time of the last saved report to make sure the data is not outdated. And then it saves the attested_data
using the static request hash as a key.
Functions#
verify_sgx_report#
verify_sgx_report
This function is used to verify that the provided report
is valid. As a first step, it takes Poseidon8 hash of data
, which is the report data that was included into the report. Then it checks that certain enclave flags are set correctly, for example that the SGX enclave is not in a debug mode (see here for more about SGX flags). Then it checks that the Poseidon8 data_hash
that we created is matching the one included in the report. For verification that the report is valid it takes the Poseidon8 hash of the report itself and checks the provided signature on top of the report hash against the provided public_key
generated by the enclave (we are going to check that this public key is allowed later in the finalize
step of set_data
).
Here is a detailed explanation of report
fields that are being verified.
extract_value#
extract_value
This function is used for a Nitro reports to extract u128
data which is present in a 2 u128
chunks. Since Nitro report is not alligned perfectly with u128
structure, some of the required values might be present in between the 2 u128
chunks. In order to use them for verification we need to extract only the part that we need with bitwise operations. position_data
contains information on which parts to extract from the first and second chunks and then we combine extracted parts into the one u128.
This function is used for report_data_hash
as well as for pcr_values
extraction.
You can read more about data extraction from Nitro reports here.
select_chunk#
select_chunk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
This is a workaround function for Aleo not having dynamic array/struct access. It is used to select chunks of data from the report to later use in extract_value
to get required information from the Nitro report. Function takes an index of a chunk and returns 4 u128
chunks starting from the index.
You can read more about data extraction from Nitro reports here.
verify_nitro_report#
verify_nitro_report
This function is used to verify that the provided report
is valid. As a first step, it takes Poseidon8 hash of data
, which is the report data that was included into the report. Then it checks that the Poseidon8 data_hash
that we created is matching the one that was extracted from the report. For verification that the report is valid it takes the Poseidon8 hash of the report itself and checks the provided signature on top of the report hash against the provided public_key
generated by the enclave (we are going to check that this public key is allowed later in the finalize
step of set_data
).
Here is a detailed explanation of report
fields that are being verified.
get_request_hash#
get_request_hash
get_request_hash
computes a hash of the report data without the variable properties like the attestation data and attestation
timestamp, as described in the Request Hash guide.
Full source code#
Here is the source code of the Aleo Oracle in both Leo and compiled to Aleo versions:
Aleo Oracle program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
|
How to use the Oracle#
Here is an example on how to use values stored in the Aleo Oracle.
If you want to use the values outside of the Aleo blockchain then you can simply query the mapping value from the blockchain using the Request Hash as a key:
Querying the Aleo Oracle
curl https://api.explorer.aleo.org/v1/mainnet/program/official_oracle.aleo/mapping/attested_data/{request_hash}
Where request_hash
is a Hash of the Request that was used to get data (with zeroed Data and Timestamp to make it static).
To use Aleo Oracle in your Aleo program you need to import the Oracle program first and then get a value from the attested_data
mapping. Keep in mind that in Aleo you can only read mappings during the Finalize function.
Querying the Aleo Oracle from Aleo program
Example of a simple program that will transfer an attested amount of some token.
Since you can read mappings only during the finalize
stage, in this example we provide the amount as a function parameter to a Transition
function to use and then verify that the provided value is correct during the finalize
step.
Where request_hash
is the Hash of the Request that was used during the Notarization process (with Data and Timestamp zeroed out to make it static).
Historical data#
In case you want to use the data that was attested at a specific timestamp you can use Timestamped Request Hash as a key to attested data mappings. It contains the Request Hash that was used to get the data as well as the timestamp of when the this data was notarized.
Using timestamped_request_hash to query the Aleo Oracle
Example of a simple program that will transfer an attested amount of some token.
Since you can read mappings only during the finalize
stage, in this example we provide the amount as a function parameter to a Transition
function to use and then verify that the provided value is correct during the finalize
step.
Where request_hash
is the Hash of the Request that was used during the Notarization process (with Data and Timestamp zeroed out to make it static).
Value type#
Aleo Oracle stores all the values as u128
numbers. In case you require smaller numbers, e.g. u64
, you can cast u128
to the desired size.
(!) Keep in mind that a u128
number should be small enough to fit into the desired type (for example u64
). A u64
number represents 8 bytes of information while u128
is 16 bytes. So if you want to cast u128
to u64
then the u128
value should only contain not more than 8 bytes of information, not more than 4 bytes for u32
, etc. Use this trick only if you know that your data provider gives smaller than u128
responses.
Casting a u128
to a smaller number
Migration guide#
Since the testnet deployment we have updated the oracle to also support reports from another type of TEE enclave.
There's a deployed dual TEE version of the Oracle program in:
- the mainnet network - official_oracle.aleo;
- and the testnet network - official_oracle_v2.aleo.
They are the same program.
For comparison, here is the old version of the Oracle program in the testnet network - official_oracle.aleo (testnet). This program is not supported anymore.
To migrate to the new dual TEE version of the Oracle program you need to perform the following changes:
- Change
set_data
call toset_data_sgx
; - Change reads of the
attested_data
mapping tosgx_attested_data
, which now returns anAttestedData
struct instead of the attested datau128
value. Usedata
property to access the data; - Remove reads of the
last_update_timestamp
mapping, if you had any.
In addition to having the latest data in the mapping, you can now read the historical (timestamped) data too.
See more in set_data_sgx
and the Timestamped Request Hash.